pindo 5.10.6 → 5.10.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pindo/base/funlog.rb +12 -1
  3. data/lib/pindo/base/pindocontext.rb +3 -0
  4. data/lib/pindo/command/android/autobuild.rb +62 -9
  5. data/lib/pindo/command/android/build.rb +6 -6
  6. data/lib/pindo/command/android/debug.rb +9 -9
  7. data/lib/pindo/command/android.rb +1 -1
  8. data/lib/pindo/command/appstore.rb +1 -1
  9. data/lib/pindo/command/deploy/build.rb +2 -4
  10. data/lib/pindo/command/deploy/cert.rb +4 -5
  11. data/lib/pindo/command/deploy/configproj.rb +3 -3
  12. data/lib/pindo/command/deploy/confusecode.rb +1 -1
  13. data/lib/pindo/command/deploy/confuseproj.rb +1 -1
  14. data/lib/pindo/command/deploy/pem.rb +3 -4
  15. data/lib/pindo/command/dev/autobuild.rb +1 -1
  16. data/lib/pindo/command/dev/build.rb +1 -1
  17. data/lib/pindo/command/dev/feishu.rb +1 -1
  18. data/lib/pindo/command/gplay/pullconfig.rb +48 -0
  19. data/lib/pindo/command/gplay.rb +7 -6
  20. data/lib/pindo/command/ios/adhoc.rb +6 -5
  21. data/lib/pindo/command/ios/autobuild.rb +24 -24
  22. data/lib/pindo/command/ios/build.rb +7 -6
  23. data/lib/pindo/command/ios/debug.rb +1 -0
  24. data/lib/pindo/command/ios.rb +1 -1
  25. data/lib/pindo/command/ipa/import.rb +2 -3
  26. data/lib/pindo/command/ipa/output.rb +2 -3
  27. data/lib/pindo/command/jps/upload.rb +6 -5
  28. data/lib/pindo/command/unity/apk.rb +19 -2
  29. data/lib/pindo/command/unity/autobuild.rb +58 -63
  30. data/lib/pindo/command/unity/initpack.rb +1 -1
  31. data/lib/pindo/command/unity/ipa.rb +17 -14
  32. data/lib/pindo/command/unity/pack.rb +1 -1
  33. data/lib/pindo/command/unity/upload.rb +1 -1
  34. data/lib/pindo/command/unity/web.rb +2 -2
  35. data/lib/pindo/command/utils/icon.rb +1 -1
  36. data/lib/pindo/command/utils/renewcert.rb +1 -2
  37. data/lib/pindo/command/utils/renewproj.rb +9 -10
  38. data/lib/pindo/command/web/autobuild.rb +2 -2
  39. data/lib/pindo/command/web/run.rb +1 -1
  40. data/lib/pindo/command.rb +2 -1
  41. data/lib/pindo/module/android/android_build_config_helper.rb +267 -35
  42. data/lib/pindo/module/android/android_build_helper.rb +300 -0
  43. data/lib/pindo/module/android/android_project_helper.rb +279 -0
  44. data/lib/pindo/module/android/gp_compliance_helper.rb +33 -87
  45. data/lib/pindo/module/android/gradle_helper.rb +524 -255
  46. data/lib/pindo/module/android/java_env_helper.rb +633 -0
  47. data/lib/pindo/module/android/keystore_helper.rb +1118 -0
  48. data/lib/pindo/module/appselect.rb +109 -0
  49. data/lib/pindo/module/appstore/appstore_in_app_purchase.rb +1 -1
  50. data/lib/pindo/module/build/{buildhelper.rb → build_helper.rb} +1 -2
  51. data/lib/pindo/module/build/{commonconfuseproj.rb → confuse_xcodeproj.rb} +2 -2
  52. data/lib/pindo/module/cert/cert_helper.rb +245 -0
  53. data/lib/pindo/module/cert/keychain_helper.rb +152 -0
  54. data/lib/pindo/module/cert/pem_helper.rb +67 -0
  55. data/lib/pindo/module/cert/xcodecerthelper.rb +2 -2
  56. data/lib/pindo/module/pgyer/pgyerhelper.rb +21 -1
  57. data/lib/pindo/module/{build/unityhelper.rb → unity/unity_helper.rb} +0 -17
  58. data/lib/pindo/module/xcode/{xcodereshandler.rb → res/xcode_res_handler.rb} +1 -1
  59. data/lib/pindo/module/xcode/{xcodebuildconfig.rb → xcode_build_config.rb} +7 -6
  60. data/lib/pindo/module/xcode/{xcodebuildhelper.rb → xcode_build_helper.rb} +4 -3
  61. data/lib/pindo/module/xcode/{xcodehelper.rb → xcode_project_helper.rb} +4 -2
  62. data/lib/pindo/module/xcode/{xcodereshelper.rb → xcode_res_helper.rb} +12 -9
  63. data/lib/pindo/version.rb +185 -7
  64. data/lib/pindo.rb +14 -9
  65. metadata +24 -23
  66. data/lib/pindo/module/android/apk_helper.rb +0 -138
  67. data/lib/pindo/module/android/base_helper.rb +0 -964
  68. data/lib/pindo/module/android/build_helper.rb +0 -128
  69. data/lib/pindo/module/android/so_helper.rb +0 -18
  70. data/lib/pindo/module/cert/certhelper.rb +0 -246
  71. data/lib/pindo/module/cert/keychainhelper.rb +0 -150
  72. data/lib/pindo/module/cert/pemhelper.rb +0 -65
  73. /data/lib/pindo/module/android/{androidreshelper.rb → android_res_helper.rb} +0 -0
  74. /data/lib/pindo/module/appstore/{iap_tier.json → appstore_iap_tier.json} +0 -0
  75. /data/lib/pindo/module/build/{swarkhelper.rb → swark_helper.rb} +0 -0
  76. /data/lib/pindo/module/build/{versionhelper.rb → version_helper.rb} +0 -0
  77. /data/lib/pindo/module/cert/{provisioninghelper.rb → provisioning_helper.rb} +0 -0
  78. /data/lib/pindo/module/unity/{nugethelper.rb → nuget_helper.rb} +0 -0
  79. /data/lib/pindo/module/xcode/{xcoderesconstant.rb → res/xcode_res_constant.rb} +0 -0
