Class: Pindo::Command::Appstore::Updateid

Inherits:
Pindo::Command::Appstore show all
Defined in:
lib/pindo/command/appstore/updateid.rb

Constant Summary

Constants inherited from Pindo::Command

DEFAULT_OPTIONS, DEFAULT_ROOT_OPTIONS

Instance Attribute Summary

Attributes inherited from Pindo::Command

#args_help_flag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pindo::Command

command_name, #initialize_options, run, use_cache?

Methods included from Funlog::Mixin

#pindo_log_instance

Methods included from Pindoconfig::Mixin

#pindo_single_config

Constructor Details

#initialize(argv) ⇒ Updateid

Returns a new instance of Updateid.



67
68
69
70
71
72
73
74
# File 'lib/pindo/command/appstore/updateid.rb', line 67

def initialize(argv)
    positional_config = argv.shift_argument
    @options = initialize_options(argv)
    @config_path = @options[:config] || positional_config || File.join(Dir.pwd, 'config.json')
    @dry_run = @options[:dry_run]
    super
    @additional_args = argv.remainder!
end

Class Method Details

.option_itemsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pindo/command/appstore/updateid.rb', line 43

def self.option_items
    @option_items ||= [
        Pindo::Options::OptionItem.new(
            key: :config,
            description: '指定config.json文件路径',
            type: String,
            optional: true,
            example: 'pindo appstore updateid --config=config.json'
        ),
        Pindo::Options::OptionItem.new(
            key: :dry_run,
            description: '预览模式,只显示差异不修改文件',
            type: :boolean,
            optional: true,
            default_value: false,
            example: 'pindo appstore updateid --dry-run'
        )
    ]
end

.optionsObject



63
64
65
# File 'lib/pindo/command/appstore/updateid.rb', line 63

def self.options
    option_items.map { |item| item.to_claide_option }.concat(super)
end

Instance Method Details

#runObject



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
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
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/pindo/command/appstore/updateid.rb', line 100

def run
    begin
        app_info = @config_json['app_info']
        apple_id = @config_json.dig('account_info', 'apple_acount_id')

        # 登录 Apple Developer Portal
        puts "Login #{apple_id}..."
        Spaceship.(apple_id.to_s)
        Spaceship.select_team

        bundle_id = app_info['app_identifier']
        puts
        puts "读取 #{bundle_id} 的 Capability..."

        # 读取主 Bundle ID 的 capabilities
        capabilities = Pindo::BundleIdHelper.fetch_capabilities_from_portal(bundle_id: bundle_id)

        if capabilities.nil?
            puts "无法读取 #{bundle_id} 的 Capability,跳过"
            return
        end

        # 显示读取到的 capabilities
        capabilities.each do |key, value|
            puts "  #{key}: #{value.is_a?(String) ? "\"#{value}\"" : value}"
        end

        # 更新所有 Extension Bundle ID 的名称
        puts
        extension_keys = app_info.keys.select { |k| k.start_with?("app_identifier_") }
        extension_keys.each do |key|
            ext_bundle_id = app_info[key]
            next if ext_bundle_id.nil? || ext_bundle_id.empty? || ext_bundle_id.include?("__________")

            expected_name = Pindo::BundleIdHelper.generate_bundleid_name(ext_bundle_id)
            ext_app = Spaceship::Portal.app.find(ext_bundle_id)
            if ext_app.nil?
                puts "  #{ext_bundle_id} — Portal 中未找到,跳过"
                next
            end

            if ext_app.name != expected_name
                puts "Update #{ext_bundle_id} name: #{ext_app.name} -> #{expected_name}"
                begin
                    ext_app.update_name!(expected_name)
                rescue => e
                    puts "  Portal 更新名称失败: #{e.message},尝试通过 API Key 更新..."
                    begin
                        Pindo::BundleIdHelper.update_bundleid_name(bundle_id: ext_bundle_id, new_name: expected_name)
                    rescue => e2
                        puts "  更新名称失败: #{e2.message}"
                    end
                end
            end
        end

        if @dry_run
            # dry-run 模式:对比现有 capabilities 并显示差异
            old_capabilities = app_info['capabilities'] || {}
            puts
            puts "[dry-run] 差异:"
            has_diff = false
            capabilities.each do |key, new_value|
                old_value = old_capabilities[key]
                if old_value.nil?
                    puts "  + #{key}: #{new_value.is_a?(String) ? "\"#{new_value}\"" : new_value}"
                    has_diff = true
                elsif old_value != new_value
                    old_display = old_value.is_a?(String) ? "\"#{old_value}\"" : old_value
                    new_display = new_value.is_a?(String) ? "\"#{new_value}\"" : new_value
                    puts "  ~ #{key}: #{old_display} -> #{new_display}"
                    has_diff = true
                end
            end
            old_capabilities.each do |key, old_value|
                unless capabilities.key?(key)
                    old_display = old_value.is_a?(String) ? "\"#{old_value}\"" : old_value
                    puts "  = #{key}: #{old_display} (保留)"
                    has_diff = true
                end
            end
            puts "  (无差异)" unless has_diff
            puts
            puts "[dry-run] 未修改任何文件"
        else
            # 获取 config 仓库目录并更新到最新(~/.pindo/{bundle_id}/)
            app_config_dir = Pindo::GitHandler.clong_buildconfig_repo(repo_name: bundle_id)
            config_repo_file = File.join(app_config_dir, "config.json")

            # 提交前先丢弃仓库中未跟踪的修改,确保干净状态
            Pindo::GitHandler.git!(%W(-C #{app_config_dir} checkout -- config.json)) rescue nil

            # 读取仓库中最新的 config.json 并更新 capabilities
            repo_config_json = JSON.parse(File.read(config_repo_file))
            repo_config_json['app_info']['capabilities'] = (repo_config_json['app_info']['capabilities'] || {}).merge(capabilities)

            # 只写入 config.json 一个文件
            File.open(config_repo_file, "w") do |f|
                f.write(JSON.pretty_generate(repo_config_json) + "\n")
            end

            # 同时更新本地 config.json(如果路径不同)
            if File.expand_path(@config_path) != File.expand_path(config_repo_file)
                @config_json['app_info']['capabilities'] = (app_info['capabilities'] || {}).merge(capabilities)
                File.open(@config_path, "w") do |f|
                    f.write(JSON.pretty_generate(@config_json) + "\n")
                end
            end

            puts

            # 只提交 config.json 到 Git 仓库
            Pindo::GitHandler.git_addpush_repo(
                path: app_config_dir,
                message: "sync capabilities from Apple Developer Portal",
                commit_file_params: ["config.json"]
            )

            puts "capabilities 已写入 config.json 并提交"
        end
    rescue StandardError => e
        puts "\n updateid 失败: #{e.message}"
        raise e
    end
end

#validate!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pindo/command/appstore/updateid.rb', line 76

def validate!
    super
    unless File.exist?(@config_path)
        help! "配置文件不存在: #{@config_path}"
    end
    begin
        @config_json = JSON.parse(File.read(@config_path))
    rescue JSON::ParserError => e
        help! "配置文件格式错误: #{e.message}"
    end

    # 验证 app_identifier
    app_info = @config_json['app_info']
    if app_info.nil? || app_info['app_identifier'].nil? || app_info['app_identifier'].empty?
        help! "config.json 中缺少 app_info.app_identifier"
    end

    # 验证 Apple ID
    apple_id = @config_json.dig('account_info', 'apple_acount_id')
    if apple_id.nil? || apple_id.empty? || apple_id.include?("__________")
        help! "config.json 中缺少有效的 Apple ID (account_info.apple_acount_id)"
    end
end