Skip to content

Commit 86c729f

Browse files
committed
add a way to download a dependency
1 parent 0fbdf27 commit 86c729f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

polymod/Polymod.hx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,11 @@ enum abstract PolymodErrorCode(String) from String to String
11661166
*/
11671167
var DEPENDENCY_CHECK_SKIPPED:String = 'dependency_check_skipped';
11681168

1169+
/**
1170+
* Polymod attempted to download a mod, but it failed to do so.
1171+
*/
1172+
var DEPENDENCY_NOT_DOWNLOADABLE:String = 'dependency_not_downloadable';
1173+
11691174
/**
11701175
* The given mod's API version does not match the version rule passed to Polymod.init.
11711176
* - This generally indicates the mod is outdated and should be updated by the author.

polymod/util/DependencyUtil.hx

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package polymod.util;
22

3+
import haxe.Http;
4+
import haxe.io.Bytes;
5+
import haxe.zip.Reader;
36
import polymod.Polymod.ModDependencies;
47
import polymod.Polymod.ModMetadata;
8+
import sys.FileSystem;
9+
import sys.io.File;
510
import thx.semver.VersionRule;
611

712
/**
@@ -336,4 +341,86 @@ class DependencyUtil
336341

337342
return result;
338343
}
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+
}
339426
}

0 commit comments

Comments
 (0)