Class: Pindo::BuildHelper

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

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

#find_android_subproject, #get_build_tools, #get_ext_values, #get_keystore_config, #get_main_module, #modify_il2cpp_config, #remove_desktop_google_service, #unity_android_project?

Class Method Details

.share_instanceObject



13
14
15
# File 'lib/pindo/module/build/buildhelper.rb', line 13

def share_instance
  instance
end

Instance Method Details

#add_git_cliffconfig(project_path) ⇒ Object



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

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)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/pindo/module/build/buildhelper.rb', line 208

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

  main_module = get_main_module(project_path)

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

  false
end

#check_check_and_install_cliff(project_path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pindo/module/build/buildhelper.rb', line 35

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) ⇒ Boolean

Returns:

  • (Boolean)


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

def check_is_need_add_tag?(project_path)
  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)
      cli = HighLine.new
      menu_options = {
        "新增版本号,打新Tag" => -> {
          tag_action_parms = []
          :new_tag
        },
        "将上次的Tag删除重新打Tag" => -> {
          tag_action_parms = []
          tag_action_parms << "--retag"
          :recreate_tag
        },
        "不需要Tag继续编译且上传,手动修改上传备注" => -> {
          puts ""
          :continue_without_tag
        },
        "终止退出编译" => -> {
          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?
    end
  end

  return [is_need_add_tag, tag_action_parms]
end

#delete_libtarget_firebase_shell(project_path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pindo/module/build/buildhelper.rb', line 18

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



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/pindo/module/build/buildhelper.rb', line 248

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")
    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



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/pindo/module/build/buildhelper.rb', line 272

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



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pindo/module/build/buildhelper.rb', line 112

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)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/pindo/module/build/buildhelper.rb', line 185

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)


225
226
227
228
229
230
231
232
233
# File 'lib/pindo/module/build/buildhelper.rb', line 225

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



235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/pindo/module/build/buildhelper.rb', line 235

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)


172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pindo/module/build/buildhelper.rb', line 172

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pindo/module/build/buildhelper.rb', line 53

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