forked from HaxeFoundation/as3hx
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Run.hx
125 lines (110 loc) · 4.38 KB
/
Run.hx
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
using StringTools;
import as3hx.Writer;
import sys.FileSystem;
import sys.io.File;
class Run {
static function errorString( e : as3hx.Parser.Error ) {
return switch(e) {
case EInvalidChar(c): "Invalid char '" + String.fromCharCode(c)+"' 0x"+StringTools.hex(c,2);
case EUnexpected(src): "Unexpected " + src;
case EUnterminatedString: "Unterminated string";
case EUnterminatedComment: "Unterminated comment";
case EUnterminatedXML: "Unterminated XML";
}
}
static function loop( src: String, dst : String, excludes: List<String> ) {
if (src == null) {
Sys.println("source path cannot be null");
}
if (dst == null) {
Sys.println("destination path cannot be null");
}
var subDirList : Array<String> = new Array<String>();
var writer = new Writer(cfg);
for( f in FileSystem.readDirectory(src) ) {
var srcChildAbsPath : String = src + "/" + f;
var dstChildAbsPath : String = dst + "/" + f;
if ( FileSystem.isDirectory(srcChildAbsPath) ) {
subDirList.push(f);
}
else
if( f.endsWith(".as") && !isExcludeFile(excludes, srcChildAbsPath) ) {
var file = srcChildAbsPath;
Sys.println("source AS3 file: " + file);
var p = new as3hx.Parser(cfg);
var content = sys.io.File.getContent(file);
var program = try p.parseString(content,src,f) catch( e : as3hx.Parser.Error ) {
#if macro
sys.io.File.stderr().writeString(file+":"+p.line+": "+errorString(e)+"\n");
#end
if(cfg.errorContinue) {
errors.push("In " + file + "("+p.line+") : " + errorString(e));
continue;
}
else
neko.Lib.rethrow("In " + file + "("+p.line+") : " + errorString(e));
}
var out = dst;
ensureDirectoryExists(out);
var name = out + "/" + Writer.properCase(f.substr(0, -3),true) + ".hx";
Sys.println("target HX file: " + name);
var fw = File.write(name, false);
warnings.set(name, writer.process(program, fw));
fw.close();
}
}
for (name in subDirList) {
loop((src + "/" + name), (dst + "/" + name), excludes);
}
}
static function isExcludeFile(excludes: List<String>, file: String)
return Lambda.filter(excludes, function (path) return as3hx.Config.toPath(file).indexOf(path.replace(".", "/")) > -1).length > 0;
static var errors : Array<String> = new Array();
static var warnings : Map<String,Map<String,Bool>> = new Map();
static var cfg : as3hx.Config;
public static function main() {
cfg = new as3hx.Config();
loop(cfg.src, cfg.dst, cfg.excludePaths);
Sys.println("");
Writer.showWarnings(warnings);
Sys.println("");
if(errors.length > 0) {
Sys.println("ERRORS: These files were not written due to source parsing errors:");
for(i in errors)
Sys.println(i);
}
}
static function ensureDirectoryExists(dir : String)
{
var tocreate = [];
while (!FileSystem.exists(dir) && dir != '')
{
var parts = dir.split("/");
tocreate.unshift(parts.pop());
dir = parts.join("/");
}
for (part in tocreate)
{
if (part == '')
continue;
dir += "/" + part;
try {
FileSystem.createDirectory(dir);
} catch (e : Dynamic) {
throw "unable to create dir: " + dir;
}
}
}
static var reabs = ~/^([a-z]:|\\\\|\/)/i;
public static function directory(dir : String, alt = ".")
{
if (null == dir)
dir = alt;
if( dir.endsWith("/") || dir.endsWith("\\") )
dir = dir.substr(0, -1);
if (!reabs.match(dir))
dir = Sys.getCwd() + dir;
dir = StringTools.replace(dir, "\\", "/");
return dir;
}
}