Skip to content

Commit

Permalink
Merge pull request #119 from back2dos/overhaul
Browse files Browse the repository at this point in the history
Overhaul
  • Loading branch information
back2dos authored Apr 22, 2020
2 parents b92388c + 6be44b9 commit fd28764
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 202 deletions.
2 changes: 1 addition & 1 deletion .haxerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "3.4.7",
"version": "4.0.5",
"resolveLibs": "scoped"
}
7 changes: 0 additions & 7 deletions .vscode/settings.json

This file was deleted.

19 changes: 0 additions & 19 deletions .vscode/tasks.json

This file was deleted.

7 changes: 7 additions & 0 deletions build-node.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-lib travix
-lib hxnodejs
-main travix.Travix
-js bin/run.js

-lib tink_cli
--no-traces
6 changes: 3 additions & 3 deletions haxe_libraries/hxnodejs.hxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-D hxnodejs=6.9.1
# @install: lix --silent download "gh://github.com/haxefoundation/hxnodejs#974f456cfa90c0ca1cc92681eca8665e40b4ab1f" into hxnodejs/6.9.1/github/974f456cfa90c0ca1cc92681eca8665e40b4ab1f
-cp ${HAXE_LIBCACHE}/hxnodejs/6.9.1/github/974f456cfa90c0ca1cc92681eca8665e40b4ab1f/src
-D hxnodejs=10.0.0
# @install: lix --silent download "haxelib:/hxnodejs#10.0.0" into hxnodejs/10.0.0/haxelib
-cp ${HAXE_LIBCACHE}/hxnodejs/10.0.0/haxelib/src
--macro allowPackage('sys')
# should behave like other target defines and not be defined in macro context
--macro define('nodejs')
6 changes: 3 additions & 3 deletions haxe_libraries/tink_chunk.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @install: lix --silent download "haxelib:tink_chunk#0.2.0" into tink_chunk/0.2.0/haxelib
-D tink_chunk=0.2.0
-cp ${HAXESHIM_LIBCACHE}/tink_chunk/0.2.0/haxelib/src
-D tink_chunk=0.3.1
# @install: lix --silent download "haxelib:/tink_chunk#0.3.1" into tink_chunk/0.3.1/haxelib
-cp ${HAXE_LIBCACHE}/tink_chunk/0.3.1/haxelib/src
6 changes: 3 additions & 3 deletions haxe_libraries/tink_core.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @install: lix --silent download "haxelib:tink_core#1.16.1" into tink_core/1.16.1/haxelib
-D tink_core=1.16.1
-cp ${HAXESHIM_LIBCACHE}/tink_core/1.16.1/haxelib/src
-D tink_core=1.24.0
# @install: lix --silent download "haxelib:/tink_core#1.24.0" into tink_core/1.24.0/haxelib
-cp ${HAXE_LIBCACHE}/tink_core/1.24.0/haxelib/src
6 changes: 3 additions & 3 deletions haxe_libraries/tink_io.hxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-D tink_io=0.6.2
# @install: lix --silent download "gh://github.com/haxetink/tink_io#9ad28aefacad69c4415d8a458c1a1e920052560e" into tink_io/0.6.2/github/9ad28aefacad69c4415d8a458c1a1e920052560e
-D tink_io=0.7.1
# @install: lix --silent download "haxelib:/tink_io#0.7.1" into tink_io/0.7.1/haxelib
-lib tink_chunk
-lib tink_streams
-cp ${HAXE_LIBCACHE}/tink_io/0.6.2/github/9ad28aefacad69c4415d8a458c1a1e920052560e/src
-cp ${HAXE_LIBCACHE}/tink_io/0.7.1/haxelib/src
10 changes: 5 additions & 5 deletions haxe_libraries/tink_streams.hxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @install: lix --silent download "https://github.com/haxetink/tink_streams/archive/a38e3a97e8512830b31f1fe9476a752cf6fb51ff.tar.gz" into tink_streams/0.2.1/github/a38e3a97e8512830b31f1fe9476a752cf6fb51ff
-D tink_streams=0.2.1
-cp ${HAXESHIM_LIBCACHE}/tink_streams/0.2.1/github/a38e3a97e8512830b31f1fe9476a752cf6fb51ff/src
-D tink_streams=0.3.2
# @install: lix --silent download "haxelib:/tink_streams#0.3.2" into tink_streams/0.3.2/haxelib
-lib tink_core
-cp ${HAXE_LIBCACHE}/tink_streams/0.3.2/haxelib/src
# temp for development, delete this file when pure branch merged
-D pure
-lib tink_core
-D pure
2 changes: 2 additions & 0 deletions haxe_libraries/travix.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# @run: haxelib run-dir travix .
-cp src
-lib tink_cli
-lib hx3compat
-D travix
76 changes: 38 additions & 38 deletions src/travix/Command.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package travix;

import Sys.*;
import sys.io.Process;
import travix.Os.*;

using StringTools;
using haxe.io.Path;
Expand Down Expand Up @@ -47,40 +48,25 @@ class Command {
return throw 'unreachable';
}

