Class: Pindo::Command::Unity::Autobuild

Inherits:
Pindo::Command::Unity show all
Includes:
Appselect, Githelper
Defined in:
lib/pindo/command/unity/autobuild.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 included from Githelper

#add_branch, #add_tag, #add_tag_with_check, #clone_clang_repo, #clone_devclang_repo, #clone_pindo_common_config_repo, #clone_pindo_env_config_repo, #clong_buildconfig_repo, #get_repo_base_name, #getcode_to_dir, #git_addpush_repo, #git_latest_commit_id, #git_root_directory, #is_git_directory?, #local_branch_exists?, #local_tag_exists?, #prepare_gitenv, #process_need_add_files, #remote_branch_exists?, #remote_tag_exists?, #remove_branch, #remove_tag

Methods included from Executable

capture_command, #executable, execute_command, which, which!

Methods included from Appselect

#all_deploy_bundle_name, #all_dev_bundle_name, #all_dev_bundleid, #all_itc_bundleid, #all_release_bundleid, #all_tool_bundleid, #deploy_build_setting_json, #dev_build_setting_json, #get_deploy_repo_with_modul_name, #get_deploy_setting_repo, #get_dev_setting_repo, #get_selected_deploy_bundle_name, #get_selected_deploy_bundleid, #get_selected_dev_bundle_name, #get_selected_dev_bundleid, #get_setting_bundleid_withdir, #load_setting, #select_main_app

Methods inherited from Pindo::Command

run, use_cache?, #validate!

Methods included from Funlog::Mixin

#pindo_log_instance

Methods included from Pindoconfig::Mixin

#pindo_single_config

Constructor Details

#initialize(argv) ⇒ Autobuild

Returns a new instance of Autobuild.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pindo/command/unity/autobuild.rb', line 83

def initialize(argv)

    @args_proj_name = argv.option('proj')
    @args_upload_flag = argv.flag?('upload', false)
    @args_send_flag = argv.flag?('send', false)
    @args_adhoc_flag = argv.flag?('adhoc', false)
    @args_deploy_flag = argv.flag?('deploy', false)
    @args_release_flag = argv.flag?('release', false)
    @args_bundle_id = argv.option('bundleid')
    @args_bundle_name = argv.option('bundle_name')
    @args_types = argv.option('types')

    # 如果开启 send,自动开启 upload
    if @args_send_flag
        @args_upload_flag = true
    end

    super
end

Class Method Details

.optionsObject

命令选项



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pindo/command/unity/autobuild.rb', line 68

def self.options
    [
        ['--bundleid=ID', '指定iOS打包的bundleID'],
        ['--bundle_name=NAME', '指定Android Package Name(如com.example.app,会拉取对应配置)'],
        ['--proj=NAME',   '指定上传到测试平台的项目名称'],
        ['--types=TYPES', '指定要构建的类型,逗号分隔(如:ipa,apk,html),默认全部类型'],
        ['--upload', '上传编译后的IPA/APK到测试平台'],
        ['--send', '发送通知到测试群组(同时也会发送给自己)'],
        ['--adhoc', '使用AdHoc证书打包iOS(默认使用Dev证书)'],
        ['--deploy', '使用发布证书打包iOS'],
        ['--release', '使用Release模式构建Android(默认使用Debug)'],
    ].concat(super)
end

Instance Method Details

#create_build_task(platform, platform_config, all_platform_configs, unity_task) ⇒ Object

创建构建任务



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/pindo/command/unity/autobuild.rb', line 248

def create_build_task(platform, platform_config, all_platform_configs, unity_task)
    # 确定构建模式
    build_mode = determine_build_mode(platform)

    # 获取平台路径
    platform_path = get_platform_path(platform)

    # 准备构建选项
    build_options = {
        proj_name: all_platform_configs["project_name"],
        upload: @args_upload_flag,
        send: @args_send_flag,
        app_info_obj: all_platform_configs["app_info_obj"],      # 共享的项目信息
        workflow_info: platform_config["workflow_info"],     # 平台特定的工作流
        project_path: platform_path
    }

    # 设置平台特定参数
    case platform
    when 'ios'
        build_options[:bundle_id] = platform_config["bundle_id"]
        build_options[:unity_root_path] = Dir.pwd  # 传递 Unity 根目录路径
    when 'android'
        build_options[:bundle_name] = platform_config["bundle_name"]
    when 'web'
        build_options[:project_path] = Dir.pwd  # Web 平台使用根目录
        build_options[:force_select_unity] = false
    end

    # 使用工厂方法创建任务
    build_task = Pindo::TaskSystem::BuildTask.create_task(
        platform: platform,
        mode: build_mode,
        options: build_options
    )

    # 设置依赖关系
    build_task.dependencies << unity_task.id

    build_task
end

