Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions src/haxelib/api/ScriptRunner.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ typedef LibraryRunData = {
}

private enum RunType {
/** Runs compiled neko file at `path` **/
Neko(path:String);
/** **/
/** Runs compiled neko file **/
Neko;
/** Compiles and runs script using haxe's eval target **/
Script(main:String, name:ProjectName, version:VersionOrDev, dependencies:Dependencies);
}

Expand All @@ -75,6 +75,8 @@ class ScriptError extends haxe.Exception {
class ScriptRunner {
static final HAXELIB_RUN = "HAXELIB_RUN";
static final HAXELIB_RUN_NAME = "HAXELIB_RUN_NAME";
static final RUN_HXB_CACHE = "run.hxb";
static final RUN_NEKO_SCRIPT = "run.n";

/**
Run `library`, with `callData`.
Expand All @@ -85,7 +87,7 @@ class ScriptRunner {
final type = getType(library);

final cmd = getCmd(type);
final args = generateArgs(type, callData, getCompilerVersion);
final args = generateArgs(library.path, type, callData, getCompilerVersion);

final oldState = getState();

Expand All @@ -108,56 +110,69 @@ class ScriptRunner {
static function getType(library:LibraryRunData) {
if (library.main != null)
return Script(library.main, library.name, library.version, library.dependencies);
if (FileSystem.exists(library.path + 'run.n'))
return Neko(library.path + 'run.n');
if (FileSystem.exists(library.path + RUN_NEKO_SCRIPT))
return Neko;
if (FileSystem.exists(library.path + 'Run.hx'))
return Script("Run", library.name, library.version, library.dependencies);
throw 'Library ${library.name} version ${library.version} does not have a run script';
}

static function getCmd(runType:RunType):String {
return switch runType {
case Neko(_): "neko";
case Neko: "neko";
case Script(_): "haxe";
}
};
}

static function generateArgs(runType:RunType, callData:CallData, getCompilerVersion:() -> SemVer):Array<String> {
static function generateArgs(path:String, runType:RunType, callData:CallData, getCompilerVersion:() -> SemVer):Array<String> {
switch runType {
case Neko(path):
case Neko:
final callArgs = callData.args.copy();
callArgs.unshift(path);
callArgs.unshift(path + RUN_NEKO_SCRIPT);
callArgs.push(callData.dir);
return callArgs;
case Script(main, name, version, dependencies):
final isHaxe4 = SemVer.compare(getCompilerVersion(), SemVer.ofString('4.0.0')) >= 0;
final compilerVersion = getCompilerVersion();
final isHaxe4 = compilerVersion >= SemVer.ofString('4.0.0');
final useCache = compilerVersion >= SemVer.ofString('5.0.0-preview.1');
final useGlobalRepo = isHaxe4 && callData.useGlobalRepo;

final callArgs = generateScriptArgs(main, name, version, dependencies, useGlobalRepo);
final callArgs = generateScriptArgs(main, name, version, dependencies, path, useGlobalRepo, useCache);
for (arg in callData.args)
callArgs.push(arg);
callArgs.push(callData.dir);
return callArgs;
}
}

static function generateScriptArgs(main:String, name:ProjectName, version:VersionOrDev, dependencies:Dependencies, useGlobalRepo:Bool):Array<String> {
static function generateScriptArgs(main:String, name:ProjectName, version:VersionOrDev, dependencies:Dependencies, path:String, useGlobalRepo:Bool, useCache:Bool):Array<String> {
final args = [];

function addLib(data:String):Void {
args.push("--library");
args.push(data);
final cachePath = path + RUN_HXB_CACHE;

if (useCache && FileSystem.exists(cachePath)) {
args.push("--hxb-lib");
args.push(cachePath);
} else {
function addLib(data:String):Void {
args.push("--library");
args.push(data);
}
if (useGlobalRepo)
args.push('--haxelib-global');

// add the project itself first
addLib(if (version != Dev.Dev) '$name:$version' else '$name');

for (d in dependencies)
addLib(d);

if (useCache) {
args.push("--hxb");
args.push(cachePath);
}
}

if (useGlobalRepo)
args.push('--haxelib-global');

// add the project itself first
addLib(if (version != Dev.Dev) '$name:$version' else '$name');

for (d in dependencies)
addLib(d);

args.push('--run');
args.push(main);
return args;
Expand Down
Loading