Class: Highway::Runtime::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/highway/runtime/context.rb

Overview

This class is responsible for maintaining runtime context between step invocations, allowing steps to access runtimes of both Fastlane and Highway.

Context and environment collapse

Reports collapse

Context and environment collapse

Reports collapse

Assertions collapse

Interfacing with Fastlane collapse

Interfacing with shell collapse

Instance Method Summary collapse

Constructor Details

#initialize(fastlane_runner:, fastlane_lane_context:, env:, interface:) ⇒ Context

Initialize an instance.

Parameters:

  • fastlane_runner (Fastlane::Runner)

    The Fastlane runner.

  • fastlane_lane_context (Hash)

    The Fastlane lane context.

  • env (Highway::Runtime::Environment)

    The runtime environment.

  • reporter (Highway::Interface)

    The interface.



26
27
28
29
30
31
32
# File 'lib/highway/runtime/context.rb', line 26

def initialize(fastlane_runner:, fastlane_lane_context:, env:, interface:)
  @fastlane_runner = fastlane_runner
  @fastlane_lane_context = fastlane_lane_context
  @env = env
  @interface = interface
  @reports = Array.new()
end

Instance Attribute Details

#envHighway::Runtime::Environment (readonly)

The environment of the runtime context.

Returns:

  • (Highway::Runtime::Environment)


39
40
41
# File 'lib/highway/runtime/context.rb', line 39

def env
  @env
end

#fastlane_lane_contextHash (readonly)

The Fastlane lane context.

Returns:

  • (Hash)


44
45
46
# File 'lib/highway/runtime/context.rb', line 44

def fastlane_lane_context
  @fastlane_lane_context
end

#interfaceHighway::Interface (readonly)

The interface.

Returns:



49
50
51
# File 'lib/highway/runtime/context.rb', line 49

def interface
  @interface
end

#reportsArray<Highway::Runtime::Report> (readonly)

All reports in the runtime context.

Returns:



86
87
88
# File 'lib/highway/runtime/context.rb', line 86

def reports
  @reports
end

Instance Method Details

#add_report(report) ⇒ Void

Add a runtime report to the context.

Parameters:

Returns:

  • (Void)


93
94
95
# File 'lib/highway/runtime/context.rb', line 93

def add_report(report)
  @reports << report
end

#archive_reportsArray<Highway::Runtime::Report>

Reports containing information about archives.

Returns:



121
122
123
# File 'lib/highway/runtime/context.rb', line 121

def archive_reports
  @reports.select { |report| report[:archive] != nil }
end

#artifacts_dirString

The path to directory containing artifacts.

Returns:

  • (String)


54
55
56
# File 'lib/highway/runtime/context.rb', line 54

def artifacts_dir
  File.join(File.expand_path(FastlaneCore::FastlaneFolder.path), "highway")
end

#assert_executable_available!(name) ⇒ Object

Assert that an executable with specified name is available.

Parameters:

  • name (String)

    Name of executable.



144
145
146
147
148
# File 'lib/highway/runtime/context.rb', line 144

def assert_executable_available!(name)
  unless FastlaneCore::CommandExecutor.which(name) != nil
    @interface.fatal!("Required executable '#{name}' could not be found. Make sure it's installed.")
  end
end

#assert_gem_available!(name) ⇒ Object

Assert that a gem with specified name is available.

Parameters:

  • name (String)

    Name of the gem.



137
138
139
# File 'lib/highway/runtime/context.rb', line 137

def assert_gem_available!(name)
  Fastlane::Actions.verify_gem!(name)
end

#deployment_reportsArray<Highway::Runtime::Report>

Reports containing information about deployments.

Returns:



128
129
130
# File 'lib/highway/runtime/context.rb', line 128

def deployment_reports
  @reports.select { |report| report[:deployment] != nil }
end

#duration_so_farNumeric

Total duration of all previous reports.

Returns:

  • (Numeric)


107
108
109
# File 'lib/highway/runtime/context.rb', line 107

def duration_so_far
  @reports.reduce(0) { |memo, report| memo + report.duration }
end

#reports_any_failed?Boolean

Whether any of the previous reports failed.

Returns:

  • (Boolean)


100
101
102
# File 'lib/highway/runtime/context.rb', line 100

def reports_any_failed?
  @reports.any? { |report| report.result == :failure }
end

#run_action(name, options:) ⇒ Object

Run a Fastlane action.

Parameters:

  • name (String, Symbol)

    Name of the action.

  • options (Hash)

    Options passed to the action.



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/highway/runtime/context.rb', line 174

def run_action(name, options:)

  unless contains_action?(name)
    @interface.fatal!("Can't execute action '#{name}' because it doesn't exist.")
  end

  unless !contains_lane?(name)
    @interface.fatal!("Can't execute action '#{name}' because a lane with the same name exists.")
  end

  run_lane_or_action(name, options)

end

#run_lane(name, options:) ⇒ Object

Run a Fastlane lane.

Parameters:

  • name (String, Symbol)

    Name of the lane.

  • options (Hash)

    Options passed to the lane.



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/highway/runtime/context.rb', line 156

def run_lane(name, options:)

  unless contains_lane?(name)
    @interface.fatal!("Can't execute lane '#{name}' because it doesn't exist.")
  end

  unless !contains_action?(name)
    @interface.fatal!("Can't execute lane '#{name}' because an action with the same name exists.")
  end

  run_lane_or_action(name, options)

end

#run_sh(command, bundle_exec: false, silent: false, on_error: nil) ⇒ Object

Run a shell command.

Parameters:

  • command (String, Array)

    A shell command.

  • bundle_exec (Boolean) (defaults to: false)

    Whether to use ‘bundle exec` if possible.

  • silent (Boolean) (defaults to: false)

    Whether to run the command silently.

  • on_error (Proc) (defaults to: nil)

    Called if command exits with a non-zero code.



203
204
205
206
# File 'lib/highway/runtime/context.rb', line 203

def run_sh(command, bundle_exec: false, silent: false, on_error: nil)
  command = ["bundle exec", command].flatten if bundle_exec && should_use_bundle_exec?
  Fastlane::Actions.sh(command, log: !silent, error_callback: on_error)
end

#should_use_bundle_exec?Boolean

Whether ‘bundle exec` is available and should be used if possible.

Returns:

  • (Boolean)


193
194
195
# File 'lib/highway/runtime/context.rb', line 193

def should_use_bundle_exec?
  return File.exist?("Gemfile")
end

#test_reportsArray<Highway::Runtime::Report>

Reports containing information about tests.

Returns:



114
115
116
# File 'lib/highway/runtime/context.rb', line 114

def test_reports
  @reports.select { |report| report[:test] != nil }
end

#with_modified_env(new_env, &block) ⇒ Object

Execute the given block in the scope of overridden ENV variables. After that, old values will be restored.

Parameters:

  • new_env (Hash)

    ENV variables to override.

  • &block (Proc)

    A block to execute.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/highway/runtime/context.rb', line 63

def with_modified_env(new_env, &block)

  old_env = Utilities::hash_map(new_env.keys) { |name|
    [name, ENV[name]]
  }

  new_env.each_pair { |name, value|
    ENV[name] = value
  }

  block.call()

  old_env.each_pair { |name, value|
    ENV[name] = value
  }

end