Class: Pindo::Command::Utils::Copyconfig

Inherits:
Pindo::Command::Utils show all
Defined in:
lib/pindo/command/utils/copyconfig.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) ⇒ Copyconfig

Returns a new instance of Copyconfig.



49
50
51
52
53
54
# File 'lib/pindo/command/utils/copyconfig.rb', line 49

def initialize(argv)
  @new_bundle_id = argv.shift_argument
  @from_bundle_id = argv.option('from')
  @new_apple_id = argv.option('apple-id')
  super
end

Class Method Details

.optionsObject



42
43
44
45
46
47
# File 'lib/pindo/command/utils/copyconfig.rb', line 42

def self.options
  [
    ['--from', '源配置仓库的bundle id'],
    ['--apple-id', '新的Apple开发者账号邮箱']
  ].concat(super)
end

Instance Method Details

#runObject



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
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
# File 'lib/pindo/command/utils/copyconfig.rb', line 62

def run
  # 提取组织前缀: com.heroneverdie101.xxx -> com.heroneverdie101
  from_parts = @from_bundle_id.split('.')
  new_parts = @new_bundle_id.split('.')

  if from_parts.length < 2 || new_parts.length < 2
    puts "bundle id 格式错误,至少需要两段 (如 com.org)"
    return
  end

  from_prefix = from_parts[0..1].join('.')
  new_prefix = new_parts[0..1].join('.')

  puts "源仓库: #{@from_bundle_id}"
  puts "新仓库: #{@new_bundle_id}"
  puts "前缀替换: #{from_prefix} -> #{new_prefix}"
  puts

  # 拉取源仓库
  puts "拉取源配置仓库..."
  source_dir = Pindo::GitHandler.clong_buildconfig_repo(repo_name: @from_bundle_id)
  source_config = File.join(source_dir, "config.json")

  unless File.exist?(source_config)
    puts "源仓库 #{@from_bundle_id} 中不存在 config.json"
    return
  end

  # 创建新仓库
  puts "创建新配置仓库 #{@new_bundle_id}..."
  require_relative '../../client/giteeclient'

  pindo_single_config = Pindoconfig.instance
  gitee_client = GiteeClient.new(access_token: pindo_single_config.gitee_api_key)

  owner_org = pindo_single_config.build_test_org

  puts "目标组织: #{owner_org}"

  success = gitee_client.gitee_create_repo(
    owner: owner_org,
    repo_name: @new_bundle_id,
    public_type: 2
  )

  unless success
    puts "创建仓库失败,仓库可能已存在"
    return
  end

  # 将新仓库注册到 git_base_url.json,确保 clong_buildconfig_repo 能找到正确的 org
  build_info_manager = Pindo::BuildInfoManager.share_instance
  build_info_manager.modify_repo_setting(
    repo_name: @new_bundle_id,
    owner_org: owner_org
  )

  Funlog.instance.fancyinfo_update("等待 Gitee 仓库初始化...")
  sleep 2

  # 克隆新仓库
  new_config_dir = nil
  3.times do |i|
    begin
      new_config_dir = Pindo::GitHandler.clong_buildconfig_repo(repo_name: @new_bundle_id)
      break if new_config_dir && File.exist?(new_config_dir)
    rescue => e
      if i < 2
        Funlog.instance.fancyinfo_update("克隆失败 (#{i + 1}/3),3秒后重试...")
        sleep 3
      else
        puts "克隆仓库失败: #{e.message}"
        return
      end
    end
  end

  # 复制源仓库所有内容到新仓库
  puts "复制源仓库内容..."
  Dir.glob(File.join(source_dir, '*'), File::FNM_DOTMATCH).each do |item|
    basename = File.basename(item)
    next if basename == '.' || basename == '..' || basename == '.git'
    FileUtils.cp_r(item, new_config_dir)
  end

  # 修改 config.json
  new_config_file = File.join(new_config_dir, "config.json")
  if File.exist?(new_config_file)
    config_json = JSON.parse(File.read(new_config_file))

    # 替换所有 bundle id 的组织前缀
    replace_prefix_in_hash(config_json, from_prefix, new_prefix)
    puts "已替换 config.json 中的 #{from_prefix} -> #{new_prefix}"

    # 更新 Apple ID
    if @new_apple_id && !@new_apple_id.empty?
      if config_json['account_info'] && config_json['account_info']['apple_acount_id']
        old_apple_id = config_json['account_info']['apple_acount_id']
        config_json['account_info']['apple_acount_id'] = @new_apple_id
        puts "已替换 apple_acount_id: #{old_apple_id} -> #{@new_apple_id}"
      end
    end

    File.open(new_config_file, "w") do |f|
      f.write(JSON.pretty_generate(config_json) + "\n")
    end
  end

  # 只提交 config.json
  Pindo::GitHandler.git_addpush_repo(
    path: new_config_dir,
    message: "init config from #{@from_bundle_id}"
  )

  puts
  puts "配置仓库创建完成: #{new_config_dir}"
end

#validate!Object



56
57
58
59
60
# File 'lib/pindo/command/utils/copyconfig.rb', line 56

def validate!
  super
  help! "请指定新的 bundle id" if @new_bundle_id.nil? || @new_bundle_id.empty?
  help! "请通过 --from 指定源 bundle id" if @from_bundle_id.nil? || @from_bundle_id.empty?
end