Class: Pindo::BuildHelper

Inherits:
Object
  • Object
show all
Includes:
BaseAndroidHelper, Githelper, Singleton
Defined in:
lib/pindo/module/build/buildhelper.rb

Instance Attribute Summary collapse

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 BaseAndroidHelper

#build_jdk_path_from_unity_root, #check_and_replace_unity_jdk, #check_and_update_unity_jdk, #create_and_install_openjdk, #ensure_java_version_compliance, #extract_config_block_groovy, #extract_config_block_kts, #extract_keystore_fields_common, #extract_keystore_fields_groovy, #extract_keystore_fields_kts, #extract_signing_configs_groovy, #extract_signing_configs_kts, #extract_var_or_value, #find_android_subproject, #find_java11_installation, #find_java_command, #find_unity_editor_paths, #fix_store_file_path, #get_build_tools, #get_ext_values, #get_keystore_config, #get_keystore_config_groovy, #get_keystore_config_kts, #get_main_module, #install_adoptium_java_11, #install_java_11, #install_java_11_macos, #modify_il2cpp_config, #remove_desktop_google_service, #remove_groovy_comments, #remove_kts_comments, #replace_unity_jdk_with_java11, #resolve_var, #unity_android_project?, #verify_java_version

Instance Attribute Details

#temp_tag_decisionObject

临时缓存,只在内存中,不保存到文件



15
16
17
# File 'lib/pindo/module/build/buildhelper.rb', line 15

def temp_tag_decision
  @temp_tag_decision
end

Class Method Details

.share_instanceObject



18
19
20
# File 'lib/pindo/module/build/buildhelper.rb', line 18

def share_instance
  instance
end

Instance Method Details

#add_git_cliffconfig(project_path) ⇒ Object



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
# File 'lib/pindo/module/build/buildhelper.rb', line 86

