Class: Fastlane::Actions::AnalyzeSwiftlintAction

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

Overview

代码分析

Class Method Summary collapse

Class Method Details

.available_optionsObject



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
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fastlane/plugin/fastci/actions/analyze_swiftlint_action.rb', line 88

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :is_all,
      description: "是否检查所有文件",
      optional: true, 
      default_value: true,
      type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
      key: :is_from_package,
      description: "是否从打包流程调用",
      optional: true,
      default_value: false,
      type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
      key: :configuration,
      description: "构建配置",
      optional: true,
      default_value: "Release",
      type: String,
      verify_block: proc do |value|
        valid_params = ["Release", "Debug"]
        unless valid_params.include?(value)
          UI.user_error!("无效的编译环境: #{value}。支持的环境: #{valid_params.join(', ')}")
        end
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :commit_hash,
      description: "上一次提交哈希, 会比较该哈希到最新哈希",
      optional: true,
      default_value: nil,
      type: String
    )
  ]
end

.categoryObject



131
132
133
# File 'lib/fastlane/plugin/fastci/actions/analyze_swiftlint_action.rb', line 131

def self.category
  :testing
end

.descriptionObject



84
85
86
# File 'lib/fastlane/plugin/fastci/actions/analyze_swiftlint_action.rb', line 84

def self.description
  "静态代码分析"
end

.is_supported?(platform) ⇒ Boolean



127
128
129
# File 'lib/fastlane/plugin/fastci/actions/analyze_swiftlint_action.rb', line 127

def self.is_supported?(platform)
  platform == :ios
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
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
# File 'lib/fastlane/plugin/fastci/actions/analyze_swiftlint_action.rb', line 8

def self.run(params)
  UI.message("*************| 开始代码分析 |*************")

  # 检查是否安装了 SwiftLint
  unless system("which swiftlint > /dev/null")
    sh("brew install swiftlint")
  end

  is_all = params[:is_all] || true
  is_from_package = params[:is_from_package] || false
  configuration = params[:configuration] || "Debug"

  # 如果不是从打包流程调用,需要先构建项目
  if is_from_package == false
    UI.message("*************| 构建项目以生成索引存储 |*************")

    other_action.gym(
      clean: true,
      silent: true,
      workspace: Environment.workspace,
      scheme: Environment.scheme,
      configuration: configuration,
      buildlog_path: Constants.BUILD_LOG_DIR,
      skip_archive: true,
      skip_package_ipa: true
    )
  end

  log_dir = File.expand_path(Constants.BUILD_LOG_DIR)
  log_file = sh("ls -t #{log_dir}/*.log | head -n 1").strip

  yml_file = File.expand_path(".swiftlint.yml")

  analyze_file = File.expand_path(Constants.SWIFTLINT_ANALYZE_FILE)

  if is_all
    sh("
      swiftlint analyze \
      --quiet \
      --config #{yml_file} \
      --compiler-log-path #{log_file} \
      --force-exclude \
      > #{analyze_file} || true
    ")
  else
    commit_hash = params[:commit_hash] || read_cached_txt(Constants.COMMIT_HASH_FILE)
    swift_files = CommonHelper.get_git_modified_swift_files(commit_hash)

    if swift_files.empty?
      UI.message("*************|❗没有 Swift 变更文件,跳过代码分析❗|*************")
      return
    end

    files_to_analyze = swift_files.join(" ")
    sh("
      swiftlint analyze \
      --quiet \
      --config #{yml_file} \
      --compiler-log-path #{log_file} \
      #{files_to_analyze} \
      --force-exclude \
      > #{analyze_file} || true
    ")
  end

  # 输出代码分析报告
  UI.message("*************| 开始输出代码分析报告 |*************")

  CommonHelper.generate_and_open_html(
    "Code Analyze Report",
    "generate_lint_html.py",
    Constants.SWIFTLINT_ANALYZE_FILE,
    Constants.SWIFTLINT_ANALYZE_HTML_FILE
  )
end