Class: Fastlane::Actions::CommitAndroidVersionBumpAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



112
113
114
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 112

def self.authors
  ["Jems"]
end

.available_optionsObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 116

def self.available_options
  [
      FastlaneCore::ConfigItem.new(key: :message,
                                  env_name: "CVB_COMMIT_BUMP_MESSAGE",
                                  description: "The commit message when committing the version bump",
                                  optional: true),
      FastlaneCore::ConfigItem.new(key: :app_folder_name,
                              env_name: "CVB_APP_VERSION_NAME",
                           description: "The name of the application source folder in the Android project (default: app)",
                              optional: true,
                                  type: String,
                          default_value:"app"),
      FastlaneCore::ConfigItem.new(key: :gradle_file_folder,
                              env_name: "CVB_GRADLE_FILE_FOLDER",
                           description: "The relative path to the folder that contains the gradle file containing the modification to commit (example :project/app)",
                              optional: true,
                                  type: String,
                             default_value: nil),
     FastlaneCore::ConfigItem.new(key: :force,
                             env_name: "CVB_FORCE_COMMIT",
                          description: "Forces the commit, even if other files than the ones containing the version number have been modified",
                             optional: true,
                        default_value: false,
                            is_string: false)
  ]
end

.descriptionObject



108
109
110
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 108

def self.description
  "This Android plugins allow you to commit every modification done in your build.gradle file during the execution of a lane. In fast, it do the same as the commit_version_bump action, but for Android"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 143

def self.is_supported?(platform)
  [:android].include?(platform)
  true
end

.run(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 4

def self.run(params)
    require 'xcodeproj'
    require 'pathname'
    require 'set'
  require 'shellwords'

  build_file_folder ||= params[:gradle_file_folder]
  if build_file_folder != nil
      UI.message("The commit_android_version_bump plugin will use gradle file at (#{build_file_folder})!")

      absolute_path = File.expand_path(build_file_folder)
      build_file_path = build_file_folder+"/build.gradle"
      # find the repo root path
      repo_path = Actions.sh("git -C #{absolute_path} rev-parse --show-toplevel").strip
      repo_pathname = Pathname.new(repo_path)
  else
      app_folder_name ||= params[:app_folder_name]
      UI.message("The commit_android_version_bump plugin is looking inside your project folder (#{app_folder_name})!")

      build_folder_paths = Dir[File.expand_path(File.join('**/', app_folder_name))].reject { |file| ['build/', 'node_modules/'].any? { |part| file.include? part } }
      # no build.gradle found: error
      UI.user_error!('Could not find a build folder in the current repository\'s working directory.') if build_folder_paths.count == 0

      UI.message("Found the following project path: #{build_folder_paths}")
      # too many projects found: error
      if build_folder_paths.count > 1
          UI.user_error!("Found multiple build.gradle projects in the current repository's working directory.")
      end

      build_folder_path = build_folder_paths.first

      # find the repo root path
      repo_path = Actions.sh("git -C #{build_folder_path} rev-parse --show-toplevel").strip
      repo_pathname = Pathname.new(repo_path)


      build_file_paths = Dir[File.expand_path(File.join('**/', app_folder_name, 'build.gradle'))].reject { |file| file.include? 'node_modules/' }

      # no build.gradle found: error
      UI.user_error!('Could not find a build.gradle in the current repository\'s working directory.') if
          build_file_paths.count == 0
      # too many projects found: error
      if build_file_paths.count > 1
          UI.user_error!("Found multiple build.gradle projects in the current repository's working directory.")
      end

      build_file_path = build_file_paths.first
      build_file_path.replace build_file_path.sub("#{repo_pathname}/", "")
  end

  # create our list of files that we expect to have changed, they should all be relative to the project root, which should be equal to the git workdir root
  expected_changed_files = []
  expected_changed_files << build_file_path

  # get the list of files that have actually changed in our git workdir
 git_dirty_files = Actions.sh("git -C #{repo_path} diff --name-only HEAD").split("\n") + Actions.sh("git -C #{repo_path} ls-files --other --exclude-standard").split("\n")

 # little user hint
 UI.user_error!("No file changes picked up. Make sure you run the `increment_version_code` action first.") if git_dirty_files.empty?

 # check if the files changed are the ones we expected to change (these should be only the files that have version info in them)
 changed_files_as_expected = (Set.new(git_dirty_files.map(&:downcase)).subset? Set.new(expected_changed_files.map(&:downcase)))

 unless changed_files_as_expected
     unless params[:force]
         error = [
             "Found unexpected uncommited changes in the working directory. Expected these files to have ",
             "changed: \n#{expected_changed_files.join("\n")}.\nBut found these actual changes: ",
             "#{git_dirty_files.join("\n")}.\nMake sure you have cleaned up the build artifacts and ",
             "are only left with the changed version files at this stage in your lane, and don't touch the ",
             "working directory while your lane is running. You can also use the :force option to bypass this ",
             "check, and always commit a version bump regardless of the state of the working directory."
         ].join("\n")
         UI.user_error!(error)
     end
  end

  # get the absolute paths to the files
  git_add_paths = expected_changed_files.map do |path|
      updated = path
      updated.replace updated.sub("#{repo_pathname}", "./")#.gsub(repo_pathname, ".")
      puts " -- updated = #{updated}".yellow
      File.expand_path(File.join(repo_pathname, updated))
  end

  # then create a commit with a message
  Actions.sh("git -C #{repo_path} add #{git_add_paths.map(&:shellescape).join(' ')}")

  begin
      version_code = Actions.lane_context["VERSION_CODE"]

      params[:message] ||= (version_code ? "Version Bump to #{version_code}" : "Version Bump")

      Actions.sh("git -C #{repo_path} commit -m '#{params[:message]}'")

      UI.success("Committed \"#{params[:message]}\" 💾.")
  rescue => ex
      UI.error(ex)

      UI.important("Didn't commit any changes.")
  end

end