-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gruntfile.js
145 lines (124 loc) · 6.3 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
module.exports = function (grunt) {
var demoConfig = require('./src/demonstrators/index.js');
function getTemplateFromFile(filename, fs) {
return grunt.util._.template(fs.readFileSync(filename, 'utf8'));
}
function setTemplateSettings() {
grunt.util._.templateSettings.interpolate = /\{\{=(.+?)\}\}/g;
grunt.util._.templateSettings.evaluate = /\{\{(.+?)\}\}/g;
}
function resetTemplateSettings() {
grunt.util._.templateSettings.interpolate =/<%=([\s\S]+?)%>/g;
grunt.util._.templateSettings.evaluate = /<%([\s\S]+?)%>/g;
}
function getNonUrlDemos() {
var demos = []
.concat(demoConfig.demonstrators)
.concat(demoConfig.demonstrators_extra)
.concat(demoConfig.demonstrators_dev);
return grunt.util._.filter(demos, function (d) {
return !d.url;
});
}
var taskConfig = {
'file-creator': {
options: {
openFlags: 'w'
},
'create-template-import': {
'src/templates/list.js': function (fs, fd, done) {
var glob = grunt.file.glob;
var _ = grunt.util._;
glob('src/templates/templates/**/*.tmpl', function (err, files) {
var templates = _.map(files, function (filename) {
filename = filename.replace('src/templates/templates/', '');
var parts = filename.split('/');
var id = parts[parts.length - 1].split('.')[0];
if (parts[0] === 'datasets') {
id += '_template';
}
return {
filename: filename,
id: id
};
});
var pageTemplate = getTemplateFromFile('./grunt_templates/templateList.js.tpl', fs);
fs.writeSync(fd, pageTemplate({templates: templates}));
done();
});
}
},
'build-demos': {
files: grunt.util._.map(getNonUrlDemos(), function (demonstrator) {
var _ = grunt.util._;
var method;
if (_.has(demonstrator, 'params')) {
method = function (fs, fd, done) {
setTemplateSettings();
if (demonstrator.name && !demonstrator.params.title) {
demonstrator.params.title = demonstrator.name;
}
var d = {
name: demonstrator.name || null,
desc: demonstrator.desc || null,
image: demonstrator.image || null,
params: JSON.stringify(demonstrator.params, null, 4),
};
var pageTemplate = getTemplateFromFile('./grunt_templates/demonstratorTemplateWithParams.html.tpl', fs);
fs.writeSync(fd, pageTemplate({data: d}));
resetTemplateSettings();
done();
};
} else {
method = function (fs, fd, done) {
setTemplateSettings();
if (!demonstrator.image) {
demonstrator.image = null;
}
try {
demonstrator.desc = fs.readFileSync('demonstratorer_content/' + demonstrator.id + '.txt', 'utf8');
} catch (e) {
demonstrator.desc = '';
}
demonstrator.inline_js = fs.readFileSync('demonstratorer_content/' + demonstrator.id + '.js', 'utf8');
var pageTemplate = getTemplateFromFile('./grunt_templates/demonstratorTemplateWithCode.html.tpl', fs);
fs.writeSync(fd, pageTemplate({data: demonstrator}));
resetTemplateSettings();
done();
};
}
return {
file: 'public/demonstratorer/' + demonstrator.id + '.html',
method: method
};
})
}
},
watch: {
'create-template-import': {
files: ['templates/**/*.tmpl'],
tasks: ['create-template-import'],
options: {
atBegin: true,
spawn: false
}
},
'build-demos': {
files: ['demonstratorer_content/**/*.*', './src/demonstrators/index.js'],
tasks: ['build-demos'],
options: {
atBegin: true,
spawn: false
}
}
}
};
grunt.initConfig(grunt.util._.extend(taskConfig));
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-file-creator');
grunt.registerTask('create-template-import', ['file-creator:create-template-import']);
grunt.registerTask('build-demos', ['file-creator:build-demos']);
grunt.registerTask('watch-create-template-import', ['watch:create-template-import']);
grunt.registerTask('watch-build-demos', ['watch:build-demos']);
};