#create_unity_export_task(platform, all_platform_configs) ⇒ Object

创建 Unity 导出任务



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/pindo/command/unity/autobuild.rb', line 223

def create_unity_export_task(platform, all_platform_configs)
    platform_config = all_platform_configs[platform]

    # 构建 context
    context = {
    }

    # 添加 project_name
    context[:project_name] = all_platform_configs["project_name"] if all_platform_configs["project_name"]

    # 添加 index_count
    context[:index_count] = platform_config["index_count"] if platform_config["index_count"]

    # 获取平台路径
    export_path = get_platform_path(platform)

    Pindo::TaskSystem::UnityExportTask.new(
        platform,
        project_path: Dir.pwd,
        export_path: export_path,
        context: context
    )
end

#create_upload_task(platform, all_platform_configs, build_task) ⇒ Object

创建上传任务



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/pindo/command/unity/autobuild.rb', line 291

def create_upload_task(platform, all_platform_configs, build_task)
    # 获取平台路径
    platform_path = get_platform_path(platform)

    # 获取平台特定的配置
    platform_config = all_platform_configs[platform]

    # 确定文件类型和上传路径
    file_type, upload_path = case platform
                             when 'ios'
                                 ['ipa', File.join(platform_path, 'build')]
                             when 'android'
                                 ['apk', File.join(platform_path, 'build')]
                             when 'web'
                                 ['html', File.join(platform_path, 'build')]
                             else
                                 [platform, platform_path]
                             end

    upload_task = Pindo::TaskSystem::UploadTask.new(
        file_type,
        upload_path,
        nil,
        app_info_obj: all_platform_configs["app_info_obj"],  # 使用共享的项目信息
        workflow_info: platform_config["workflow_info"],  # 使用平台特定的工作流
        project_name: all_platform_configs["project_name"],
        context: {
            send_to_chat: @args_send_flag  # 是否发送到测试群
        },
        dependencies: [build_task.id]  # 依赖构建任务
    )

    upload_task
end

#get_platform_path(platform) ⇒ Object

获取平台对应的导出/构建路径优先使用 Base 目录,如果不存在则使用普通目录



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/pindo/command/unity/autobuild.rb', line 205

def get_platform_path(platform)
    case platform
    when 'ios'
        base_ios_path = File.join(Dir.pwd, "GoodPlatform", "BaseiOS")
        ios_path = File.join(Dir.pwd, "GoodPlatform", "iOS")
        File.exist?(base_ios_path) ? base_ios_path : ios_path
    when 'android'
        base_android_path = File.join(Dir.pwd, "GoodPlatform", "BaseAndroid")
        android_path = File.join(Dir.pwd, "GoodPlatform", "Android")
        File.exist?(base_android_path) ? base_android_path : android_path
    when 'web'
        File.join(Dir.pwd, "GoodPlatform", "WebGL")
    else
        Dir.pwd
    end
end

#get_selected_build_typesObject

获取要构建的类型列表



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
# File 'lib/pindo/command/unity/autobuild.rb', line 104

def get_selected_build_types
    # 定义类型映射关系
    type_mapping = {
        'ipa' => 'ios',
        'apk' => 'android',
        'html' => 'web'
    }

    available_types = ['ipa', 'apk', 'html']

    # 如果指定了 --types 参数
    if @args_types
        # 解析类型参数
        types = @args_types.downcase.split(',').map(&:strip)

        # 验证类型是否有效
        invalid_types = types - available_types
        unless invalid_types.empty?
            raise Informative, "无效的构建类型: #{invalid_types.join(', ')}。可用类型: #{available_types.join(', ')}"
        end

        # 转换为平台列表
        platforms = types.map { |type| type_mapping[type] }
        return platforms.empty? ? type_mapping.values : platforms
    end

    # 默认返回所有平台
    return type_mapping.values
end

#make_task_with_config(selected_platforms, all_platform_configs) ⇒ Object

按平台顺序创建所有任务



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
# File 'lib/pindo/command/unity/autobuild.rb', line 166

def make_task_with_config(selected_platforms, all_platform_configs)
    all_tasks = []
    platform_build_tasks = {}  # 存储平台对应的构建任务

    # 第零步:创建 Git 标签任务(所有编译场景都需要打 Tag)
    git_tag_task = Pindo::TaskSystem::GitTagTask.new(Dir.pwd)
    all_tasks << git_tag_task

    # 第一步:为每个平台创建导出和构建任务
    selected_platforms.each do |platform|
        platform_config = all_platform_configs[platform]

        # 1. 创建 Unity 导出任务(依赖 Git 标签任务)
        unity_task = create_unity_export_task(platform, all_platform_configs)
        unity_task.dependencies << git_tag_task.id
        all_tasks << unity_task

        # 2. 创建构建任务(依赖导出任务)
        build_task = create_build_task(platform, platform_config, all_platform_configs, unity_task)
        all_tasks << build_task
        platform_build_tasks[platform] = build_task
    end

    # 第二步:创建所有上传任务(最后添加,只依赖对应平台的构建任务)
    # 注意:create_upload_task 内部已设置 dependencies: [build_task.id]
    if @args_upload_flag
        selected_platforms.each do |platform|
            build_task = platform_build_tasks[platform]
            upload_task = create_upload_task(platform, all_platform_configs, build_task)
            all_tasks << upload_task
        end
    end

    # 返回所有任务
    all_tasks
