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

[Draft] Update to PR 610 #612

Closed
wants to merge 10 commits into from
Binary file modified run.n
Binary file not shown.
18 changes: 18 additions & 0 deletions src/haxelib/VersionData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ class VcsData {
**/
@:optional
var subDir:Null<String>;

public function toString(): String {
var qualifier =
if (ref != null) ref
else if (tag != null) tag
else if (branch != null) branch
else null;
return if (qualifier != null)
'$url#$qualifier'
else
url;
}
}

/** Data required to reproduce a library version **/
Expand Down Expand Up @@ -84,4 +96,10 @@ class VersionDataHelper {
}
}
}

public static function toString(data: VersionData): String
return switch data {
case Haxelib(semver): semver;
case VcsInstall(vcsId, vcsData): '$vcsId:${vcsData.toString()}';
}
}
2 changes: 1 addition & 1 deletion src/haxelib/api/RepoManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class RepoManager {

Returns the directory path if it is found, otherwise returns null.
**/
static function getLocalPath(dir:String):Null<String> {
public static function getLocalPath(dir:String):Null<String> {
if (dir == "")
return null;
final repo = Path.join([dir, LOCAL_REPO_DIR]);
Expand Down
5 changes: 5 additions & 0 deletions src/haxelib/api/Repository.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package haxelib.api;

import haxelib.VersionData.VcsData;
import sys.FileSystem;
import sys.io.File;

Expand Down Expand Up @@ -470,4 +471,8 @@ class Repository {
function getDevFilePath(name:ProjectName):String {
return addToRepoPath(name, DEV_FILE);
}

public function getVcsData(name: ProjectName, version: VcsID): VcsData {
return Vcs.get(version).getReproducibleVersion(getProjectVersionPath(name, version));
}
}
40 changes: 37 additions & 3 deletions src/haxelib/api/Vcs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package haxelib.api;

import haxelib.VersionData.VcsData;
import sys.FileSystem;
import sys.thread.Thread;
import sys.thread.Lock;
Expand Down Expand Up @@ -232,6 +233,8 @@ abstract class Vcs implements IVcs {
public abstract function clone(libPath:String, vcsPath:String, ?branch:String, ?version:String, ?debugLog:(msg:String)->Void):Void;

public abstract function update(?confirm:() -> Bool, ?debugLog:(msg:String) -> Void, ?summaryLog:(msg:String) -> Void):Bool;

public abstract function getReproducibleVersion(libPath: String): VcsData;
}

/** Class wrapping `git` operations. **/
Expand Down Expand Up @@ -347,6 +350,20 @@ class Git extends Vcs {
// return prev. cwd:
Sys.setCwd(oldCwd);
}

public function getReproducibleVersion(libPath: String): VcsData {
final oldCwd = Sys.getCwd();
Sys.setCwd(libPath);

final ret: VcsData = {
// could the remote's name not be "origin"?
url: run(["remote", "get-url", "origin"], true).out.trim(),
ref: run(["rev-parse", "HEAD"], true).out.trim(),
};

Sys.setCwd(oldCwd);
return ret;
}
}

/** Class wrapping `hg` operations. **/
Expand Down Expand Up @@ -407,17 +424,34 @@ class Mercurial extends Vcs {
public function clone(libPath:String, url:String, ?branch:String, ?version:String, ?debugLog:(msg:String)->Void):Void {
final vcsArgs = ["clone", url, libPath];

if (branch != null) {
if (branch != null && version != null) {
vcsArgs.push("--branch");
vcsArgs.push(branch);
}

if (version != null) {
vcsArgs.push("--rev");
vcsArgs.push(version);
} else if (branch != null) {
vcsArgs.push("--updaterev");
vcsArgs.push(branch);
} else if (version != null) {
vcsArgs.push("--updaterev");
vcsArgs.push(version);
}

if (run(vcsArgs, debugLog).code != 0)
throw VcsError.CantCloneRepo(this, url/*, ret.out*/);
}

public function getReproducibleVersion(libPath: String): VcsData {
final oldCwd = Sys.getCwd();
Sys.setCwd(libPath);

final ret: VcsData = {
url: run(["paths", "default"], true).out.trim(),
ref: run(["identify", "-i", "--debug"], true).out.trim(),
};

Sys.setCwd(oldCwd);
return ret;
}
}
3 changes: 3 additions & 0 deletions src/haxelib/client/Args.hx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ enum abstract Command(String) to String {
// deprecated commands
final Local = "local";
final SelfUpdate = "selfupdate";

final Lock = "lock";
}

@:build(haxelib.client.Util.buildArgType())
Expand Down Expand Up @@ -276,6 +278,7 @@ class Args {
addCommand(Setup, "set the haxelib repository path", Miscellaneous);
addCommand(NewRepo, "create a new local repository", Miscellaneous);
addCommand(DeleteRepo, "delete the local repository", Miscellaneous);
addCommand(Lock, "output an hxml file with all the installed libraries", Miscellaneous);
addCommand(ConvertXml, "convert haxelib.xml file to haxelib.json", Miscellaneous);
addCommand(Run, "run the specified library with parameters", Miscellaneous);
addCommand(Proxy, "setup the Http proxy", Miscellaneous);
Expand Down
40 changes: 40 additions & 0 deletions src/haxelib/client/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package haxelib.client;

import haxelib.VersionData.VersionDataHelper;
import haxe.crypto.Md5;
import haxe.iterators.ArrayIterator;

Expand Down Expand Up @@ -232,6 +233,8 @@ class Main {
// deprecated commands
Local => create(local, 1, 'haxelib install <file>'),
SelfUpdate => create(updateSelf, 0, true, 'haxelib --global update $HAXELIB_LIBNAME'),

Lock => create(lock, 0),
];
}

Expand Down Expand Up @@ -843,6 +846,43 @@ class Main {
Cli.print('Local repository deleted ($path)');
}

/** Outputs a lock file with all the installed libraries **/
function lock() {
if (RepoManager.getLocalPath(Sys.getCwd()) == null) {
Cli.printWarning("no local repository found, dumping lock from global");
}

final scope = getScope();
final libraryInfo = scope.getArrayOfLibraryInfo();

// sort projects alphabetically
libraryInfo.sort(function(a, b) return Reflect.compare(a.name.toLowerCase(), b.name.toLowerCase()));

for (library in libraryInfo) {
if (library.devPath != null) {
Cli.printError('Error: version of "${library.name}" is "dev". The lockfile may not work properly in other environments!');
}

final version = try scope.getVersion(library.name) catch (e) {
Cli.printError('Error: ${e.message}');
continue;
};

var versionData: VersionData;
if (VcsID.isValid(version)) {
final vcsId = VcsID.ofString(version);
versionData = VcsInstall(
vcsId,
scope.repository.getVcsData(library.name, vcsId)
);
} else {
versionData = Haxelib(SemVer.ofString(version));
}
final versionStr = VersionDataHelper.toString(versionData);
Cli.print('--lib ${library.name}:$versionStr');
}
}

// ----------------------------------

static function main() {
Expand Down
15 changes: 7 additions & 8 deletions test/HaxelibTests.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ class HaxelibTests {

final isCI = Sys.getEnv("CI") != null;

// The test repo https://bitbucket.org/fzzr/hx.signal is gone.
// if (isCI || cmdSucceed("hg", ["version"])) {
// // Hg impl. suports tags & revs. Here "78edb4b" is a first revision "initial import" at that repo:
// TestHg.init();
// r.add(new TestHg());
// } else {
// Sys.println("hg not found.");
// }
if (isCI || cmdSucceed("hg", ["version"])) {
// Hg impl. suports tags & revs. Here "b022617bccfb" is a first revision at that repo:
TestHg.init();
r.add(new TestHg());
} else {
Sys.println("hg not found.");
}
if (isCI || cmdSucceed("git", ["version"])) {
// Git impl. suports only tags. Here "0.9.2" is a first revision too ("initial import"):
TestGit.init();
Expand Down
2 changes: 1 addition & 1 deletion test/tests/TestGit.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class TestGit extends TestVcs {
}

public function new():Void {
super(VcsID.Git, "Git", FileSystem.fullPath(REPO_PATH), "0.9.2");
super(VcsID.Git, "Git", FileSystem.fullPath(REPO_PATH), "develop", "0.9.2");
}
}
6 changes: 3 additions & 3 deletions test/tests/TestHg.hx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package tests;

import sys.FileSystem;
import haxelib.api.Vcs;
import haxelib.VersionData.VcsID;

class TestHg extends TestVcs {
static final REPO_PATH = 'test/repo/hg';

static public function init() {
HaxelibTests.deleteDirectory(REPO_PATH);
HaxelibTests.runCommand('hg', ['clone', 'https://bitbucket.org/fzzr/hx.signal', REPO_PATH]);
HaxelibTests.runCommand('hg', ['clone', 'http://hg.code.sf.net/p/hx-signal/mercurial', REPO_PATH]);
}

public function new():Void {
super(VcsID.Hg, "Mercurial", FileSystem.fullPath(REPO_PATH), "78edb4b");
super(VcsID.Hg, "Mercurial", FileSystem.fullPath(REPO_PATH), "default", "b022617bccfb");
}
}
12 changes: 7 additions & 5 deletions test/tests/TestVcs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ class TestVcs extends TestBase
final id:VcsID = null;
final vcsName:String = null;
final url:String = null;
final branch:String = null;
final rev:String = null;
var counter:Int = 0;

//--------------- constructor ---------------//

public function new(id:VcsID, vcsName:String, url:String, ?rev:String) {
public function new(id:VcsID, vcsName:String, url:String, ?branch:String, ?rev:String) {
super();
this.id = id;
this.url = url;
this.branch = branch;
this.rev = rev;
this.vcsName = vcsName;

Expand Down Expand Up @@ -92,7 +94,7 @@ class TestVcs extends TestBase
public function testCloneBranch():Void {
final vcs = getVcs();
final dir = vcs.directory + counter++;
vcs.clone(dir, url, "develop");
vcs.clone(dir, url, branch);

assertTrue(FileSystem.exists(dir));
assertTrue(FileSystem.isDirectory(dir));
Expand All @@ -104,7 +106,7 @@ class TestVcs extends TestBase
public function testCloneBranchTag_0_9_2():Void {
final vcs = getVcs();
final dir = vcs.directory + counter++;
vcs.clone(dir, url, "develop", "0.9.2");
vcs.clone(dir, url, branch, "0.9.2");
Sys.sleep(3);
assertTrue(FileSystem.exists(dir));
assertTrue(FileSystem.exists('$dir/.${vcs.directory}'));
Expand All @@ -116,7 +118,7 @@ class TestVcs extends TestBase
public function testCloneBranchTag_0_9_3():Void {
final vcs = getVcs();
final dir = vcs.directory + counter++;
vcs.clone(dir, url, "develop", "0.9.3");
vcs.clone(dir, url, branch, "0.9.3");

assertTrue(FileSystem.exists(dir));
assertTrue(FileSystem.exists('$dir/.${vcs.directory}'));
Expand All @@ -128,7 +130,7 @@ class TestVcs extends TestBase
public function testCloneBranchRev():Void {
final vcs = getVcs();
final dir = vcs.directory + counter++;
vcs.clone(dir, url, "develop", rev);
vcs.clone(dir, url, branch, rev);

assertTrue(FileSystem.exists(dir));
assertTrue(FileSystem.exists('$dir/.${vcs.directory}'));
Expand Down