Class: Shell_Command

Inherits:
Object
  • Object
show all
Includes:
Uki_config::Mixin
Defined in:
lib/cocoapods-uki-packager/shell.rb

Defined Under Namespace

Modules: Mixin

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Uki_config::Mixin

#uki_config

Class Attribute Details

.instanceObject



410
411
412
# File 'lib/cocoapods-uki-packager/shell.rb', line 410

def self.instance
    @instance ||= new
end

Instance Method Details

#buildObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cocoapods-uki-packager/shell.rb', line 51

def build

    puts("*********************************************************  开始编译")

    if uki_config.verbose?
      puts("xcodebuild build -workspace #{uki_config.workspace} -scheme #{uki_config.scheme} -destination generic/platform=iOS -configuration #{uki_config.configuration} DEBUG_INFORMATION_FORMAT=#{uki_config.dsym_config} DWARF_DSYM_FOLDER_PATH=#{uki_config.dsym_path}")
    end
    
    log = %x{
        xcodebuild build -workspace #{uki_config.workspace} -scheme #{uki_config.scheme} -destination generic/platform=iOS -configuration #{uki_config.configuration} DEBUG_INFORMATION_FORMAT=#{uki_config.dsym_config} DWARF_DSYM_FOLDER_PATH=#{uki_config.dsym_path}
    }

   if log[/BUILD SUCCEEDED/]
     puts("*********************************************************  编译完成")
     # 直接开始导出
     self.export
   elsif 
     puts("*********************************************************  编译失败")
     exit
   end 
end

#ding_notifi(par) ⇒ Object



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
# File 'lib/cocoapods-uki-packager/shell.rb', line 136

def ding_notifi(par)
  
  uki_config.upload_time = Time.now.to_i - uki_config.s_time

  branch = %x{
    if branch=$(git symbolic-ref --short -q HEAD)
        then
          echo  $branch
        else
          echo "未知分支"
    fi
}

hostname = Socket.gethostname
project_name = uki_config.group_name
project_env = uki_config.configuration
project_dsym = uki_config.dsym ? "已上传": "无符号表"
qrUrl = par["buildQRCodeURL"]

  message = {
    "msgtype": "actionCard",
    "actionCard": {
        "title": "打包成功", 
        "text": " ![](#{qrUrl}) # 沪学习iOS打包成功 \n\n #### 打包分支: #{branch} \n\n #### 打包环境: #{project_env} \n\n #### 打包机器: #{hostname}  \n\n #### dsym: #{project_dsym} \n\n #### App版本: #{uki_config.app_version} \n\n #### build版本: #{uki_config.build_version}  \n\n #### 包体积: #{uki_config.ipa_size} \n\n #### 编译耗时: #{uki_config.build_time}s \n\n #### 上传耗时: #{uki_config.upload_time}s", 
        "btnOrientation": "0", 
        "btns": [
            {
                "title": "去下载", 
                "actionURL": "#{uki_config.fir_url}"
            }
        ]
    }
}

  log = %x{
    curl -X POST -H 'Content-Type: application/json' -d '#{message.to_json}' "https://oapi.dingtalk.com/robot/send?access_token=878146b6cef93f214feef79879fa4fb1d1c643a36cf314822aaea7eeac4298fe"
}

if uki_config.verbose?
    puts(log)
end

local_notfi = %x{
  notification="Did uploaded to fir caost time:#{uki_config.upload_time}s"
  osascript -e 'display notification "size:'#{uki_config.ipa_size}'m\ncaost:'#{uki_config.upload_time}'s" with title "Uploaded Success" sound name "default" '
}

puts("********************************************************* 打包结束已通知钉钉请下载")

exit

end

#exportObject

暂时先不copy到跟目录, 防止Git提交代码时提交上去需安装Fir 不太友好安装命令 sudo gem install fir-cli



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
# File 'lib/cocoapods-uki-packager/shell.rb', line 76

