Class: Pindo::PgyerHelper

Inherits:
Object
  • Object
show all
Includes:
Githelper
Defined in:
lib/pindo/module/pgyer/pgyerhelper.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Githelper

#add_branch, #add_tag, #add_tag_with_check, #clone_clang_repo, #clone_devclang_repo, #clone_pindo_common_config_repo, #clone_pindo_env_config_repo, #clong_buildconfig_repo, #get_repo_base_name, #getcode_to_dir, #git_addpush_repo, #git_latest_commit_id, #local_branch_exists?, #local_tag_exists?, #prepare_gitenv, #process_need_add_files, #remote_branch_exists?, #remote_tag_exists?, #remove_branch, #remove_tag

Methods included from Executable

capture_command, #executable, execute_command, which, which!

Constructor Details

#initializePgyerHelper

Returns a new instance of PgyerHelper.



28
29
30
31
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 28

def initialize()
    @force_login = false
    @has_login = false
end

Class Attribute Details

.instance=(value) ⇒ Object (writeonly)

Sets the attribute instance

Parameters:

  • value

    the value to set the attribute instance to.



20
21
22
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 20

def instance=(value)
  @instance = value
end

Class Method Details

.share_instaceObject



23
24
25
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 23

def self.share_instace
  @instance ||= new
end

Instance Method Details

#find_app_info_with_obj_list(proj_name: nil) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 343

def find_app_info_with_obj_list(proj_name:nil)

    if proj_name.nil?
        return nil
    end

    Funlog.instance.fancyinfo_start("正在拉取app信息列表,查找app信息...")

    get_app_list_in_pgyer()

    key_proj_name = proj_name.downcase.strip.gsub(/[\s\-_]/, '')

    app_info_list = Pindoconfig.instance.get_pgyerapps_info_list()
    app_info_obj = app_info_list&.find do |app_info_item|
        app_info_item_name = app_info_item["appName"].to_s.downcase.strip.gsub(/[\s\-_]/, '')
        app_info_item_name.eql?(key_proj_name) && app_info_item["appType"].eql?("iOS")
    end

    Funlog.instance.fancyinfo_success("拉取app信息列表完成,app信息获取成功!")

    app_info_obj
end

#get_app_list_in_pgyerObject



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
409
410
411
412
413
414
415
416
417
418
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 384

def get_app_list_in_pgyer()


    params = {
        pageNo:1,
        pageSize:1000
    }

    app_info_list = Pindoconfig.instance.get_pgyerapps_info_list()
    if app_info_list.nil?
        res_data = @pgyer_client.get_app_list_req(params:params)
        # puts JSON.pretty_generate(res_data)
        if !res_data["data"].nil? && res_data["data"].size > 0
            app_info_list = res_data["data"]
            Pindoconfig.instance.set_pgyerapps_info_list(app_info_list:app_info_list)
        end
    end

    if app_info_list.nil?
        res_data = @pgyer_client.get_app_list_req(params:params)
        # puts JSON.pretty_generate(res_data)
        if !res_data["data"].nil? && res_data["data"].size > 0
            app_info_list = res_data["data"]
            Pindoconfig.instance.set_pgyerapps_info_list(app_info_list:app_info_list)
        end
    end

    if app_info_list.nil?
        Funlog.instance.fancyinfo_error("拉取app信息列表失败!")
        raise Informative, "Pgyer网络数据异常!!!"
    end


    return app_info_list
end

#get_app_version(appId: nil, app_version_index: nil, latest_version: true) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 366

def get_app_version(appId:nil, app_version_index:nil, latest_version:true)

    version_data = @pgyer_client.get_app_version_list_req(appId:appId)

    version_item_obj = nil
    if latest_version
       version_item_obj = version_data["data"].find{ |item| item["latest"]}
    else
        if !app_version_index.nil?
            version_item_obj = version_data["data"].find{ |item| item["incId"].to_s.eql?(app_version_index.to_s)}
        end
    end

    # puts JSON.pretty_generate(version_item_obj)

    return version_item_obj
end

#get_cert_listObject



