Class: Fastlane::Actions::UpdateFastlaneAction

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

Overview

Makes sure fastlane tools are up-to-date when running fastlane

Constant Summary collapse

ALL_TOOLS =
[
  "fastlane",
  "fastlane_core",
  "deliver",
  "snapshot",
  "frameit",
  "pem",
  "sigh",
  "produce",
  "cert",
  "codes",
  "credentials_manager"
]

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, details, output, sh, step_text

Class Method Details

.all_installed_toolsObject



88
89
90
# File 'lib/fastlane/actions/update_fastlane.rb', line 88

def self.all_installed_tools
  Gem::Specification.select { |s| ALL_TOOLS.include? s.name }.map {|s| s.name}.uniq
end

.authorObject



110
111
112
# File 'lib/fastlane/actions/update_fastlane.rb', line 110

def self.author
  "milch"
end

.available_optionsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fastlane/actions/update_fastlane.rb', line 96

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :tools,
                                 env_name: "FL_TOOLS_TO_UPDATE",
                                 description: "Comma separated list of fastlane tools to update (e.g. fastlane,deliver,sigh). If not specified, all currently installed fastlane-tools will be updated",
                                 optional: true),
   FastlaneCore::ConfigItem.new(key: :no_update,
                                env_name: "FL_NO_UPDATE",
                                description: "Don't update during this run. Defaults to false",
                                is_string: false,
                                default_value: false),
  ]
end

.descriptionObject



92
93
94
# File 'lib/fastlane/actions/update_fastlane.rb', line 92

def self.description
  "Makes sure fastlane-tools are up-to-date when running fastlane"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/fastlane/actions/update_fastlane.rb', line 114

def self.is_supported?(platform)
  true
end

.run(options) ⇒ Object



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

def self.run(options)
  if options[:no_update]
    return
  end

  tools_to_update = options[:tools].split ',' unless options[:tools].nil?
  tools_to_update ||= all_installed_tools

  if tools_to_update.count == 0
    Helper.log.error "No tools specified or couldn't find any installed fastlane.tools".red
    return
  end

  updater = Gem::CommandManager.instance[:update]
  cleaner = Gem::CommandManager.instance[:cleanup]

  sudo_needed = !File.writable?(Gem.dir)

  if sudo_needed
    Helper.log.warn "It seems that your Gem directory is not writable by your current User."
    Helper.log.warn "Fastlane would need sudo rights to update itself, however, running 'sudo fastlane' is not recommended."
    Helper.log.warn "If you still want to use this action, please read the Actions.md documentation on a guide how to set this up."
    return
  end

  highest_versions = updater.highest_installed_gems.keep_if {|key| tools_to_update.include? key }
  update_needed = updater.which_to_update(highest_versions, tools_to_update)

  if update_needed.count == 0
    Helper.log.info "Nothing to update ✅".yellow
    return
  end

  #suppress updater output - very noisy
  Gem::DefaultUserInteraction.ui = Gem::SilentUI.new

  update_needed.each do |tool_info| 
    tool = tool_info[0]
    local_version = Gem::Version.new(highest_versions[tool].version)
    latest_version = FastlaneCore::UpdateChecker.fetch_latest(tool)
    Helper.log.info "Updating #{tool} from #{local_version} to #{latest_version} ... 🚀"

    # Approximate_recommendation will create a string like "~> 0.10" from a version 0.10.0, e.g. one that is valid for versions >= 0.10 and <1.0
    updater.update_gem tool, Gem::Requirement.new(local_version.approximate_recommendation)
    
    Helper.log.info "Finished updating #{tool}"
  end

  all_updated_tools = updater.installer.installed_gems.select do |updated_tool|
    updated_tool.version > highest_versions[updated_tool.name].version
  end

  unless all_updated_tools.empty?
    Helper.log.info "Cleaning up old versions..."
    cleaner.options[:args] = all_updated_tools.map {|t| t.name }
    cleaner.execute
    Helper.log.info "fastlane.tools succesfully updated! I will now restart myself... 😴"
    
    # Set no_update to true so we don't try to update again
    exec "FL_NO_UPDATE=true #{$PROGRAM_NAME} #{ARGV.join ' '}"
  else 
    Helper.log.info "All fastlane tools are up-to-date!"
  end
end