Class: Pindo::ItcAppHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/appstore/itcapp_helper.rb

Overview

iTunes Connect App 管理辅助类负责在 App Store Connect 中创建和管理应用

Class Method Summary collapse

Class Method Details

.create_itcapp(bundle_id:, app_version:, company_name: nil) ⇒ Object

创建 iTunes Connect App

Parameters:

  • bundle_id (String)

    Bundle ID

  • app_version (String)

    应用版本号

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

    公司名称

Returns:

  • (Object)

    创建的 App 对象



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
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/pindo/module/appstore/itcapp_helper.rb', line 112

def self.create_itcapp(bundle_id:, app_version:, company_name: nil)
  return nil if bundle_id.nil?

  # 检查 App 是否已存在
  app = Spaceship::ConnectAPI::App.find(bundle_id)
  if app
    puts "App is already exist !!!"
    return app
  end

  # 生成默认 SKU ID
  app_sku_id_fixed = bundle_id + ".sku"
  puts "App的默认sku id 为: #{app_sku_id_fixed}"

  # 让用户自定义 SKU ID
  input_skuid = ask("请自定义输入App Sku ID: ") || nil
  if !input_skuid.nil? && input_skuid.length > 1
    app_sku_id_fixed = input_skuid
  end

  # 获取公司名称
  if company_name.nil?
    company_name = ask('Company Name : ') || nil
  end

  puts "App Sku ID   : #{app_sku_id_fixed}"
  puts "Company Name : #{company_name}"

  # 确认信息
  answer = agree("请确认上面信息是否正确(Y/n):")
  unless answer
    raise "用户取消创建,请重新运行命令"
  end

  # 生成 App 名称(移除点号,最多30个字符)
  app_default_name = bundle_id.gsub('.', '')
  if app_default_name.length > 30
    app_default_name = app_default_name[0, 30]
  end

  # 创建 App
  app = Spaceship::ConnectAPI::App.create(
    name: app_default_name,
    version_string: app_version,
    sku: app_sku_id_fixed,
    primary_locale: "en-US",
    bundle_id: bundle_id,
    platforms: ["IOS"],
    company_name: company_name
  )

  puts "App is created !!!"
  app
end

.ensure_bundle_id_exists(apple_id:, bundle_id:, config_path:) ⇒ Object

确保 Bundle ID 在开发者中心存在

Parameters:

  • apple_id (String)

    Apple ID

  • bundle_id (String)

    Bundle ID

  • config_path (String)

    配置文件路径



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pindo/module/appstore/itcapp_helper.rb', line 95

def self.ensure_bundle_id_exists(apple_id:, bundle_id:, config_path:)
  Spaceship::Portal.(apple_id.to_s)
  Spaceship::Portal.select_team

  app = Spaceship::Portal.app.find(bundle_id)
  if app.nil?
    puts "Bundle ID #{bundle_id} 不存在,需要先创建"
    puts "请先运行: pindo appstore bundleid #{config_path}"
    raise "Bundle ID #{bundle_id} 不存在于 Apple 开发者中心"
  end
end

.execute_itcapp_creation(config_json:, config_path:, app_version: nil, company_name: nil) ⇒ Object

执行 iTunes Connect App 创建流程

Parameters:

  • config_json (Hash)

    配置 JSON 对象

  • config_path (String)

    配置文件路径

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

    应用版本号

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

    公司名称



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
# File 'lib/pindo/module/appstore/itcapp_helper.rb', line 46

def self.execute_itcapp_creation(config_json:, config_path:, app_version: nil, company_name: nil)
  # 提取配置信息
  config_info = extract_config_info(config_json)

  # 使用命令行参数覆盖配置
  config_info[:app_version] = app_version if app_version
  config_info[:company_name] = company_name if company_name

  # 验证必需字段
  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?

  # 确保 Bundle ID 在开发者中心存在
  ensure_bundle_id_exists(
    apple_id: config_info[:apple_id],
    bundle_id: config_info[:bundle_id],
    config_path: config_path
  )

  # 创建 iTunes Connect App
  puts "Create app in itc ..."
  puts config_info[:apple_id]
  puts "Login #{config_info[:apple_id]}..."

  Spaceship::Tunes.(config_info[:apple_id].to_s)
  Spaceship::Tunes.select_team

  app = create_itcapp(
    bundle_id: config_info[:bundle_id],
    app_version: config_info[:app_version],
    company_name: config_info[:company_name]
  )

  # 修改配置文件,添加 App ID
  if app
    modify_appconfig_with_appid(
      config_path: config_path,
      bundle_id: config_info[:bundle_id],
      app: app
    )
  end

  app
end

.extract_config_info(config_json) ⇒ Hash

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

Parameters:

  • config_json (Hash)

    配置 JSON 对象

Returns:

  • (Hash)

    提取的配置信息



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
# File 'lib/pindo/module/appstore/itcapp_helper.rb', line 13

def self.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

  # 提取 Company Name
  if config_json['account_info'] && config_json['account_info']['acount_company_name']
    config_info[:company_name] = config_json['account_info']['acount_company_name'].strip
  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

  # 提取版本号
  if config_json['app_info'] && config_json['app_info']['app_version']
    config_info[:app_version] = config_json['app_info']['app_version']
  else
    config_info[:app_version] = "1.0.0"  # 默认版本
  end

  config_info
end

.modify_appconfig_file(config_file:, app_info_detail:) ⇒ Object

修改单个配置文件

Parameters:

  • config_file (String)

    配置文件路径

  • app_info_detail (Object)

    App 信息对象



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/pindo/module/appstore/itcapp_helper.rb', line 202

def self.modify_appconfig_file(config_file:, app_info_detail:)
  return unless File.exist?(config_file)

  config_content = JSON.parse(File.read(config_file))

  File.open(config_file, "w") do |f|
    # 更新 app_id_ios
    config_content['app_info']['app_id_ios'] = app_info_detail.id

    # 更新 apple_app_id(如果存在)
    if config_content['app_setting'] && config_content['app_setting']["apple_app_id"]
      config_content['app_setting']["apple_app_id"] = app_info_detail.id
    end

    # 更新 kGUKeyAppId(如果存在)
    if config_content['app_setting'] && config_content['app_setting']["kGUKeyAppId"]
      config_content['app_setting']["kGUKeyAppId"] = app_info_detail.id
    end

    f.write(JSON.pretty_generate(config_content))
  end

  puts "✓ 配置文件已更新: #{config_file}"
end

.modify_appconfig_with_appid(config_path:, bundle_id:, app:) ⇒ Object

修改配置文件,添加 App ID

Parameters:

  • config_path (String)

    配置文件路径

  • bundle_id (String)

    Bundle ID

  • app (Object)

    App 对象



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
# File 'lib/pindo/module/appstore/itcapp_helper.rb', line 171

def self.modify_appconfig_with_appid(config_path:, bundle_id:, app:)
  return if app.nil?

  puts
  puts "app.id : #{app.id} +++++++++"
  puts

  # 修改本地配置文件
  modify_appconfig_file(config_file: config_path, app_info_detail: app)

  # 修改远程配置仓库
  begin
    require_relative '../../base/githelper'
    include Pindo::Githelper

    app_config_dir = clong_buildconfig_repo(repo_name: bundle_id)
    app_config_file = File.join(app_config_dir, "config.json")

    if File.exist?(app_config_file)
      modify_appconfig_file(config_file: app_config_file, app_info_detail: app)
      prepare_gitenv()
      git_addpush_repo(path: app_config_dir, message: "modify apple id")
    end
  rescue StandardError => e
    puts "更新远程配置仓库失败: #{e.message}"
  end
end