|
1 | 1 | package polymod.util;
|
2 | 2 |
|
| 3 | +import haxe.Http; |
| 4 | +import haxe.io.Bytes; |
| 5 | +import haxe.zip.Reader; |
3 | 6 | import polymod.Polymod.ModDependencies;
|
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 | /**
|
@@ -336,4 +341,86 @@ class DependencyUtil
|
336 | 341 |
|
337 | 342 | return result;
|
338 | 343 | }
|
| 344 | + |
| 345 | + /** |
| 346 | + * Given a github url and commit hash, download the dependency. |
| 347 | + * This is done using an http request. |
| 348 | + * @param outputDir the directory to download the dependency to |
| 349 | + * @param url github repository url: https://github.com/OWNER/REPOSITORY |
| 350 | + * @param commit commit hash |
| 351 | + */ |
| 352 | + public static function downloadDependency(outputDir:String, url:String, commit:String):Void |
| 353 | + { |
| 354 | + var zipBytes:Null<Bytes> = getDependencyAsZip(url, commit); |
| 355 | + |
| 356 | + if (zipBytes == null || zipBytes.length == 0) |
| 357 | + return; |
| 358 | + |
| 359 | + var reader = new Reader(new haxe.io.BytesInput(zipBytes)); |
| 360 | + var entries = reader.read(); |
| 361 | + |
| 362 | + if (!FileSystem.exists(outputDir)) |
| 363 | + { |
| 364 | + FileSystem.createDirectory(outputDir); |
| 365 | + } |
| 366 | + |
| 367 | + for (entry in entries) |
| 368 | + { |
| 369 | + var fileName = entry.fileName; |
| 370 | + if (entry.fileSize > 0) |
| 371 | + { |
| 372 | + var content = entry.data; |
| 373 | + var filePath = outputDir + "/" + fileName; |
| 374 | + |
| 375 | + var dirs = fileName.split("/"); |
| 376 | + dirs.pop(); |
| 377 | + var currentDir = outputDir; |
| 378 | + for (dir in dirs) |
| 379 | + { |
| 380 | + currentDir += "/" + dir; |
| 381 | + if (!FileSystem.exists(currentDir)) |
| 382 | + { |
| 383 | + FileSystem.createDirectory(currentDir); |
| 384 | + } |
| 385 | + } |
| 386 | + |
| 387 | + File.saveBytes(filePath, content); |
| 388 | + trace("Extracted: " + filePath); |
| 389 | + } |
| 390 | + } |
| 391 | + |
| 392 | + trace("Extraction complete. Files saved in: " + outputDir); |
| 393 | + } |
| 394 | + |
| 395 | + /** |
| 396 | + * Get the bytes of a dependency as a zip file. |
| 397 | + * @param url github repository url: https://github.com/OWNER/REPOSITORY |
| 398 | + * @param commit commit hash |
| 399 | + * @return Null<Bytes> |
| 400 | + */ |
| 401 | + public static function getDependencyAsZip(url:String, commit:String):Null<Bytes> |
| 402 | + { |
| 403 | + // url structure of the code to download |
| 404 | + // https://codeload.github.com/OWNER/REPOSITORY/zip/COMMIT |
| 405 | + |
| 406 | + var urlSplit = url.split('/'); |
| 407 | + var repository = urlSplit[urlSplit.length - 1]; |
| 408 | + var owner = urlSplit[urlSplit.length - 2]; |
| 409 | + |
| 410 | + var downloadUrl = 'https://codeload.github.com/${owner}/${repository}/zip/${commit}'; |
| 411 | + |
| 412 | + var zipBytes:Null<Bytes> = null; |
| 413 | + |
| 414 | + var http = new Http(downloadUrl); |
| 415 | + http.onBytes = function(bytes:Bytes) |
| 416 | + { |
| 417 | + zipBytes = bytes; |
| 418 | + } |
| 419 | + http.request(false); |
| 420 | + |
| 421 | + if (zipBytes == null || zipBytes.length == 0) |
| 422 | + Polymod.error(DEPENDENCY_NOT_DOWNLOADABLE, 'Failed to download dependency: ${downloadUrl}'); |
| 423 | + |
| 424 | + return zipBytes; |
| 425 | + } |
339 | 426 | }
|
0 commit comments