-
Notifications
You must be signed in to change notification settings - Fork 86
/
Gruntfile.js
173 lines (155 loc) · 5.01 KB
/
Gruntfile.js
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
'use strict';
var _ = require('lodash');
var open = require('open');
var child_process = require('child_process');
var path = require('path');
var fs = require('fs');
function isCodeGenFile(fn) {
return isPlainJsFile(fn) && fs.existsSync(adSource(fn));
}
function isPlainJsFile(fn) {
return path.extname(fn) === '.js' &&
path.extname(path.parse(fn).name) !== '.ad';
}
function adSource(fn) {
return fn.slice(0, -3) + '.ad.js';
}
module.exports = function(grunt) {
grunt.initConfig({
nodeunit: {
all: ['tests/test-*.js']
},
eslint: {
lib: {
src: [
'Gruntfile.js',
'src/**/*.js'
],
filter: _.negate(isCodeGenFile)
},
test: {
src: ['tests/**/*.js']
},
wppl: {
src: [
'src/header.wppl',
'examples/*.wppl',
'tests/test-data/**/*.wppl'
],
options: {
configFile: '.eslintrc.wppl.js'
}
},
options: {fix: "<%= grunt.option('fix') %>"}
},
jshint: {
all: {
src: [
'Gruntfile.js',
'src/header.wppl',
'src/**/*.js',
'tests/**/*.js'
],
filter: _.negate(isCodeGenFile)
},
options: {
maxerr: 500,
camelcase: true,
nonew: true,
curly: true,
noarg: true,
trailing: true,
forin: true,
noempty: true,
node: true,
eqeqeq: true,
strict: false,
evil: true,
undef: true,
bitwise: true,
browser: true,
gcl: true,
newcap: false
}
},
clean: ['bundle/*.js'],
watch: {
ad: {
files: ['**/*.ad.js'],
tasks: ['build-ad']
},
dist: {
files: ['src/dists.ad.js'],
tasks: ['build-dist-header']
}
}
});
function browserifyArgs(args) {
var pkgArg = '';
var requires = _.chain(_.toArray(args))
.map(function(name) { return ['--require', name]; })
.flatten().value();
pkgArg = ' -t [' + ['./src/bundle.js'].concat(requires).join(' ') + ']';
// We don't want to browserify the mongodb package, so we mark it as "external"
return pkgArg + ' -t brfs src/browser.js -o bundle/webppl.js -x mongodb';
}
grunt.loadNpmTasks('grunt-eslint');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['eslint', 'nodeunit']);
grunt.registerTask('test', ['nodeunit']);
grunt.registerTask('lint', ['eslint']);
grunt.registerTask('hint', ['jshint']);
grunt.registerTask('travis-phantomjs', ['bundle', 'test-phantomjs']);
grunt.registerTask('build-ad', function() {
var output = child_process.execSync('scripts/adify');
grunt.log.writeln(output);
});
grunt.registerTask('build-dist-header', function() {
var output = child_process.execSync('scripts/distHeader');
grunt.log.writeln(output);
});
grunt.registerTask('build', 'Build WebPPL', ['build-ad', 'build-dist-header']);
grunt.registerTask('build-watch', 'Run the build task on fs changes.', ['watch']);
grunt.registerTask('bundle', 'Create browser bundle.', function() {
var taskArgs = (arguments.length > 0) ? ':' + _.toArray(arguments).join(':') : '';
grunt.task.run('browserify' + taskArgs, 'uglify');
});
grunt.registerTask('browserify', 'Generate "bundle/webppl.js".', function() {
child_process.execSync('mkdir -p bundle');
child_process.execSync('browserify' + browserifyArgs(arguments));
});
grunt.registerTask('browserify-watch', 'Run the browserify task on fs changes.', function() {
var done = this.async();
child_process.execSync('mkdir -p bundle');
var args = '-v' + browserifyArgs(arguments);
var p = child_process.spawn('watchify', args.split(' '));
p.stdout.on('data', grunt.log.writeln);
p.stderr.on('data', grunt.log.writeln);
p.on('close', done);
});
grunt.registerTask('uglify', 'Generate "bundle/webppl.min.js".', function() {
child_process.execSync('mkdir -p bundle');
child_process.execSync('uglifyjs bundle/webppl.js -b ascii_only=true,beautify=false > bundle/webppl.min.js');
});
grunt.registerTask('test-browser', 'Run browser tests in default browser.', function() {
open('tests/browser/index.html', process.env.BROWSER);
});
grunt.registerTask('test-phantomjs', 'Run browser tests in phantomjs.', function() {
var timeout = 10; // seconds
try {
var cmd = 'phantomjs node_modules/qunit-phantomjs-runner/runner-list.js tests/browser/index.html ' + timeout;
var output = child_process.execSync(cmd);
grunt.log.writeln(output);
} catch (e) {
grunt.log.writeln(e.output.join('\n'));
throw e;
}
});
grunt.registerTask('generate-docs', 'Generate documentation.', function() {
var output = child_process.execSync('scripts/distributionDocs > docs/primitive-distributions.txt');
grunt.log.writeln(output);
});
};