end

#prepare_all_platform_configs(selected_platforms) ⇒ Object

按平台顺序准备所有配置(iOS → Android → Web)



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/pindo/command/unity/autobuild.rb', line 327

def prepare_all_platform_configs(selected_platforms)

    # 按优先级排序:iOS → Android → Web
    platform_order = ['ios', 'android', 'web']
    sorted_platforms = selected_platforms.sort_by { |p| platform_order.index(p) || 999 }

    # 初始化配置结构
    configs = {
        "project_name" => nil,
        "app_info_obj" => nil,
        "ios" => {},
        "android" => {},
        "web" => {}
    }

    # ========== 为每个平台准备特定配置 ==========

    sorted_platforms.each do |platform|
        puts "\n准备 #{platform.upcase} 配置:"
        puts "----------------------------------------"

        platform_config = {}

        # 确定当前平台的 package_type
        package_type = case platform
                       when 'ios'
                           'ipa'
                       when 'android'
                           'apk'
                       when 'web'
                           'zip'
                       else
                           'ipa'
                       end

        # 获取当前平台的 JPS 配置
        app_info_obj, workflow_info = PgyerHelper.share_instace.prepare_upload(
            working_directory: Dir.pwd,
            proj_name: @args_proj_name,
            package_type: package_type
        )

        # 保存 app_info_obj 到共享配置(所有平台共享同一个项目)
        if configs["app_info_obj"].nil?
            configs["app_info_obj"] = app_info_obj
            configs["project_name"] = app_info_obj ? app_info_obj["projectName"] : nil
        end

        # 保存平台特定的 workflow_info(每个平台有不同的工作流)
        platform_config["workflow_info"] = workflow_info

        puts "  ✓ Project: #{app_info_obj ? app_info_obj["projectName"] : "N/A"}"
        puts "  ✓ Package Type: #{package_type}"
        puts "  ✓ Workflow: #{workflow_info[:package_name]}" if workflow_info

        case platform
        when 'ios'
            # 获取 iOS Bundle ID
            if @args_bundle_id
                platform_config["bundle_id"] = @args_bundle_id
            else
                if @args_deploy_flag
                    platform_config["bundle_id"] = get_selected_deploy_bundleid()
                else
                    platform_config["bundle_id"] = get_selected_dev_bundleid()
                end
            end
            puts "  Bundle ID: #{platform_config["bundle_id"]}"

        when 'android'
            # 获取 Android Package Name
            if @args_bundle_name
                platform_config["bundle_name"] = @args_bundle_name
            else
                platform_config["bundle_name"] = get_selected_dev_bundle_name()
            end
            puts "  Package Name: #{platform_config["bundle_name"]}"

        when 'web'
            # Web 平台获取 indexNo
            platform_config["index_count"] = fetch_webgl_index_no(
                app_info_obj: app_info_obj,
                workflow_info: workflow_info
            )
            puts "  WebGL Index: #{platform_config["index_count"]}"
        end

        # 存储平台配置
        configs[platform] = platform_config
    end

    configs
end

#runObject



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
# File 'lib/pindo/command/unity/autobuild.rb', line 134

def run
    pindo_project_dir = Dir.pwd

    # 检查是否是Unity工程
    unity_helper = Pindo::Client::UnityHelper.share_instance
    unless unity_helper.unity_project?(pindo_project_dir)
        raise Informative, "当前目录不是Unity工程,请在Unity工程根目录下执行此命令"
    end

    # 1. 获取要构建的类型列表
    selected_platforms = get_selected_build_types()

    # 2. 按平台顺序准备所有配置(iOS → Android → Web)
    all_platform_configs = prepare_all_platform_configs(selected_platforms)

    # 3. 按平台顺序创建所有任务(包含 GitTagTask)
    all_tasks = make_task_with_config(selected_platforms, all_platform_configs)

    # 4. 统一添加到任务管理器并执行
    task_manager = Pindo::TaskSystem::TaskManager.instance
    task_manager.clear_all  # 清空之前的任务

    # 按顺序添加所有任务
    all_tasks.each do |task|
        task_manager.add_task(task)
    end

    # 执行任务
    task_manager.start
end