Class: Fastlane::Actions::CommitVersionBumpAction

Inherits:
Fastlane::Action show all
Defined in:
lib/fastlane/actions/commit_version_bump.rb

Overview

Commits the current changes in the repo as a version bump, checking to make sure only files which contain version information have been changed.

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, details, output, sh, step_text

Class Method Details

.authorObject


107
108
109
# File 'lib/fastlane/actions/commit_version_bump.rb', line 107

def self.author
  "lmirosevic"
end

.available_optionsObject


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fastlane/actions/commit_version_bump.rb', line 84

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :message,
                                 env_name: "FL_COMMIT_BUMP_MESSAGE",
                                 description: "The commit message when committing the version bump",
                                 default_value: "Version Bump"),
    FastlaneCore::ConfigItem.new(key: :xcodeproj,
                                 env_name: "FL_BUILD_NUMBER_PROJECT",
                                 description: "The path to your project file (Not the workspace). If you have only one, this is optional",
                                 optional: true,
                                 verify_block: Proc.new do |value|
                                  raise "Please pass the path to the project, not the workspace".red if value.include?"workspace"
                                  raise "Could not find Xcode project".red unless File.exists?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :force,
                                 env_name: "FL_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


80
81
82
# File 'lib/fastlane/actions/commit_version_bump.rb', line 80

def self.description
  "Creates a 'Version Bump' commit. Run after `increment_build_number`"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)

111
112
113
# File 'lib/fastlane/actions/commit_version_bump.rb', line 111

def self.is_supported?(platform)
  [:ios, :mac].include?platform
end

.run(params) ⇒ Object


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
# File 'lib/fastlane/actions/commit_version_bump.rb', line 5

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

  xcodeproj_path = params[:xcodeproj] ? File.expand_path(File.join('.', params[:xcodeproj])) : nil

  # find the repo root path
  repo_path = Actions.sh('git rev-parse --show-toplevel').strip
  repo_pathname = Pathname.new(repo_path)

  if xcodeproj_path
    # ensure that the xcodeproj passed in was OK
    raise "Could not find the specified xcodeproj: #{xcodeproj_path}" unless File.directory?(xcodeproj_path)
  else
    # find an xcodeproj (ignoring the Cocoapods one)
    xcodeproj_paths = Dir[File.expand_path(File.join(repo_path, '**/*.xcodeproj'))].reject { |path| /Pods\/.*.xcodeproj/ =~ path }

    # no projects found: error
    raise 'Could not find a .xcodeproj in the current repository\'s working directory.'.red if xcodeproj_paths.count == 0

    # too many projects found: error
    if xcodeproj_paths.count > 1
      relative_projects = xcodeproj_paths.map { |e| Pathname.new(e).relative_path_from(repo_pathname).to_s }.join("\n")
      raise "Found multiple .xcodeproj projects in the current repository's working directory. Please specify your app's main project: \n#{relative_projects}".red
    end

    # one project found: great
    xcodeproj_path = xcodeproj_paths.first
  end

  # find the pbxproj path, relative to git directory
  pbxproj_pathname = Pathname.new(File.join(xcodeproj_path, 'project.pbxproj'))
  pbxproj_path = pbxproj_pathname.relative_path_from(repo_pathname).to_s

  # find the info_plist files
  project = Xcodeproj::Project.open(xcodeproj_path)
  info_plist_files = project.objects.select { |object| object.isa == 'XCBuildConfiguration' }.map(&:to_hash).map { |object_hash| object_hash['buildSettings'] }.select { |build_settings| build_settings.key?('INFOPLIST_FILE') }.map { |build_settings| build_settings['INFOPLIST_FILE'] }.uniq.map { |info_plist_path| Pathname.new(File.expand_path(File.join(xcodeproj_path, '..', info_plist_path))).relative_path_from(repo_pathname).to_s }

  # 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 << pbxproj_path
  expected_changed_files << info_plist_files
  expected_changed_files.flatten!.uniq!

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

  # little user hint
  raise 'No file changes picked up. Make sure you run the `increment_build_number` action first.'.red 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)) == Set.new(expected_changed_files.map(&:downcase)))
  unless changed_files_as_expected
    unless params[:force]
      raise "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: \n#{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.".red
    end
  end

  # get the absolute paths to the files
  git_add_paths = expected_changed_files.map { |path| File.expand_path(File.join(repo_pathname, path)) }

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

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

    Helper.log.info "Committed \"#{params[:message]}\" 💾.".green
  rescue => ex
    Helper.log.info "Didn't commit any changes.".yellow
  end
end