Work.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. require 'spaceship'
  2. require 'openssl'
  3. require 'mysql2'
  4. require 'json'
  5. require 'pathname'
  6. require Pathname.new(File.dirname(__FILE__)).realpath.to_s + '/Config'
  7. require Pathname.new(File.dirname(__FILE__)).realpath.to_s + '/Login'
  8. require Pathname.new(File.dirname(__FILE__)).realpath.to_s + '/Mysql'
  9. username = ENV["FASTLANE_USER"]
  10. password = ENV["FASTLANE_PASSWORD"]
  11. uuid = ARGV[0].to_s
  12. bundleId = ARGV[1].to_s
  13. jsonData = {
  14. "status" => 0,
  15. "msg" => "处理失败"
  16. }
  17. addUDID = false
  18. def ad_hocCreate(bundleId, certificateId, proname)
  19. cert = Spaceship::Portal.certificate.production.find(certificateId)
  20. if !cert
  21. raise "证书#{certificateId} 不存在"
  22. end
  23. Spaceship::Portal.provisioning_profile.ad_hoc.create!(bundle_id: bundleId, certificate: cert, name: proname)
  24. sleep 1
  25. end
  26. begin
  27. userlogin = Login.new(username, password, 1)
  28. userlogin.login()
  29. #Spaceship::Portal.login(username, password)
  30. app = Spaceship::Portal.app.find(bundleId)
  31. if !app
  32. app = Spaceship::Portal.app.create!(bundle_id: bundleId, name: bundleId)
  33. end
  34. app.update_service(Spaceship::Portal.app_service.push_notification.on)
  35. app.update_service(Spaceship::Portal.app_service.vpn_configuration.on)
  36. app.update_service(Spaceship::Portal.app_service.network_extension.on)
  37. certificates = Spaceship::Portal.certificate.all
  38. if certificates.empty?
  39. raise "证书为空"
  40. end
  41. client = Mysql.instance.getClient();
  42. if !Spaceship::Portal.device.find_by_udid(uuid)
  43. Spaceship::Portal.device.create!(name:uuid, udid: uuid)
  44. addUDID = true
  45. total_count = Spaceship::Portal.device.all.length
  46. limit_count = 100 - total_count
  47. if limit_count < 0
  48. limit_count = 0
  49. end
  50. client.query("update prefix_super_cert set total_count = '#{total_count}',limit_count = '#{limit_count}' where username = '#{username}'")
  51. end
  52. results = client.query("SELECT tid FROM prefix_super_cert where username = '#{username}' limit 1")
  53. if !results.any?
  54. raise "苹果账号#{username}不存在, 请先添加"
  55. end
  56. certificateObj = results.first
  57. certificateId = certificateObj['tid']
  58. Spaceship.provisioning_profile.ad_hoc.all.each do |p|
  59. p.certificates.each do |cs|
  60. if cs.id == certificateId && p.app.bundle_id == bundleId
  61. $ad_hocProfile = p
  62. break
  63. end
  64. end
  65. end
  66. if !defined? $ad_hocProfile
  67. ad_hocCreate(bundleId, certificateId, bundleId + '.' + certificateId)
  68. sleep 1
  69. $ad_hocProfile = Spaceship.provisioning_profile.ad_hoc.all.first
  70. end
  71. if !defined? $ad_hocProfile
  72. raise "描述文件生成失败"
  73. end
  74. devices = Spaceship.device.all
  75. $ad_hocProfile.devices = devices
  76. $ad_hocProfile.update!
  77. Spaceship.provisioning_profile.ad_hoc.all.each do |p|
  78. if p.name == $ad_hocProfile.name
  79. mobileprovision = '/' + uuid + '.mobileprovision'
  80. File.write(Config::MOVILE + mobileprovision, p.download)
  81. end
  82. end
  83. jsonData = {
  84. "status" => 1,
  85. "msg" => "处理成功",
  86. "addUDID" => addUDID
  87. }
  88. rescue Exception => e
  89. if e.message.include?("=>")
  90. response = JSON.parse e.message.gsub('=>', ':')
  91. jsonData = {
  92. "status" => 0,
  93. "msg" => "处理失败",
  94. "response" => response
  95. }
  96. else
  97. jsonData = {
  98. "status" => 0,
  99. "msg" => e.message
  100. }
  101. end
  102. ensure
  103. Mysql.instance.close()
  104. puts JSON[jsonData]
  105. end