Class: Fastlane::Actions::AndroidUnpackAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



159
160
161
# File 'lib/fastlane/plugin/android_unpack/actions/android_unpack_action.rb', line 159

def self.authors
  ["yejinxin"]
end

.available_optionsObject



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
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
# File 'lib/fastlane/plugin/android_unpack/actions/android_unpack_action.rb', line 172

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :type,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "type",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :typeName,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "typeName",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :task,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "task",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :app,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "app",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :build_type,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "build_type",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :project,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "project",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :project_flag,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "project_flag",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :project_dir,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "project_dir",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :comment,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "comment",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :versionCode,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "versionCode",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :version,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "version",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :fir_token,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "fir_token",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :dingtalk_access_key,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "dingtalk_access_key",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :oss_endpoint,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "oss_endpoint",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :oss_access_key_id,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "oss_access_key_id",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :oss_access_key_secret,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "oss_access_key_secret",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :oss_bucket,
                            env_name: "ANDROID_UNPACK_YOUR_OPTION",
                         description: "oss_bucket",
                            optional: false,
                                type: String),
  ]
end

.descriptionObject



155
156
157
# File 'lib/fastlane/plugin/android_unpack/actions/android_unpack_action.rb', line 155

def self.description
  "Fastlane Android Unpack"
end

.detailsObject



167
168
169
170
# File 'lib/fastlane/plugin/android_unpack/actions/android_unpack_action.rb', line 167

def self.details
  # Optional:
  "通过fastlane打包上传fir后,发送通知到钉钉平台"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


262
263
264
265
266
267
268
# File 'lib/fastlane/plugin/android_unpack/actions/android_unpack_action.rb', line 262

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  # [:ios, :mac, :android].include?(platform)
  true
end

.return_valueObject



163
164
165
# File 'lib/fastlane/plugin/android_unpack/actions/android_unpack_action.rb', line 163

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



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
# File 'lib/fastlane/plugin/android_unpack/actions/android_unpack_action.rb', line 11

def self.run(params)
  UI.message("The android_unpack plugin is working!")

  target_type = params[:type]
  target_type_name = params[:typeName]
  target_task = params[:task]
  target_app = params[:app]
  build_type = options[:build_type]
  project_name = params[:project]
  project_flag = params[:project_flag]
  app_project_dir = options[:project_dir]
  comment_content = params[:comment]
  version_code_ = params[:versionCode]
  version_name_ = options[:version]
  fir_token = options[:fir_token]
  dingtalk_access_key = options[:dingtalk_access_key]
  oss_endpoint = options[:oss_endpoint]
  access_key_id = options[:oss_access_key_id]
  access_key_secret = options[:oss_access_key_secret]
  oss_bucket = options[:oss_bucket]

  if !app_project_dir
    app_project_dir = "./app"
  end

  timespans = Hash.new(Time.now)
  timespans["version_handle_start"] = Time.now

  if !version_name_
    version_name_ = git_branch.match(/(\d{1,3}\.){1,2}\d{1,3}/).to_s
    if version_name_.length == 0
        version_name_ = get_version_name(
          app_project_dir: app_project_dir
        )
    end
  else
    version_name_ = version_name_.match(/(\d{1,3}\.){1,2}\d{1,3}/).to_s
  end

  build_time_ = Time.now.strftime("%Y%m%d%H%M")

  if !version_code_
    version_code_ = build_time_[2..10]
  else
    version_code_ = version_code_.match(/\d{6,9}/).to_s
  end

  set_value_in_build(
      app_project_dir:app_project_dir,
      key:"versionCode",
      value:version_code_
  )

  set_value_in_build(
      app_project_dir:app_project_dir,
      key:"versionName",
      value:version_name_
  )

  gradle(task: "clean")

  # 获取最后一次 git 提交记录
  commit = last_git_commit
  # 获取 git branch
  branch = git_branch

  timespans["version_handle_end"] = Time.now;

  timespans["compile_start"] = Time.now;
  gradle(
    task: target_task,
    flavor: target_app,
    build_type: build_type,
    #flags: "--stacktrace",
    properties: {'versionCode' => version_code_}
  )

  timespans["compile_end"] = Time.now;

  # 发版时间
  release_time_ = Time.now.strftime("%Y年%m月%d日%H点%M分")
  release_time_upload = Time.now.strftime("%Y%m%d%H%M")
  apk_nameS_backup = "#{project_flag}-" + target_app + "-" + version_name_ + "." + release_time_upload+".apk"
  apk_remote_path_backup = "#{project_flag}/app/" + target_type + "/android/backup/" + apk_name_backup
  apk_path_local = lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]

  timespans["upload_apk_start"] = Time.now;
  upload_oss({
    endpoint:oss_endpoint, 
    access_key_id:access_key_id,
    access_key_secret:access_key_secret, 
    bucket:oss_bucket,
    remote_path:apk_remote_path_backup, 
    file_path:apk_path_local})

  fir_result = fir_cli({
    api_token: fir_token,
    specify_file_path: apk_path_local,
    need_release_id: true,
    force_pin_history: true,
    changelog: "#{branch}#{commit[:message]}"
  })
  timespans["upload_apk_end"] = Time.now;

  # 钉钉通知
  dingtalk_message = "![logo](http://shouqianba.oss-cn-hangzhou.aliyuncs.com/fastlane/fastlane_app_android_logo.gif)\n" +
  "### 收钱吧\n" +
  "*`Android`*  *`" + target_type_name + "`* for **`" + target_app + "`**\n" +
  "- version:" + version_name_ + "\n" +
  "- versionCode:" + version_code_ + "\n" +
  "- build:" + build_time_ + "\n" +
  "- 打包分支:" + git_branch + "\n" +
  "- 发版时间:" + release_time_ + "\n"

  if project_name
    dingtalk_message = dingtalk_message + "- 项目:**" + project_name + "**\n"
  end
  dingtalk_message = dingtalk_message + "- 修改内容:\n> " + commit[:message] + "\n"
  if comment_content
    dingtalk_message = dingtalk_message + comment_content + "\n"
  end

  p_preprocess = Time.at(Integer(timespans["version_handle_end"].to_f - timespans["version_handle_start"].to_f)).strftime("%M分%S秒").to_s + "\n";
  p_compile = Time.at(Integer(timespans["compile_end"].to_f - timespans["compile_start"].to_f)).strftime("%M分%S秒").to_s + "\n";
  p_upload = Time.at(Integer(timespans["upload_apk_end"].to_f - timespans["upload_apk_start"].to_f)).strftime("%M分%S秒").to_s + "\n";
  dingtalk_message = dingtalk_message + "- 打包性能:\n> " +
  "各种预处理: " + p_preprocess +
  "安装包编译: " + p_compile +
  "安装包上传: " + p_upload

  apksize = (File.size(apk_path_local).to_f / 2**20).round(2);
  dingtalk_message = dingtalk_message + "- 文件大小: " + apksize.to_s + ' MB' + "\n";
  dingtalk_message = dingtalk_message + "- 安装包下载链接:**[" + target_url + "](" + target_url + ")**\n"
  dingtalk_message = dingtalk_message + "- 感谢关注!欢迎反馈问题!O(∩_∩)O"

  dingtalk(
    access_token: dingtalk_access_key,
    title: "Android" + target_type_name + "发布了",
    message: dingtalk_message,
    #more_title: "安装包下载地址",
    #more_url: target_url,
  )
end