Class: Fastlane::Actions::PeripheryAction::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/periphery/actions/periphery_action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Runner

Returns a new instance of Runner.



52
53
54
55
56
57
58
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 52

def initialize(params)
  @executable = params[:executable] || 'periphery'
  @config = expand_and_verify_path(params[:config])
  @skip_build = params[:skip_build]
  @index_store_path = expand_and_verify_path(params[:index_store_path])
  @results = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



41
42
43
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 41

def config
  @config
end

#executableObject (readonly)

Returns the value of attribute executable.



41
42
43
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 41

def executable
  @executable
end

#index_store_pathObject (readonly)

Returns the value of attribute index_store_path.



41
42
43
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 41

def index_store_path
  @index_store_path
end

#resultsObject (readonly)

Returns the value of attribute results.



41
42
43
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 41

def results
  @results
end

#skip_buildObject (readonly)

Returns the value of attribute skip_build.



41
42
43
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 41

def skip_build
  @skip_build
end

Instance Method Details

#expand_and_verify_path(path) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 43

def expand_and_verify_path(path)
  return nil if path.nil?

  path = File.expand_path(path)
  UI.user_error!("File or directory does not exist at path '#{path}'") unless File.exist?(path)

  path
end

#find_derived_data_pathObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 135

def find_derived_data_path
  # These values are set by other actions that may have been used to build an app previously
  candidates = [
    Actions.lane_context[SharedValues::SCAN_DERIVED_DATA_PATH],
    Actions.lane_context[SharedValues::XCODEBUILD_DERIVED_DATA_PATH]
  ]

  # Return the first candidate where the value was set and the directory still exists
  candidates.find { |x| !x.nil? && File.exist?(x) }
end

#perform_scanObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 74

def perform_scan
  # Run the periphery scan command and collect the output
  UI.message("Performing scan. This might take a few moments...")
  output = Actions.sh_control_output(scan_command, print_command_output: false, error_callback: lambda { |result|
    UI.error(result)
    UI.user_error!("The scan could not be completed successfully")
  })

  # Decode the JSON output and assign to the property/lane_context
  @results = Results.new(JSON.parse(output).map { |raw| Result.new(raw) })
  Actions.lane_context[SharedValues::PERIPHERY_RESULTS] = results
end


146
147
148
149
150
151
152
153
154
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 146

def print_summary
  # Group the results by their first hint (assume there is only one).
  grouped_results = results
                    .group_by { |result| result.hints.first }
                    .transform_values(&:count)

  # Print the counts in a table
  FastlaneCore::PrintTable.print_values(config: grouped_results, title: 'Summary of Results') unless Helper.test?
end

#resolve_index_store_pathObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 115

def resolve_index_store_path
  # If it was explicitly specified, return the path to the index store
  return index_store_path unless index_store_path.nil?

  # Alternatively, use the derived data path defined by a prior action
  derived_data_path = find_derived_data_path

  # Fail if we couldn't automatically resolve the path
  if derived_data_path.nil?
    UI.user_error!("The index store path could not be resolved. Either specify it using the index_store_path argument or provide a path to derived data when using build_app or xcodebuild actions.")
  end

  # https://github.com/peripheryapp/periphery#xcode
  if Helper.xcode_at_least?("14.0.0")
    return File.join(derived_data_path, 'Index.noindex', 'DataStore')
  else
    return File.join(derived_data_path, 'Index', 'DataStore')
  end
end

#runObject



60
61
62
63
64
65
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 60

def run
  verify_executable
  perform_scan
  print_summary
  results
end

#scan_commandObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 87

def scan_command
  # Build up the initial part of the command
  command = [
    executable,
    'scan',
    '--disable-update-check',
    '--quiet',
    '--format',
    'json'
  ]

  # Specify the path to the config if it was provided
  if config
    command << '--config'
    command << config
  end

  # Support --skip-build mode
  if skip_build || !index_store_path.nil?
    command << '--skip-build'
    command << '--index-store-path'
    command << resolve_index_store_path
  end

  # Return the complete array of arguments
  command
end

#verify_executableObject



67
68
69
70
71
72
# File 'lib/fastlane/plugin/periphery/actions/periphery_action.rb', line 67

def verify_executable
  version = Actions.sh_control_output([executable, 'version'], print_command_output: false).strip!
  UI.message("Using periphery version #{version}")
rescue
  UI.user_error!("Unable to invoke periphery executable '#{executable}'. Is it installed?")
end