Class: Fastlane::Actions::CheckForToolkitUpdatesAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb

Constant Summary collapse

TOOLKIT_SPEC_NAME =
'fastlane-plugin-wpmreleasetoolkit'

Class Method Summary collapse

Class Method Details

.authorsObject



64
65
66
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb', line 64

def self.authors
  ['Automattic']
end

.available_optionsObject



86
87
88
89
90
91
92
93
94
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb', line 86

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :skip_update_suggestion,
                                 env_name: 'CHECK_FOR_TOOLKIT_UPDATES_SKIP_UPDATE_SUGGESTION',
                                 description: 'If true, will still check for new versions, but will not ask if you want to run bundle update if an update is found',
                                 type: Boolean,
                                 default_value: false),
  ]
end

.descriptionObject



60
61
62
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb', line 60

def self.description
  'Check that we are on the latest version of the release toolkit, and propose to update if not'
end

.detailsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb', line 72

def self.details
  <<~DETAILS
    Check that we are on the latest version of the release toolkit, and propose to update if not.

    This action will check if you are on the *latest* version, regardless of the version requirement restriction you might have
    set in your Gemfile/Pluginfile; which means that even if you agree to the prompt suggesting you to run bundle update, it might not
    actually end up updating to the *latest* version automatically (esp. if the latest introduces breaking changes).

    Note that if it finds an update and you then agree to the prompt and run bundle update, the action will abort your fastlane invocation
    after running bundle update. This will let you check and commit the changes, before restarting the lane by re-invoking fastlane.
    This is also needed to ensure fastlane loads the new toolkit version after update on second run.
  DETAILS
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb', line 96

def self.is_supported?(platform)
  true
end

.return_valueObject



68
69
70
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb', line 68

def self.return_value
  'Returns the latest version of the toolkit available if your toolkit is not up-to-date, or nil if you are already up-to-date.'
end

.run(params) ⇒ Object



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
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb', line 11

def self.run(params)
  updater = Gem::CommandManager.instance[:update]
  installed_gems = updater.highest_installed_gems.select { |spec| spec == TOOLKIT_SPEC_NAME }
  local_version = Gem::Version.new(installed_gems[TOOLKIT_SPEC_NAME].version)
  UI.message("Currently using release toolkit version #{local_version.to_s.yellow}.")
  UI.message('Checking for updates now...')
  updates_needed = updater.which_to_update(installed_gems, [TOOLKIT_SPEC_NAME])

  if updates_needed.empty?
    UI.success('Your release toolkit is up-to-date! ✅')
    return nil
  end

  # Return type of which_to_update differs before/after RubyGems 3.1.0, so normalize to always use Gem::NameTuple
  updates_needed = Gem::NameTuple.from_list(updates_needed)
  latest_version = updates_needed.find { |gem_info| gem_info.name == TOOLKIT_SPEC_NAME }.version

  UI.message(['There is a newest version '.yellow, latest_version.to_s.red, ' of the release toolkit!'.yellow].join)
  warn_on_breaking_update(local_version, latest_version)

  return latest_version if params[:skip_update_suggestion] || !UI.confirm('Do you want to run bundle update now?')

  sh('bundle', 'update', TOOLKIT_SPEC_NAME)
  UI.abort_with_message! <<~UPDATE_MESSAGE
    #{TOOLKIT_SPEC_NAME} have been updated. Please check and commit the changes in your Gemfile.lock file,
    then restart your previous invocation of fastlane to use the new toolkit.
  UPDATE_MESSAGE
end

.warn_on_breaking_update(current_version, latest_version) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/check_for_toolkit_updates_action.rb', line 40

def self.warn_on_breaking_update(current_version, latest_version)
  current_semver_requirement = Gem::Requirement.new(current_version.approximate_recommendation)
  compatible_update = current_semver_requirement.satisfied_by?(latest_version)
  return if compatible_update

  new_semver_requirement = latest_version.approximate_recommendation
  UI.important <<~BREAKING_CHANGE_MESSAGE
    The latest version available (#{latest_version}) introduces breaking changes compared to the #{current_version} you are currently using.

     - To update to #{latest_version}, first edit your `Pluginfile` to use '#{new_semver_requirement}', run `bundle update`,
       then be sure to make all the necessary changes to your `Fastfile` (see the toolkit's CHANGELOG)
       to take those breaking changes into account.

     - If you are not ready to make the major version bump, you can still try to update to the latest compatible,
       non-breaking version by running `bundle update` now. This will not update to the latest #{latest_version}, but
       might still update to a newer version compatible with '#{current_semver_requirement}' if one exists; which is still valuable
       to at least get bugfixes, until you are ready to jump to the next major version later.
  BREAKING_CHANGE_MESSAGE
end