Skip to content

Commit 48e5461

Browse files
committed
[hx rc] install/select Haxe version specified by .haxerc
1 parent cd7be22 commit 48e5461

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

src/HaxeDownload.hx

+11-9
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,35 @@ class HaxeDownload {
5353
installFile(dest, filename, alias);
5454
}
5555

56-
static function downloadLatest(?alias:String = "dev"):Void {
56+
static function downloadLatest(?alias:String = "dev", ?cb:String->Void):Void {
5757
final url = Utils.getBuildUrl("latest");
58-
install(url[0], url[1], alias);
58+
install(url[0], url[1], alias, cb);
5959
}
6060

61-
static function downloadNightly(v:String, ?alias:String):Void {
61+
public static function downloadNightly(v:String, ?alias:String, ?cb:String->Void):Void {
6262
v = HaxeNightlies.resolve(v);
6363
final url = Utils.getBuildUrl(v);
64-
install(url[0], url[1], alias);
64+
install(url[0], url[1], alias, cb);
6565
}
6666

67-
static function downloadRelease(v:String, ?alias:String):Void {
67+
public static function downloadRelease(v:String, ?alias:String, ?cb:String->Void):Void {
6868
final url = Utils.getReleaseUrl(v);
69-
install(url[0], url[1], alias);
69+
install(url[0], url[1], alias, cb);
7070
}
7171

72-
static function install(url:String, filename:String, ?alias:String):Void {
72+
static function install(url:String, filename:String, ?alias:String, ?cb:String->Void):Void {
7373
url = url + filename;
7474
filename = Path.withoutDirectory(filename);
7575
final path = Path.join([Utils.releasesDir, filename]);
7676

7777
DownloadHelper.download(url, path, () -> {
7878
Sys.println('Downloaded $filename');
79-
installFile(path, filename, alias);
79+
alias = installFile(path, filename, alias);
80+
if (cb != null) cb(alias);
8081
});
8182
}
8283

83-
static function installFile(path:String, filename:String, ?alias:String):Void {
84+
static function installFile(path:String, filename:String, ?alias:String):String {
8485
final out = DownloadHelper.extract(path);
8586
FileSystem.deleteFile(path);
8687

@@ -91,6 +92,7 @@ class HaxeDownload {
9192
try FileSystem.deleteFile(versionPath) catch(_) {}
9293
FileSync.symlink(releasePath, versionPath);
9394
Sys.println('Installed $filename as $alias');
95+
return alias;
9496
}
9597

9698
public static function displayUsage() {

src/HaxeManager.hx

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class HaxeManager {
1313
case [null, _]: HaxeSelect.fzf();
1414
case ["download", args]: HaxeDownload.run(args);
1515
case ["select", args]: HaxeSelect.run(args);
16+
case ["rc", _]: HaxeRc.resolve();
1617

1718
case ["install", [file]]: HaxeDownload.installLocal(file);
1819
case ["install", [file, alias]]: HaxeDownload.installLocal(file, alias);
@@ -91,6 +92,9 @@ class HaxeManager {
9192
}
9293

9394
lines = lines.concat([
95+
' or: ${ORANGE}hx rc${RESET}',
96+
' Install and select Haxe version specified by .haxerc file',
97+
'',
9498
' or: ${ORANGE}hx list${RESET}',
9599
' Display all installed Haxe versions',
96100
'',

src/HaxeRc.hx

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import haxelib.SemVer;
2+
import sys.io.File;
3+
import haxe.Json;
4+
import sys.FileSystem;
5+
import haxe.io.Path;
6+
7+
import tools.Utils;
8+
9+
class HaxeRc {
10+
public static function resolve() {
11+
final cwd = Utils.getCallSite();
12+
13+
// TODO (?): check parent directories too
14+
final rc = Path.join([cwd, ".haxerc"]);
15+
16+
if (FileSystem.exists(rc)) {
17+
try {
18+
final version = Json.parse(File.getContent(rc)).version;
19+
20+
if (SemVer.isValid(version)) {
21+
HaxeDownload.downloadRelease(version, r -> HaxeSelect.select(r));
22+
} else if (HaxeNightlies.isValid(version)) {
23+
switch (Utils.resolveRelease(version)) {
24+
case null:
25+
HaxeDownload.downloadNightly(version, r -> HaxeSelect.select(r));
26+
case r:
27+
HaxeSelect.select(r);
28+
}
29+
}
30+
} catch (e) {
31+
Utils.displayError('Could not get Haxe version from .haxerc: $e');
32+
Sys.exit(1);
33+
}
34+
} else {
35+
Utils.displayError('Did not find any .haxerc to apply');
36+
Sys.exit(1);
37+
}
38+
}
39+
}

src/tools/Utils.hx

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ class Utils {
127127
link(dir);
128128
}
129129

130+
public static function resolveRelease(ref:String):Null<String> {
131+
for (r in FileSystem.readDirectory(releasesDir)) {
132+
if (StringTools.endsWith(r, '_$ref')) return r;
133+
}
134+
135+
return null;
136+
}
137+
130138
public static function selectRelease(r:String):Void {
131139
final dir = Path.join([releasesDir, r]);
132140
if (!FileSystem.exists(dir)) throw 'Version $r is not installed';

0 commit comments

Comments
 (0)