Class: Fastlane::Actions::PackageAction

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

Overview

打包

Class Method Summary collapse

Class Method Details

.available_optionsObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 203

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :configuration,
      description: "编译环境 Release or Debug",
      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
    ),
    FastlaneCore::ConfigItem.new(
      key: :export_method,
      description: "打包方式 ad-hoc, enterprise, app-store, development",
      optional: true,
      default_value: "development",
      type: String,
      verify_block: proc do |value|
        valid_params = ["ad-hoc", "enterprise", "app-store", "development"]
        unless valid_params.include?(value)
          UI.user_error!("无效的打包方式: #{value}。支持的方式: #{valid_params.join(', ')}")
        end
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :version,
      description: "自定义 `version`。在 Xcode13 之后创建的项目,不再支持脚本修改。需要兼容请在 Build settings 中将 GENERATE_INFOPLIST_FILE 设置为 NO",
      optional: true,
      default_value: nil,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :build,
      description: "不采取自动更新,自定义 `build` 号",
      optional: true,
      default_value: nil,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :is_analyze_swiftlint,
      description: "是否代码分析",
      optional: true,
      default_value: false,
      type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
      key: :is_detect_duplicity_code,
      description: "是否检查重复代码",
      optional: true,
      default_value: false,
      type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
      key: :is_detect_unused_code,
      description: "是否检查无用代码",
      optional: true,
      default_value: false,
      type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
      key: :is_detect_unused_image,
      description: "是否检查无用图片",
      optional: true,
      default_value: false,
      type: Boolean
    ),
  ]
end

.categoryObject



280
281
282
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 280

def self.category
  :building
end

.descriptionObject



199
200
201
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 199

def self.description
  "打包"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 276

def self.is_supported?(platform)
  platform == :ios
end

.run(params) ⇒ Object



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
83
84
85
86
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 10

