1
1
package polymod .util ;
2
2
3
- import polymod .Polymod .ModDependencies ;
3
+ import haxe .Http ;
4
+ import haxe .io .Bytes ;
5
+ import haxe .zip .Reader ;
6
+ import polymod .Polymod .ModDependency ;
4
7
import polymod .Polymod .ModMetadata ;
8
+ import sys .FileSystem ;
9
+ import sys .io .File ;
5
10
import thx .semver .VersionRule ;
6
11
7
12
/**
@@ -53,7 +58,7 @@ class DependencyUtil
53
58
var result : Array <ModMetadata > = [];
54
59
55
60
// Compile a map of mod dependencies.
56
- var deps : ModDependencies = compileDependencies (modList );
61
+ var deps : Map < String , ModDependency > = compileDependencies (modList );
57
62
58
63
// Check that all mods are in the mod list.
59
64
var relevantMods : Array <ModMetadata > = [];
@@ -68,7 +73,7 @@ class DependencyUtil
68
73
// Check that all dependencies are satisfied.
69
74
for (dep in deps .keys ())
70
75
{
71
- var depRule : VersionRule = deps .get (dep );
76
+ var depRule : VersionRule = deps .get (dep ). version ;
72
77
73
78
// Check that the dependency is in the mod list.
74
79
var depMod : ModMetadata = null ;
@@ -112,7 +117,7 @@ class DependencyUtil
112
117
static function validateDependencies (modList : Array <ModMetadata >): Bool
113
118
{
114
119
// Compile a map of mod dependencies.
115
- var deps : ModDependencies = compileDependencies (modList );
120
+ var deps : Map < String , ModDependency > = compileDependencies (modList );
116
121
117
122
// Check that all mods are in the mod list.
118
123
var relevantMods : Array <ModMetadata > = [];
@@ -127,7 +132,7 @@ class DependencyUtil
127
132
// Check that all dependencies are satisfied.
128
133
for (dep in deps .keys ())
129
134
{
130
- var depRule : VersionRule = deps .get (dep );
135
+ var depRule : VersionRule = deps .get (dep ). version ;
131
136
132
137
// Check that the dependency is in the mod list.
133
138
var depMod : ModMetadata = null ;
@@ -307,33 +312,124 @@ class DependencyUtil
307
312
* For example, if one mod requires `>1.2.0` of `modA` and another requires `>1.3.0` of `modA`,
308
313
* the merged list will be `[modA: '>1.2.0 && >1.3.0']`.
309
314
*/
310
- public static function compileDependencies (modList : Array <ModMetadata >): Map <String , VersionRule >
315
+ public static function compileDependencies (modList : Array <ModMetadata >): Map <String , ModDependency >
311
316
{
312
- var result : Map <String , VersionRule > = [];
317
+ var result : Map <String , ModDependency > = [];
313
318
314
319
for (mod in modList )
315
320
{
316
321
if (result [mod .id ] == null )
317
- result [mod .id ] = VersionUtil .DEFAULT_VERSION_RULE ;
322
+ result [mod .id ] = {
323
+ version : VersionUtil .DEFAULT_VERSION_RULE
324
+ };
318
325
319
326
if (mod .dependencies != null )
320
327
{
321
328
for (dependencyId in mod .dependencies .keys ())
322
329
{
323
- var dependencyRule : VersionRule = mod .dependencies [dependencyId ];
330
+ if (result [dependencyId ] == null )
331
+ result [dependencyId ] = {
332
+ version : VersionUtil .DEFAULT_VERSION_RULE
333
+ };
324
334
325
- if (result [dependencyId ] != null )
335
+ result [dependencyId ].url = mod .dependencies [dependencyId ].url ;
336
+ result [dependencyId ].commit = mod .dependencies [dependencyId ].commit ;
337
+ var dependencyRule : VersionRule = mod .dependencies [dependencyId ].version ;
338
+
339
+ if (result [dependencyId ].version != null )
326
340
{
327
- result [dependencyId ] = VersionUtil .combineRulesAnd (result [dependencyId ], dependencyRule );
341
+ result [dependencyId ]. version = VersionUtil .combineRulesAnd (result [dependencyId ]. version , dependencyRule );
328
342
}
329
343
else
330
344
{
331
- result [dependencyId ] = dependencyRule ;
345
+ result [dependencyId ]. version = dependencyRule ;
332
346
}
333
347
}
334
348
}
335
349
}
336
350
337
351
return result ;
338
352
}
353
+
354
+ /**
355
+ * Given a github url and commit hash, download the dependency.
356
+ * This is done using an http request.
357
+ * @param outputDir the directory to download the dependency to
358
+ * @param url github repository url: https://github.com/OWNER/REPOSITORY
359
+ * @param commit commit hash
360
+ */
361
+ public static function downloadDependency (outputDir : String , url : String , commit : String ): Void
362
+ {
363
+ var zipBytes : Null <Bytes > = getDependencyAsZip (url , commit );
364
+
365
+ if (zipBytes == null || zipBytes .length == 0 )
366
+ return ;
367
+
368
+ var reader = new Reader (new haxe.io. BytesInput (zipBytes ));
369
+ var entries = reader .read ();
370
+
371
+ if (! FileSystem .exists (outputDir ))
372
+ {
373
+ FileSystem .createDirectory (outputDir );
374
+ }
375
+
376
+ for (entry in entries )
377
+ {
378
+ var fileName = entry .fileName ;
379
+ if (entry .fileSize > 0 )
380
+ {
381
+ var content = entry .data ;
382
+ var filePath = outputDir + " /" + fileName ;
383
+
384
+ var dirs = fileName .split (" /" );
385
+ dirs .pop ();
386
+ var currentDir = outputDir ;
387
+ for (dir in dirs )
388
+ {
389
+ currentDir + = " /" + dir ;
390
+ if (! FileSystem .exists (currentDir ))
391
+ {
392
+ FileSystem .createDirectory (currentDir );
393
+ }
394
+ }
395
+
396
+ File .saveBytes (filePath , content );
397
+ trace (" Extracted: " + filePath );
398
+ }
399
+ }
400
+
401
+ trace (" Extraction complete. Files saved in: " + outputDir );
402
+ }
403
+
404
+ /**
405
+ * Get the bytes of a dependency as a zip file.
406
+ * @param url github repository url: https://github.com/OWNER/REPOSITORY
407
+ * @param commit commit hash
408
+ * @return Null<Bytes>
409
+ */
410
+ public static function getDependencyAsZip (url : String , commit : String ): Null <Bytes >
411
+ {
412
+ // url structure of the code to download
413
+ // https://codeload.github.com/OWNER/REPOSITORY/zip/COMMIT
414
+
415
+ var urlSplit = url .split (' /' );
416
+ var repository = urlSplit [urlSplit .length - 1 ];
417
+ var owner = urlSplit [urlSplit .length - 2 ];
418
+
419
+ var downloadUrl = ' https://codeload.github.com/ ${owner }/ ${repository }/zip/ ${commit }' ;
420
+
421
+ var zipBytes : Null <Bytes > = null ;
422
+
423
+ var http = new Http (downloadUrl );
424
+ http .onBytes = function (bytes : Bytes )
425
+ {
426
+ zipBytes = bytes ;
427
+ }
428
+ http .request (false );
429
+
430
+ if (zipBytes == null || zipBytes .length == 0 )
431
+ Polymod .error (DEPENDENCY_NOT_DOWNLOADABLE , ' Failed to download dependency: ${downloadUrl }' );
432
+
433
+ return zipBytes ;
434
+ }
339
435
}
0 commit comments