520
521
522
523
524
525
526
527
528
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 520

def get_cert_list( )

    Funlog.instance.fancyinfo_start("正在获取可用证书...")
    result = @pgyer_client.get_cert_list()

    Funlog.instance.fancyinfo_success("获取可用证书完成!")
    return result["data"]

end

#get_descriptionObject



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 471

def get_description()
    comment_array = []
    puts "请输入备注信息(按Esc结束):"
    loop do

      line = STDIN.gets.chomp
      comment_array << line

      key = STDIN.getch
      break if key == "\e"  # Break the loop if Esc key is pressed
    end
    puts "================================="
    puts "输入结束,输入内容为:"
    puts "================================="
    comment_str = comment_array.join("\n")
    puts comment_array
    puts "================================="
    return comment_str
end

#get_user_local_wechat_urlObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 214

def get_user_local_wechat_url( )

    wechat_msg_url = nil
    wechat_msg_url = PindoUserLocalConfig.instance.read_local_wechat_url
    if !wechat_msg_url.nil? && wechat_msg_url.length > 1
    else

        wechat_msg_url = ask('请输入Wechat Url:')
        if !wechat_msg_url.nil? && wechat_msg_url.length > 1
            PindoUserLocalConfig.instance.write_local_wechat_url(wechat_msg_url:wechat_msg_url)
        end

    end
    wechat_msg_url
end

#get_versioon_history_item(app_info_obj: nil, list_select_flat: false) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 420

def get_versioon_history_item(app_info_obj:nil, list_select_flat:false)

        Funlog.instance.fancyinfo_start("正在拉取app上传记录...")

        appId = app_info_obj["appId"]
        # puts "appId #{appId}"
        version_data = @pgyer_client.get_app_version_list_req(appId:appId) || {}

        Funlog.instance.fancyinfo_success("拉取app上传记录完成!")

        if version_data["data"].nil? || version_data["data"].size <=0
            raise Informative, "#{proj_name} 错误, 没有找到上传记录"
        end

        app_incId_index = nil
        if list_select_flat

            #获取version_data 最后10个数
            list_num = [version_data["data"].size, 10].min
            #获取version_data 最后10个的incId, bundleId, updateTime组成新数组
            version_array_temp = version_data["data"].take(list_num).map do |item|
                {
                  "appVersion" => item["appVersion"] + "(build " + item["build"] + " )",
                  "incId" => item["incId"],
                  "bundleId" => item["bundleId"],
                  "updateTime" => item["updateTime"]
                }
            end

            version_array_temp.each do |item|
                puts "版本:#{item["appVersion"]}  Build号:#{item["incId"]}  bundleId:#{item["bundleId"]} 上传时间:#{item["updateTime"]}"
            end
            app_incId_index = ask('请选择上面的Build号:')

        end



        version_item_obj = nil
        if !app_incId_index.nil?
            version_item_obj = version_data["data"].find{ |item| item["incId"].to_s.eql?(app_incId_index.to_s)}
        else
            version_item_obj = version_data["data"].find{ |item| item["latest"]}
        end


        version_item_obj

end

#loginObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 37

def 
    @pgyer_client = PgyerClient.new
    if @force_login || !@has_login
        @has_login = @pgyer_client.(force_login:@force_login)
        @force_login = false
    else
        @has_login = @pgyer_client.(force_login:false)
    end
    return @has_login
end

#make_msg_data(app_info_obj: nil, app_version_info_obj: nil) ⇒ Object



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 546

def make_msg_data(app_info_obj:nil, app_version_info_obj:nil)

    msg_from =  @pgyer_client.token["username"]

    app_name = app_info_obj["appName"]
    download_password = app_info_obj["password"]
    download_url = app_info_obj["downloadUrl"] + "?" + "id=" + app_version_info_obj["id"]
    bundle_id = app_version_info_obj["bundleId"]
    app_version = app_version_info_obj["appVersion"]
    index_num = app_version_info_obj["incId"]
    time = app_version_info_obj["updateTime"]
    description = app_version_info_obj["description"]
    msg_from = msg_from || " "
    msg_data  ={
        app_name: app_name,
        download_password: download_password,
        download_url: download_url,
        bundle_id: bundle_id,
        app_version: app_version,
        index_num: index_num,
        time: time,
        description: description,
        msg_from:msg_from
    }

    return msg_data