def self.run(params)

  # 入参配置
  configuration = params[:configuration] || "Debug"
  export_method = params[:export_method] || "development"
  build = params[:build] || nil
  version = params[:version] || nil
  is_analyze_swiftlint = params[:is_analyze_swiftlint] || false
  is_detect_duplicity_code = params[:is_detect_duplicity_code] || false
  is_detect_unused_code = params[:is_detect_unused_code] || false
  is_detect_unused_image = params[:is_detect_unused_image] || false
  if export_method == "app-store"
    configuration = "Release"
  end

  # 清理上一次的打包缓存
  FileUtils.rm_rf(Dir.glob("#{Constants.BUILD_LOG_DIR}/*"))
  FileUtils.rm_rf(Dir.glob("#{Constants.IPA_OUTPUT_DIR}/*"))
  
  # 安装证书
  InstallCertificateAction.run({})
  # 安装 provisioningProfile
  InstallProfileAction.run({})

  scheme = Environment.scheme

  # 更改项目version
  other_action.increment_version_number(
    version_number: version
  ) if CommonHelper.is_validate_string(version)

  # 更改项目build号
  UpdateBuildNumberAction.run(
    build: build
  )
  time = Time.new.strftime("%Y%m%d%H%M")
  version = Actions::GetVersionNumberAction.run(target: Environment.target)
  build = Actions::GetBuildNumberAction.run({})
  # 生成ipa包的名字格式
  ipaName = "#{Environment.scheme}_#{export_method}_#{version}_#{build}.ipa"
  
  # 获取 Extension 的 Bundle ID(可能有多个,用逗号分隔)
  extension_bundle_ids = Environment.extension_bundle_ids
  extension_profile_names = []
  # profile 名字
  profile_name = ""

  case export_method
  when "development"
    profile_name = Environment.provisioningProfiles_development
    extension_profile_names = Environment.extension_profiles_development
  when "ad-hoc"
    profile_name = Environment.provisioningProfiles_adhoc
    extension_profile_names = Environment.extension_profiles_adhoc
  when "app-store"
    profile_name = Environment.provisioningProfiles_appstore
    extension_profile_names = Environment.extension_profiles_appstore
  else
    raise "Unsupported export method: #{export_method}"
  end

  # 组装 provisioningProfiles 
  provisioningProfiles_map = {
    "#{Environment.bundleID}" => "#{profile_name}"
  }
  extension_bundle_ids.each_with_index do |ext_bundle_id, idx|
    provisioningProfiles_map[ext_bundle_id.strip] = extension_profile_names[idx]&.strip
  end

  UI.message("*************| 开始打包 |*************")

  other_action.gym(
    clean: true,
    silent: true,
    workspace: Environment.workspace,
    scheme: scheme,
    configuration: configuration,
    buildlog_path: Constants.BUILD_LOG_DIR,
    output_name: ipaName,
    output_directory: Constants.IPA_OUTPUT_DIR,
    export_options: {
      method: export_method,
      provisioningProfiles: provisioningProfiles_map
    }
  )

  UI.message("*************| 打包完成 |*************")

  UI.message("*************| 复制打包产物 |*************")
  # 定义桌面路径
  desktop_path = File.expand_path("~/Desktop")
  output_path = File.join(desktop_path, "BuildOutput_#{scheme}")
  target_path = File.join(output_path, "#{build}")
  FileUtils.mkdir_p(target_path)
  # 构建复制到桌面
  Dir.glob("#{Constants.IPA_OUTPUT_DIR}/*").each do |file|
    UI.message("准备复制文件:#{file} 到 #{target_path}")
    FileUtils.cp_r(file, target_path)
  end

  # UI.message("*************| 重置 Git 仓库 |*************")
  # 重置 Git 仓库
  # system("git reset --hard")

  ipa_path = "#{Constants.IPA_OUTPUT_DIR}/#{ipaName}"

  if export_method == "app-store"
    notiText = "🚀🚀🚀🚀🚀🚀\n\n#{scheme}-iOS-打包完成\n\n#{version}_#{build}_#{export_method}\n\n🚀🚀🚀🚀🚀🚀"
    NotiDingdingAction.run(notiText: notiText)

    if CommonHelper.is_validate_string(Environment.connect_key_id) && CommonHelper.is_validate_string(Environment.connect_issuer_id)

      UploadStoreAction.run({})
      notiText = "🚀🚀🚀🚀🚀🚀\n\n#{scheme}-iOS-上传完成\n\n#{version}_#{build}_#{export_method}\n\n🚀🚀🚀🚀🚀🚀"
      NotiDingdingAction.run(notiText: notiText)
    end
  else
    # 上传蒲公英
    pgy_upload_info = UploadPgyAction.run(
      "ipa_path": ipa_path
    )
    qrCode = pgy_upload_info["buildQRCodeURL"]

    # 钉钉通知
    notiText = "🚀🚀🚀🚀🚀🚀\n\n#{scheme}-iOS-打包完成\n\n#{version}_#{build}_#{export_method}\n\n🚀🚀🚀🚀🚀🚀"
    if CommonHelper.is_validate_string(qrCode)
      notiText << "\n\n⬇️⬇️⬇️ 扫码安装 ⬇️⬇️⬇️\n\n![screenshot](#{qrCode})"
    end
    NotiDingdingAction.run(notiText: notiText)
  end

  # 代码分析
  if is_analyze_swiftlint && export_method != "app-store"
    analyze_swiftlint(is_from_package: true, configuration: configuration)
    # 结果复制到桌面
    FileUtils.cp(SWIFTLINT_HTML_FILE, target_path)
    FileUtils.cp(SWIFTLINT_ANALYZE_HTML_FILE, target_path)
    UI.message("*************| 代码分析完成 |*************")
  else
    UI.message("*************| 跳过代码分析 |*************")
  end

  # 重复代码检查
  if is_detect_duplicity_code && export_method != "app-store"
    detect_code_duplicity(is_all: true)
    # 结果复制到桌面
    FileUtils.cp(DUPLICITY_CODE_HTML_FILE, target_path)
    UI.message("*************| 重复代码检查完成 |*************")
  else
    UI.message("*************| 跳过重复代码检查 |*************")
  end

  # 无用代码检查
  if is_detect_unused_code && export_method != "app-store"
    DetectUnusedCodeAction.run(
      is_from_package: true,
      configuration: configuration
    )
    # 结果复制到桌面
    FileUtils.cp(Constants.UNUSED_CODE_HTML_FILE, target_path)
    UI.message("*************| 无用代码检查完成 |*************")
  else
    UI.message("*************| 跳过无用代码检查 |*************")
  end

  # 无用图片检查
  if is_detect_unused_image && export_method != "app-store"
    DetectUnusedImageAction.run({})
    # 结果复制到桌面
    FileUtils.cp(Constants.UNUSED_IMAGE_HTML_FILE, target_path)
    UI.message("*************| 无用图片检查完成 |*************")
  else
    UI.message("*************| 跳过未使用图片检查 |*************")
  end

  if is_swiftlint ||
    is_detect_duplicity_code ||
    is_detect_unused_code ||
    is_detect_unused_image
    # 钉钉通知
    notiText = "🚀🚀🚀🚀🚀🚀\n\n#{scheme}-iOS-代码检查完成\n\n#{version}_#{build}_#{export_method}\n\n🚀🚀🚀🚀🚀🚀"
    NotiDingdingAction.run(notiText: notiText)
  else
    UI.message("*************| 跳过代码检查 |*************")
  end

  UI.message("*************| 脚本完成 |*************")
end