@@ -0,0 +1,633 @@
1
+ require 'fileutils'
2
+ require 'tmpdir'
3
+ require_relative 'android_project_helper'
4
+ require_relative '../../base/funlog'
5
+
6
+ module Pindo
7
+ class JavaEnvHelper
8
+ class << self
9
+ # 从 gradle.properties 文件中读取并设置 Java Home 环境变量
10
+ # @param project_dir [String] Android 项目目录路径
11
+ # @param use_main [Boolean] Unity as lib 工程中,是否使用主工程的配置(默认false使用Unity配置)
12
+ # @return [String, nil] 返回设置的 Java Home 路径,如果没有找到则返回 nil
13
+ def setup_java_home_from_project(project_dir, use_main: false)
14
+
15
+ gradle_properties_path = File.join(project_dir, "gradle.properties")
16
+
17
+ if Pindo::AndroidProjectHelper.unity_android_project?(project_dir)
18
+ gradle_properties_path = File.join(project_dir, "gradle.properties")
19
+ # 2. 处理 Unity 作为 lib 的原生工程(Unity 放在 Unity 目录下)
20
+ elsif Pindo::AndroidProjectHelper.unity_as_lib_android_project?(project_dir)
21
+ if use_main
22
+ # 编译主工程 APK 时,使用主工程的 gradle.properties
23
+ gradle_properties_path = File.join(project_dir, "gradle.properties")
24
+ puts "使用主工程的Java配置: #{gradle_properties_path}"
25
+ else
26
+ # 编译 Unity SO 库时,使用 Unity 的 gradle.properties
27
+ gradle_properties_path = File.join(project_dir, "Unity", "gradle.properties")
28
+ puts "使用Unity模块的Java配置: #{gradle_properties_path}"
29
+ end
30
+ end
31
+
32
+ return nil unless File.exist?(gradle_properties_path)
33
+ content = File.read(gradle_properties_path)
34
+ java_home_match = content.match(/org\.gradle\.java\.home\s*=\s*(.+)/)
35
+
36
+ if java_home_match && !java_home_match[1].empty?
37
+ java_home_path = java_home_match[1].strip
38
+
39
+ # 处理路径:将相对路径转换为绝对路径
40
+ if java_home_path.start_with?('/')
41
+ # 已经是绝对路径
42
+ absolute_java_home = java_home_path
43
+ elsif java_home_path.start_with?('~')
44
+ # 处理 ~ 开头的路径
45
+ absolute_java_home = File.expand_path(java_home_path)
46
+ else
47
+ # 相对路径,相对于 gradle.properties 文件所在目录
48
+ gradle_dir = File.dirname(gradle_properties_path)
49
+ absolute_java_home = File.expand_path(File.join(gradle_dir, java_home_path))
50
+ end
51
+
52
+ # 验证路径是否存在
53
+ unless File.directory?(absolute_java_home)
54
+ return nil
55
+ end
56
+
57
+ # 验证是否包含有效的 Java 安装
58
+ java_executable = File.join(absolute_java_home, "bin", "java")
59
+ unless File.exist?(java_executable)
60
+ return nil
61
+ end
62
+
63
+ # 设置环境变量
64
+ ENV['JAVA_HOME'] = absolute_java_home
65
+ ENV['PATH'] = "#{absolute_java_home}/bin:#{ENV['PATH']}"
66
+
67
+ puts " ✓ Java Home 已设置: #{absolute_java_home}"
68
+ return absolute_java_home
69
+ end
70
+
71
+ # 如果 gradle.properties 中没有配置 Java Home,尝试自动查找
72
+ if use_main
73
+ puts " ⚠ gradle.properties 中未配置 org.gradle.java.home"
74
+ puts " ✓ 尝试查找系统 Java 17..."
75
+
76
+ java17_path = find_java_17
77
+ if java17_path
78
+ ENV['JAVA_HOME'] = java17_path
79
+ ENV['PATH'] = "#{java17_path}/bin:#{ENV['PATH']}"
80
+ puts " ✓ 找到 Java 17: #{java17_path}"
81
+ return java17_path
82
+ else
83
+ puts " ✗ 未找到 Java 17,请安装 Java 17 或在 gradle.properties 中配置 org.gradle.java.home"
84
+ end
85
+ end
86
+
87
+ nil
88
+ end
89
+
90
+ # 查找系统中的 Java 17
91
+ def find_java_17
92
+ # 1. 检查 Android Studio 内置的 JDK
93
+ android_studio_paths = [
94
+ "/Applications/Android Studio.app/Contents/jbr/Contents/Home",
95
+ "/Applications/Android Studio Preview.app/Contents/jbr/Contents/Home",
96
+ File.expand_path("~/Applications/Android Studio.app/Contents/jbr/Contents/Home")
97
+ ]
98
+
99
+ android_studio_paths.each do |path|
100
+ if File.directory?(path)
101
+ java_bin = File.join(path, "bin", "java")
102
+ if File.exist?(java_bin)
103
+ # 验证版本
104
+ version_output = `"#{java_bin}" -version 2>&1`
105
+ # Android Studio 的 JBR 通常是 Java 17+
106
+ if version_output =~ /version "(\d+)/
107
+ version = $1.to_i
108
+ if version >= 17
109
+ puts " ✓ 使用 Android Studio 内置 JDK: Java #{version}"
110
+ return path
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ # 2. 尝试使用 /usr/libexec/java_home(macOS)
118
+ if File.exist?('/usr/libexec/java_home')
119
+ java17_home = `/usr/libexec/java_home -v 17 2>/dev/null`.strip
120
+ if !java17_home.empty? && File.directory?(java17_home)
121
+ return java17_home
122
+ end
123
+ end
124
+
125
+ # 3. 检查常见的 Java 17 安装路径
126
+ java17_paths = [
127
+ # Homebrew 安装路径
128
+ "/opt/homebrew/opt/openjdk@17",
129
+ "/usr/local/opt/openjdk@17",
130
+ # 系统安装路径
131
+ "/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home",
132
+ "/Library/Java/JavaVirtualMachines/openjdk-17.jdk/Contents/Home",
133
+ "/Library/Java/JavaVirtualMachines/adoptopenjdk-17.jdk/Contents/Home",
134
+ "/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home",
135
+ ]
136
+
137
+ java17_paths.each do |path|
138
+ if File.directory?(path)
139
+ java_bin = File.join(path, "bin", "java")
140
+ if File.exist?(java_bin)
141
+ # 验证版本
142
+ version_output = `"#{java_bin}" -version 2>&1`
143
+ if version_output =~ /version "17/ || version_output =~ /version "(\d+)/ && $1.to_i == 17
144
+ return path
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ nil
151
+ end
152
+
153
+ # 查找 Java 命令
154
+ def find_java_command
155
+ # 检查 JAVA_HOME 环境变量
156
+ if ENV['JAVA_HOME'] && File.exist?(File.join(ENV['JAVA_HOME'], 'bin', 'java'))
157
+ java_path = File.join(ENV['JAVA_HOME'], 'bin', 'java')
158
+ # 验证 Java 版本
159
+ if verify_java_version(java_path)
160
+ return java_path
161
+ end
162
+ end
163
+
164
+ # 尝试常见的 Java 路径
165
+ common_java_paths = [
166
+ '/usr/libexec/java_home', # macOS
167
+ '/usr/bin/java',
168
+ '/usr/local/bin/java',
169
+ '/opt/homebrew/bin/java', # macOS Homebrew
170
+ '/usr/local/opt/openjdk/bin/java' # macOS Homebrew OpenJDK
171
+ ]
172
+
173
+ common_java_paths.each do |java_path|
174
+ if File.exist?(java_path)
175
+ # 对于 macOS 的 java_home,需要特殊处理
176
+ if java_path.include?('java_home')
177
+ java_home_output = `#{java_path} 2>/dev/null`.strip
178
+ if !java_home_output.empty?
179
+ actual_java_path = File.join(java_home_output, 'bin', 'java')
180
+ if File.exist?(actual_java_path) && verify_java_version(actual_java_path)
181
+ return actual_java_path
182
+ end
183
+ end
184
+ elsif verify_java_version(java_path)
185
+ return java_path
186
+ end
187
+ end
188
+ end
189
+
190
+ # 如果都找不到,返回默认的 java 命令
191
+ 'java'
192
+ end
193
+
194
+ # 验证 Java 版本是否满足要求 (Java 11+)
195
+ def verify_java_version(java_path)
196
+ begin
197
+ version_output = `"#{java_path}" -version 2>&1`
198
+ if version_output =~ /version "(\d+)/
199
+ major_version = $1.to_i
200
+ return major_version >= 11
201
+ end
202
+ rescue
203
+ # 如果执行失败,返回 false
204
+ end
205
+ false
206
+ end
207
+
208
+ # 检查并确保 Java 版本符合要求 (Java 11+)
209
+ def ensure_java_version_compliance
210
+ # 仅检测Unity Editor JDK版本,不进行替换
211
+ unity_root_paths = find_unity_editor_paths
212
+ if unity_root_paths.any?
213
+ unity_root_paths.each do |unity_root|
214
+ # 构建JDK路径
215
+ jdk_path = build_jdk_path_from_unity_root(unity_root)
216
+
217
+ if File.exist?(jdk_path)
218
+ java_cmd = File.join(jdk_path, "bin", "java")
219
+ if File.exist?(java_cmd)
220
+ version_output = `"#{java_cmd}" -version 2>&1`
221
+ if version_output =~ /version "(\d+)/
222
+ major_version = $1.to_i
223
+ unless major_version >= 11
224
+ Funlog.warning("Unity Editor JDK 版本 #{major_version} 不符合要求 (需要 Java 11+)")
225
+ end
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
231
+
232
+ # 查找当前可用的 Java 命令
233
+ current_java = find_java_command
234
+
235
+ # 验证当前 Java 版本
236
+ if verify_java_version(current_java)
237
+ version_output = `"#{current_java}" -version 2>&1`
238
+ if version_output =~ /version "(\d+)/
239
+ major_version = $1.to_i
240
+ return true
241
+ end
242
+ end
243
+
244
+ Funlog.error("当前 Java 版本不符合要求,需要 Java 11+")
245
+ Funlog.info("请手动安装 Java 11: brew install openjdk@11")
246
+
247
+ return false
248
+ end
249
+
250
+ # 查找 Unity Editor 安装路径
251
+ def find_unity_editor_paths
252
+ unity_paths = []
253
+
254
+ # 1. 检查全局 Unity PlaybackEngines 路径(最常见)
255
+ global_playback_engines = "/Applications/Unity/PlaybackEngines"
256
+ if File.directory?(global_playback_engines)
257
+ unity_paths << "/Applications/Unity"
258
+ end
259
+
260
+ # 2. 检查 Unity Hub 中的多个版本(支持 2021.3.* 版本范围)
261
+ unity_hub_patterns = [
262
+ "/Applications/Unity/Hub/Editor/2021.3.*/PlaybackEngines"
263
+ ]
264
+
265
+ unity_hub_patterns.each do |pattern|
266
+ Dir.glob(pattern).each do |playback_engines_path|
267
+ # Unity Hub 版本:/Applications/Unity/Hub/Editor/2022.3.15f1/PlaybackEngines
268
+ unity_root = playback_engines_path.gsub("/PlaybackEngines", "")
269
+ unity_paths << unity_root
270
+ end
271
+ end
272
+
273
+ # 3. 检查其他常见的 Unity 安装路径
274
+ other_pattern = "/Applications/Unity/*/PlaybackEngines"
275
+ Dir.glob(other_pattern).each do |playback_engines_path|
276
+ # 其他 Unity 版本:/Applications/Unity/2022.3.15f1/PlaybackEngines
277
+ unity_root = playback_engines_path.gsub("/PlaybackEngines", "")
278
+ unity_paths << unity_root
279
+ end
280
+
281
+ unity_paths.uniq
282
+ end
283
+
284
+ # 根据 Unity 根路径构建 JDK 路径
285
+ def build_jdk_path_from_unity_root(unity_path)
286
+ # 1. 全局 Unity 路径
287
+ if unity_path == "/Applications/Unity"
288
+ return "/Applications/Unity/PlaybackEngines/AndroidPlayer/OpenJDK"
289
+ end
290
+
291
+ # 2. Unity Hub 路径(包含版本号)
292
+ if unity_path.include?("/Unity/Hub/Editor/")
293
+ # 检查是否有 PlaybackEngines 目录
294
+ playback_engines_path = File.join(unity_path, "PlaybackEngines")
295
+ if File.directory?(playback_engines_path)
296
+ return File.join(playback_engines_path, "AndroidPlayer", "OpenJDK")
297
+ end
298
+ end
299
+
300
+ # 3. 其他 Unity 版本路径
301
+ if unity_path.include?("/Unity/") && !unity_path.include?("/Hub/")
302
+ # 检查是否有 PlaybackEngines 目录
303
+ playback_engines_path = File.join(unity_path, "PlaybackEngines")
304
+ if File.directory?(playback_engines_path)
305
+ return File.join(playback_engines_path, "AndroidPlayer", "OpenJDK")
306
+ end
307
+ end
308
+
309
+ # 4. 默认尝试
310
+ File.join(unity_path, "PlaybackEngines", "AndroidPlayer", "OpenJDK")
311
+ end
312
+
313
+ # 自动安装 Java 11
314
+ def install_java_11
315
+ begin
316
+ # 检查是否在 macOS 上
317
+ if RbConfig::CONFIG['host_os'] =~ /darwin/
318
+ return install_java_11_macos
319
+ else
320
+ Funlog.warning("当前系统不是 macOS,请手动安装 Java 11")
321
+ return false
322
+ end
323
+ rescue => e
324
+ Funlog.error("安装 Java 11 时出错: #{e.message}")
325
+ return false
326
+ end
327
+ end
328
+
329
+ # 在 macOS 上安装 Java 11
330
+ def install_java_11_macos
331
+ Funlog.fancyinfo_start("在 macOS 上安装 Java 11")
332
+
333
+ # 方法1: 使用 Homebrew 安装
334
+ if system("which brew > /dev/null 2>&1")
335
+ if system("brew install openjdk@11")
336
+ # 设置 JAVA_HOME
337
+ java_home_path = `brew --prefix openjdk@11`.strip
338
+ if !java_home_path.empty? && File.directory?(java_home_path)
339
+ java_bin_path = File.join(java_home_path, "bin", "java")
340
+ if File.exist?(java_bin_path)
341
+ # 设置环境变量
342
+ ENV['JAVA_HOME'] = java_home_path
343
+ Funlog.fancyinfo_success("Java 11 安装成功")
344
+ return true
345
+ end
346
+ end
347
+ end
348
+
349
+ # 方法2: 使用 SDKMAN 安装
350
+ if system("which sdk > /dev/null 2>&1")
351
+ if system("sdk install java 11.0.21-tem")
352
+ # 设置环境变量
353
+ sdkman_home = ENV['SDKMAN_DIR'] || File.expand_path("~/.sdkman")
354
+ java_home_path = File.join(sdkman_home, "candidates", "java", "11.0.21-tem")
355
+ if File.directory?(java_home_path)
356
+ ENV['JAVA_HOME'] = java_home_path
357
+ Funlog.fancyinfo_success("Java 11 安装成功")
358
+ return true
359
+ end
360
+ end
361
+ end
362
+
363
+ # 方法3: 下载并安装 Adoptium OpenJDK
364
+ Funlog.fancyinfo_update("下载 Adoptium OpenJDK 11...")
365
+ return install_adoptium_java_11
366
+ end
367
+
368
+ # 下载并安装 Adoptium OpenJDK 11
369
+ def install_adoptium_java_11
370
+ begin
371
+ # 创建临时目录
372
+ temp_dir = Dir.mktmpdir("java_install_")
373
+
374
+ # 下载 URL (macOS x64)
375
+ download_url = "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.21%2B9/OpenJDK11U-jdk_x64_mac_hotspot_11.0.21_9.tar.gz"
376
+ tar_file = File.join(temp_dir, "openjdk11.tar.gz")
377
+
378
+ if system("curl -L -o \"#{tar_file}\" \"#{download_url}\"")
379
+ if system("tar -xzf \"#{tar_file}\" -C \"#{temp_dir}\"")
380
+ # 查找解压后的目录
381
+ jdk_dir = Dir.glob(File.join(temp_dir, "jdk-11*")).first
382
+ if jdk_dir && File.directory?(jdk_dir)
383
+ # 尝试安装到用户目录而不是系统目录
384
+ user_install_dir = File.expand_path("~/.pindo/java11")
385
+ if system("mkdir -p \"#{File.dirname(user_install_dir)}\" && mv \"#{jdk_dir}\" \"#{user_install_dir}\"")
386
+ # 设置环境变量
387
+ ENV['JAVA_HOME'] = user_install_dir
388
+ return true
389
+ else
390
+ # 回退到系统目录
391
+ install_dir = "/usr/local/openjdk11"
392
+ if system("sudo mkdir -p /usr/local && sudo mv \"#{jdk_dir}\" \"#{install_dir}\"")
393
+ # 设置环境变量
394
+ ENV['JAVA_HOME'] = install_dir
395
+ return true
396
+ end
397
+ end
398
+ end
399
+ end
400
+ end
401
+
402
+ return false
403
+ rescue => e
404
+ return false
405
+ ensure
406
+ # 清理临时目录
407
+ FileUtils.rm_rf(temp_dir) if temp_dir && File.directory?(temp_dir)
408
+ end
409
+ end
410
+
411
+ # 检查并更新 Unity Editor 中的 JDK 版本
412
+ def check_and_update_unity_jdk
413
+ return false unless RbConfig::CONFIG['host_os'] =~ /darwin/
414
+
415
+ # 查找 Unity Editor 安装路径
416
+ unity_paths = find_unity_editor_paths
417
+ if unity_paths.empty?
418
+ return false
419
+ end
420
+
421
+ updated_any = false
422
+ unity_paths.each do |unity_path|
423
+ # 根据不同的 Unity 路径结构构建 JDK 路径
424
+ jdk_path = build_jdk_path_from_unity_root(unity_path)
425
+
426
+ if File.directory?(jdk_path)
427
+ if check_and_replace_unity_jdk(jdk_path)
428
+ updated_any = true
429
+ end
430
+ else
431
+ # OpenJDK 目录不存在,尝试创建并安装 Java 11
432
+ if create_and_install_openjdk(jdk_path)
433
+ updated_any = true
434
+ end
435
+ end
436
+ end
437
+
438
+ updated_any
439
+ end
440
+
441
+ # 创建 OpenJDK 目录并安装 Java 11
442
+ def create_and_install_openjdk(jdk_path)
443
+ begin
444
+ # 创建 OpenJDK 目录(使用 sudo 权限)
445
+ unless system("sudo mkdir -p \"#{jdk_path}\"")
446
+ return false
447
+ end
448
+
449
+ # 查找或安装 Java 11
450
+ java11_path = find_java11_installation
451
+ unless java11_path && File.directory?(java11_path)
452
+ # 自动下载并安装 Java 11
453
+ if install_java_11
454
+ java11_path = find_java11_installation
455
+ unless java11_path && File.directory?(java11_path)
456
+ return false
457
+ end
458
+ else
459
+ return false
460
+ end
461
+ end
462
+
463
+ # 复制 Java 11 到 OpenJDK 目录(使用 sudo 权限)
464
+ if system("sudo cp -R \"#{java11_path}\"/* \"#{jdk_path}\"/")
465
+
466
+ # 验证安装
467
+ java_bin = File.join(jdk_path, "bin", "java")
468
+ if File.exist?(java_bin)
469
+ version_output = `"#{java_bin}" -version 2>&1`
470
+ if version_output =~ /version "(\d+)/
471
+ version = $1.to_i
472
+ return version >= 11
473
+ end
474
+ end
475
+
476
+ return true
477
+ else
478
+ return false
479
+ end
480
+
481
+ rescue => e
482
+ return false
483
+ end
484
+ end
485
+
486
+ # 检查并替换 Unity Editor 中的 JDK
487
+ def check_and_replace_unity_jdk(jdk_path)
488
+ begin
489
+ # 检查当前 JDK 版本
490
+ java_bin = File.join(jdk_path, "bin", "java")
491
+ if File.exist?(java_bin)
492
+ version_output = `"#{java_bin}" -version 2>&1`
493
+ if version_output =~ /version "(\d+)/
494
+ current_version = $1.to_i
495
+ puts "Unity Editor JDK 当前版本: #{current_version}"
496
+
497
+ if current_version >= 11
498
+ puts "\e[32m✓ Unity Editor JDK 版本 #{current_version} 符合要求\e[0m"
499
+ return false
500
+ else
501
+ puts "\e[31m✗ Unity Editor JDK 版本 #{current_version} 不符合要求,需要 Java 11+\e[0m"
502
+ return replace_unity_jdk_with_java11(jdk_path)
503
+ end
504
+ end
505
+ else
506
+ puts "\e[31m✗ Unity Editor JDK 中未找到 java 可执行文件\e[0m"
507
+ return replace_unity_jdk_with_java11(jdk_path)
508
+ end
509
+
510
+ return false
511
+ rescue => e
512
+ puts "\e[31m检查 Unity Editor JDK 时出错: #{e.message}\e[0m"
513
+ return false
514
+ end
515
+ end
516
+
517
+ # 替换 Unity Editor 中的 JDK 为 Java 11
518
+ def replace_unity_jdk_with_java11(jdk_path)
519
+ begin
520
+ puts "\e[33m正在替换 Unity Editor JDK 为 Java 11...\e[0m"
521
+
522
+ # 查找系统中的 Java 11
523
+ java11_path = find_java11_installation
524
+ unless java11_path && File.directory?(java11_path)
525
+ puts "\e[33m未找到系统中的 Java 11 安装,正在自动下载...\e[0m"
526
+
527
+ # 自动下载并安装 Java 11
528
+ if install_java_11
529
+ java11_path = find_java11_installation
530
+ unless java11_path && File.directory?(java11_path)
531
+ puts "\e[31m✗ Java 11 自动安装失败\e[0m"
532
+ return false
533
+ end
534
+ puts "\e[32m✓ Java 11 自动安装成功\e[0m"
535
+ else
536
+ puts "\e[31m✗ Java 11 自动安装失败\e[0m"
537
+ return false
538
+ end
539
+ end
540
+
541
+ puts "找到 Java 11 安装路径: #{java11_path}"
542
+
543
+ # 备份原始 JDK(使用 sudo 权限)
544
+ backup_path = "#{jdk_path}_backup_#{Time.now.strftime('%Y%m%d_%H%M%S')}"
545
+ puts "备份原始 JDK 到: #{backup_path}"
546
+ puts "\e[33m需要管理员权限来备份和替换 Unity Editor JDK,请输入密码...\e[0m"
547
+
548
+ if system("sudo cp -R \"#{jdk_path}\" \"#{backup_path}\"")
549
+ puts "\e[32m✓ 原始 JDK 备份成功\e[0m"
550
+ else
551
+ puts "\e[31m✗ 原始 JDK 备份失败\e[0m"
552
+ return false
553
+ end
554
+
555
+ # 删除原始 JDK 内容(使用 sudo 权限)
556
+ puts "删除原始 JDK 内容..."
557
+ if system("sudo rm -rf \"#{jdk_path}/*\"")
558
+ puts "\e[32m✓ 原始 JDK 内容删除成功\e[0m"
559
+ else
560
+ puts "\e[31m✗ 原始 JDK 内容删除失败\e[0m"
561
+ return false
562
+ end
563
+
564
+ # 复制 Java 11 到 Unity JDK 目录(使用 sudo 权限)
565
+ puts "复制 Java 11 到 Unity JDK 目录..."
566
+ if system("sudo cp -R \"#{java11_path}\"/* \"#{jdk_path}/\"")
567
+ puts "\e[32m✓ Java 11 复制成功\e[0m"
568
+
569
+ # 验证替换结果
570
+ new_java_bin = File.join(jdk_path, "bin", "java")
571
+ if File.exist?(new_java_bin)
572
+ version_output = `"#{new_java_bin}" -version 2>&1`
573
+ if version_output =~ /version "(\d+)/
574
+ new_version = $1.to_i
575
+ if new_version >= 11
576
+ puts "\e[32m✓ Unity Editor JDK 已成功替换为 Java #{new_version}\e[0m"
577
+ return true
578
+ else
579
+ puts "\e[31m✗ 替换后的 JDK 版本仍然不符合要求: #{new_version}\e[0m"
580
+ end
581
+ end
582
+ end
583
+ else
584
+ puts "\e[31m✗ Java 11 复制失败\e[0m"
585
+ return false
586
+ end
587
+
588
+ return false
589
+ rescue => e
590
+ puts "\e[31m替换 Unity Editor JDK 时出错: #{e.message}\e[0m"
591
+ return false
592
+ end
593
+ end
594
+
595
+ # 查找系统中的 Java 11 安装
596
+ def find_java11_installation
597
+ # 检查常见的 Java 11 安装路径
598
+ java11_paths = [
599
+ # Homebrew 安装路径
600
+ "/opt/homebrew/opt/openjdk@11",
601
+ "/usr/local/opt/openjdk@11",
602
+ # SDKMAN 安装路径
603
+ File.expand_path("~/.sdkman/candidates/java/11.0.21-tem"),
604
+ File.expand_path("~/.sdkman/candidates/java/11.0.20-tem"),
605
+ # 系统安装路径
606
+ "/Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home",
607
+ "/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home",
608
+ # Pindo 安装的 Java 11
609
+ File.expand_path("~/.pindo/java11")
610
+ ]
611
+
612
+ java11_paths.each do |path|
613
+ if File.directory?(path)
614
+ java_bin = File.join(path, "bin", "java")
615
+ if File.exist?(java_bin)
616
+ # 验证版本
617
+ version_output = `"#{java_bin}" -version 2>&1`
618
+ if version_output =~ /version "(\d+)/
619
+ version = $1.to_i
620
+ if version >= 11
621
+ return path
622
+ end
623
+ end
624
+ end
625
+ end
626
+ end
627
+
628
+ nil
629
+ end
630
+ end
631
+ end
632
+ end
633
+ end