function tryToRun(cmd:String, ?params:Array<String>)
return try {
#if (hxnodejs && !macro)
var ret = js.node.ChildProcess.spawnSync(cmd, params);
function str(buf:js.node.Buffer)
return buf.toString();
if (ret.status == 0)
Success(str(ret.stderr) + str(ret.stdout));
else
Failure(ret.status, str(ret.stderr));
#else
var p = new Process(cmd, params);
switch p.exitCode() {
case 0:
Success(switch p.stdout.readAll().toString() {
case '': p.stderr.readAll().toString(); //some execs print to stderr
case v: v;
});
case v:
Failure(v, p.stderr.readAll().toString());
}
#end
} catch (e:Dynamic) {
Failure(404, 'Unknown command $cmd');
function tryToRun(cmd:String, ?args:Array<String>)
return
switch cmdResult(cmd, args) {
case Success({ code: 0, stdout: '', stderr: v } | { code: 0, stdout: v }):
Success(v);
case Success({ code: code, stderr: msg }):
Failure(code, msg);
case Failure(e):
Failure(e.code, e.message);
}

function run(cmd:String, ?params:Array<String>) {
function run(cmd:String, ?args:Array<String>) {
var a = [cmd];
if (params != null)
a = a.concat(params);
if (args != null)
a = a.concat(args);

print('> ${a.join(" ")} ...');
return
switch tryToRun(cmd, params) {
switch tryToRun(cmd, args) {
case Success(v):
println(' done');
v;
Expand All @@ -93,18 +79,27 @@ class Command {
}

function libInstalled(lib:String)
return tryToRun('haxelib', ['path', lib]).match(Success(_));
return tryToRun(force(which('haxelib')), ['path', lib]).match(Success(_));

function installLib(lib:String, ?version = '') {

foldOutput('installLib-$lib', function() {
if (!libInstalled(lib))
switch version {
case null | '':
exec('haxelib', ['install', lib, '--always']);
default:
exec('haxelib', ['install', lib, version, '--always']);
}
switch which('lix') {
case Success(cmd):
var arg = switch version {
case null | '': lib;
case v: '$lib#$v';
}
exec('lix', ['install', 'haxelib:$lib']);
default:
switch version {
case null | '':
exec('haxelib', ['install', lib, '--always']);
default:
exec('haxelib', ['install', lib, version, '--always']);
}
}
});
}

Expand Down Expand Up @@ -138,7 +133,7 @@ class Command {
case Some(info): args = args.concat(['-lib', info.name]);
}
if(Travix.TESTS.exists()) args.push(Travix.TESTS);

foldOutput('build-$tag', exec.bind('haxe', args));
run();
}
Expand Down Expand Up @@ -176,10 +171,15 @@ class Command {
}
#end

function exec(cmd, ?args) {
function exec(cmd, ?args:Array<String>) {
var a = [cmd];
if (args != null)
if (args != null) {
a = a.concat(args);
if (isWindows) {//this is pure madness
cmd = [cmd].concat(args.map(function (a) return '"${a.replace('"', '""')}"')).join(' ');
args = null;
}
}
println('> ' + a.join(' '));
switch command(cmd, args) {
case 0:
Expand Down
84 changes: 84 additions & 0 deletions src/travix/Os.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package travix;

using tink.CoreApi;
using StringTools;
using haxe.io.Path;

typedef CommandResult = {
var code(default, null):Int;
var stdout(default, null):String;
var stderr(default, null):String;
}

class Os {
static public var isWindows(default, never):Bool = Sys.systemName() == 'Windows';
static public function force<T>(o:Outcome<T, Error>)
return switch o {
case Success(v): v;
case Failure(e):
Sys.println(e.message);
Sys.exit(e.code);
throw 'unreachable';
}
/**
* Attempts to run a command.
* - results in failure if process creation fails
* - otherwise results in success (even for non-zero exit codes)
*/
static public function cmdResult(cmd:String, ?args:Array<String>):Outcome<CommandResult, Error>
return try {
#if (hxnodejs && !macro)
var ret = js.node.ChildProcess.spawnSync(cmd, args);
function str(buf:js.node.Buffer)
return buf.toString();
Success({
code: ret.status,
stdout: str(ret.stdout),
stderr: str(ret.stderr),
});
#else
var p = new sys.io.Process(cmd, args);

function str(buf:haxe.io.Input)
return buf.readAll().toString();

Success({
code: p.exitCode(),
stdout: str(p.stdout),
stderr: str(p.stderr),
});
#end
}
catch (e:Dynamic) {
Failure(Error.withData(404, 'could not run `$cmd ${args.join(" ")}`', e));
}

/**
* Runs a command and returns stdout if process successfully terminated with code 0.
*/
static public function cmdOutput(cmd:String, args:Array<String>):Outcome<String, Error>
return
switch cmdResult(cmd, args) {
case Success({ code: 0, stdout: v }): Success(v);
case Success(d = { code: code, stderr: v }):
Failure(Error.withData(code, 'The command `$cmd ${args.join(" ")}` exited with code $code', d));
case Failure(e): Failure(e);
}

static public function which(cmd:String) {
return switch cmdOutput(isWindows ? 'where' : 'which', [cmd]) {
case Failure(e): Failure(e);
case Success(out) if (isWindows):
var ret = Failure(new Error(404, 'could not find $cmd'));
for (l in out.split('\n'))
switch l.trim() {
case _.extension() => null | '':
case v:
ret = Success(v);
break;
}
ret;
case Success(out): Success(out.split('\n')[0]);
}
}
}
Loading

0 comments on commit fd28764

Please sign in to comment.