end

#modify_coment(app_info_obj: nil, version_item_obj: nil) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 491

def modify_coment(app_info_obj:nil, version_item_obj:nil)

    comment_str = get_description()
    puts
    puts "确修改信息:"
    puts
    puts  "================================="
    puts "App: #{app_info_obj["appName"]}"
    puts "版本: #{version_item_obj["appVersion"]} (build #{version_item_obj["build"]})"
    puts "Build号: #{version_item_obj["incId"]}"
    puts "bundleId: #{version_item_obj["bundleId"]}"
    puts "上传时间: #{version_item_obj["updateTime"]}"
    puts
    puts "旧备注: \n#{version_item_obj["description"]}"
    puts
    puts "修改备注: \n#{comment_str.strip}"
    puts  "================================="
    puts
    answer = agree("确认修改备注信息(Y/n)?:")
    unless answer
        raise Informative, "暂停修改!!"
    end

    @pgyer_client.post_update_upload_comment(appId:app_info_obj["appId"], id:version_item_obj["id"], comment:comment_str.strip)

    puts "备注信息修改成功!!"

end

#prepare_upload(working_directory: nil, proj_name: nil) ⇒ Object



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
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 48

def prepare_upload(working_directory:nil, proj_name:nil)
    upload_proj_name = proj_name
    if upload_proj_name.nil? || upload_proj_name.empty?
        upload_proj_name = @proj_name
    end

    app_info_obj = nil
    if 

        if !upload_proj_name.nil?
            app_info_obj = PgyerHelper.share_instace.find_app_info_with_obj_list(proj_name:upload_proj_name)
        end

        if !app_info_obj.nil?
            return app_info_obj
        end

        proj_name_array = []
        if File.exist?(File.join(working_directory, "config.json"))
            config_json = JSON.parse(File.read(File.join(working_directory, "config.json")))
            proj_name_array << config_json["project_info"]["project_name"]
        end

        xcodeproj_file_name = Dir.glob(File.join(Dir.pwd, "/*.xcodeproj")).max_by {|f| File.mtime(f)}
        upload_proj_name = File.basename(xcodeproj_file_name, ".xcodeproj") if xcodeproj_file_name
        if !upload_proj_name.nil? && !upload_proj_name.empty?
            proj_name_array << upload_proj_name
        end

        puts
        dir_base_name = File.basename(working_directory)
        unless ["Desktop", "Documents", "Downloads"].include?(dir_base_name)
            proj_name_array << dir_base_name
        end

        proj_name_array << "自定义输入Pyger上的App代号"

        if proj_name_array.size > 1
            cli = HighLine.new
            upload_proj_name = cli.choose do |menu|
                menu.prompt = "请选择Pgyer上的App代号:"
                menu.choices(*proj_name_array)
            end
            if upload_proj_name.include?("自定义输入")
                upload_proj_name = ask('请输入Pyger上的App代号(大小写空格忽略):') || nil
                upload_proj_name = upload_proj_name.strip
            end
        else
            upload_proj_name = ask('请输入Pyger上的App代号(大小写空格忽略):') || nil
            upload_proj_name = upload_proj_name.strip
        end


        app_info_obj = PgyerHelper.share_instace.find_app_info_with_obj_list(proj_name:upload_proj_name)

        if app_info_obj.nil?
            upload_proj_name = ask('没有找到结果,请重新输入Pyger上的App代号(大小写空格忽略):') || nil
            upload_proj_name = upload_proj_name.strip
            app_info_obj = PgyerHelper.share_instace.find_app_info_with_obj_list(proj_name:upload_proj_name)
        end

        if app_info_obj.nil?
            raise Informative, "#{upload_proj_name} 名称错误, 请输入正确的App代号,Pgyer网站没有该App!!!"
        end
    else
        raise Informative, "请先登录Pgyer网站"
    end
    @proj_name = upload_proj_name
    app_info_obj
