Skip to content

Commit 26b66e4

Browse files
committed
New lix tools
* hx rc update * hx rc filename (for setup-haxe or other similar tools that need the download path)
1 parent 060d561 commit 26b66e4

File tree

4 files changed

+79
-16
lines changed

4 files changed

+79
-16
lines changed

src/HaxeManager.hx

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class HaxeManager {
1616

1717
// Lix related commands
1818
case ["rc", []]: LixTools.resolveHaxe();
19+
case ["rc", ["update"]]: LixTools.updateHaxeRc();
20+
case ["rc", ["filename"]]: Sys.println(LixTools.getFilename());
1921
case ["lix-to-install", [file]]: LixTools.generateInstallHxml(file);
2022
case ["lix-libs", []]: LixTools.applyLibs();
2123

@@ -35,6 +37,12 @@ class HaxeManager {
3537

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

40+
case ["current", ["--sha"]]:
41+
if (Sys.systemName() == "Windows")
42+
throw "`hx current --sha` is not supported on Windows";
43+
44+
Sys.println(HaxeNightlies.resolveSha(Utils.getCurrentSha()).or(""));
45+
3846
case ["list", []]: for (v in Utils.getVersions()) Sys.println(v);
3947
case ["list-classpath", []]: HaxeClasspath.list();
4048
case ["list-classpath", [hxml]]: HaxeClasspath.list(hxml);

src/HaxeNightlies.hx

+16-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@ class HaxeNightlies {
88
static inline var HAXE_REPO = "https://github.com/HaxeFoundation/haxe.git";
99
static var ref_check = ~/^[a-f0-9]{7,}$/i;
1010

11-
public static function resolve(ref:String):String {
11+
public static function resolve(ref:String, ?strict:Bool = false):Null<String> {
1212
if (SemVer.isValid(ref)) return getNightly(ref, true);
1313
if (isValid(ref)) return getNightly(ref);
14-
return ref;
14+
return strict ? null : ref;
15+
}
16+
17+
public static function resolveSha(ref:String):Null<String> {
18+
final date = getCommitDate(ref);
19+
if (date == null) {
20+
if (!updated) {
21+
updateNightliesData();
22+
return resolveSha(ref);
23+
} else {
24+
return null;
25+
}
26+
} else {
27+
return getShortSha(ref);
28+
}
1529
}
1630

1731
public static function isValid(ref:String):Bool {

src/LixTools.hx

+47-14
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,49 @@ enum HaxeRcError {
1414
ParseError(err:String);
1515
}
1616

17+
typedef HaxeshimConfig = {
18+
var version(default, null):String;
19+
var resolveLibs(default, null):String; // TODO if it becomes relevant
20+
}
21+
1722
class LixTools {
1823
public static function safeGetRc():Null<String> {
1924
return try getRc() catch(_) null;
2025
}
2126

22-
public static function getRc():Null<String> {
27+
static function getFullRc():HaxeshimConfig {
2328
final cwd = Utils.getCallSite();
2429

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

2833
if (FileSystem.exists(rc)) {
29-
try {
30-
final version = Json.parse(File.getContent(rc)).version;
31-
return version;
32-
} catch (e) {
33-
throw ParseError(Std.string(e));
34-
}
34+
try return Json.parse(File.getContent(rc)) catch (e) throw ParseError(Std.string(e));
3535
} else {
3636
throw NotFound;
3737
}
3838
}
3939

40+
public static function getFilename():String {
41+
final version = getFullRc().version;
42+
if (version == null) Utils.failWith('Invalid .haxerc: missing version');
43+
44+
var filename = HaxeNightlies.resolve(version, true);
45+
if (filename == null) Utils.failWith('Can only get filename for nightlies.');
46+
return filename;
47+
}
48+
49+
static function writeRc(rc:HaxeshimConfig):Void {
50+
final cwd = Utils.getCallSite();
51+
File.saveContent(Path.join([cwd, ".haxerc"]), Json.stringify(rc, ' '));
52+
}
53+
54+
public static function getRc():String {
55+
final version = getFullRc().version;
56+
if (version == null) Utils.failWith('Invalid .haxerc: missing version');
57+
return version;
58+
}
59+
4060
public static function resolveHaxe() {
4161
try {
4262
final version = getRc();
@@ -52,14 +72,27 @@ class LixTools {
5272
}
5373
}
5474
} catch (e:HaxeRcError) {
55-
switch e {
56-
case NotFound:
57-
Utils.displayError('Did not find any .haxerc to apply');
58-
case ParseError(e):
59-
Utils.displayError('Could not get Haxe version from .haxerc: $e');
60-
}
75+
Utils.failWith(switch e {
76+
case NotFound: 'Did not find any .haxerc to apply';
77+
case ParseError(e): 'Could not parse .haxerc: $e';
78+
});
79+
}
80+
}
81+
82+
//TODO: support releases too
83+
public static function updateHaxeRc() {
84+
final sha = HaxeNightlies.resolveSha(Utils.getCurrentSha());
85+
if (sha == null) Utils.failWith('Could not get current Haxe short sha');
6186

62-
Sys.exit(1);
87+
try {
88+
final rc = getFullRc();
89+
writeRc({version: sha, resolveLibs: rc.resolveLibs});
90+
} catch (e:HaxeRcError) {
91+
//TODO: factorize with resolveHaxe error handling
92+
Utils.failWith(switch e {
93+
case NotFound: 'Did not find any .haxerc to apply';
94+
case ParseError(e): 'Could not parse .haxerc: $e';
95+
});
6396
}
6497
}
6598

src/tools/Utils.hx

+8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ class Utils {
9797
};
9898
}
9999

100+
public static function getCurrentSha():Null<String> {
101+
final version = getCurrent();
102+
if (version == null) return null;
103+
final parts = version.split("+");
104+
if (parts.length != 2) return null;
105+
return parts.pop();
106+
}
107+
100108
public static function runHaxe(path:String, args:Array<String>):Void {
101109
final exe = switch Sys.systemName() {
102110
case "Windows": "haxe.exe";

0 commit comments

Comments
 (0)