Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize git library installation #607

Open
wants to merge 13 commits into
base: development
Choose a base branch
from
Binary file modified run.n
Binary file not shown.
40 changes: 38 additions & 2 deletions src/haxelib/VersionData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ package haxelib;
else
throw 'Invalid VscID $s';
}

public function getName() {
return switch cast(this, VcsID) {
case Git: "Git";
case Hg: "Mercurial";
};
}
}

/** Class containing repoducible git or hg library data. **/
Expand All @@ -28,7 +35,7 @@ class VcsData {
var url:String;
/** Commit hash **/
@:optional
var ref:Null<String>;
var commit:Null<String>;
/** The git tag or mercurial revision **/
@:optional
var tag:Null<String>;
Expand All @@ -42,6 +49,35 @@ class VcsData {
**/
@:optional
var subDir:Null<String>;

public function isReproducible() {
return commit != null;
}

/**
Returns an object containing the filled-in VcsData fields,
without the empty ones.
**/
public function getCleaned() {
var data:{
url:String,
?commit:String,
?tag:String,
?branch:String,
?subDir:String
} = { url : url };

if (commit != null)
data.commit = commit;
if (tag != null)
data.tag = tag;
if (!(branch == null || branch == ""))
data.branch = branch;
if (!(subDir == null || haxe.io.Path.normalize(subDir) == ""))
data.subDir = subDir;

return data;
}
}

/** Data required to reproduce a library version **/
Expand Down Expand Up @@ -77,7 +113,7 @@ class VersionDataHelper {
type: type,
data: {
url: vcsRegex.matched(2),
ref: vcsRegex.matched(3),
commit: vcsRegex.matched(3),
branch: vcsRegex.matched(4),
subDir: null,
tag: null
Expand Down
17 changes: 17 additions & 0 deletions src/haxelib/api/FsUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,21 @@ class FsUtils {
seek(root);
return ret;
}

/**
Switches to directory found at `path`, executes `f()` in this directory,
before switching back to the previous directory.
**/
public static function runInDirectory<T>(path:String, f:() -> T):T {
final oldCwd = Sys.getCwd();
try {
Sys.setCwd(path);
final value = f();
Sys.setCwd(oldCwd);
return value;
} catch (e) {
Sys.setCwd(oldCwd);
throw e;
}
}
}
23 changes: 20 additions & 3 deletions src/haxelib/api/GlobalScope.hx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ class GlobalScope extends Scope {
}

public function setVcsVersion(library:ProjectName, vcsVersion:VcsID, ?data:VcsData):Void {
if (data == null) data = {url: "unknown"};
if (data != null) {
repository.setVcsData(library, vcsVersion, data);
}

if (data.subDir != null) {
if (!(data == null || data.subDir == "" || data.subDir == null)) {
final devDir = repository.getValidVersionPath(library, vcsVersion) + data.subDir;
repository.setDevPath(library, devDir);
} else {
Expand All @@ -87,7 +89,11 @@ class GlobalScope extends Scope {
if (devPath != null)
return devPath;

final current = repository.getCurrentVersion(library);
final current = try {
repository.getCurrentVersion(library);
} catch (e:Repository.CurrentVersionException) {
throw new ScopeException('Library `$library` has no version set in the global scope');
};
return repository.getValidVersionPath(library, current);
}

Expand Down Expand Up @@ -273,4 +279,15 @@ class GlobalScope extends Scope {
return {path: path, version: current};
}

public function resolve(library:ProjectName):VersionData {
final version = repository.getCurrentVersion(library);

return switch version {
case vcs if (VcsID.isValid(vcs)):
final vcsId = VcsID.ofString(vcs);
VcsInstall(vcsId, repository.getVcsData(library, vcsId));
case semVer:
Haxelib(SemVer.ofString(semVer));
};
}
}
Loading