end


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 320

def print_app_version_info(msg_data:nil)

    qrcode = RQRCode::QRCode.new(msg_data[:download_url])
    svg = qrcode.as_ansi(
      light: "\033[47m", dark: "\033[40m",
      fill_character: "  ",
      quiet_zone_size: 1,
      size: 300
    )
    puts "\r\n\r\n\r\n\r\n"
    puts svg
    puts
    puts "#{msg_data[:app_name]} 测试"
    puts "Bundle Id:  #{msg_data[:bundle_id]}"
    puts "版本号:    #{msg_data[:app_version]}  build: #{msg_data[:index_num]}"
    puts "安装密码:  #{msg_data[:download_password]}"
    puts "上传时间:  #{msg_data[:time]}"
    puts "链接:     #{msg_data[:download_url]}"
    puts "说明:     #{msg_data[:description]}"
    puts "\r\n\r\n\r\n\r\n"

end

#resign(version_id: nil, cert_id: nil) ⇒ Object



530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 530

def resign(version_id:nil, cert_id:nil)

    Funlog.instance.fancyinfo_start("正在重签名...")
    result =  @pgyer_client.post_resign(appVersionId:version_id, certId:cert_id)
    if !result.nil? && result["code"] == 200 && result["data"] == "success"
        Funlog.instance.fancyinfo_success("重签名成功!")
        return true
    else
        Funlog.instance.fancyinfo_error("重签名失败!")
        return false
    end

end

#send_apptest_wechat_msg(msg_data: nil, wechat_url: nil) ⇒ Object



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
318
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 231

def send_apptest_wechat_msg(msg_data:nil, wechat_url:nil)

        wechat_msg_url = wechat_url
        if wechat_msg_url.nil? || wechat_msg_url.empty?
            wechat_msg_url = get_user_local_wechat_url()
        end

        if !wechat_msg_url.nil? && wechat_msg_url.length >1

        else
            puts "企业微信Wechat Url无效"
        end

        begin

                download_url = msg_data[:download_url]
                app_name = msg_data[:app_name]
                download_password = msg_data[:download_password]
                bundle_id = msg_data[:bundle_id]
                app_version = msg_data[:app_version]
                index_num = msg_data[:index_num]
                upload_time = msg_data[:time]
                description = msg_data[:description]
                msg_from = msg_data[:msg_from]


                # current_dir = current_dir = Dir.pwd
                # output_file = File.join(current_dir, app_name + "_qrcode.png")
                qrcode = RQRCode::QRCode.new(download_url)

                png = qrcode.as_png(
                  bit_depth: 1,
                  border_modules: 4,
                  color_mode: ChunkyPNG::COLOR_GRAYSCALE,
                  color: "black",
                  file: nil,
                  fill: "white",
                  module_px_size: 6,
                  resize_exactly_to: false,
                  resize_gte_to: false,
                  size: 300
                )


                checksum = Digest::MD5.hexdigest(png.to_s)
                image_base64_data =  Base64.strict_encode64(png.to_s)

                body_data = {
                    msgtype:"image",
                    image:{
                        base64: image_base64_data,
                        md5:checksum
                    }
                }

                con = Faraday.new
                res = con.post do |req|
                    req.url wechat_msg_url
                    req.headers['Content-Type'] = 'application/json'
                    req.body =  body_data.to_json
                end


                body_data = {
                    msgtype:"text",
                    text:{
                        content: "#{app_name} 测试\r\r版本号: #{app_version}   build:#{index_num}\r安装密码: #{download_password}\r上传时间: #{upload_time}\rBundle Id: #{bundle_id}\r链接:#{download_url}\r\n说明:\r\r#{description}\r\r消息发送来自: #{msg_from}"

                    }
                }

                con = Faraday.new
                res = con.post do |req|
                    req.url wechat_msg_url
                    req.headers['Content-Type'] = 'application/json'
                    req.body =  body_data.to_json
                end
                puts
                puts "发送企业微信成功 !!!"
                puts

        rescue StandardError => e
                puts
                puts "发送企业微信失败 !!!"
                puts
        end

