Class: Pindo::PemHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/cert/pem_helper.rb

Class Method Summary collapse

Class Method Details

.create_certificate(bundle_id: nil, type: "prod", output_path: "") ⇒ Object



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
# File 'lib/pindo/module/cert/pem_helper.rb', line 79

def create_certificate(bundle_id:nil, type:"prod", output_path:"")

  if bundle_id.empty? || output_path.empty?
    UI.user_error!("bundle id is nil or output_path is nil.")
  end

  puts "Creating a new push certificate for app '#{bundle_id}'. "

  csr, pkey = Spaceship::Portal.certificate.create_certificate_signing_request

  cert = nil
  begin
    if type == "dev"
      cert = Spaceship::Portal.certificate.development_push.create!(csr: csr, bundle_id: bundle_id)
    else
      cert = Spaceship::Portal.certificate.production_push.create!(csr: csr, bundle_id: bundle_id)
    end
  rescue => ex
    if ex.to_s.include?("You already have a current")
      # That's the most common failure probably
      raise Informative, "You already have 2 active push profiles for this application/environment. You'll need to revoke an old certificate to make room for a new one"
    else
      raise ex
    end
  end


  x509_certificate = cert.download
  certificate_type = (type == "dev" ? 'development' : 'production')

  base_base = bundle_id.gsub('.', '_')
  puts base_base
  filename_base = base_base + '_' + type
  
  
  private_key_path = File.join(output_path, "#{filename_base}.pkey")
  File.write(private_key_path, pkey.to_pem)
  puts "key: #{private_key_path}"


  p12_cert_path = File.join(output_path, "#{filename_base}.p12")
  p12 = OpenSSL::PKCS12.create('goodcert1', certificate_type, pkey, x509_certificate)
  File.write(p12_cert_path, p12.to_der)
  puts "p12 : #{p12_cert_path}"


  x509_cert_path = File.join(output_path, "#{filename_base}.pem")
  File.write(x509_cert_path, x509_certificate.to_pem + pkey.to_pem)
  puts "pem : #{x509_cert_path}"

  return x509_cert_path
end

.execute_pem_creation(config_json:, dev_flag: false, deploy_repo_name: nil) ⇒ Object

执行 Push 证书创建流程

Parameters:

  • config_json (Hash)

    配置 JSON 对象

  • dev_flag (Boolean) (defaults to: false)

    是否为开发证书

  • deploy_repo_name (String, nil) (defaults to: nil)

    部署仓库名称



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
# File 'lib/pindo/module/cert/pem_helper.rb', line 30

def execute_pem_creation(config_json:, dev_flag: false, deploy_repo_name: nil)
  require_relative '../../base/githelper'
  include Pindo::Githelper

  # 提取配置信息
  config_info = extract_config_info(config_json)

  # 验证必需字段
  raise "配置文件中缺少 Apple ID" if config_info[:apple_id].nil? || config_info[:apple_id].empty?
  raise "配置文件中缺少 Bundle ID" if config_info[:bundle_id].nil? || config_info[:bundle_id].empty?

  # 登录 Apple 开发者中心
  puts config_info[:apple_id]
  puts "Login #{config_info[:apple_id]}..."
  (apple_id: config_info[:apple_id])

  # 确定证书类型
  pem_type = dev_flag ? "dev" : "prod"

  # 创建输出目录
  temp_dir = "push_" + Time.now.to_i.to_s
  push_path = File.join(File.expand_path('~/Desktop/'), temp_dir)
  FileUtils.mkdir_p(push_path) unless File.exist?(push_path)
  puts "证书输出路径: #{push_path}"

  # 创建证书
  x509_cert_path = create_certificate(
    bundle_id: config_info[:bundle_id],
    type: pem_type,
    output_path: push_path
  )

  # 如果提供了部署仓库名称,则上传到 Git 仓库
  if !deploy_repo_name.nil? && !deploy_repo_name.empty? && File.exist?(x509_cert_path) && File.exist?(push_path)
    app_config_dir = clong_buildconfig_repo(repo_name: deploy_repo_name)
    push_repo_dir = File.join(app_config_dir, "push")

    FileUtils.mkdir_p(push_repo_dir) unless File.exist?(push_repo_dir)
    FileUtils.cp_r(File.join(push_path, "."), push_repo_dir)

    prepare_gitenv()
    git_addpush_repo(path: app_config_dir, message: "add push cert")

    puts "✓ 证书已上传到配置仓库: #{push_repo_dir}"
  end

  x509_cert_path
end

.extract_config_info(config_json) ⇒ Hash

从配置文件中提取所有必要的信息

Parameters:

  • config_json (Hash)

    配置 JSON 对象

Returns:

  • (Hash)

    提取的配置信息



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pindo/module/cert/pem_helper.rb', line 10

def extract_config_info(config_json)
  config_info = {}

  # 提取 Apple ID
  if config_json['account_info'] && config_json['account_info']['apple_acount_id']
    config_info[:apple_id] = config_json['account_info']['apple_acount_id']
  end

  # 提取 Bundle ID
  if config_json['app_info'] && config_json['app_info']['app_identifier']
    config_info[:bundle_id] = config_json['app_info']['app_identifier']
  end

  config_info
end

.login(apple_id: nil) ⇒ Object



132
133
134
135
136
# File 'lib/pindo/module/cert/pem_helper.rb', line 132

def (apple_id:nil)
  puts apple_id
  Spaceship::Portal.(apple_id.to_s)
  Spaceship::Portal.select_team
end