more debuggable #extern

This commit is contained in:
2021-08-12 23:12:10 -06:00
parent d1254f5a7f
commit 6bfa807ac4
3 changed files with 42 additions and 12 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
bin/ bin/
externLog.txt

View File

@@ -864,15 +864,22 @@ class Macros {
} }
var externExps = [ var externExps = [
b.let([b.symbol("__args__"), b.callSymbol("Sys.args", [])], b.let([b.symbol("__args__"), b.callSymbol("Sys.args", [])], [
[b.print( b.callSymbol("set", [
b.symbol("Prelude.printStr"),
b.symbol("Prelude._externPrintStr")
]),
b.callSymbol("Prelude._printStr", [
b.callSymbol("tink.Json.stringify", [ b.callSymbol("tink.Json.stringify", [
b.the(bodyType, if (bindingList.length > 0) { b.the(bodyType, if (bindingList.length > 0) {
b.let(parseBindingList, exps); b.let(parseBindingList, exps);
} else { } else {
b.begin(exps); b.begin(exps);
}) })
]))]) ])
]),
b.callSymbol("Sys.exit", [b.symbol("0")])
])
]; ];
b.the( b.the(
bodyType, bodyType,

View File

@@ -13,6 +13,7 @@ import sys.io.Process;
#end #end
#if (sys || hxnodejs) #if (sys || hxnodejs)
import sys.FileSystem; import sys.FileSystem;
import sys.io.File;
#end #end
#if python #if python
import python.lib.subprocess.Popen; import python.lib.subprocess.Popen;
@@ -334,7 +335,7 @@ class Prelude {
return f; return f;
} }
public static dynamic function printStr(s:String) { public static function _printStr(s:String) {
#if (sys || hxnodejs) #if (sys || hxnodejs)
Sys.println(s); Sys.println(s);
#else #else
@@ -342,6 +343,21 @@ class Prelude {
#end #end
} }
#if (sys || hxnodejs)
static var externLogFile = "externLog.txt";
public static function _externPrintStr(s:String) {
var logContent = try {
File.getContent(externLogFile);
} catch (e) {
"";
}
File.saveContent(externLogFile, '${logContent}${s}\n');
}
#end
public static var printStr:(String) -> Void = _printStr;
public static function print<T>(v:T, label = ""):T { public static function print<T>(v:T, label = ""):T {
var toPrint = label; var toPrint = label;
if (label.length > 0) { if (label.length > 0) {
@@ -497,9 +513,9 @@ class Prelude {
} }
} }
if (fullProcess) { var output = if (fullProcess) {
if (p.wait() == 0) { if (p.wait() == 0) {
return p.stdout.readall().decode().trim(); p.stdout.readall().decode().trim();
} else { } else {
throw 'process $command $args failed:\n${p.stdout.readall().decode().trim() + p.stderr.readall().decode().trim();}'; throw 'process $command $args failed:\n${p.stdout.readall().decode().trim() + p.stderr.readall().decode().trim();}';
} }
@@ -507,8 +523,10 @@ class Prelude {
// The haxe extern for FileIO.readline() says it's a string, but it's not, it's bytes! // The haxe extern for FileIO.readline() says it's a string, but it's not, it's bytes!
var bytes:Dynamic = p.stdout.readline(); var bytes:Dynamic = p.stdout.readline();
var s:String = bytes.decode(); var s:String = bytes.decode();
return s.trim(); s.trim();
} }
p.terminate();
return output;
#elseif sys #elseif sys
var p = new Process(command, args); var p = new Process(command, args);
if (inputLines != null) { if (inputLines != null) {
@@ -516,30 +534,34 @@ class Prelude {
p.stdin.writeString('$line\n'); p.stdin.writeString('$line\n');
} }
} }
if (fullProcess) { var output = if (fullProcess) {
if (p.exitCode() == 0) { if (p.exitCode() == 0) {
return p.stdout.readAll().toString().trim(); p.stdout.readAll().toString().trim();
} else { } else {
throw 'process $command $args failed:\n${p.stdout.readAll().toString().trim() + p.stderr.readAll().toString().trim()}'; throw 'process $command $args failed:\n${p.stdout.readAll().toString().trim() + p.stderr.readAll().toString().trim()}';
} }
} else { } else {
return p.stdout.readLine().toString().trim(); p.stdout.readLine().toString().trim();
} }
p.kill();
p.close();
return output;
#elseif hxnodejs #elseif hxnodejs
var p = if (inputLines != null) { var p = if (inputLines != null) {
ChildProcess.spawnSync(command, args, {input: inputLines.join("\n")}); ChildProcess.spawnSync(command, args, {input: inputLines.join("\n")});
} else { } else {
ChildProcess.spawnSync(command, args); ChildProcess.spawnSync(command, args);
} }
switch (p.status) { var output = switch (p.status) {
case 0: case 0:
var output:Buffer = p.stdout; var output:Buffer = p.stdout;
return output.toString(); output.toString();
default: default:
var output:Buffer = p.stdout; var output:Buffer = p.stdout;
var error:Buffer = p.stderr; var error:Buffer = p.stderr;
throw 'process $command $args failed:\n${output.toString() + error.toString()}'; throw 'process $command $args failed:\n${output.toString() + error.toString()}';
} }
return output;
#else #else
throw "Can't run a subprocess on this target."; throw "Can't run a subprocess on this target.";
#end #end