def export

    unless uki_config.build_exe_path
        puts "未找到目标文件目录"
        exit
    end

    puts("*********************************************************开始导出ipa")

    log = %x{
        product_path=#{uki_config.build_exe_path}
        if [[ $product_path == "" ]]; then
            echo "===no path input==="
            product_path=`dirname $0`
        fi

        echo -e “=====path=$product_path =====“

        cd $product_path

        fir_log="fir auto release"

        APP=$(find $product_path -type d | grep ".app$" | head -n 1)
        APP=${APP%.*}

        echo -e “=====APP=$APP =====“

        rm -rf "$APP.ipa";
        rm -rf ./Payload;
        mkdir Payload;
        cp -rf "$APP.app" ./Payload;
        zip -r -q "$APP.ipa" ./Payload;
        rm -rf ./Payload;

        size=$(ls -l $APP.ipa | awk '{ print $5 }')
        sizeM=`echo "scale=2; ${size}/1024.0/1024" | bc`
        #sizeM="$((${size}/1000.0))

        echo -e "包体积=${sizeM}m\n"  
    }

 
    if !File.exist?(uki_config.ipa_path)
      puts("*********************************************************导出失败")
      exit
    end

    ipa_size = log[/包体积.*$/].split('=').last

    uki_config.build_time = Time.now.to_i - uki_config.build_time
    uki_config.ipa_size = ipa_size

    if uki_config.verbose?
        puts(log)
    end

    getCOSToken
    
end

#export_xcode_envObject



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
# File 'lib/cocoapods-uki-packager/shell.rb', line 14

def export_xcode_env
  puts("*********************************************************  开始导出Xcode环境变量")

  log = %x{
    rootPath=`pwd`
    fileName="xcode_env.json"
    work_path="${rootPath}/huxue"
    full_path="${work_path}/${fileName}"
    cd $work_path
    if [ -f "$full_path" ]
      then
          echo "存在xcode_env.json"
      else
          echo "不存在xcode_env.json 创建一个"
          touch ${full_path}
      fi

      if [ -f "$full_path" ]
        then
        echo `xcodebuild -showBuildSettings -json` > "$full_path"
      fi  
  }

  if !File.exist?(uki_config.xcode_env_path)
    puts("当前目录未查找到xcode_env.json文件, 请检查执行目录")
    exit
  end

  json_from_file = File.read(uki_config.xcode_env_path)
  uki_config.xcode_envs = JSON.parse(json_from_file).first

  if uki_config.verbose?
    puts(log)
  end

end

#getBuildInfo(par) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/cocoapods-uki-packager/shell.rb', line 373

def getBuildInfo(par)
  puts("********************************************************* 别着急大兄弟 官方建议睡3秒,等待蒲公英构建发布")
  sleep(3)
  puts("********************************************************* 帅的人已醒来")

  buildKey = par["key"]

  url = URI('https://www.pgyer.com/apiv2/app/buildInfo')
  # 设置请求参数
  params = {"_api_key": uki_config.pgyer_apiKey, "buildKey": buildKey}
  url.query = URI.encode_www_form(params)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.get(url, params)
  resbody = JSON.parse(response.body)
  code = resbody["code"]

  if uki_config.verbose?
    puts("打印#{resbody}")
  end

  if code.to_i == 1247 || code.to_i == 1246 # 正在解析中
    getBuildInfo(par)
  end

  if code.to_i == 0
    uki_config.fir_url = "https://www.pgyer.com/#{resbody["data"]["buildShortcutUrl"]}"
  else
    puts("*********************************************************上传失败 code = #{resbody["code"]} message = #{resbody["message"]}")
    exit
  end

  ding_notifi(resbody["data"])

end

#getCOSTokenObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/cocoapods-uki-packager/shell.rb', line 320

def getCOSToken
  # 开始上传时间
  uki_config.s_time = Time.now.to_i
  puts("********************************************************* 获取蒲公英上传地址")
  #application/json
  url = URI('https://www.pgyer.com/apiv2/app/getCOSToken')
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  # 设置请求参数
  data = URI.encode_www_form({'_api_key': uki_config.pgyer_apiKey, 'buildType':'ios', 'buildInstallType': 2, 'buildPassword': '1234'})
  # 设置请求头
  header = {'content-type':'application/x-www-form-urlencoded'}
  response = http.post(url, data, header)
  resbody = JSON.parse(response.body)

  upload_ipa(resbody["data"])

end

#send_notifiObject



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
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
# File 'lib/cocoapods-uki-packager/shell.rb', line 189

