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
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
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
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))
replace_prefix_in_hash(config_json, from_prefix, new_prefix)
puts "已替换 config.json 中的 #{from_prefix} -> #{new_prefix}"
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
Pindo::GitHandler.git_addpush_repo(
path: new_config_dir,
message: "init config from #{@from_bundle_id}"
)
puts
puts "配置仓库创建完成: #{new_config_dir}"
end
|