Class: Pindo::BuildInfoManager

Inherits:
Object
  • Object
show all
Includes:
Githelper, Pindoconfig::Mixin, Singleton
Defined in:
lib/pindo/config/build_info_manager.rb

Overview

构建信息管理器负责管理应用配置仓库的拉取、更新等操作

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pindoconfig::Mixin

#pindo_single_config

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, popen3, reader, which, which!

Class Method Details

.share_instanceObject



16
17
18
# File 'lib/pindo/config/build_info_manager.rb', line 16

def share_instance
  instance
end

Instance Method Details

#create_appconfig_with_bundleid(bundle_id:, test_flag: false) ⇒ String?

创建应用配置仓库

Parameters:

  • bundle_id (String)

    Bundle ID(作为仓库名称)

  • test_flag (Boolean) (defaults to: false)

    是否为测试环境(默认:false)

Returns:

  • (String, nil)

    成功返回配置仓库目录路径,失败返回 nil



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
# File 'lib/pindo/config/build_info_manager.rb', line 65

def create_appconfig_with_bundleid(bundle_id:, test_flag: false)
  return nil if bundle_id.nil? || bundle_id.empty?

  require_relative '../client/giteeclient'

  # 获取 Gitee 客户端
  gitee_client = GiteeClient.new(access_token: pindo_single_config.gitee_api_key)

  # 获取模板配置仓库目录
  demo_dir = clong_buildconfig_repo(repo_name: pindo_single_config.demo_bundle_id)

  # 确定仓库公开类型和所有者组织
  public_type = 0
  owner_org = pindo_single_config.build_deploy_org
  repo_name = bundle_id

  if test_flag
    owner_org = pindo_single_config.build_test_org
    public_type = 2
  end

  # 创建 Gitee 仓库
  success = gitee_client.gitee_create_repo(
    owner: owner_org,
    repo_name: repo_name,
    public_type: public_type
  )

  if success
    # 如果是测试环境,修改仓库设置
    if test_flag
      modify_repo_setting(repo_name: repo_name, owner_org: owner_org)
    end

    # 克隆配置仓库
    app_config_dir = clong_buildconfig_repo(repo_name: repo_name)
    system "open #{app_config_dir}"

    if File.exist?(app_config_dir)
      # 从模板仓库复制配置文件
      update_appconfig_repo(
        bundle_id: bundle_id,
        demo_dir: demo_dir,
        app_config_dir: app_config_dir
      )
      puts "✓ 配置仓库创建成功: #{app_config_dir}"
      return app_config_dir
    else
      puts "❌ 创建应用配置仓库失败"
      return nil
    end
  else
    # 仓库已存在,直接打开
    app_config_dir = clong_buildconfig_repo(repo_name: repo_name)
    system "open #{app_config_dir}"
    puts "⚠ 配置仓库已存在: #{app_config_dir}"
    return app_config_dir
  end
end

#get_deploy_repo_with_modul_name(module_name:) ⇒ String?

根据模块名称获取部署仓库名称

Parameters:

  • module_name (String)

    模块名称(来自 config.json 的 app_type)

Returns:

  • (String, nil)

    返回对应的 git_repo_name,失败返回 nil



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
# File 'lib/pindo/config/build_info_manager.rb', line 128

def get_deploy_repo_with_modul_name(module_name:)
  return nil if module_name.nil? || module_name.empty?

  # 读取 deploy_build_setting.json
  setting_file = File.join(pindo_single_config.pindo_env_configdir, 'deploy_build_setting.json')
  unless File.exist?(setting_file)
    puts "⚠ deploy_build_setting.json 不存在: #{setting_file}"
    return nil
  end

  begin
    setting_json = JSON.parse(File.read(setting_file))
    repo_name = setting_json.dig(module_name, 'git_repo_name')

    if repo_name.nil? || repo_name.empty?
      raise Informative, "config.json app_type is error!!!"
    end

    return repo_name
  rescue JSON::ParserError => e
    puts "⚠ 解析 deploy_build_setting.json 失败: #{e.message}"
    return nil
  rescue => e
    raise Informative, "config.json app_type is error!!!"
  end
end

#pull_appconfig_with_reponame(repo_name:, target_dir: nil) ⇒ Boolean

拉取应用配置仓库

Parameters:

  • repo_name (String)

    仓库名称(Bundle ID)

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

    目标目录(默认:当前目录)

Returns:

  • (Boolean)

    是否成功拉取配置



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pindo/config/build_info_manager.rb', line 25

def pull_appconfig_with_reponame(repo_name:, target_dir: nil)
  return false if repo_name.nil? || repo_name.empty?

  # 规范化仓库名称:去除尾部的 .* (通配符转换)
  normalized_repo_name = repo_name.end_with?('.*') ? repo_name.chomp('.*') : repo_name

  # 获取配置仓库目录
  begin
    app_config_dir = clong_buildconfig_repo(repo_name: normalized_repo_name)
  rescue StandardError => e
    puts "拉取配置仓库失败: #{e.message}"
    return false
  end

  # 确定目标目录
  target_dir ||= Dir.pwd

  # 如果配置仓库目录与目标目录不同,且存在 config.json,则复制
  if !app_config_dir.eql?(target_dir) && File.exist?(File.join(app_config_dir, 'config.json'))
    begin
      FileUtils.cp_r(File.join(app_config_dir, "config.json"), target_dir)
      puts "✓ 配置文件已复制到: #{target_dir}"
      return true
    rescue StandardError => e
      puts "复制配置文件失败: #{e.message}"
      return false
    end
  elsif app_config_dir.eql?(target_dir)
    puts "✓ 配置文件已存在于目标目录"
    return true
  else
    puts "⚠ 配置仓库中未找到 config.json"
    return false
  end
end