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



98
99
100
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 98

def self.authors
  ["jems"]
end

.available_optionsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 102

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: :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



94
95
96
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 94

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)


123
124
125
126
# File 'lib/fastlane/plugin/commit_android_version_bump/actions/commit_android_version_bump_action.rb', line 123

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
# 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'

  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))]
  # 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
  # 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'))]

  # 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}/", "")

  # 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