forked from CodenameCrew/hscript-improved
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunScript.hx
More file actions
154 lines (136 loc) · 3.7 KB
/
RunScript.hx
File metadata and controls
154 lines (136 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package;
import haxe.*;
import hscript.*;
import sys.*;
import sys.io.*;
import sys.net.*;
#if neko
import neko.Lib;
#end
#if cpp
import cpp.Lib;
#end
class RunScript {
public static function main () {
var args = Sys.args ();
var workingDirectory = args.pop();
try {
Sys.setCwd(workingDirectory);
} catch (e:Dynamic) {
error("Failed to set current working directory to [" + workingDirectory + "]");
}
if (args.length == 0) {
error("Argument missing. Expected 1 got 0");
}
if (args[0] == "-f" || args[0] == "--file") {
if (args.length == 1) {
error("Argument missing. Expected 2 got 1");
}
if (args.length > 2) {
error("Too many arguments. Expected 2 got " + args.length);
}
executeScriptFile(args[1]);
}
if (args[0] == "--help") {
printHelp();
}
executeScript(args.join(" "));
}
static function executeScriptFile(scriptPath:String) {
if (!FileSystem.exists(scriptPath)) {
error("Specified file [" + scriptPath + "] not found");
}
if (FileSystem.isDirectory(scriptPath)) {
error("Specified file [" + scriptPath + "] is a directory");
}
var script = File.getContent(scriptPath);
executeScript(script);
}
static function executeScript(script:String) {
var parser = new hscript.Parser();
var program = parser.parseString(script);
var interp = new hscript.Interp();
// export some useful classes
interp.variables.set("Array", Array);
interp.variables.set("DateTools", DateTools);
interp.variables.set("Math", Math);
interp.variables.set("StringTools", StringTools);
interp.variables.set("Sys", Sys);
interp.variables.set("Xml", Xml);
interp.variables.set("sys", {
"FileSystem": sys.FileSystem,
"io": {
"File": sys.io.File
},
"net": {
"Host": sys.net.Host
}
});
interp.variables.set("haxe", {
"Json": haxe.Json,
"Http": haxe.Http,
"Serializer": haxe.Serializer,
"Unserializer": haxe.Unserializer
});
info(interp.execute(program));
}
static function printHelp(exit:Bool = true) {
var hscriptPath = getHaxelibPath("hscript");
var meta:Dynamic = Json.parse(File.getContent(hscriptPath + "haxelib.json"));
info('${meta.name} v${meta.version}');
info('${meta.description}');
info("");
info("usage: haxelib run hscript SCRIPT");
info(" or: haxelib run hscript -f SCRIPTPATH");
info(" or: haxelib run hscript --file SCRIPTPATH");
info("");
info("examples:");
info(" haxelib run hscript var x = 4; 1 + 2 * x");
info(" 9");
info("");
info(" haxelib run hscript \"5 | 6\"");
info(" 7");
info("");
info(" haxelib run hscript for(i in 0...5) Sys.stdout().writeString(i + ', '); 'done'");
info(" 0, 1, 2, 3, 4, done");
info("");
info(" haxelib run hscript sys.net.Host.localhost()");
info(" mycomputer");
info("");
if(Sys.systemName() == "Windows") {
info(" for /f %i in ('haxelib run hscript \"new sys.net.Host('')\"') do @set MY_IP=%i");
info(" echo %MY_IP%");
} else {
info(" MY_IP=$(haxelib run hscript \"new sys.net.Host('')\"')");
info(" echo $MY_IP");
}
info(" 192.168.248.1");
if(exit)
Sys.exit(0);
}
static function info(msg:Dynamic) {
Sys.stdout().writeString("" + msg + "\n");
}
static function error(msg:Dynamic, exit:Bool = true) {
Sys.stderr().writeString("Error: " + msg + "!\n");
if(exit)
Sys.exit(1);
}
static function getHaxelibPath(libraryName:String):String {
var proc = new Process("haxelib", ["path", libraryName]);
var result = "";
var ex:Dynamic = null;
try {
while(true) {
var line = proc.stdout.readLine();
if (line.substr(0, 1) != "-")
result = line;
}
} catch (e:Dynamic) {
ex = e;
};
proc.close();
if (ex) error(ex);
return result;
}
}