Skip to content

Commit

Permalink
Run arbitrary haxelib commands + add compiler flags
Browse files Browse the repository at this point in the history
  • Loading branch information
l0go committed Jun 30, 2024
1 parent 2f36bf7 commit 3174ccb
Showing 1 changed file with 14 additions and 57 deletions.
71 changes: 14 additions & 57 deletions src/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ using StringTools;

enum abstract Action(String) {
var Run = "run";
var HaxelibInstall = "haxelib_install";
var HaxelibUpdate = "haxelib_update";
var HaxelibRun = "haxelib_run";
}

enum abstract Status(String) {
Expand All @@ -19,6 +18,7 @@ enum abstract Status(String) {
typedef Request = {
action: Action,
?input: String,
?hxml: String,
};

typedef Response = {
Expand All @@ -34,11 +34,6 @@ class Main {
}));
var log = new logging.Logger(Main);

final haxelibLocation = getHaxelibLocation();
if (!sys.FileSystem.exists(haxelibLocation + "libs.hxml")) {
sys.io.File.saveContent(haxelibLocation + "libs.hxml", "# Libs installed during sandbox runtime\n");
}

var server = new http.server.HttpServer();
server.onRequest = (request, response) -> {
return new promises.Promise((resolve, reject) -> {
Expand All @@ -49,7 +44,7 @@ class Main {
case Run:
var r: Response;
if (body.action != Run) throw "Invalid Action";
runHaxe(body.input, (output) -> {
runHaxe(body.input, body.hxml ?? "", (output) -> {
r = {
status: Ok,
output: output,
Expand All @@ -64,52 +59,18 @@ class Main {
sendResponse(response, r);
resolve(response);
});
case HaxelibInstall:
final hl = ChildProcess.spawn("haxelib", ["install", body.input]);
final libsHxml = sys.io.File.append(haxelibLocation + "libs.hxml", false);

var data: String;
hl.stdout.on("data", (d) -> {
data = (cast d : js.node.Buffer).toString();
});

hl.on('exit', (code) -> {
var r: Response;
if (code == 0) {
r = {
status: Ok,
output: data,
};
libsHxml.writeString('-lib ${body.input.trim()}\n');
} else {
r = {
status: OhNo,
output: "Installation Failed",
};
}
sendResponse(response, r);
resolve(response);
});
case HaxelibUpdate:
final hl = ChildProcess.spawn("haxelib", ["update"]);

hl.on('exit', (code) -> {
var r: Response;
if (code == 0) {
r = {
status: Ok,
output: "Updated"
};
} else {
r = {
status: OhNo,
output: "Update Failed",
};
}
case HaxelibRun:
ChildProcess.exec("haxelib " + body.input, null, (_, stdout, stderr) -> {
var r: Response = {
status: stderr != "" ? Ok : OhNo,
output: (cast stdout : js.node.Buffer).toString(),
};
sendResponse(response, r);
resolve(response);
});
}
default:
throw "Action doesn't exist";
}

} else {
var error = new http.HttpError(405);
Expand All @@ -131,19 +92,15 @@ class Main {
response.write(haxe.Json.stringify(with));
}

static function runHaxe(src: String, onOutput: (String) -> Void, onError: (String) -> Void) {
static function runHaxe(src: String, hxml: String, onOutput: (String) -> Void, onError: (String) -> Void) {
// Create a temporary folder in memory for holding the source
final dir = new String(ChildProcess.spawnSync("mktemp", ["-d", "-p", "/dev/shm/"]).stdout).trim();
sys.io.File.saveContent('$dir/Main.hx', src);
ChildProcess.exec('haxe params.hxml ${getHaxelibLocation()}libs.hxml -cp $dir', {timeout: 10000}, (error, stdout, stderr) -> {
ChildProcess.exec('haxe params.hxml $hxml -cp $dir', {timeout: 10000}, (error, stdout, stderr) -> {
if (stderr != "") onError((cast stderr : js.node.Buffer).toString());
if (stdout != "") onOutput((cast stdout : js.node.Buffer).toString());
if (error?.signal == "SIGTERM") onError("Timed out, try again");
ChildProcess.exec('rm -rf $dir', null, null);
});
}

inline static function getHaxelibLocation(): String {
return (cast ChildProcess.execSync("haxelib config") : js.node.Buffer).toString().trim();
}
}

0 comments on commit 3174ccb

Please sign in to comment.