Class: EasyCI::Command::Unitydraw

Inherits:
EasyCI::Command show all
Defined in:
lib/easyci/command/unitydraw.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Unitydraw

Returns a new instance of Unitydraw.



16
17
18
19
20
# File 'lib/easyci/command/unitydraw.rb', line 16

def initialize(argv)
  super
  @runner_repo = argv.option('runner-repo')
  @use_https = argv.flag?('https', false)
end

Class Method Details

.optionsObject



9
10
11
12
13
14
# File 'lib/easyci/command/unitydraw.rb', line 9

def self.options
  [
    ['--runner-repo=REPO', 'Runner仓库路径,例如:mygroup/FGUIWorkRunner'],
    ['--https', '使用HTTPS方式克隆仓库,默认使用SSH方式'],
  ].concat(super)
end

Instance Method Details

#runObject



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
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
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
124
125
126
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
# File 'lib/easyci/command/unitydraw.rb', line 26

def run
  puts "开始Unity项目绘制流程..." 
  
  gitee_draw_dir = File.expand_path('~/Documents/gitee_draw')
  FileUtils.mkdir_p(gitee_draw_dir)
  
  gitee_repo = ENV['GITEE_REPO'] || 'fundesignexport/MagicSortDemoDesignExport'
  gitee_repo_name = gitee_repo.split('/').last
  gitee_branch = ENV['GITEE_BRANCH'] || 'master'
  if gitee_repo.empty? || gitee_branch.empty?
    puts "GITEE_REPO 和 GITEE_BRANCH 不能为空"
    exit 1
  end
  
  ssh_host_gitee = 'gitee.com'
  if !gitee_repo.empty? && gitee_repo.start_with?('fundesignexport')
    ssh_host_gitee = 'gitee-fununitydraw'
  elsif !gitee_repo.empty? && gitee_repo.start_with?('gooddesignexport')
    ssh_host_gitee = 'gitee-joyunitydraw'
  else
    ssh_host_gitee = 'gitee.com'
  end

  work_dir = File.join(gitee_draw_dir, gitee_repo_name)
  if File.exist?(work_dir)
    FileUtils.rm_rf(work_dir)
  end
  FileUtils.mkdir_p(work_dir)
  
  # 如果未提供runner-repo参数,使用默认值
  @runner_repo ||= ENV['RUNNER_REPO'] || 'bestwebprimary/FGUIWorkRunner'
  runner_branch = ENV['RUNNER_BRANCH'] ||'master'
  if @runner_repo.empty? || gitee_branch.empty?
    puts "RUNNER_REPO 和 RUNNER_BRANCH 不能为空"
    exit 1
  end
  runner_repo_name = @runner_repo.split('/').last
  

  puts "@runner_repo: #{@runner_repo}"
  puts "runner_repo_name: #{runner_repo_name}"
  puts "runner_branch: #{@runner_branch}"
  puts "gitee_repo: #{gitee_repo}"
  puts "gitee_repo_name: #{gitee_repo_name}"
  puts "gitee_branch: #{gitee_branch}"
  puts "work_dir: #{work_dir}"

  # 清理工作目录
  
  runner_dir = File.join(work_dir, runner_repo_name)
  FileUtils.rm_rf(runner_dir) if File.exist?(runner_dir)
  git_url = @use_https ? "https://gitee.com/#{@runner_repo}.git" : "git@#{ssh_host_gitee}:#{@runner_repo}.git"
  puts "git_url: #{git_url}"
  clone_runner_result = system("cd #{work_dir} && git clone --depth 1 -b #{runner_branch} #{git_url} #{runner_repo_name}")
  unless clone_runner_result
    puts "克隆Runner仓库失败,请检查网络和权限设置"
    exit 1
  end
  unless File.exist?(runner_dir)
    puts "Runner目录不存在,克隆可能失败"
    exit 1
  end
  
  FileUtils.chdir(runner_dir)
  resources_dir = File.join(runner_dir, 'Assets/Resources')
  if File.exist?(resources_dir)
    FileUtils.rm_rf(resources_dir)
  end
  FileUtils.mkdir_p(File.join(runner_dir, 'Assets'))
  puts "下载截图仓库..." 
  git_url = @use_https ? "https://gitee.com/#{gitee_repo}.git" : "git@#{ssh_host_gitee}:#{gitee_repo}.git"
  puts "git_url: #{git_url}"
  gitee_repo_local_path = File.join(work_dir, gitee_repo_name)
  FileUtils.rm_rf(gitee_repo_local_path) if File.exist?(gitee_repo_local_path)
  clone_result = system("cd #{work_dir} && git clone --depth 1 -b #{gitee_branch} #{git_url} #{gitee_repo_name}")

  unless clone_result
    puts "克隆Assets仓库失败,请检查网络和权限设置"
    exit 1
  end
  
  if !File.exist?(File.join(gitee_repo_local_path, 'Resources'))
    FileUtils.mkdir_p(resources_dir)
    FileUtils.cp_r(File.join(gitee_repo_local_path, '2D', '.'), resources_dir)
    FileUtils.cp_r(File.join(gitee_repo_local_path, 'Common/Font'), File.join(resources_dir, 'Fonts'))
     # 安全复制 Language 文件夹(如果存在)
    languages_path = File.join(gitee_repo_local_path, 'Common/Language')
    if Dir.exist?(languages_path)
      FileUtils.cp_r(languages_path, resources_dir)
    else
      puts "⚠️ 跳过复制:#{languages_path} 不存在"
    end
  else
    FileUtils.mv(File.join(gitee_repo_local_path, 'Resources'), resources_dir)
  end

  # 如果 resources_dir 的FGUI目录下有Code文件夹,则删除
  fgui_dir = File.join(resources_dir, 'FGUI')
  if File.exist?(File.join(fgui_dir, 'Code'))
    FileUtils.rm_rf(File.join(fgui_dir, 'Code'))
  end

  # 运行Unity截图任务
  unity_path = find_unity_path(runner_dir)
  puts "运行Unity截图任务..." 
  FileUtils.chdir(runner_dir)
  system("mkdir -p ~/UnityTemp && TMPDIR=~/UnityTemp #{unity_path} -projectPath  #{runner_dir} -executeMethod SpineAutoImporter.CIRunner -logFile screenshot-log.txt")
  
  # 压缩截图
  puts "开始压缩截图..." 
  system("cd #{runner_dir} && find . -path \"*/Screenshots/*.png\" -exec pngquant --quality=70-95 --ext .png --force {} \\;")
  # 运行Node脚本(如果需要)
  puts "运行后处理脚本..."
  # 你想传入的变量前缀,比如 GITEE_ 
  env_prefixes = ["GITEE_"]

  # 构建 docker run 的 -e 参数
  env_flags = ENV
    .select { |k, _| env_prefixes.any? { |prefix| k.start_with?(prefix) } }
    .map { |k, _| "-e #{k}" }
    .join(" ")

  # 可选:你也可以在这里补充额外变量
  extra_env = {
    "BUILD_FROM_PRO" => gitee_repo_name,
    "BUILD_FROM_REPO" => gitee_repo
  }
  extra_env.each { |k, v| ENV[k] = v }

  # 构建并执行命令
  cmd = <<~CMD
    cd #{runner_dir} && \
    docker run --rm #{env_flags} \
      -v "$(pwd):/app" \
      -w /app \
      node:22.0.0 bash -c "cd Scripts && yarn install && npx ts-node index.ts"
  CMD

  system(cmd)
  puts "执行命令:\n#{cmd}"
  # export  gitee_repo 和 gitee_repo_name
  # system("cd #{runner_dir} && docker run --rm -v \"$(pwd):/app\" -w /app node:22.0.0 bash -c \"cd Scripts && export BUILD_FROM_PRO=#{gitee_repo_name} && export BUILD_FROM_REPO=#{gitee_repo} && yarn install && npx ts-node index.ts\"")
  
end

#validate!Object



22
23
24
# File 'lib/easyci/command/unitydraw.rb', line 22

def validate!
  super
end