forked from googlesamples/unity-jar-resolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
437 lines (397 loc) · 15.1 KB
/
build.gradle
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
334
335
336
337
338
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Gradle file to build the Jar Resolver Unity plugin.
*/
buildscript {
repositories {
jcenter()
mavenLocal()
}
}
/*
* Project level variables
*/
project.ext {
sdk_root = System.getProperty("ANDROID_HOME")
if (sdk_root == null || sdk_root.isEmpty()) {
sdk_root = System.getenv("ANDROID_HOME")
}
// Determine the current OS.
os_name = System.getProperty("os.name").toLowerCase();
os_osx = os_name.contains("mac os x");
os_windows = os_name.contains("windows");
os_linux = os_name.contains("linux");
// Search for the Unity editor executable.
// The Unity editor is required to package the plug-in.
unity_exe = System.getProperty("UNITY_EXE")
if (unity_exe == null || unity_exe.isEmpty()) {
unity_exe = System.getenv("UNITY_EXE")
}
if (unity_exe == null || unity_exe.isEmpty()) {
// TODO: Should probably also search the path using Exec / which / where.
if (os_osx) {
unity_exe ='/Applications/Unity/Unity.app/Contents/MacOS/Unity'
} else if (os_windows) {
unity_exe = 'C:\\Program Files\\Unity\\Editor\\Unity.exe'
} else if (os_linux) {
unity_exe = '/opt/Unity/Editor/Unity'
}
}
unity_exe_found = (new File(unity_exe)).exists();
if (!unity_exe_found) {
logger.warn('Unity editor executable not found, plug-in packaging ' +
'may fail.')
}
// find the unity root directory by working up from the executable.
// not, perfect, but pretty close, work up the tree until the
// name starts with unity.
unity_root = (new File(unity_exe)).getParentFile().getParentFile();
while (!unity_root.name.toLowerCase().startsWith("unity")) {
if (unity_root.getParentFile() != null) {
unity_root = unity_root.getParentFile();
} else {
break;
}
}
logger.info( "Unity root is $unity_root")
// find unity engine dll
unity_dll_path = findUnityPath("UNITY_DLL_PATH", true,
fileTree(dir: unity_root).matching {
include '**/Managed/UnityEngine.dll'});
if (unity_dll_path == null || !unity_dll_path.exists()) {
logger.warn('Unity Editor and Runtime DLLs not found, compilation may fail!')
} else {
logger.info("Unity Engine DLL path is $unity_dll_path")
}
// ios runtime dll. This is with the playback engine, so the
// structure is different for MacOS and the others.
unity_ios_dll_path = findUnityPath("UNITY_IOS_PLAYBACK_PATH", true,
os_osx ?
fileTree(dir: unity_root.parentFile).matching {
include '**/PlaybackEngines/iOSSupport/UnityEditor.iOS.Extensions.dll'
} :
fileTree(dir: unity_root).matching {
include '**/PlaybackEngines/iOSSupport/UnityEditor.iOS.Extensions.dll'
})
if (unity_ios_dll_path == null || !unity_ios_dll_path.exists()) {
logger.warn('Unity iOS Playback engine not found, compilation may fail!')
} else {
logger.info("Unity iOS Playback engine is $unity_ios_dll_path")
}
// find the NUnit framework dll.
unity_nunit_dll_path = findUnityPath("UNITY_NUNIT_PATH", true,
fileTree(dir: unity_root).matching {
include '**/nunit.framework.dll'
})
if (unity_nunit_dll_path == null || !unity_nunit_dll_path.exists()) {
logger.warn('Unity NUnit framework not found, compilation may fail!')
} else {
logger.info("Unity NUnity framework found in $unity_nunit_dll_path")
}
// xbuild is used to build the dlls.
if (os_windows) {
xbuild_exe = findUnityPath("XBUILD_EXE", false,
fileTree(dir: unity_root).matching { include '**/Mono/bin/xbuild.bat'})
} else if (os_osx || os_linux) {
xbuild_exe = findUnityPath("XBUILD_EXE", false,
fileTree(dir: unity_root).matching { include '**/xbuild'})
}
if (xbuild_exe == null || !xbuild_exe.exists()) {
logger.warn("xbuild command not found, compilation may fail.")
xbuild_exe = null
} else {
logger.info("xbuild found at $xbuild_exe")
}
// nunit-console is used to run tests.
if (os_windows) {
nunit_console_exe = findUnityPath("NUNIT_CONSOLE_EXE", false,
fileTree(dir: unity_root).matching { include '**/Mono/bin/nunut-console2.bat'})
} else if (os_osx) {
nunit_console_exe = findUnityPath("NUNIT_CONSOLE_EXE", false,
fileTree(dir: unity_root).matching { include '**/nunit-console2'})
} else if (os_linux) {
nunit_console_exe = findUnityPath("NUNIT_CONSOLE_EXE", false,
fileTree(dir: unity_root).matching { include '**/nunit-console'})
}
if (nunit_console_exe == null || !nunit_console_exe.exists()) {
logger.warn("nunit_console command not found, compilation may fail.")
nunit_console_exe = null
} else {
logger.info("nunit_console found at $nunit_console_exe")
}
pluginSrc = file('plugin').absolutePath
pluginProj = file('build/PluginSrc').absolutePath
buildPath = file('build').absolutePath
exportPath = file('build/plugin.unitypackage').absolutePath
dllDir = 'Assets/PlayServicesResolver/Editor'
pluginVersion = '1.2.6.0'
currentPluginPath = file('.').absolutePath
currentPluginBasename = 'play-services-resolver'
currentPluginName = (currentPluginBasename + '-' + pluginVersion +
'.unitypackage')
}
task compile_resolverTests(type: Exec) {
description 'Compile the tests for the Mono framework component.'
workingDir 'source'
commandLine "${xbuild_exe}", '/target:JarResolverTests',
"/property:NUnityHintPath=$unity_nunit_dll_path.absolutePath"
ext.remoteTaskPhase = 'build'
}
task test_resolverLib(type: Exec, dependsOn: compile_resolverTests) {
description 'Runs the tests.'
workingDir 'source'
commandLine "$nunit_console_exe",
"JarResolverTests/bin/Debug/JarResolverTests.dll"
ext.remoteTaskPhase = 'build'
}
/// find paths within the Unity file tree used for building.
def findUnityPath(propertyKey, wantDirectory, fileTree) {
def propValue;
def fileValue;
propValue = System.getProperty(propertyKey)
if (propValue == null || propValue.isEmpty()) {
propValue = System.getenv(propertyKey)
}
// convert string to file object
if (propValue != null) {
fileValue = file(propValue)
} else {
fileValue = null
}
if (fileValue == null || !fileValue.exists()) {
// take the shortest path location.
fileValue = null
fileTree.files.each { p ->
if (fileValue == null ||
fileValue.absolutePath.length() > p.absolutePath.length()) {
fileValue = p;
}
}
}
if (wantDirectory) {
return fileValue != null ? fileValue.parentFile : null;
} else {
return fileValue
}
}
// Construct the name of a versioned asset from the source filename and version
// string.
// The encoded string takes the form...
// ${filename}_v${version}_.${extension}
// where extension is derived from the specified filename.
def versionedAssetName(filename, version) {
def extensionIndex = filename.lastIndexOf('.')
def basename = filename.substring(0, extensionIndex)
def extension = filename.substring(extensionIndex)
// Encode the DLL version and target names into the DLL in the form...
// ${dllname}_t${hypen_separated_target_names}_v${version}.dll
def targetName = basename
if (version != null && !version.isEmpty()) {
targetName += '_v' + version
}
return targetName + extension
}
// Generate the tasks to compile a plugin DLL and copy it to the Unity
// plugin packaging folder.
def build_pluginDll(projectName, assemblyDllBasename) {
def version = "${pluginVersion}"
def projectPath = 'source/' + projectName + '/' + projectName + '.csproj'
def assemblyDll = versionedAssetName(assemblyDllBasename, version)
def compileTask = tasks.create(name: "compile_" + projectName,
type: Exec,
description: 'Compile ' + projectName
)
compileTask.workingDir('source')
compileTask.commandLine(["${xbuild_exe}", "/target:" + projectName,
"/property:UnityHintPath=$unity_dll_path.absolutePath",
"/property:UnityIosPath=$unity_ios_dll_path.absolutePath"
])
compileTask.ext.remoteTaskPhase = "build"
def assemblyDllSource = file("source/" + projectName + "/bin/Debug/" +
assemblyDllBasename)
def dllMetaBasename = assemblyDllBasename + ".meta"
def dllMeta = "${pluginSrc}/${dllDir}/" + dllMetaBasename
def dllMetaVersioned = assemblyDll + ".meta"
def copyTask = tasks.create(name: "copy_" + projectName,
type: Copy,
description: ('Copy ' + projectName +
' to unity packaging folder'),
dependsOn: compileTask)
copyTask.from(files(assemblyDllSource, dllMeta))
copyTask.into("${pluginProj}/${dllDir}")
copyTask.duplicatesStrategy('include')
copyTask.rename({
String fn ->
if (fn.endsWith(dllMetaBasename)) {
return fn.replace(dllMetaBasename, dllMetaVersioned)
} else if (fn.endsWith(assemblyDllBasename)) {
return fn.replace(assemblyDllBasename, assemblyDll)
}
return fn
})
copyTask.ext.remoteTaskPhase = "build"
}
build_pluginDll("PlayServicesResolver", "Google.JarResolver.dll")
build_pluginDll("VersionHandler", "Google.VersionHandler.dll")
build_pluginDll("IOSResolver", "Google.IOSResolver.dll")
task update_metadataForVersion() {
description 'Update plugin metadata if the plugin version changed.'
if (!file(currentPluginName).exists()) {
for (fileobj in fileTree("${pluginSrc}")) {
def lines = fileobj.text.split('\n')
def outputLines = []
for (line in lines) {
if (line.contains('guid:')) {
line = (
'guid: ' + java.util.UUID.randomUUID().toString().replace('-', ''))
}
outputLines.add(line)
}
fileobj.write(outputLines.join('\n') + '\n')
}
}
}
task copy_pluginTemplate(type: Copy, dependsOn: update_metadataForVersion) {
description 'Copy the template project into the Unity plugin packaging dir.'
from "${pluginSrc}"
into "${pluginProj}"
exclude '**/*.dll.*', '**/*.txt.meta'
duplicatesStrategy 'include'
ext.remoteTaskPhase = 'build'
}
task inject_versionIntoMetaFiles(dependsOn: [copy_pluginTemplate,
'copy_PlayServicesResolver',
'copy_VersionHandler',
'copy_IOSResolver']) {
description 'Inject the version number into the plugin\'s meta files.'
ext.remoteTaskPhase = 'build'
doLast {
for (fileobj in fileTree("${pluginProj}")) {
if (fileobj.path.endsWith('.meta')) {
def lines = fileobj.text.split('\n')
def outputLines = []
for (line in lines) {
outputLines.add(line)
if (line.contains('labels:')) {
outputLines.add("- gvh_v${pluginVersion}")
}
}
fileobj.write(outputLines.join('\n') + '\n')
}
}
}
}
task copy_manifestMetadata(type: Copy) {
def manifestBasename = versionedAssetName(
currentPluginBasename + '.txt', null) + '.meta'
description 'Copy .meta file for the plugin manifest.'
from file("${pluginSrc}/${dllDir}/" + manifestBasename)
into file("${pluginProj}/${dllDir}/")
rename {
String fn ->
return fn.replace(manifestBasename,
versionedAssetName(currentPluginBasename + '.txt',
"${pluginVersion}") +
'.meta')
}
ext.remoteTaskPhase = 'prebuild'
}
task generate_manifest(dependsOn: ['copy_manifestMetadata',
'inject_versionIntoMetaFiles']) {
description 'Generate a manifest for the files in the plug-in.'
doLast {
def dir = file("${pluginProj}/Assets")
def list = []
dir.eachFileRecurse(groovy.io.FileType.FILES) { filename ->
def path = filename.path
if (!(path.toLowerCase().endsWith('.meta') ||
path.toLowerCase().endsWith('.txt'))) {
list << filename.path.replace("${pluginProj}/", '')
}
}
def manifest = file("${pluginProj}/${dllDir}/" +
versionedAssetName(currentPluginBasename + '.txt',
"${pluginVersion}"))
manifest.write(list.join('\n') + '\n')
}
ext.remoteTaskPhase = 'build'
}
task export_package(dependsOn: 'generate_manifest') {
description 'Creates and exports the Plugin unity package.'
doLast {
def argv = ["-g.building",
"-buildTarget",
"android",
"-batchmode",
"-projectPath",
"${pluginProj}",
"-logFile",
"build/unity.log",
"-exportPackage",
"Assets/PlayServicesResolver",
"${exportPath}",
"-quit"]
exec {
executable "${unity_exe}"
args argv
}
}
ext.remoteTaskPhase = 'build'
dependsOn test_resolverLib
}
task copy_plugin() {
description 'Copy plugin to the current-build directory'
doFirst {
// If the version number has been bumped delete the exploded directory.
if (!file(currentPluginName).exists()) {
delete file(currentPluginPath +"/exploded")
}
copy {
from file(exportPath)
into file(currentPluginPath)
rename ('plugin.unitypackage', currentPluginName)
}
}
doLast {
copy {
from file("${pluginProj}")
into file(currentPluginPath +"/exploded")
include "Assets/PlayServicesResolver/**/*"
}
}
ext.remoteTaskPhase = 'postbuild'
}
task build_unityPackage(dependsOn: 'copy_plugin') {
description "Top level task for building the unity package."
doLast { println "Packaging Complete!" }
ext.remoteTaskPhase = 'build'
dependsOn {
tasks.findAll { task -> task.name.startsWith('PackageSample') }
}
}
task clean() {
doFirst {
delete 'build'
file("source").listFiles().each { f ->
if (file("$f/obj").exists()) {
delete "$f/obj"
}
if (file("$f/bin").exists()) {
delete "$f/bin"
}
}
}
}
defaultTasks = ['prebuild', 'build', 'postbuild']
for (phase in defaultTasks) {
if (!tasks.findByName(phase)) {
def build_phase_name = 'Local ' + phase
tasks.create(name: phase,
action: { println(build_phase_name) },
description: build_phase_name,
dependsOn: project.tasks.findAll {
task -> task.ext.has('remoteTaskPhase') &&
task.ext.remoteTaskPhase == phase
})
}
}
project.defaultTasks = defaultTasks