Skip to content

Commit

Permalink
New lix tools
Browse files Browse the repository at this point in the history
* hx rc update

* hx rc filename (for setup-haxe or other similar tools that need the download path)
  • Loading branch information
kLabz committed Jan 7, 2025
1 parent 060d561 commit 26b66e4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/HaxeManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class HaxeManager {

// Lix related commands
case ["rc", []]: LixTools.resolveHaxe();
case ["rc", ["update"]]: LixTools.updateHaxeRc();
case ["rc", ["filename"]]: Sys.println(LixTools.getFilename());
case ["lix-to-install", [file]]: LixTools.generateInstallHxml(file);
case ["lix-libs", []]: LixTools.applyLibs();

Expand All @@ -35,6 +37,12 @@ class HaxeManager {

Sys.println(Utils.getCurrentFull().or(""));

case ["current", ["--sha"]]:
if (Sys.systemName() == "Windows")
throw "`hx current --sha` is not supported on Windows";

Sys.println(HaxeNightlies.resolveSha(Utils.getCurrentSha()).or(""));

case ["list", []]: for (v in Utils.getVersions()) Sys.println(v);
case ["list-classpath", []]: HaxeClasspath.list();
case ["list-classpath", [hxml]]: HaxeClasspath.list(hxml);
Expand Down
18 changes: 16 additions & 2 deletions src/HaxeNightlies.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@ class HaxeNightlies {
static inline var HAXE_REPO = "https://github.com/HaxeFoundation/haxe.git";
static var ref_check = ~/^[a-f0-9]{7,}$/i;

public static function resolve(ref:String):String {
public static function resolve(ref:String, ?strict:Bool = false):Null<String> {
if (SemVer.isValid(ref)) return getNightly(ref, true);
if (isValid(ref)) return getNightly(ref);
return ref;
return strict ? null : ref;
}

public static function resolveSha(ref:String):Null<String> {
final date = getCommitDate(ref);
if (date == null) {
if (!updated) {
updateNightliesData();
return resolveSha(ref);
} else {
return null;
}
} else {
return getShortSha(ref);
}
}

public static function isValid(ref:String):Bool {
Expand Down
61 changes: 47 additions & 14 deletions src/LixTools.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,49 @@ enum HaxeRcError {
ParseError(err:String);
}

typedef HaxeshimConfig = {
var version(default, null):String;
var resolveLibs(default, null):String; // TODO if it becomes relevant
}

class LixTools {
public static function safeGetRc():Null<String> {
return try getRc() catch(_) null;
}

public static function getRc():Null<String> {
static function getFullRc():HaxeshimConfig {
final cwd = Utils.getCallSite();

// TODO (?): check parent directories too
final rc = Path.join([cwd, ".haxerc"]);

if (FileSystem.exists(rc)) {
try {
final version = Json.parse(File.getContent(rc)).version;
return version;
} catch (e) {
throw ParseError(Std.string(e));
}
try return Json.parse(File.getContent(rc)) catch (e) throw ParseError(Std.string(e));
} else {
throw NotFound;
}
}

public static function getFilename():String {
final version = getFullRc().version;
if (version == null) Utils.failWith('Invalid .haxerc: missing version');

var filename = HaxeNightlies.resolve(version, true);
if (filename == null) Utils.failWith('Can only get filename for nightlies.');
return filename;
}

static function writeRc(rc:HaxeshimConfig):Void {
final cwd = Utils.getCallSite();
File.saveContent(Path.join([cwd, ".haxerc"]), Json.stringify(rc, ' '));
}

public static function getRc():String {
final version = getFullRc().version;
if (version == null) Utils.failWith('Invalid .haxerc: missing version');
return version;
}

public static function resolveHaxe() {
try {
final version = getRc();
Expand All @@ -52,14 +72,27 @@ class LixTools {
}
}
} catch (e:HaxeRcError) {
switch e {
case NotFound:
Utils.displayError('Did not find any .haxerc to apply');
case ParseError(e):
Utils.displayError('Could not get Haxe version from .haxerc: $e');
}
Utils.failWith(switch e {
case NotFound: 'Did not find any .haxerc to apply';
case ParseError(e): 'Could not parse .haxerc: $e';
});
}
}

//TODO: support releases too
public static function updateHaxeRc() {
final sha = HaxeNightlies.resolveSha(Utils.getCurrentSha());
if (sha == null) Utils.failWith('Could not get current Haxe short sha');

Sys.exit(1);
try {
final rc = getFullRc();
writeRc({version: sha, resolveLibs: rc.resolveLibs});
} catch (e:HaxeRcError) {
//TODO: factorize with resolveHaxe error handling
Utils.failWith(switch e {
case NotFound: 'Did not find any .haxerc to apply';
case ParseError(e): 'Could not parse .haxerc: $e';
});
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/tools/Utils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ class Utils {
};
}

public static function getCurrentSha():Null<String> {
final version = getCurrent();
if (version == null) return null;
final parts = version.split("+");
if (parts.length != 2) return null;
return parts.pop();
}

public static function runHaxe(path:String, args:Array<String>):Void {
final exe = switch Sys.systemName() {
case "Windows": "haxe.exe";
Expand Down

0 comments on commit 26b66e4

Please sign in to comment.