Class: Fastlane::Actions::RubyAnalyzerAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



94
95
96
97
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 94

def self.authors
  # So no one will ever forget your contribution to fastlane :) You are awesome btw!
  ["olgakn"]
end

.available_optionsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 64

def self.available_options
  # Define all options your action supports.

  # Below a few examples
  [
    FastlaneCore::ConfigItem.new(key: :result_dir,
                  env_name: "FL_RUBY_ANALYZER_RESULT_DIR",
                  description: "Directory's name for storing  analysis results",
                  optional: true,
                  type: String,
                  default_value: 'artifacts'),
    FastlaneCore::ConfigItem.new(key: :ruby_files,
                  env_name: "FL_RUBY_ANALYZER_FILES_TO_INSPECT",
                  description: "List of path (relative to work directory) to ruby files to be inspected",
                  optional: true,
                  type: Array)
  ]
end

.descriptionObject



54
55
56
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 54

def self.description
  "This analyzer detect warnings, errors and check syntax in ruby files. This is based on rubocop"
end

.detailsObject



58
59
60
61
62
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 58

def self.details
  # Optional:
  # this is your chance to provide a more detailed description of this action
  # "You can use this action to do cool things..."
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 99

def self.is_supported?(platform)
  # you can do things like
  #
  #  true
  #
  #  platform == :ios
  #
  #  [:ios, :mac].include?(platform)
  #
  true
end

.outputObject



83
84
85
86
87
88
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 83

def self.output
  # Define the shared values you are going to provide
  [
    ['RUBY_ANALYZER_STATUS', 'Ruby analyzer result status (0 - success, any other value - failed)']
  ]
end

.return_valueObject



90
91
92
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 90

def self.return_value
  # If you method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 8

def self.run(params)
  UI.header 'Ruby analyzer' if Actions::CodeStaticAnalyzerAction.run_from_main_action
  work_dir = Actions::CodeStaticAnalyzerAction.work_dir

  # checking files for analysing
  files_to_inspect = params[:ruby_files]

  UI.message '[!] Ruby analyzer will be run for all ruby files in work directory'.blue if !files_to_inspect or files_to_inspect.empty?
  Actions::CodeStaticAnalyzerAction.check_file_exist(work_dir, files_to_inspect, 'ruby_files')

  # prepare script and metadata for saving results
  result_dir_path = "#{work_dir}#{params[:result_dir]}"
  FileUtils.mkdir_p(result_dir_path) unless File.exist?(result_dir_path)
  temp_result_file = "#{result_dir_path}/temp_ruby.json"
  result_file = "#{result_dir_path}/codeAnalysResults_ruby.xml"
  files = Actions::CodeStaticAnalyzerAction.add_root_path(work_dir, files_to_inspect, true)
  run_script = "bundle exec rubocop -f j -a #{files}"
  run_script_path = File.join CodeStaticAnalyzer::ROOT, "assets/run_script.sh"
  run_script = "#{run_script_path} \"#{run_script}\" '#{temp_result_file}'"
  # use analyzer
  FastlaneCore::CommandExecutor.execute(command: run_script.to_s,
                                      print_all: false,
                                      error: proc do |error_output|
                                               # handle error here
                                             end)
  status = $?.exitstatus
  # prepare results
  if Dir.glob(temp_result_file).empty?
    info = (status == 2) ? 'Rubocop return 2: terminates abnormally due to invalid configuration, invalid CLI options, or an internal error' : ''
    Actions::CodeStaticAnalyzerAction.start_xml_content unless Actions::CodeStaticAnalyzerAction.run_from_main_action
    Actions::CodeStaticAnalyzerAction.add_xml_content("#{result_dir_path}/", 'Ruby', temp_result_file, info)
    Actions::CodeStaticAnalyzerAction.create_analyzers_run_result("#{result_dir_path}/") unless Actions::CodeStaticAnalyzerAction.run_from_main_action
    status = 43
  else
    status = 0 if File.read(temp_result_file).empty?
    xml_content = JunitParser.parse_json(temp_result_file)
    junit_xml = JunitParser.add_testsuite('rubocop', xml_content)
    JunitParser.create_junit_xml(junit_xml, result_file)
  end
  Actions.lane_context[SharedValues::RUBY_ANALYZER_STATUS] = status
end