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



108
109
110
111
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 108

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

.available_optionsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 72

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),
    FastlaneCore::ConfigItem.new(key: :use_junit_format,
                  env_name: "FL_RUBY_ANALYZER_USE_JUNIT_RESULTS",
                  description: "Generate results in JUnit format.",
                  optional: true,
                  type: BOOL,
                  default_value: true)
  ]
end

.descriptionObject



62
63
64
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 62

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

.detailsObject



66
67
68
69
70
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 66

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)


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 113

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

.outputObject



97
98
99
100
101
102
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 97

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



104
105
106
# File 'lib/fastlane/plugin/code_static_analyzer/actions/ruby_analyzer.rb', line 104

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
49
50
51
52
53
54
55
56
# 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)
  result_file = "#{result_dir_path}/codeAnalysResults_ruby.xml"
  files = Actions::CodeStaticAnalyzerAction.add_root_path(work_dir, files_to_inspect, true)
  if params[:use_junit_format]
    temp_result_file = "#{result_dir_path}/ruby.json"
    run_script = "bundle exec rubocop -f j -a #{files}"
  else
    temp_result_file = "#{result_dir_path}/ruby.log"
    run_script = "bundle exec rubocop -a #{files}"
  end
  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?
    if params[:use_junit_format]
   UI.message 'Ruby analyzer generates result in JUnit format'
      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
  end
  Actions.lane_context[SharedValues::RUBY_ANALYZER_STATUS] = status
end