Class: FastlaneCore::ToolCollector

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

Constant Summary collapse

HOST_URL =
ENV['FASTLANE_ENHANCER_URL'] || "https://fastlane-enhancer.herokuapp.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeToolCollector

Returns a new instance of ToolCollector.



23
24
25
# File 'lib/fastlane_core/tool_collector.rb', line 23

def initialize
  @crash = false
end

Instance Attribute Details

#crashObject (readonly)

This is the newer field for tracking only uncontrolled exceptions.

This is written to only when ‘did_crash` is called, and therefore excludes controlled exceptions.

This value is a boolean, which is true if the error was an uncontrolled exception



21
22
23
# File 'lib/fastlane_core/tool_collector.rb', line 21

def crash
  @crash
end

#errorObject (readonly)

This is the original error reporting mechanism, which has always represented either controlled (UI.user_error!), or uncontrolled (UI.crash!, anything else) exceptions.

Thus, if you call ‘did_crash`, it will record the failure both here, and in the newer, more specific `crash` field.

This value is a String, which is the name of the tool that caused the error



13
14
15
# File 'lib/fastlane_core/tool_collector.rb', line 13

def error
  @error
end

Class Method Details

.determine_version(name) ⇒ Object



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

def self.determine_version(name)
  begin
    name = name.to_s.downcase

    # We need to pre-load the version file because tools that are invoked through their actions
    # will not yet have run their action, and thus will not yet have loaded the file which defines
    # the module and constant we need.
    require File.join(name, "version")

    # Go from :foo_bar to 'FooBar'
    module_name = name.fastlane_module

    # Look up the VERSION constant defined for the given tool name,
    # or return 'unknown' if we can't find it where we'd expect
    if Kernel.const_defined?(module_name)
      tool_module = Kernel.const_get(module_name)

      if tool_module.const_defined?('VERSION')
        return tool_module.const_get('VERSION')
      end
    end
  rescue LoadError
    # If there is no version file to load, this is not a tool for which
    # we can report a particular version
  end

  return nil
end

Instance Method Details

#determine_version(name) ⇒ Object



138
139
140
# File 'lib/fastlane_core/tool_collector.rb', line 138

def determine_version(name)
  self.class.determine_version(name)
end

#did_crash(name) ⇒ Object

Call when the problem is an uncaught/uncontrolled exception (e.g. via UI.crash!)



46
47
48
49
50
51
52
53
54
55
# File 'lib/fastlane_core/tool_collector.rb', line 46

def did_crash(name)
  name = name_to_track(name.to_sym)
  return unless name

  # Write to the @error field to maintain the historical behavior of the field, so
  # that the server gets the same data in that field from old and new clients
  @error = name
  # Also specifically note that this exception was uncontrolled in the @crash field
  @crash = true
end

#did_finishObject



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
# File 'lib/fastlane_core/tool_collector.rb', line 57

def did_finish
  return false if ENV["FASTLANE_OPT_OUT_USAGE"]

  if !did_show_message? and !Helper.is_ci?
    show_message
  end

  # `fastfile_id` helps us track success/failure metrics for Fastfiles we
  # generate as part of an automated process.
  require 'excon'
  url = HOST_URL + '/did_launch?'
  url += URI.encode_www_form(
    versions: versions.to_json,
    steps: launches.to_json,
    error: @error || "",
    crash: @crash ? @error : "",
    fastfile_id: ENV["GENERATED_FASTFILE_ID"] || ""
  )

  if Helper.is_test? # don't send test data
    return url
  else
    fork do
      begin
        Excon.post(url)
      rescue
        # we don't want to show a stack trace if something goes wrong
      end
    end
    return true
  end
rescue
  # We don't care about connection errors
end

#did_launch_action(name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/fastlane_core/tool_collector.rb', line 27

def did_launch_action(name)
  name = name_to_track(name.to_sym)
  return unless name

  launches[name] += 1
  versions[name] ||= determine_version(name)
end

#did_raise_error(name) ⇒ Object

Call when the problem is a caught/controlled exception (e.g. via UI.user_error!)



36
37
38
39
40
41
42
43
# File 'lib/fastlane_core/tool_collector.rb', line 36

def did_raise_error(name)
  name = name_to_track(name.to_sym)
  return unless name

  @error = name
  # Don't write to the @crash field so that we can distinguish this exception later
  # as being controlled
end

#did_show_message?Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
# File 'lib/fastlane_core/tool_collector.rb', line 131

def did_show_message?
  path = File.join(File.expand_path('~'), '.did_show_opt_info')
  did_show = File.exist?(path)
  File.write(path, '1') unless did_show
  did_show
end

#is_official?(name) ⇒ Boolean

Override this in subclasses

Returns:

  • (Boolean)


119
120
121
# File 'lib/fastlane_core/tool_collector.rb', line 119

def is_official?(name)
  return true
end

#launchesObject



101
102
103
# File 'lib/fastlane_core/tool_collector.rb', line 101

def launches
  @launches ||= Hash.new(0)
end

#name_to_track(name) ⇒ Object

Returns nil if we shouldn’t track this action Returns a (maybe modified) name that should be sent to the enhancer web service Modificiation is used to prefix the action name with the name of the plugin



126
127
128
129
# File 'lib/fastlane_core/tool_collector.rb', line 126

def name_to_track(name)
  return nil unless is_official?(name)
  name
end

#show_messageObject



92
93
94
95
96
97
98
99
# File 'lib/fastlane_core/tool_collector.rb', line 92

def show_message
  UI.message("Sending Crash/Success information. More information on: https://github.com/fastlane/enhancer")
  UI.message("No personal/sensitive data is sent. Only sharing the following:")
  UI.message(launches)
  UI.message(@error) if @error
  UI.message("This information is used to fix failing tools and improve those that are most often used.")
  UI.message("You can disable this by setting the environment variable: FASTLANE_OPT_OUT_USAGE=1")
end

#versionsObject

Maintains a hash of tool names to their detected versions.

This data is sent in the same manner as launches, as an inline form-encoded JSON value in the POST. For example:

match: '0.5.0',
fastlane: '1.86.1'



114
115
116
# File 'lib/fastlane_core/tool_collector.rb', line 114

def versions
  @versions ||= {}
end