end

#setForeLogin(beforeLogin: nil) ⇒ Object



33
34
35
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 33

def setForeLogin(beforeLogin:nil)
    @force_login = beforeLogin
end

#start_upload(app_info_obj: nil, ipa_file_upload: nil, description: nil) ⇒ Object



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
# File 'lib/pindo/module/pgyer/pgyerhelper.rb', line 119

def start_upload(app_info_obj:nil, ipa_file_upload:nil, description:nil)

        args_ipa_file_dir = File.expand_path(File::dirname(ipa_file_upload))
        ipa_file_upload=File.join(args_ipa_file_dir, File.basename(ipa_file_upload))
        current_project_dir = Dir.pwd
        if description.nil? && File.exist?(File.join(current_project_dir, ".git"))
            description = git!(%W(-C #{current_project_dir} show -s --format=commit::%H)).strip
            xcodeproj_file_name = Dir.glob(File.join(current_project_dir, "/*.xcodeproj")).max_by {|f| File.mtime(f)}
            if !xcodeproj_file_name.nil? && !xcodeproj_file_name.empty? && File.exist?(xcodeproj_file_name)
                project_obj = Xcodeproj::Project.open(xcodeproj_file_name)
                main_target = project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])  }.first
                provisioning_profile_name = main_target.build_configurations.first.build_settings['PROVISIONING_PROFILE_SPECIFIER'].downcase.split(" ")
                if provisioning_profile_name.include?("adhoc")
                    description = git!(%W(-C #{current_project_dir} show -s --format=commit::%H)).strip
                elsif provisioning_profile_name.include?("development")
                    if File.exist?(File.join(current_project_dir, ".release_info"))
                        description = File.read(File.join(current_project_dir, ".release_info"))
                    else
                        description = git!(%W(-C #{current_project_dir} show -s --format=%h::%s)).strip
                    end
                elsif provisioning_profile_name.include?("appstore")
                    description = "提交包重签名"
                end
            end
        end

        addtach_file = nil
        if ipa_file_upload.include?(File.join(current_project_dir, "build")) && File.exist?(File.join(current_project_dir, "VMData"))

            server_file_directory = Dir.glob(File.join(current_project_dir, "VMData/Archives/*")).max_by {|f| File.mtime(f)}
            zip_base_name = File.basename(server_file_directory)

            server_zipfile_name  = File.join(current_project_dir, "VMData", zip_base_name+".zip")

            if File.exist?(server_zipfile_name)
                FileUtils.rm_rf(server_zipfile_name)
            end

            Zip::File.open(server_zipfile_name, Zip::File::CREATE) do |zipfile|
                Dir.chdir server_file_directory
                Dir.glob("**/*").reject {|fn| File.directory?(fn) }.each do |file|
                    zipfile.add(file.sub(server_file_directory + '/', ''), file)
                end
            end

            addtach_file = server_zipfile_name
            Dir.chdir current_project_dir
        end


        puts
        puts "上传项目: #{app_info_obj["appName"]}"
        print "上传备注: "
        puts description


        # aws_client = AWSS3Client.new
        aws_client = PgyerUploadClient.new
        upload_res = aws_client.upload_file(binary_file:ipa_file_upload)

        attach_key_url = nil
        attachFileUrls = []

        begin
            if !addtach_file.nil? && File.exist?(addtach_file)
                puts "存在附件, 继续上传附件..."
                attach_key_url = aws_client.upload_file(binary_file:addtach_file, isAttach:true)
            end

            if !attach_key_url.nil?
                attachFileUrls << attach_key_url
            end

        rescue => error
            puts "附件上传失败".bold.red
        end

        if !upload_res.nil? && !app_info_obj.nil?

            puts attachFileUrls

            result_data = @pgyer_client.post_app_update_binary(appId:app_info_obj["appId"], params:{
                description: description,
                s3Url: upload_res,
                attachFileUrls:attachFileUrls
            })

            return result_data
        else
            raise Informative, "上传失败!"
        end


end