def add_git_cliffconfig(project_path)

  temp_dir = Dir.pwd
  Funlog.instance.fancyinfo_start("添加日志变更git-cliff配置...")
  if is_git_directory?(local_repo_dir: project_path)
    current_git_root_path = git_root_directory(local_repo_dir: project_path)
    Dir.chdir(current_git_root_path)
    pindo_common_dir = clone_pindo_common_config_repo(force_delete:false)
    if File.exist?(File.join(pindo_common_dir, 'cliff.toml'))
      FileUtils.cp_r(File.join(pindo_common_dir, 'cliff.toml'), File.join(current_git_root_path, 'cliff.toml'))
    end
    Funlog.instance.fancyinfo_update("仓库添加git-cliff配置")
    write_gitignore(current_git_root_path)
    Funlog.instance.fancyinfo_update("仓库添加.gitignore")
    Dir.chdir(current_git_root_path)
    current_branch = git!(%W(-C #{current_git_root_path} rev-parse --abbrev-ref HEAD)).strip
    git!(%W(-C #{current_git_root_path} add cliff.toml))
    git!(%W(-C #{current_git_root_path} add .gitignore))
    commit_message = "docs: 添加日志变更配置".encode('UTF-8')
    git!(%W(-C #{current_git_root_path} commit -m #{commit_message}))
    git!(%W(-C #{current_git_root_path} push origin #{current_branch}))

  else
    Funlog.instance.fancyinfo_error("当前目录不是git仓库,请在git仓库根目录下执行此命令")
    raise Informative, "当前目录不是git仓库,请在git仓库根目录下执行此命令"
    Dir.chdir(temp_dir)
  end
  Funlog.instance.fancyinfo_success("日志变更git-cliff配置完成!")
  Dir.chdir(temp_dir)
end

#android_project?(project_path) ⇒ Boolean

Returns:

  • (Boolean)


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/pindo/module/build/buildhelper.rb', line 277

def android_project?(project_path)
  # 检查Android工程的关键文件和目录
  gradle_file = File.exist?(File.join(project_path, "build.gradle")) || File.exist?(File.join(project_path, "build.gradle.kts"))
  settings_gradle = File.exist?(File.join(project_path, "settings.gradle")) || File.exist?(File.join(project_path, "settings.gradle.kts"))

  # 尝试获取主模块
  main_module = nil
  begin
    main_module = get_main_module(project_path)
  rescue => e
    puts "获取主模块失败: #{e.message}" if ENV['DEBUG']
  end

  # Android Studio项目结构
  if gradle_file && settings_gradle && main_module
    app_gradle = File.exist?(File.join(main_module, "build.gradle")) || File.exist?(File.join(main_module, "build.gradle.kts"))
    app_manifest = File.exist?(File.join(main_module, "src", "main", "AndroidManifest.xml"))
    return true if app_gradle && app_manifest
  end

  # 如果无法通过标准方式检测,尝试更宽松的检测
  # 检查是否有任何包含 build.gradle 的子目录
  if gradle_file && settings_gradle
    Dir.entries(project_path).each do |entry|
      next if entry.start_with?('.')
      entry_path = File.join(project_path, entry)
      if File.directory?(entry_path)
        app_gradle = File.exist?(File.join(entry_path, "build.gradle")) || File.exist?(File.join(entry_path, "build.gradle.kts"))
        app_manifest = File.exist?(File.join(entry_path, "src", "main", "AndroidManifest.xml"))
        if app_gradle && app_manifest
          return true
        end
      end
    end
  end

  false
end

#check_check_and_install_cliff(project_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pindo/module/build/buildhelper.rb', line 40

def check_check_and_install_cliff(project_path)
  if is_git_directory?(local_repo_dir: project_path)
    current_git_root_path = git_root_directory(local_repo_dir: project_path)
    unless File.exist?(File.join(current_git_root_path, 'cliff.toml'))
      add_git_cliffconfig(current_git_root_path)
    end
  end

  begin
    if !system('which git-cliff > /dev/null 2>&1')
      puts "安装git-cliff..."
      install_gitcliff()
    end
  rescue
    raise Informative, "安装git-cliff出现错误"
  end
end

#check_is_need_add_tag?(project_path, auto_mode: false) ⇒ Boolean

Returns:

  • (Boolean)


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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/pindo/module/build/buildhelper.rb', line 132

def check_is_need_add_tag?(project_path, auto_mode: false)
  tag_action_parms = nil
  is_need_add_tag = false

  if is_git_directory?(local_repo_dir: project_path)
    current_git_root_path = git_root_directory(local_repo_dir: project_path)
    latest_tag = get_latest_version_tag(project_dir: current_git_root_path)

    unless is_tag_at_head?(git_root_dir: current_git_root_path, tag_name: latest_tag)
      if auto_mode 
        # 在自动化模式或没有交互式终端时,默认选择新增版本号打新Tag
        puts "检测到当前代码没有打Tag,在自动模式下将自动新增版本号并打新Tag"
        tag_action_parms = []
        is_need_add_tag = false
      else
        # 检查是否已有用户选择
        context = PindoContext.instance
        build_helper = BuildHelper.share_instance
        # 优先从持久化缓存读取,如果没有则从内存临时缓存读取
        cached_decision = context.get_selection(PindoContext::SelectionKey::TAG_DECISION) ||
                         build_helper.temp_tag_decision

        if cached_decision
          # 使用之前的选择
          puts "\n使用之前的选择:#{cached_decision[:description]}"
          tag_action_parms = cached_decision[:tag_action_parms]
          is_need_add_tag = cached_decision[:is_need_add_tag]

          # 如果是退出选择,则抛出异常
          if cached_decision[:action] == :exit
            raise Informative, "终止退出编译!"
          end
        else
          # 第一次询问
          cli = HighLine.new
          selected_action = nil
          selected_description = nil

          menu_options = {
            "新增版本号,打新Tag" => -> {
              tag_action_parms = []
              selected_action = :new_tag
              selected_description = "新增版本号,打新Tag"
              :new_tag
            },
            "将上次的Tag删除重新打Tag" => -> {
              tag_action_parms = []
              tag_action_parms << "--retag"
              selected_action = :recreate_tag
              selected_description = "将上次的Tag删除重新打Tag"
              :recreate_tag
            },
            "不需要Tag继续编译且上传,手动修改上传备注" => -> {
              puts ""
              selected_action = :continue_without_tag
              selected_description = "不需要Tag继续编译且上传"
              :continue_without_tag
            },
            "终止退出编译" => -> {
              selected_action = :exit
              selected_description = "终止退出编译"
              raise Informative, "终止退出编译!"
              :exit
            }
          }

          result = cli.choose do |menu|
            menu.header = "当前代码并没有打Tag,上传的Changelog需要Tag"
            menu.prompt = "请选中打Tag的方式, 请输入选项(1/2/3...):"
            menu_options.each do |option, action|
              menu.choice(option) { action.call }
            end
          end

          is_need_add_tag = !tag_action_parms.nil?

          # 保存用户选择
          build_helper = BuildHelper.share_instance
          # 只有选择"新增版本号,打新Tag"时才保存到持久化缓存
          # 其他选项只保存到临时缓存(内存中)
          if selected_action == :new_tag
            # 保存到持久化缓存(文件)
            context.set_selection(PindoContext::SelectionKey::TAG_DECISION, {
              action: selected_action,
              description: selected_description,
              tag_action_parms: tag_action_parms,
              is_need_add_tag: is_need_add_tag
            })
            # 清除临时缓存
            build_helper.temp_tag_decision = nil
          else
            # 清空持久化缓存
            context.set_selection(PindoContext::SelectionKey::TAG_DECISION, nil)
            # 保存到临时缓存(仅在内存中,不会写入文件)
            build_helper.temp_tag_decision = {
              action: selected_action,
              description: selected_description,
              tag_action_parms: tag_action_parms,
              is_need_add_tag: is_need_add_tag
            }
          end
        end
      end
    end
  end

  return [is_need_add_tag, tag_action_parms]
end

#delete_libtarget_firebase_shell(project_path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pindo/module/build/buildhelper.rb', line 23

def delete_libtarget_firebase_shell(project_path)
  if File.directory?(File.join(project_path, 'Unity')) && File.exist?(File.join(project_path, 'Unity', 'Unity-iPhone.xcodeproj'))
    unity_project_path = File.join(project_path, 'Unity', 'Unity-iPhone.xcodeproj')
    xcdoe_unitylib_project = Xcodeproj::Project::open(unity_project_path)
    xcdoe_unitylib_project.targets.each do |target|
      if target.name == 'Unity-iPhone'
        target.shell_script_build_phases.each do |phase|
          if phase.name.eql?("Crashlytics Run Script")
            target.remove_build_phase(phase)
          end
        end
      end
    end
    xcdoe_unitylib_project.save()
  end
end

#get_project_name(project_path) ⇒ Object



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
# File 'lib/pindo/module/build/buildhelper.rb', line 339

def get_project_name(project_path)
  case project_type(project_path)
  when :unity
    File.basename(project_path)
  when :ios
    xcodeproj = Dir.glob(File.join(project_path, "*.xcodeproj")).first
    File.basename(xcodeproj, ".xcodeproj") if xcodeproj
  when :android
    settings_gradle = File.join(project_path, "settings.gradle")
    settings_gradle_kts = File.join(project_path, "settings.gradle.kts")
    
    # 优先使用 settings.gradle.kts,如果不存在则使用 settings.gradle
    if File.exist?(settings_gradle_kts)
      settings_gradle = settings_gradle_kts
    end
    
    if File.exist?(settings_gradle)
      content = File.read(settings_gradle)
      if content =~ /rootProject\.name\s*=\s*['"](.+)['"]/
        $1
      else
        File.basename(project_path)
      end
    else
      File.basename(project_path)
    end
  else
    File.basename(project_path)
  end
end

#get_project_version(project_path) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/pindo/module/build/buildhelper.rb', line 370

def get_project_version(project_path)
  case project_type(project_path)
  when :unity
    version_file = File.join(project_path, "ProjectSettings", "ProjectVersion.txt")
    if File.exist?(version_file)
      content = File.read(version_file)
      if content =~ /m_EditorVersion: (.*)/
        $1.strip
      end
    end
  when :ios
    # 从Info.plist获取版本号
    nil
  when :android
    # 从build.gradle获取版本号
    nil
  end
end

#install_gitcliffObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pindo/module/build/buildhelper.rb', line 117

def install_gitcliff()
  puts "\n检查git-cliff命令是否安装"
  # 请检查git-cliff命令是否安装
  begin
    if !system('which git-cliff > /dev/null 2>&1')
      # /bin/bash -c "$(curl -fsSL https://gitee.com/goodtools/env/raw/master/gitcliff_install.sh)"
      #安装git-cliff
      system('bash -c "$(curl -fsSL https://gitee.com/goodtools/env/raw/master/gitcliff_install.sh)"')
    end
  rescue
    raise Informative, "安装git-cliff出现错误"
  end
end

#ios_project?(project_path) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/pindo/module/build/buildhelper.rb', line 254

def ios_project?(project_path)
  # 检查iOS工程的关键文件
  xcodeproj_files = Dir.glob(File.join(project_path, "*.xcodeproj"))
  workspace_files = Dir.glob(File.join(project_path, "*.xcworkspace"))

  # 至少要有.xcodeproj文件或.xcworkspace文件
  return false if xcodeproj_files.empty? && workspace_files.empty?

  if !xcodeproj_files.empty?
    # 检查.xcodeproj内部结构
    project_file = File.join(xcodeproj_files.first, "project.pbxproj")
    return true if File.exist?(project_file)
  end

  if !workspace_files.empty?
    # 检查.xcworkspace内部结构
    contents_file = File.join(workspace_files.first, "contents.xcworkspacedata")
    return true if File.exist?(contents_file)
  end

  false
end

#project_type(project_path) ⇒ Object

Raises:

  • (ArgumentError)


316
317
318
319
320
321
322
323
324
# File 'lib/pindo/module/build/buildhelper.rb', line 316

def project_type(project_path)
  raise ArgumentError, "项目路径不能为空" if project_path.nil? || project_path.empty?
  raise ArgumentError, "项目路径不存在: #{project_path}" unless File.directory?(project_path)

  return :unity if unity_project?(project_path)
  return :ios if ios_project?(project_path)
  return :android if android_project?(project_path)
  :unknown
end

#project_type_name(project_path) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/pindo/module/build/buildhelper.rb', line 326

def project_type_name(project_path)
  case project_type(project_path)
  when :unity
    "Unity"
  when :ios
    "iOS"
  when :android
    "Android"
  else
    "Unknown"
  end
end

#unity_project?(project_path) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/pindo/module/build/buildhelper.rb', line 241

def unity_project?(project_path)
  # 检查Unity工程的关键文件和目录
  project_settings_path = File.join(project_path, "ProjectSettings")
  assets_path = File.join(project_path, "Assets")
  packages_path = File.join(project_path, "Packages")

  # Unity工程必须包含这些目录和文件
  File.directory?(project_settings_path) &&
  File.directory?(assets_path) &&
  File.directory?(packages_path) &&
  File.exist?(File.join(project_settings_path, "ProjectSettings.asset"))
end

#write_gitignore(git_root_dir) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pindo/module/build/buildhelper.rb', line 58

def write_gitignore(git_root_dir)
  gitignore_path = File.join(git_root_dir, '.gitignore')
  gitignore_content = <<~GITIGNORE
    # pindo_common_ignore_1.0.0
    build_ios.log
    feishu.json
    CHANGELOG.md

    # Platform specific directories
    /GoodPlatform/iOS/config.json
    /GoodPlatform/iOS/*
    /GoodPlatform/iOS/build/*
    /GoodPlatform/Android/*
    /GoodPlatform/BaseiOS/Unity/*
    /GoodPlatform/BaseiOS/Pods/
    /GoodPlatform/BaseiOS/build/
    /GoodPlatform/BaseiOS/config.json
    /GoodPlatform/BaseiOS/Podfile.lock

    /GoodPlatform/BaseAndroid/Unity/*
  GITIGNORE
  # Append to existing .gitignore or create new one
  File.open(gitignore_path, 'a') do |f|
    f.puts("\n# Added by Pindo")
    f.puts(gitignore_content)
  end
end