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",
  "gym",
  "spaceship",
  "pilot",
  "supply",
  "scan",
  "screengrab",
  "match"
]

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, lane_context, method_missing, other_action, output, return_value, sample_return_value, sh, step_text

Class Method Details

.all_installed_toolsObject



106
107
108
# File 'lib/fastlane/actions/update_fastlane.rb', line 106

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

.authorObject



142
143
144
# File 'lib/fastlane/actions/update_fastlane.rb', line 142

def self.author
  "milch"
end

.available_optionsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fastlane/actions/update_fastlane.rb', line 128

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

.categoryObject



159
160
161
# File 'lib/fastlane/actions/update_fastlane.rb', line 159

def self.category
  :misc
end

.descriptionObject



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

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

.detailsObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fastlane/actions/update_fastlane.rb', line 114

def self.details
  [
    "This action will look at all installed fastlane tools and update them to the next available minor version - major version updates will not be performed automatically, as they might include breaking changes. If an update was performed, fastlane will be restarted before the run continues.",
    "If you are using rbenv or rvm, everything should be good to go. However, if you are using the system's default ruby, some additional setup is needed for this action to work correctly. In short, fastlane needs to be able to access your gem library without running in `sudo` mode.",
    "The simplest possible fix for this is putting the following lines into your `~/.bashrc` or `~/.zshrc` file:",
    "```bash",
    "export GEM_HOME=~/.gems",
    "export PATH=$PATH:~/.gems/bin",
    "```",
    "After the above changes, restart your terminal, then run `mkdir $GEM_HOME` to create the new gem directory. After this, you're good to go!",
    "Recommended usage of the `update_fastlane` action is at the top of the `before_all` block, before running any other action"
  ].join("\n\n")
end

.example_codeObject



150
151
152
153
154
155
156
157
# File 'lib/fastlane/actions/update_fastlane.rb', line 150

def self.example_code
  [
    'before_all do
      update_fastlane
      # ...
    end'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/fastlane/actions/update_fastlane.rb', line 146

def self.is_supported?(platform)
  true
end

.run(options) ⇒ Object



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

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
    UI.error("No tools specified or couldn't find any installed fastlane.tools")
    return
  end

  UI.message("Looking for updates for #{tools_to_update.join(', ')}...")

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

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

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

  unless updater.respond_to? :highest_installed_gems
    UI.important "The update_fastlane action requires rubygems version 2.1.0 or greater."
    UI.important "Please update your version of ruby gems before proceeding."
    UI.command "gem install rubygems-update"
    UI.command "update_rubygems"
    UI.command "gem update --system"
    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
    UI.success("Nothing to update ✅")
    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)
    update_url = FastlaneCore::UpdateChecker.generate_fetch_url(tool)
    latest_version = FastlaneCore::UpdateChecker.fetch_latest(update_url)
    UI.message("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)

    UI.message("Finished updating #{tool}")
  end

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

  if all_updated_tools.empty?
    UI.message("All fastlane tools are up-to-date!")
  else
    UI.message("Cleaning up old versions...")
    cleaner.options[:args] = all_updated_tools.map(&:name)
    cleaner.execute
    UI.message("fastlane.tools successfully 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 ' '}"
  end
end