Class: Fastlane::Actions::DetectUnusedCodeAction

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

Overview

无用代码检查

Class Method Summary collapse

Class Method Details

.available_optionsObject



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

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :is_from_package,
      description: "是否从打包流程调用",
      optional: true,
      default_value: false,
      type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
      key: :configuration,
      description: "构建配置。默认只支持 Debug,需要支持 Release 请在 Build settings 中将 Enable Index-While-Building Functionality 设置为 Yes",
      optional: true,
      default_value: "Debug",
      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
    )
  ]
end

.categoryObject



100
101
102
# File 'lib/fastlane/plugin/fastci/actions/detect_unused_code_action.rb', line 100

def self.category
  :testing
end

.descriptionObject



67
68
69
# File 'lib/fastlane/plugin/fastci/actions/detect_unused_code_action.rb', line 67

def self.description
  "无用代码检测"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/fastlane/plugin/fastci/actions/detect_unused_code_action.rb', line 96

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
# File 'lib/fastlane/plugin/fastci/actions/detect_unused_code_action.rb', line 8

def self.run(params)
  UI.message("*************| 开始无用代码检查 |*************")

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

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

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

    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
  index_store_path = CommonHelper.extract_index_store_path(log_file)

  schemes = Environment.schemes
  if schemes.empty?
    schemes = Environment.scheme
  end

  # 运行 Periphery 扫描
  periphery_output = sh("
  periphery scan \
  --skip-build \
  --project #{Environment.workspace} \
  --schemes #{schemes.map(&:strip).join(" ")} \
  --index-store-path '#{index_store_path}' \
  --format xcode 2>/dev/null || true
  ")

  CommonHelper.write_cached_txt(Constants.UNUSED_CODE_FILE, periphery_output)

  # 输出无用代码检查报告
  UI.message("*************| 开始输出无用代码检查报告 |*************")

  CommonHelper.generate_and_open_html(
    "Unused Code Report",
    "generate_unused_code_html.py",
    Constants.UNUSED_CODE_FILE,
    Constants.UNUSED_CODE_HTML_FILE
  )
end