Class: FastlaneCore::UpdateChecker

Inherits:
Object
  • Object
show all
Defined in:
fastlane_core/lib/fastlane_core/update_checker/update_checker.rb

Overview

Verifies, the user runs the latest version of this gem

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.start_timeObject (readonly)

Returns the value of attribute start_time


40
41
42
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 40

def start_time
  @start_time
end

Class Method Details

.ensure_rubygems_sourceObject

Check if RubyGems is set as a gem source on some machines that might not be the case and then users can't find the update when running the specified command


107
108
109
110
111
112
113
114
115
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 107

def self.ensure_rubygems_source
  return if Helper.contained_fastlane?
  return if `gem sources`.include?("https://rubygems.org")
  puts("")
  UI.error("RubyGems is not listed as your Gem source")
  UI.error("You can run `gem sources` to see all your sources")
  UI.error("Please run the following command to fix this:")
  UI.command("gem sources --add https://rubygems.org")
end

.event_for_completion(tool, ci, duration, timestamp_seconds) ⇒ Object


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 230

def self.event_for_completion(tool, ci, duration, timestamp_seconds)
  {
    event_source: {
      oauth_app_name: oauth_app_name,
      product: 'fastlane'
    },
    actor: {
      name: 'tool',
      detail: tool || 'unknown'
    },
    action: {
      name: 'completed_with_duration'
    },
    primary_target: {
      name: 'duration',
      detail: duration.to_s
    },
    secondary_target: {
      name: 'ci',
      detail: secondary_target_string(ci)
    },
    millis_since_epoch: timestamp_seconds * 1000,
    version: 1
  }
end

.event_for_install_method(tool, ci, install_method, timestamp_seconds) ⇒ Object


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 256

def self.event_for_install_method(tool, ci, install_method, timestamp_seconds)
  {
    event_source: {
      oauth_app_name: oauth_app_name,
      product: 'fastlane'
    },
    actor: {
      name: 'tool',
      detail: tool || 'unknown'
    },
    action: {
      name: 'completed_with_install_method'
    },
    primary_target: {
      name: 'install_method',
      detail: install_method
    },
    secondary_target: {
      name: 'ci',
      detail: secondary_target_string(ci)
    },
    millis_since_epoch: timestamp_seconds * 1000,
    version: 1
  }
end

.event_for_launch(tool, ci, timestamp_seconds) ⇒ Object


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 172

def self.event_for_launch(tool, ci, timestamp_seconds)
  {
    event_source: {
      oauth_app_name: oauth_app_name,
      product: 'fastlane'
    },
    actor: {
      name: 'tool',
      detail: tool || 'unknown'
    },
    action: {
      name: 'launched'
    },
    primary_target: {
      name: 'ci',
      detail: ci
    },
    secondary_target: {
      name: 'launch',
      detail: secondary_target_string('')
    },
    millis_since_epoch: timestamp_seconds * 1000,
    version: 1
  }
end

.event_for_p_hash(p_hash, tool, platform, timestamp_seconds) ⇒ Object


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 146

def self.event_for_p_hash(p_hash, tool, platform, timestamp_seconds)
  {
    event_source: {
      oauth_app_name: oauth_app_name,
      product: 'fastlane'
    },
    actor: {
      name: 'project',
      detail: p_hash
    },
    action: {
      name: 'update_checked'
    },
    primary_target: {
      name: 'tool',
      detail: tool || 'unknown'
    },
    secondary_target: {
      name: 'platform',
      detail: secondary_target_string(platform || 'unknown')
    },
    millis_since_epoch: timestamp_seconds * 1000,
    version: 1
  }
end

.fetch_latest(gem_name) ⇒ Object


117
118
119
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 117

def self.fetch_latest(gem_name)
  JSON.parse(Excon.get(generate_fetch_url(gem_name)).body)["version"]
end

.generate_fetch_url(gem_name) ⇒ Object


121
122
123
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 121

def self.generate_fetch_url(gem_name)
  "https://rubygems.org/api/v1/gems/#{gem_name}.json"
end

.oauth_app_nameObject


286
287
288
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 286

def self.oauth_app_name
  return 'fastlane-refresher'
end

.secondary_target_string(string) ⇒ Object


282
283
284
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 282

def self.secondary_target_string(string)
  return string
end

.send_completion_events(tool, ci, install_method, duration, timestamp_seconds) ⇒ Object


290
291
292
293
294
295
296
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 290

def self.send_completion_events(tool, ci, install_method, duration, timestamp_seconds)
  analytics = []
  analytics << event_for_completion(tool, ci, duration, timestamp_seconds)
  analytics << event_for_install_method(tool, ci, install_method, timestamp_seconds)

  send_events(analytics)
end

.send_completion_events_for(gem_name) ⇒ Object


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 198

def self.send_completion_events_for(gem_name)
  return if FastlaneCore::Env.truthy?("FASTLANE_OPT_OUT_USAGE")

  ci = Helper.ci?.to_s
  install_method = if Helper.rubygems?
                     'gem'
                   elsif Helper.bundler?
                     'bundler'
                   elsif Helper.mac_app?
                     'mac_app'
                   elsif Helper.contained_fastlane?
                     'standalone'
                   elsif Helper.homebrew?
                     'homebrew'
                   else
                     'unknown'
                   end
  duration = (Time.now - start_time).to_i
  timestamp_seconds = Time.now.to_i

  send_completion_events(gem_name, ci, install_method, duration, timestamp_seconds)
