Skip to content

Commit 962bcf2

Browse files
committed
Lix: add hx lix-libs and hx lix-to-install install.hxml commands
1 parent 2231964 commit 962bcf2

File tree

3 files changed

+178
-64
lines changed

3 files changed

+178
-64
lines changed

src/HaxeManager.hx

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ 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();
16+
17+
// Lix related commands
18+
case ["rc", []]: LixTools.resolveHaxe();
19+
case ["lix-to-install", [file]]: LixTools.generateInstallHxml(file);
20+
case ["lix-libs", []]: LixTools.applyLibs();
1721

1822
case ["install", [file]]: HaxeDownload.installLocal(file);
1923
case ["install", [file, alias]]: HaxeDownload.installLocal(file, alias);
@@ -45,7 +49,7 @@ class HaxeManager {
4549
// will not be forwarded properly
4650
case ["with", args] if (args.length > 0):
4751
var v = args.shift();
48-
if (v == "rc") v = HaxeRc.getRc();
52+
if (v == "rc") v = LixTools.getRc();
4953
final path = Utils.find(v);
5054
Utils.runHaxe(path, args);
5155

@@ -109,6 +113,13 @@ class HaxeManager {
109113
' or: ${ORANGE}hx rc${RESET}',
110114
' Install and select Haxe version specified by .haxerc file',
111115
'',
116+
' or: ${ORANGE}hx lix-libs${RESET}',
117+
' Install libraries as specified by lix in haxe_libraries folder',
118+
'',
119+
' or: ${ORANGE}hx lix-to-install ${UNDERLINE}<install.hxml>${RESET}',
120+
' Generate installation instruction for haxelib in ${BOLD}install.hxml${BOLD_OFF}',
121+
' from lix data in haxe_libraries folder',
122+
'',
112123
' or: ${ORANGE}hx list${RESET}',
113124
' Display all installed Haxe versions',
114125
'',

src/HaxeRc.hx

-62
This file was deleted.

src/LixTools.hx

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import eval.integers.Int64;
2+
import eval.luv.Buffer;
3+
import eval.luv.File.FileSync;
4+
import haxelib.SemVer;
5+
import sys.io.File;
6+
import haxe.Json;
7+
import sys.FileSystem;
8+
import haxe.io.Path;
9+
10+
import tools.Utils;
11+
12+
enum HaxeRcError {
13+
NotFound;
14+
ParseError(err:String);
15+
}
16+
17+
class LixTools {
18+
public static function safeGetRc():Null<String> {
19+
return try getRc() catch(_) null;
20+
}
21+
22+
public static function getRc():Null<String> {
23+
final cwd = Utils.getCallSite();
24+
25+
// TODO (?): check parent directories too
26+
final rc = Path.join([cwd, ".haxerc"]);
27+
28+
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+
}
35+
} else {
36+
throw NotFound;
37+
}
38+
}
39+
40+
public static function resolveHaxe() {
41+
try {
42+
final version = getRc();
43+
44+
if (SemVer.isValid(version)) {
45+
HaxeDownload.downloadRelease(version, r -> HaxeSelect.select(r));
46+
} else if (HaxeNightlies.isValid(version)) {
47+
switch (Utils.resolveRelease(version)) {
48+
case null:
49+
HaxeDownload.downloadNightly(version, r -> HaxeSelect.select(r));
50+
case r:
51+
HaxeSelect.select(r);
52+
}
53+
}
54+
} 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+
}
61+
62+
Sys.exit(1);
63+
}
64+
}
65+
66+
static var installReg = ~/^#\s@install:\slix\s--silent\sdownload\s"([^"]+)"\s/;
67+
68+
static function getInstallHxml():String {
69+
final cwd = Utils.getCallSite();
70+
final haxe_libraries = Path.join([cwd, "haxe_libraries"]);
71+
72+
var installHxml = "";
73+
74+
function addHaxelib(lib:String, version:String) {
75+
installHxml += '-lib $lib:$version\n';
76+
}
77+
78+
function addGitLib(lib:String, url:String) {
79+
installHxml += '-lib $lib:git:$url\n';
80+
}
81+
82+
function processLib(hxml) {
83+
final lib = Path.withoutExtension(hxml);
84+
final contents = File.getContent(Path.join([haxe_libraries, hxml])).split("\n");
85+
86+
// TODO: handle post-install
87+
for (line in contents) {
88+
if (installReg.match(line)) {
89+
final source = installReg.matched(1);
90+
final protocol = source.split(":").shift();
91+
92+
switch protocol {
93+
case null:
94+
throw ParseError('Cannot parse install instructions for lib $lib: $source');
95+
96+
case "haxelib":
97+
final parts = source.split("#");
98+
final libName = parts[0].substr(protocol.length + 2);
99+
addHaxelib(libName, parts[1]);
100+
101+
case "gh" | "github":
102+
addGitLib(lib, "https" + source.substr(protocol.length));
103+
104+
case "http" | "https":
105+
addGitLib(lib, source);
106+
107+
case v:
108+
trace(v);
109+
addGitLib(lib, source); // This could work? xD
110+
}
111+
112+
return;
113+
}
114+
}
115+
116+
trace('Warning: installation instructions not found for $lib');
117+
}
118+
119+
if (FileSystem.exists(haxe_libraries)) {
120+
for (lib in FileSystem.readDirectory(haxe_libraries)) {
121+
if (Path.extension(lib) == "hxml") processLib(lib);
122+
}
123+
124+
return installHxml;
125+
} else {
126+
throw NotFound;
127+
}
128+
}
129+
130+
public static function generateInstallHxml(file:String) {
131+
final installHxml = getInstallHxml();
132+
133+
if (installHxml != "") {
134+
final cwd = Utils.getCallSite();
135+
final path = Path.isAbsolute(file) ? file : Path.join([cwd, file]);
136+
File.saveContent(path, installHxml);
137+
Sys.println('Saved lib installation instructions to $file');
138+
}
139+
}
140+
141+
public static function applyLibs() {
142+
final installHxml = getInstallHxml();
143+
if (installHxml != "") {
144+
final cwd = Utils.getCallSite();
145+
final old_cwd = Sys.getCwd();
146+
Sys.setCwd(cwd);
147+
148+
switch FileSync.mkstemp("install_XXXXXX") {
149+
case Error(e):
150+
Sys.setCwd(old_cwd);
151+
throw e;
152+
153+
case Ok({name: name, file: file}):
154+
final hxml = name.toString() + ".hxml";
155+
156+
FileSync.write(file, Int64.ZERO, [Buffer.fromString(installHxml)]);
157+
FileSync.close(file);
158+
FileSync.rename(name, hxml);
159+
Sys.command("hxlib", ["state", "load", hxml]);
160+
FileSync.unlink(hxml);
161+
Sys.setCwd(old_cwd);
162+
}
163+
}
164+
}
165+
}

0 commit comments

Comments
 (0)