-
Notifications
You must be signed in to change notification settings - Fork 19
/
Cakefile
157 lines (138 loc) · 4.89 KB
/
Cakefile
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
155
156
157
fs = require 'fs'
path = require 'path'
{exec} = require 'child_process'
option '-o', '--output [DIR]', 'directory for compiled code'
task 'benchmark', 'Run a simple benchmark of Milk', ->
sys = require 'sys'
Milk = require 'milk'
tmpl = """
<h1>{{header}}</h1>
{{#list.length}}
<ul>
{{#list}}
{{#current}}
<li><strong>{{name}}</strong></li>
{{/current}}
{{^current}}
<li>a href="{{url}}">{{name}}</a></li>
{{/current}}
{{/list}}
</ul>
{{/list.length}}
{{^list.length}}
<p>The list is empty.</p>
{{/list.length}}
"""
start = new Date()
process.addListener 'exit', ->
sys.error "Time taken: #{ (new Date() - start) / 1000 } secs"
for i in [0..1000000]
Milk.render tmpl,
header: "Colors"
list: [
{ name: "red", url: "#Red", current: yes }
{ name: "green", url: "#Green", current: no }
{ name: "blue", url: "#Blue", current: no }
]
task 'build', 'Rebuilds all public web resources', ->
invoke('build:js')
invoke('build:docs')
invoke('spec:html')
task 'build:js', 'Builds Milk into ./pages (or --output)', (opts) ->
CoffeeScript = require 'coffee-script'
out = opts.output or 'pages'
out = path.join(__dirname, out) unless out[0] = '/'
fs.readFile path.join(__dirname, 'milk.coffee'), 'utf8', (err, data) ->
throw err if err
fs.writeFile path.join(out, 'milk.js'), CoffeeScript.compile(data)
task 'build:docs', 'Builds documentation with Docco', ->
chain = (commands...) ->
exec commands.shift(), (err) ->
throw err if err
chain commands... if commands.length
chain 'docco milk.coffee',
'mv docs/milk.html pages/index.html',
'rm -r docs',
task 'spec:node', 'Creates compliance tests for the Mustache spec in Vows', ->
readSpecs (file, json) ->
test = """
vows = require('vows');
equal = require('assert').equal;
Milk = require('../');
suite = vows.describe('Mustache Specification - #{file}');
tests = #{json}['tests'];
var batch = {};
for (var i = 0; i < tests.length; i++) {
(function(test) {
var context = { "topic": #{topic} };
context[test.desc] = function(r) { equal(r, test.expected) };
batch[test.name] = context;
})(tests[i]);
}
suite.addBatch(batch);
suite.export(module);
"""
testFile = file.replace(/^~/, '').replace(/\.yml$/, '_spec.js')
fs.writeFile path.join(__dirname, 'test', testFile), test
task 'spec:html', 'Creates compliance tests for the Mustache spec in HTML', ->
invoke('build:js')
spec = (file, json) ->
"""
describe("Mustache Specification - #{file}", function() {
var tests = #{json}['tests'];
for (var i = 0; i < tests.length; i++) {
it(tests[i].desc, buildTest(tests[i]));
}
});
"""
readSpecs spec, (specs) ->
lib = 'https://github.com/pivotal/jasmine/raw/1.0.1-release/lib'
fs.writeFile path.join(__dirname, 'pages', 'compliance.html'),
"""
<html>
<head>
<link rel="stylesheet" type="text/css" href="#{lib}/jasmine.css" />
<script type="text/javascript" src="#{lib}/jasmine.js"></script>
<script type="text/javascript" src="#{lib}/jasmine-html.js"></script>
<script type="text/javascript" src="milk.js"></script>
</head>
<body>
<script type="text/javascript">
function buildTest(test) {
return function() { expect((#{topic})()).toEqual(test.expected) };
}
#{specs.sort().join('\n')}
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
jasmine.getEnv().execute();
</script>
</body>
</html>
"""
readSpecs = (callback, allDone = (->)) ->
# Convert the YAML files to Javascript.
# Requires the YAML and JSON Ruby libraries.
ruby = '''
ruby -rubygems
-e 'require "yaml"'
-e 'require "json"'
-e 'YAML::add_builtin_type("code") do |_,value|
value["js"].tap do |x|
def x.to_json(_)
"function() { return #{self}; }"
end
end
end'
-e 'print YAML.load_file(ARGV[0]).to_json()'
'''.replace(/\n/gm, ' ')
results = []
dir = path.join(__dirname, 'ext', 'spec', 'specs')
for file in (files = fs.readdirSync(dir))
continue unless file.match(/\.yml$/)
do (file) ->
exec "#{ruby} -- #{path.join(dir, file)}", (err, stdout, stderr) ->
throw err if err
results.push(callback(file, stdout))
allDone(results) if (files.length == results.length)
topic = ->
try Milk.render(test.template, test.data, test.partials || {})
catch e then "ERROR: " + e