end

.send_events(analytics) ⇒ Object


221
222
223
224
225
226
227
228
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 221

def self.send_events(analytics)
  analytic_event_body = { analytics: analytics }.to_json

  url = ENV["FASTLANE_METRICS_URL"] || "https://fastlane-metrics.fabric.io/public"
  Excon.post(url,
             body: analytic_event_body,
             headers: { "Content-Type" => 'application/json' })
end

.send_launch_analytic_events(p_hash, tool, platform, ci) ⇒ Object


136
137
138
139
140
141
142
143
144
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 136

def self.send_launch_analytic_events(p_hash, tool, platform, ci)
  timestamp_seconds = Time.now.to_i

  analytics = []
  analytics << event_for_p_hash(p_hash, tool, platform, timestamp_seconds) if p_hash
  analytics << event_for_launch(tool, ci, timestamp_seconds)

  send_events(analytics)
end

.send_launch_analytic_events_for(gem_name) ⇒ Object


125
126
127
128
129
130
131
132
133
134
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 125

def self.send_launch_analytic_events_for(gem_name)
  return if FastlaneCore::Env.truthy?("FASTLANE_OPT_OUT_USAGE")
  ci = Helper.ci?.to_s
  app_id_guesser = FastlaneCore::AppIdentifierGuesser.new(args: ARGV, gem_name: gem_name)
  project_hash = app_id_guesser.p_hash
  p_hash = project_hash if project_hash
  platform = @platform if @platform # this has to be called after `p_hash`

  send_launch_analytic_events(p_hash, gem_name, platform, ci)
end

.server_resultsObject


35
36
37
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 35

def self.server_results
  @results ||= {}
end

.show_update_message(gem_name, current_version) ⇒ Object

Show a message to the user to update to a new version of fastlane (or a sub-gem) Use this method, as this will detect the current Ruby environment and show an appropriate message to the user


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 65

def self.show_update_message(gem_name, current_version)
  available = server_results[gem_name]
  puts("")
  puts('#######################################################################')
  if available
    puts("# #{gem_name} #{available} is available. You are on #{current_version}.")
  else
    puts("# An update for #{gem_name} is available. You are on #{current_version}.")
  end
  puts("# You should use the latest version.")
  puts("# Please update using `#{self.update_command(gem_name: gem_name)}`.")

  puts("# To see what's new, open https://github.com/fastlane/#{gem_name}/releases.") if FastlaneCore::Env.truthy?("FASTLANE_HIDE_CHANGELOG")

  if !Helper.bundler? && !Helper.contained_fastlane? && Random.rand(5) == 1
    # We want to show this message from time to time, if the user doesn't use bundler, nor bundled fastlane
    puts('#######################################################################')
    puts("# Run `sudo gem cleanup` from time to time to speed up fastlane")
  end
  puts('#######################################################################')
  Changelog.show_changes(gem_name, current_version, update_gem_command: UpdateChecker.update_command(gem_name: gem_name)) unless FastlaneCore::Env.truthy?("FASTLANE_HIDE_CHANGELOG")

  ensure_rubygems_source
end

.show_update_status(gem_name, current_version) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 48

def self.show_update_status(gem_name, current_version)
  Thread.new do
    begin
      send_completion_events_for(gem_name)
    rescue
      # we don't want to show a stack trace if something goes wrong
    end
  end

  if update_available?(gem_name, current_version)
    show_update_message(gem_name, current_version)
  end
end

.start_looking_for_update(gem_name) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 12

def self.start_looking_for_update(gem_name)
  return if Helper.test?
  return if FastlaneCore::Env.truthy?("FASTLANE_SKIP_UPDATE_CHECK")

  @start_time = Time.now

  Thread.new do
    begin
      send_launch_analytic_events_for(gem_name)
    rescue
      # we don't want to show a stack trace if something goes wrong
    end
  end

  Thread.new do
    begin
      server_results[gem_name] = fetch_latest(gem_name)
    rescue
      # we don't want to show a stack trace if something goes wrong
    end
  end
end

.update_available?(gem_name, current_version) ⇒ Boolean

Returns:


43
44
45
46
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 43

def self.update_available?(gem_name, current_version)
  latest = server_results[gem_name]
  return (latest and Gem::Version.new(latest) > Gem::Version.new(current_version))
end

.update_command(gem_name: "fastlane") ⇒ Object

The command that the user should use to update their mac


91
92
93
94
95
96
97
98
99
100
101
# File 'fastlane_core/lib/fastlane_core/update_checker/update_checker.rb', line 91

def self.update_command(gem_name: "fastlane")
  if Helper.bundler?
    "bundle update #{gem_name.downcase}"
  elsif Helper.contained_fastlane? || Helper.homebrew?
    "fastlane update_fastlane"
  elsif Helper.mac_app?
    "the Fabric app. Launch the app and navigate to the fastlane tab to get the most recent version."
  else
    "sudo gem install #{gem_name.downcase}"
  end
end