def send_notifi

    branch = %x{
        if branch=$(git symbolic-ref --short -q HEAD)
            then
              echo  $branch
            else
              echo "未知分支"
        fi
    }
    
    hostname = Socket.gethostname
    project_name = uki_config.group_name
    project_env = uki_config.configuration
    project_dsym = uki_config.dsym ? "已上传": "无符号表"

    message = {
        "msg_type": "interactive",
        "card": {
            "elements": [
              {
                "fields": [
                  {
                    "is_short": true,
                    "text": {
                      "content": "**打包分支**\n#{branch}",
                      "tag": "lark_md"
                    }
                  },
                  {
                    "is_short": true,
                    "text": {
                      "content": "**打包环境**\n#{project_env}",
                      "tag": "lark_md"
                    }
                  },
                  {
                    "is_short": true,
                    "text": {
                      "content": "**打包机器**\n#{hostname}",
                      "tag": "lark_md"
                    }
                  },
                  {
                    "is_short": true,
                    "text": {
                      "content": "**dsym**\n#{project_dsym}",
                      "tag": "lark_md"
                    }
                  },
                  {
                    "is_short": true,
                    "text": {
                      "content": "**App版本**\n#{uki_config.app_version}.#{uki_config.build_version}",
                      "tag": "lark_md"
                    }
                  },
                  {
                    "is_short": true,
                    "text": {
                      "content": "**包体积**\n#{uki_config.ipa_size}",
                      "tag": "lark_md"
                    }
                  },
                  {
                    "is_short": true,
                    "text": {
                      "content": "**编译耗时**\n#{uki_config.build_time}s",
                      "tag": "lark_md"
                    }
                  },
                  {
                    "is_short": true,
                    "text": {
                      "content": "**上传耗时**\n#{uki_config.upload_time}s",
                      "tag": "lark_md"
                    }
                  }
                ],
                "tag": "div"
              },
              {
                "tag": "hr"
              },
              {
                  "tag": "action",
                  "actions": [
                      {
                        "tag": "button",
                        "text": {
                          "tag": "lark_md",
                          "content": "点击下载"
                        },
                        "type": "default",
                        "url": "#{uki_config.fir_url}"
                      }
                  ]
              }
            ],
            "header": {
              "template": "green",
              "title": {
                "content": "#{project_name}",
                "tag": "plain_text"
              }
            }
          }
    }

    # https://open.feishu.cn/open-apis/bot/v2/hook/68c47d38-fc65-4cc5-a386-885a1fbff11b 测试机器人
    # https://open.feishu.cn/open-apis/bot/v2/hook/71ee99db-27fa-45ee-8cba-3004037f34ea 打包群机器人

    
    log = %x{
        curl -X POST -H "Content-Type: application/json" -d '#{message.to_json}' "https://open.feishu.cn/open-apis/bot/v2/hook/71ee99db-27fa-45ee-8cba-3004037f34ea"
    }

    if uki_config.verbose?
        puts(log)
    end

    local_notfi = %x{
      notification="Did uploaded to fir caost time:#{uki_config.upload_time}s"
      osascript -e 'display notification "size:'#{uki_config.ipa_size}'m\ncaost:'#{uki_config.upload_time}'s" with title "Uploaded Success" sound name "default" '
    }

    exit

end

#upload_ipa(par) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/cocoapods-uki-packager/shell.rb', line 340

def upload_ipa(par) 
  puts("********************************************************* 开始上传")
  CLI::UI::StdoutRouter.enable
  spinner = CLI::UI::Spinner::Async.start('上传中...')

   # multipart/form-data
  url = URI(par["endpoint"])
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  file = File.open(uki_config.ipa_path, 'rb')
  
  # 设置请求参数
  data = par["params"]
  data["file"] = file.read

  request = Net::HTTP::Post.new(par["endpoint"])
  request.set_form(data, 'multipart/form-data')
  response = http.request(request)

  file.close
  spinner.stop()

  if response.code.to_i == 204 
    puts("********************************************************* 上传成功正在获取包处理信息,请稍等...")
    getBuildInfo(par["params"])
  else
    puts("*********************************************************上传失败 code = #{response.code} message = #{response.message}")
    exit
  end
end