-
Notifications
You must be signed in to change notification settings - Fork 3
/
make
203 lines (168 loc) · 4.99 KB
/
make
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env node
path = require('path');
showdown = require('./docs/assets/showdown.min.js');
packageJson = require('./package.json');
require('shelljs/make');
// writes an array of files join it and writes
// the result to a targetFile
function writeToDist(files, targetFile) {
var
header = '/*\n' + cat('LICENSE') + '*/\n',
content = cat(files);
(header + content).to(targetFile + '.js');
}
// find the firts <h1> tag an uses its content
// to set <title> tag inside page body
function setPageTitle(html) {
var parsed, title = '';
parsed = /<h1 ([\s\S]+)?>([\w\s]+)?<\/h1>/.exec(html);
if (parsed) {
title = parsed[2] || '';
}
return html.replace(/<title>([\w\s]+)?<\/title>/, '<title>Cor - ' + title + '</title>');
}
// takes HTML source as input, regarding h1, h2, h3... tags
// it generates a "table of contents"
function generateTableOfContents(html) {
var toc = '<h2>Table of Content</h2>';
html.replace(/(?:\<h)(\d)\s*(?:id\=")(\w*)(?:"\>)([\s\S]*?)(?:\<\/h)/g, function() {
var
level = arguments[1],
id = arguments[2],
text = arguments[3];
if (level != 1) {
toc += '<a class="toclink toclink-'+ level +'" href="#' + id + '"' + '>' + text + '</a>';
}
});
return toc;
}
// clean and build the project including the parser,
// distribution packages and documentation
// usage: make
target.all = function() {
target.clean();
target.parser();
target.dist();
target.docs();
target.test();
}
// runs all clean tasks
// usage: make clean
target.clean = function() {
target.cleandist();
target.cleandocs();
target.cleantest();
target.cleanparser();
}
// clean `dist` directory
// usage: make cleandist
target.cleandist = function() {
echo(' + cleaning dist');
rm('-rf', 'dist');
}
// removes all .html files inside
// `docs` directory
// usage: make cleandocs
target.cleandocs = function() {
echo(' + cleaning doc');
rm('-rf', 'docs/*.html');
}
// clean `dist` directory
// usage: make cleandist
target.cleantest = function() {
echo(' + cleaning test');
rm('-rf', 'test/test*.js');
}
// remove the `parser`
// usage: make cleanparser
target.cleanparser = function() {
echo(' + removing parser');
rm('-rf', 'src/parser.js');
}
// to run test from nodejs
// usage: make test
target.test = function() {
echo(' + testing');
exec(__dirname + '/bin/cor ' + __dirname + '/test/node.cor');
}
// generates the parser using Jison(jison.org) library
// usage: make parser
// for debugging: make parser -- debug
target.parser = function(debug) {
echo(' + generating parser');
var
gen, parserSrc,
opts = {
debug: debug == 'debug' || false,
type: 'lalr',
moduleName: 'CorParser',
moduleType: 'js'
},
fs = require('fs'),
jison = require('jison'),
bnf = require('jison/node_modules/ebnf-parser'),
lex = require('jison/node_modules/lex-parser'),
grammar = bnf.parse(fs.readFileSync('./src/bnf/cor.y', 'utf8')),
lexer = lex.parse(fs.readFileSync('./src/bnf/cor.l', 'utf8'));
grammar.lex = lexer;
gen = new jison.Generator(grammar, opts);
parserSrc = gen.generate(opts);
parserSrc += '\ncor.Parser = CorParser.Parser; delete CorParser;';
fs.writeFileSync('./src/parser.js', parserSrc);
}
// build and create distribution packages
// and writes to ./dist/*
// usage: make dist
target.dist = function() {
target.cleandist();
echo(' + creating distribution packages');
var
files = [
'src/crl.js',
'src/cor.js',
'src/class.js',
'src/parser.js',
'src/scope/env.js',
'src/scope/scope.js',
'src/compiler.js',
'src/sourcemap.js',
'src/loader/path.js',
'src/loader/loader.js',
'src/loader/browser.js',
'src/loader/plugins.js',
];
if (! test('-d', 'dist')){
mkdir('dist');
}
writeToDist(files, 'dist/cor');
writeToDist('src/crl.js', 'dist/crl');
}
// generate project documentation
// usage: make docs
target.docs = function() {
target.cleandocs();
var
i, len, outFile, converter,
html, outPath,
prefix = cat('docs/assets/docs.prefix'),
suffix = cat('docs/assets/docs.suffix'),
files = ls('docs/*.md');
echo(' + generating doc');
for (i = 0, len = files.length; i < len; i++) {
converter = new showdown.Converter();
outFile = path.basename(files[i], '.md') + '.html';
html = converter.makeHtml(cat(files[i]));
if (files[i] === 'docs/documentation.md') {
html = html.replace('<toc/>', generateTableOfContents(html));
}
html = prefix + html + suffix;
outPath = path.normalize('./docs/' + outFile);
setPageTitle(html).to(outPath);
echo(' ' + outPath);
}
}
// run tests
target.test = function() {
echo(' + testing')
exec('node ./bin/cor run ./test/node.cor');
}