Skip to content

Commit 4c8aecd

Browse files
committed
add haxelib run support
1 parent b7600ca commit 4c8aecd

11 files changed

+118
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/Main.hx
33
/bin
44
/release.zip
5+
/test/test_output.html

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ install:
5151
- haxelib install buddy
5252
- haxelib install utest
5353
- haxelib install hxnodejs
54+
# needed for haxelib run test
55+
- haxelib dev hxmustache .
5456
- haxelib list
5557
- git clone https://github.com/mustache/spec
5658

run.hxml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-cp src
2+
-main mustache.Run
3+
-neko run.n

run.n

52.3 KB
Binary file not shown.

src/mustache/Run.hx

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package mustache;
2+
3+
class Run {
4+
static function main() {
5+
var args = Sys.args();
6+
7+
if (Sys.getEnv("HAXELIB_RUN") != null)
8+
Sys.setCwd(args.pop());
9+
10+
try {
11+
var partialIdx = -1;
12+
var partialPaths = [];
13+
while ((partialIdx = args.indexOf("-p")) != -1) {
14+
var parts = args.splice(partialIdx, 2);
15+
if (parts.length < 2)
16+
throw "Missing filename for `-p` argument";
17+
partialPaths.push(parts[1]);
18+
}
19+
20+
if (args.length < 2)
21+
usage();
22+
23+
var viewFile = args[0];
24+
var viewContent = try sys.io.File.getContent(viewFile) catch(e:Dynamic) throw 'Cannot open view `$viewFile`';
25+
var view:{} = try haxe.Json.parse(viewContent) catch (e:Dynamic) throw 'Cannot parse view `$viewFile`: $e';
26+
27+
var templateFile = args[1];
28+
var templateContent = try sys.io.File.getContent(templateFile) catch(e:Dynamic) throw 'Cannot open template `$templateFile`';
29+
try Mustache.parse(templateContent) catch (e:Dynamic) throw 'Cannot parse template `$templateFile`: $e';
30+
31+
var partials = new haxe.DynamicAccess();
32+
for (path in partialPaths) {
33+
var partialContent = try sys.io.File.getContent(path) catch(e:Dynamic) throw 'Cannot open partial `$path`';
34+
try Mustache.parse(partialContent) catch (e:Dynamic) throw 'Cannot parse partial `$path`: $e';
35+
var p = new haxe.io.Path(path);
36+
partials[p.file] = partialContent;
37+
}
38+
39+
var output = Mustache.render(templateContent, view, partials);
40+
var outputFile = args[2];
41+
if (outputFile == null)
42+
Sys.print(output);
43+
else
44+
sys.io.File.saveContent(outputFile, output);
45+
} catch (e:Dynamic) {
46+
Sys.println('ERROR: $e');
47+
Sys.exit(1);
48+
}
49+
}
50+
51+
static function usage() {
52+
Sys.println("Usage: haxelib run hxmustache <view.json> <template.mustache> [-p partial.mustache]* [output]");
53+
Sys.exit(1);
54+
}
55+
}

test/TestMain.hx

+3
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ class TestMain implements buddy.Buddy<[
55
#if (!flash && (!js || hxnodejs))
66
TestSpec,
77
#end
8+
#if neko
9+
TestRun,
10+
#end
811
]> {}

test/TestRun.hx

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import utest.Assert;
2+
3+
class TestRun extends buddy.BuddySuite {
4+
public function new() {
5+
super();
6+
describe("haxelib run hxmustache", function() {
7+
it("should render using given arguments", function() {
8+
var args = [
9+
"run",
10+
"hxmustache",
11+
"test/test_view.json",
12+
"test/test_template.mustache",
13+
"-p", "test/test_layout.mustache"
14+
];
15+
var proc = new sys.io.Process("haxelib", args);
16+
var exitCode = proc.exitCode();
17+
var stdout = StringTools.replace(proc.stdout.readAll().toString(), "\r\n", "\n");
18+
var expected = sys.io.File.getContent("test/test_expected.html");
19+
Assert.equals(0, exitCode);
20+
Assert.equals(expected, stdout);
21+
});
22+
it("should render to file using given arguments", function() {
23+
var args = [
24+
"run",
25+
"hxmustache",
26+
"test/test_view.json",
27+
"test/test_template.mustache",
28+
"-p", "test/test_layout.mustache",
29+
"test/test_output.html"
30+
];
31+
var proc = new sys.io.Process("haxelib", args);
32+
var exitCode = proc.exitCode();
33+
var stdout = StringTools.replace(proc.stdout.readAll().toString(), "\r\n", "\n");
34+
var expected = sys.io.File.getContent("test/test_expected.html");
35+
var actual = sys.io.File.getContent("test/test_output.html");
36+
Assert.equals(0, exitCode);
37+
Assert.equals(expected, actual);
38+
Assert.equals("", stdout);
39+
});
40+
});
41+
}
42+
}

test/test_expected.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<html>
2+
Hello, World!
3+
</html>

test/test_layout.mustache

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<html>
2+
{{$content}}Default{{/content}}
3+
</html>

test/test_template.mustache

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{<test_layout}}
2+
{{$content}}Hello, {{who}}!{{/content}}
3+
{{/test_layout}}

test/test_view.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"who": "World"
3+
}

0 commit comments

Comments
 (0)