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



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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 238

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :scheme,
      description: "不采取默认配置,自定义 `scheme` 名称",
      optional: true,
      default_value: nil,
      type: String
    ),
    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, testFlight",
      optional: true,
      default_value: "development",
      type: String,
      verify_block: proc do |value|
        valid_params = ["ad-hoc", "enterprise", "app-store", "development", "testFlight"]
        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
    ),
    FastlaneCore::ConfigItem.new(
      key: :changelog,
      description: "更新日志",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :release_notes,
      description: "更新文案, 格式为 { \"zh-Hans\": \"修复问题\", \"en-US\": \"bugfix\"} JSON 字符串",
      optional: true,
      type: String
    )
  ]
end

.categoryObject



334
335
336
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 334

def self.category
  :building
end

.descriptionObject



234
235
236
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 234

def self.description
  "打包"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/fastlane/plugin/fastci/actions/package_action.rb', line 330

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
198
199
200
201
202
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
# 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
  release_notes = params[:release_notes] || ""
  if export_method == "app-store" || export_method == "testFlight"
    configuration = "Release"
  end

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

  scheme = params[: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: scheme)
  build = Actions::GetBuildNumberAction.run({})
  # 生成ipa包的名字格式
  ipaName = "#{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", "testFlight"
    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("*************| 开始打包 |*************")

  # 对于 testFlight,使用 app-store 方法
  gym_method = export_method == "testFlight" ? "app-store" : export_method
  
  gym_options = {
    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: gym_method
    }
  }

  if Environment.is_auto_update_provisioning
    gym_options[:xcargs] = '-allowProvisioningUpdates'
  else
    gym_options[:export_options][:provisioningProfiles] = provisioningProfiles_map
  end

  other_action.gym(gym_options)

  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 gym_method == "app-store"
    notiText = "🚀🚀🚀🚀🚀🚀\n\n#{scheme}-iOS-打包完成\n\n#{version}_#{build}_#{export_method}\n\n🚀🚀🚀🚀🚀🚀"
    DingdingHelper.sendMarkdown(notiText)

    if CommonHelper.is_validate_string(Environment.connect_key_id) && CommonHelper.is_validate_string(Environment.connect_issuer_id)
      # 根据 export_method 决定是否为 TestFlight
      is_test_flight = export_method == "testFlight"
      
      other_action.upload_store(
        release_notes: release_notes,
        isTestFlight: is_test_flight
      )
      notiText = "🚀🚀🚀🚀🚀🚀\n\n#{scheme}-iOS-上传完成\n\n#{version}_#{build}_#{export_method}\n\n🚀🚀🚀🚀🚀🚀"
      DingdingHelper.sendMarkdown(notiText)
    end
  else
    # 钉钉通知
    notiText = "🚀🚀🚀🚀🚀🚀\n\n#{scheme}-iOS-打包完成\n\n#{version}_#{build}_#{export_method}\n\n🚀🚀🚀🚀🚀🚀"

    # 上传蒲公英
    if CommonHelper.is_validate_string(Environment.pgy_api_key)
      pgy_upload_info = other_action.upload_pgy()
      qrCode = pgy_upload_info["buildQRCodeURL"]

      if CommonHelper.is_validate_string(qrCode)
        notiText << "\n\n⬇️⬇️⬇️ 扫码安装 ⬇️⬇️⬇️\n\n\n密码: #{Environment.pgy_password}\n![screenshot](#{qrCode})"
      end
    end

    # 上传 fir
    if CommonHelper.is_validate_string(Environment.fir_api_token)
      fir_upload_info = other_action.upload_fir(
        changelog: params[:changelog]
      )
      download_url = fir_upload_info[:download_url]

      if CommonHelper.is_validate_string(download_url)
        notiText << "\n\n⬇️⬇️⬇️ 点击链接安装 ⬇️⬇️⬇️\n\n\n密码: #{Environment.fir_password}\n[_点击下载_](#{download_url})"
      end
    end

    DingdingHelper.sendMarkdown(notiText)
  end

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

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

  # 无用代码检查
  if is_detect_unused_code && gym_method != "app-store"
    other_action.detect_unused_code(
      scheme: scheme,
      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 && gym_method != "app-store"
    other_action.detect_unused_image()
    # 结果复制到桌面
    FileUtils.cp(Constants.UNUSED_IMAGE_HTML_FILE, target_path)
    UI.message("*************| 无用图片检查完成 |*************")
  else
    UI.message("*************| 跳过未使用图片检查 |*************")
  end

  if is_analyze_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🚀🚀🚀🚀🚀🚀"
    DingdingHelper.sendMarkdown(notiText)
  else
    UI.message("*************| 跳过代码检查 |*************")
  end

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