-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRakefile
More file actions
333 lines (271 loc) · 11.6 KB
/
Rakefile
File metadata and controls
333 lines (271 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# frozen_string_literal: true
require 'rake/clean'
require 'shellwords'
require 'tmpdir'
require 'yaml'
require 'mustache'
require 'pathname'
require 'unitypackage'
CLEAN.include '**/.DS_Store'
SPECIAL_BUILD_TYPES = %w[alpha beta rc].freeze
def parse_upm_teak_sdk_version
version = `git describe --tags`.strip
version_parts = version.split('.')
if version_parts.length > 3
version_suffix = version_parts[-1]
SPECIAL_BUILD_TYPES.each do |build_type|
match_data = version_suffix.match(/#{build_type}(?<version>[0-9]+)/)
if match_data
return "0.#{version_parts[0]}#{version_parts[1]}#{version_parts[2]}.#{match_data[:version]}"
end
end
return version
end
version
end
TEAK_SDK_VERSION = `git describe --tags`.strip
NATIVE_CONFIG = YAML.load_file('native.config.yml')
PROJECT_PATH = Rake.application.original_dir
BUILD_TYPE = ENV.fetch('BUILD_TYPE', 'Release')
UPM_BUILD_TEMP = ENV.fetch('UPM_BUILD_TEMP', 'temp-upm-build')
UPM_PACKAGE_REPO = ENV.fetch('UPM_PACKAGE_REPO', '../upm-package-teak')
TEMPLATE_PARAMETERS = {
teak_sdk_version: TEAK_SDK_VERSION
}
#
# Helper methods
#
def ci?
ENV.fetch('CI', false).to_s == 'true'
end
def build_local?
ENV.fetch('BUILD_LOCAL', false).to_s == 'true'
end
require 'securerandom'
def generate_meta_files(dir)
Dir.glob(File.join(dir, '**', '*')).each do |entry|
meta_path = "#{entry}.meta"
next if entry.end_with?('.meta') || File.exist?(meta_path)
guid = SecureRandom.hex(16)
folder_line = File.directory?(entry) ? "folderAsset: yes\n" : ''
File.write(meta_path, <<~META)
fileFormatVersion: 2
guid: #{guid}
#{folder_line}DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
META
end
end
def fetch_xcframework(name, dest)
FileUtils.rm_rf(dest)
if build_local?
FileUtils.cp_r("#{PROJECT_PATH}/../teak-ios/TeakFramework/#{name}", dest)
else
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
sh "curl --fail -o #{name}.zip https://sdks.teakcdn.com/ios/#{name.sub('.xcframework', '')}-#{NATIVE_CONFIG['version']['ios']}.xcframework.zip"
sh "unzip #{name}.zip"
FileUtils.cp_r(name, dest)
end
end
end
generate_meta_files(dest)
end
task default: ['build:android', 'build:ios', 'build:package']
def unity_path
home = ENV.fetch('UNITY_HOME') { Dir.glob('/Applications/Unity/Hub/Editor/*').last }
fail 'Unity not found. Set UNITY_HOME or install Unity via Unity Hub.' if home.nil?
File.join(home, 'Unity.app', 'Contents', 'MacOS', 'Unity')
end
task :test do
log_file = File.join(PROJECT_PATH, 'unity.test.log')
FileUtils.rm_f(log_file)
success = system(unity_path, '-batchmode', '-nographics', '-quit',
'-logFile', log_file,
'-projectPath', PROJECT_PATH,
'-executeMethod', 'TeakTestRunner.RunAll')
# Surface test output from the log
if File.exist?(log_file)
File.readlines(log_file).each do |line|
puts line if line.include?('[TeakTest]')
end
end
fail 'Tests failed' unless success
end
task :format do
sh 'astyle --project --recursive Assets/*.cs --exclude=Assets/Teak/Editor/iOS/Xcode'
end
task :version, [:v] do |_, args|
Rake::Task['version:ios'].invoke(args.v)
Rake::Task['version:android'].invoke(args.v)
Rake::Task['version:unity'].invoke(args.v)
end
namespace :version do
require 'aws-sdk-s3'
task :ios, [:v] do |_, args|
s3 = Aws::S3::Resource.new(
region: 'us-east-1'
)
bucket = s3.bucket('teak-build-artifacts')
fail "Teak iOS version #{args.v} does not exist" unless bucket.object("ios/Teak-#{args.v}.xcframework.zip").exists?
NATIVE_CONFIG['version']['ios'] = args.v
File.write('native.config.yml', NATIVE_CONFIG.to_yaml)
end
task :android, [:v] do |_, args|
s3 = Aws::S3::Resource.new(
region: 'us-east-1'
)
bucket = s3.bucket('teak-build-artifacts')
fail "Teak Android version #{args.v} does not exist" unless bucket.object("android/teak-#{args.v}.aar").exists?
NATIVE_CONFIG['version']['android'] = args.v
File.write('native.config.yml', NATIVE_CONFIG.to_yaml)
end
task :unity, [:v] do |_, args|
File.write('VERSION', "#{args.v}\n")
end
end
namespace :build do
task :android do
# Write Unity SDK version information
plugins_android = File.join(PROJECT_PATH, 'Assets', 'Teak', 'Plugins', 'Android')
template = File.read(File.join(PROJECT_PATH, 'Templates', 'Version.java.template'))
File.write(File.join(plugins_android, 'Version.java'), Mustache.render(template, TEMPLATE_PARAMETERS))
Dir.chdir(plugins_android) do
# Download or copy Teak SDK AAR
if build_local?
cp "#{PROJECT_PATH}/../teak-android/build/outputs/aar/teak-#{BUILD_TYPE.downcase}.aar", 'teak.aar'
else
sh "curl --fail -o teak.aar https://sdks.teakcdn.com/android/teak-#{NATIVE_CONFIG['version']['android']}.aar"
end
end
end
task :ios do
ios_plugin_dir = File.join(PROJECT_PATH, 'Assets', 'Teak', 'Plugins', 'iOS')
fetch_xcframework('Teak.xcframework', File.join(ios_plugin_dir, 'Teak.xcframework'))
fetch_xcframework('TeakExtension.xcframework', File.join(ios_plugin_dir, 'TeakExtension.xcframework'))
# Download or copy Teak SDK Resources bundle
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
if build_local?
cp "#{PROJECT_PATH}/../teak-ios/build/#{BUILD_TYPE}-iphoneos/TeakResources.bundle.zip", 'TeakResources.bundle.zip'
else
sh "curl --fail -o TeakResources.bundle.zip https://sdks.teakcdn.com/ios/TeakResources-#{NATIVE_CONFIG['version']['ios']}.bundle.zip"
end
sh "unzip -o TeakResources.bundle.zip -d #{File.join(PROJECT_PATH, 'Assets', 'Teak', 'Plugins', 'iOS')}"
sh "rm -rf #{File.join(PROJECT_PATH, 'Assets', 'Teak', 'Plugins', 'iOS', 'TeakResources.bundle', '_CodeSignature')}"
end
end
# Download the latest Info.plist for TeakNotificationContent
content_plist = File.join('Assets', 'Teak', 'Editor', 'iOS', 'TeakNotificationContent', 'Info.plist')
if build_local?
cp "#{PROJECT_PATH}/../teak-ios/TeakExtensions/TeakNotificationContent/Info.plist", content_plist
else
sh "curl --fail -o #{content_plist} https://sdks.teakcdn.com/ios/Info-#{NATIVE_CONFIG['version']['ios']}.plist"
end
# Write Unity SDK version information to 'Assets/Teak/Plugins/iOS/teak_version.m'
template = File.read(File.join(PROJECT_PATH, 'Templates', 'teak_version.m.template'))
File.write(File.join(PROJECT_PATH, 'Assets', 'Teak', 'Plugins', 'iOS', 'teak_version.m'), Mustache.render(template, TEMPLATE_PARAMETERS))
end
task :package do
project_path = File.expand_path('./')
package_path = File.expand_path('./Teak.unitypackage')
FileUtils.rm_f(package_path)
# TeakVersion.cs
template = File.read(File.join(PROJECT_PATH, 'Templates', 'TeakVersion.cs.template'))
File.write(File.join(PROJECT_PATH, 'Assets', 'Teak', 'TeakVersion.cs'), Mustache.render(template, TEMPLATE_PARAMETERS))
package = UnityPackage::UnityPackage.new
package << Dir['Assets/Teak/**/*']
File.open(package_path, 'wb') do |file|
package.write file
end
begin
sh 'python3 extractunitypackage.py Teak.unitypackage _temp_pkg/', verbose: false
FileUtils.rm_rf('_temp_pkg')
rescue StandardError
raise 'Unity build failed'
end
end
end
namespace :upm do
task :build do
FileUtils.rm_rf(UPM_BUILD_TEMP)
FileUtils.mkdir_p(UPM_BUILD_TEMP)
`git clone git@github.com:GoCarrot/upm-package-teak.git #{UPM_BUILD_TEMP}` if build_local?
editor_glob = Dir.glob('Assets/Teak/Editor/**/*')
runtime_exclude = Dir.glob('Assets/Teak/LICENSE*') + Dir.glob('Assets/Teak/Editor*') + editor_glob
runtime_glob = Dir.glob('Assets/Teak/**/*') - runtime_exclude
def copy_glob_to(glob, dest, hax_path)
glob.each do |filename|
abs_path = Pathname.new(File.expand_path(filename))
package_root = Pathname.new(File.expand_path(hax_path))
rel_path = abs_path.relative_path_from(package_root)
dir = File.join(dest, File.dirname(rel_path))
FileUtils.mkdir_p(dir)
FileUtils.cp(filename, dir) if File.file?(filename)
puts "#{filename} => #{dir}"
end
end
copy_glob_to(editor_glob, File.join(UPM_BUILD_TEMP, 'Editor'), 'Assets/Teak/Editor')
copy_glob_to(runtime_glob, File.join(UPM_BUILD_TEMP, 'Runtime'), 'Assets/Teak')
# package.json
template = File.read(File.join(PROJECT_PATH, 'Templates', 'package.json.template'))
File.write(File.join(UPM_BUILD_TEMP, 'package.json'), Mustache.render(template, TEMPLATE_PARAMETERS.merge(teak_sdk_version: parse_upm_teak_sdk_version)))
end
task :deploy_versioned do
# Ensure repo exists
unless Dir.exist? UPM_PACKAGE_REPO
`git clone git@github.com:GoCarrot/upm-package-teak.git #{UPM_PACKAGE_REPO}`
end
# Construct our version.
version_parts = TEAK_SDK_VERSION.split('-')
version = version_parts[0]
version_suffix = version_parts[1..-1].join('-')
major, minor, patch = version.split('.').map(&:to_i)
cd UPM_PACKAGE_REPO do
sh "git config user.email \"team@teak.io\""
sh "git config user.name \"Teak CI\""
sh "git checkout build" # Start on the 'build' branch
sh "git checkout #{major}.#{minor} || " + # If the current minor version branch exists, check it out
"(git checkout #{major}.#{minor - 1} && git checkout -b #{major}.#{minor}) || " + # Check out the previous minor revision and then create a new minor version branch off that
"(git checkout #{major} && git checkout -b #{major}.#{minor}) || " + # If there is no previous minor version branch, check out the major version and create one
"(git checkout #{major - 1} ; (git checkout -b #{major} && git checkout -b #{major}.#{minor}))" # New major version based on previous major version, or 'build'
sh "rm -fr *" # Delete all files
sh "git ls-tree --name-only -r build | xargs git checkout --" # Restore files which exist in the 'build' branch
end
sh "cp -RT #{UPM_BUILD_TEMP} #{UPM_PACKAGE_REPO}" # Copy in all the files
cd UPM_PACKAGE_REPO do
sh "git add -A" # Add all files present
sh "git commit -am \"#{TEAK_SDK_VERSION}\"" # Commit with message set to full version
sh "git tag #{TEAK_SDK_VERSION}" # Create a tag of the full version
# Push versioned
sh "git push origin #{major}.#{minor}"
sh "git push --tags"
# puts "git switch #{major} # Checkout branch '#{major}' or create it if it doesn't exist"
# puts "git reset --hard #{TEAK_SDK_VERSION} # Reset the HEAD of the '#{major}' branch to the tag we just created"
# puts "git push --all origin # Push all branches and commits"
# puts "git push --tags # Push tags"
end
end
task :deploy_latest do
# Ensure repo exists
unless Dir.exist? UPM_PACKAGE_REPO
`git clone git@github.com:GoCarrot/upm-package-teak.git #{UPM_PACKAGE_REPO}`
end
version_parts = TEAK_SDK_VERSION.split('-')
version = version_parts[0]
version_suffix = version_parts[1..-1].join('-')
major, minor, patch = version.split('.').map(&:to_i)
cd UPM_PACKAGE_REPO do
sh "git checkout #{major}.#{minor}"
sh "git reset --hard #{TEAK_SDK_VERSION}" # Move the major.minor branch HEAD to the latest tag
sh "git checkout #{major} || git checkout -b #{major}"
sh "git reset --hard #{TEAK_SDK_VERSION}" # Move the major branch HEAD to the latest tag
sh "git checkout main"
sh "git reset --hard #{TEAK_SDK_VERSION}" # Move the main branch HEAD to the latest tag
sh "git push --all"
end
end
end