Skip to content

Commit

Permalink
Lix: add hx lix-libs and hx lix-to-install install.hxml commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kLabz committed May 21, 2024
1 parent 2231964 commit 962bcf2
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 64 deletions.
15 changes: 13 additions & 2 deletions src/HaxeManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class HaxeManager {
case [null, _]: HaxeSelect.fzf();
case ["download", args]: HaxeDownload.run(args);
case ["select", args]: HaxeSelect.run(args);
case ["rc", _]: HaxeRc.resolve();

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

case ["install", [file]]: HaxeDownload.installLocal(file);
case ["install", [file, alias]]: HaxeDownload.installLocal(file, alias);
Expand Down Expand Up @@ -45,7 +49,7 @@ class HaxeManager {
// will not be forwarded properly
case ["with", args] if (args.length > 0):
var v = args.shift();
if (v == "rc") v = HaxeRc.getRc();
if (v == "rc") v = LixTools.getRc();
final path = Utils.find(v);
Utils.runHaxe(path, args);

Expand Down Expand Up @@ -109,6 +113,13 @@ class HaxeManager {
' or: ${ORANGE}hx rc${RESET}',
' Install and select Haxe version specified by .haxerc file',
'',
' or: ${ORANGE}hx lix-libs${RESET}',
' Install libraries as specified by lix in haxe_libraries folder',
'',
' or: ${ORANGE}hx lix-to-install ${UNDERLINE}<install.hxml>${RESET}',
' Generate installation instruction for haxelib in ${BOLD}install.hxml${BOLD_OFF}',
' from lix data in haxe_libraries folder',
'',
' or: ${ORANGE}hx list${RESET}',
' Display all installed Haxe versions',
'',
Expand Down
62 changes: 0 additions & 62 deletions src/HaxeRc.hx

This file was deleted.

165 changes: 165 additions & 0 deletions src/LixTools.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import eval.integers.Int64;
import eval.luv.Buffer;
import eval.luv.File.FileSync;
import haxelib.SemVer;
import sys.io.File;
import haxe.Json;
import sys.FileSystem;
import haxe.io.Path;

import tools.Utils;

enum HaxeRcError {
NotFound;
ParseError(err:String);
}

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

public static function getRc():Null<String> {
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));
}
} else {
throw NotFound;
}
}

public static function resolveHaxe() {
try {
final version = getRc();

if (SemVer.isValid(version)) {
HaxeDownload.downloadRelease(version, r -> HaxeSelect.select(r));
} else if (HaxeNightlies.isValid(version)) {
switch (Utils.resolveRelease(version)) {
case null:
HaxeDownload.downloadNightly(version, r -> HaxeSelect.select(r));
case r:
HaxeSelect.select(r);
}
}
} 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');
}

Sys.exit(1);
}
}

static var installReg = ~/^#\s@install:\slix\s--silent\sdownload\s"([^"]+)"\s/;

static function getInstallHxml():String {
final cwd = Utils.getCallSite();
final haxe_libraries = Path.join([cwd, "haxe_libraries"]);

var installHxml = "";

function addHaxelib(lib:String, version:String) {
installHxml += '-lib $lib:$version\n';
}

function addGitLib(lib:String, url:String) {
installHxml += '-lib $lib:git:$url\n';
}

function processLib(hxml) {
final lib = Path.withoutExtension(hxml);
final contents = File.getContent(Path.join([haxe_libraries, hxml])).split("\n");

// TODO: handle post-install
for (line in contents) {
if (installReg.match(line)) {
final source = installReg.matched(1);
final protocol = source.split(":").shift();

switch protocol {
case null:
throw ParseError('Cannot parse install instructions for lib $lib: $source');

case "haxelib":
final parts = source.split("#");
final libName = parts[0].substr(protocol.length + 2);
addHaxelib(libName, parts[1]);

case "gh" | "github":
addGitLib(lib, "https" + source.substr(protocol.length));

case "http" | "https":
addGitLib(lib, source);

case v:
trace(v);
addGitLib(lib, source); // This could work? xD
}

return;
}
}

trace('Warning: installation instructions not found for $lib');
}

if (FileSystem.exists(haxe_libraries)) {
for (lib in FileSystem.readDirectory(haxe_libraries)) {
if (Path.extension(lib) == "hxml") processLib(lib);
}

return installHxml;
} else {
throw NotFound;
}
}

public static function generateInstallHxml(file:String) {
final installHxml = getInstallHxml();

if (installHxml != "") {
final cwd = Utils.getCallSite();
final path = Path.isAbsolute(file) ? file : Path.join([cwd, file]);
File.saveContent(path, installHxml);
Sys.println('Saved lib installation instructions to $file');
}
}

public static function applyLibs() {
final installHxml = getInstallHxml();
if (installHxml != "") {
final cwd = Utils.getCallSite();
final old_cwd = Sys.getCwd();
Sys.setCwd(cwd);

switch FileSync.mkstemp("install_XXXXXX") {
case Error(e):
Sys.setCwd(old_cwd);
throw e;

case Ok({name: name, file: file}):
final hxml = name.toString() + ".hxml";

FileSync.write(file, Int64.ZERO, [Buffer.fromString(installHxml)]);
FileSync.close(file);
FileSync.rename(name, hxml);
Sys.command("hxlib", ["state", "load", hxml]);
FileSync.unlink(hxml);
Sys.setCwd(old_cwd);
}
}
}
}

0 comments on commit 962bcf2

Please sign in to comment.