-
Notifications
You must be signed in to change notification settings - Fork 16
/
Gruntfile.js
269 lines (237 loc) · 9.2 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* Grunt file for the WordPress plugin Arlima
*
* @usage
*
* $ grunt Creates a new release version, the version number will automatically
* be increased (add --new-verions="3.0.dev" to define your own version)
* $ grunt change-version Bump up version number, also supports --new-version
* $ grunt localization Translates pot files
* $ grunt phpunit Runs php-unit tests
* $ grunt validate-readme Validates the readme file
* $ grunt build-js Concatenate and minify all js-files in js/arlima/dev/ into arlima.js
* $ grunt create-release Copies current source code into the release directory
*
* @requirements
* - nodejs and npm
* - grunt has to be installed globally (npm install -g grunt-cli)
* - msgfmt and phpunit.phar has to be installed and added to your $PATH
*
* @todo
* - Look into using https://npmjs.org/package/node-gettext instead of msgfmt
* - Make it optional to run unit tests when building release
* - Make the translation task more dynamic, no hardcoded en -> sv
*/
module.exports = function(grunt) {
var fs = require('fs'),
sys = require('sys'),
wrench = require('./node_modules/wrench'),
mval = require('./node_modules/mval'),
exec = require('child_process').exec,
readFile = function(file) {
return fs.readFileSync(file, 'utf-8');
},
replaceInFile = function(path, from, to) {
fs.writeFileSync(path, readFile(path).replace(from, to));
},
getCurrentVersion = function() {
var versionParts = readFile(config.mainScript).split('Version: ')[1].split('\n')[0].trim().split('.');
return versionParts.join('.');
},
handleProcessError = function(grunt, stderr, error, stdout) {
var errorMess = error || stderr;
if( errorMess ) {
grunt.log.write(errorMess).error(stdout);
grunt.fail.warn(errorMess, 3);
return true;
} else {
return false;
}
},
config = JSON.parse(readFile('./package.json')).gruntConfig;
config.releaseVersion = false;
/* * * * * * * * Config * * * * * * * * */
grunt.initConfig({
config : config,
concat: {
options: {
stripBanners: true,
banner: '/*! Arlima v<%= config.releaseVersion %> */\n'
},
dist: {
files: config.filesToConcat
}
},
uglify: {
options: {
banner: '/*! Arlima v<%= config.releaseVersion %> */\n'
},
build : {
files : config.uglifyjs
}
},
less: {
development: {
options: {
compress: true,
yuicompress: true,
optimization: 2
},
files: config.lessFiles
}
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-less');
/* * * * * * * * Tasks * * * * * * * * */
/*
* Get current version
*/
grunt.registerTask('current-version', 'Get current version', function() {
config.releaseVersion = getCurrentVersion();
grunt.log.writeln('Current version: ' + getCurrentVersion());
});
/*
* Change to new version or the next version number in all files
* containing the version definition
*/
grunt.registerTask('change-version', 'Bump up the version number, or change version name by adding --new-version=3.1.0', function() {
var currentVersion = getCurrentVersion(),
newVersion = grunt.option('new-version');
if( !newVersion ) {
var versionParts = currentVersion.split('.');
var newSubVersion = parseInt(versionParts.splice(versionParts.length-1, 1)[0]) + 1;
newSubVersion = newSubVersion < 10 && newSubVersion > 0 ? '0'+newSubVersion : newSubVersion.toString();
newVersion = versionParts.join('.') + '.' + newSubVersion;
}
config.releaseVersion = newVersion;
grunt.log.writeln('* Moving from version '+currentVersion+' to '+newVersion);
replaceInFile(config.mainScript, 'Version: '+currentVersion, 'Version: '+newVersion);
replaceInFile('readme.txt', 'Stable tag: '+currentVersion, 'Stable tag: '+newVersion);
replaceInFile('constants.php', "'ARLIMA_PLUGIN_VERSION', '"+currentVersion, "'ARLIMA_PLUGIN_VERSION', '"+newVersion);
});
/*
* Validate our javascripts
*/
grunt.registerTask('validate-js', "Check that we're not doing anything wrong in our javascripts", function() {
for(var x in config.filesToConcat ) {
Object.keys(config.filesToConcat[x]).every(function(i) {
var fileName = config.filesToConcat[x][i],
filePath = __dirname +'/'+ fileName,
code = readFile(filePath);
if( fileName != 'js/arlima/dev/ArlimaUtils.js' && code.indexOf('console.') > -1 ) {
throw new Error('Javascript '+fileName+' invoked the console object, you must remove it to build the scripts!');
}
return true;
});
}
});
/*
* Build javascript
*/
grunt.registerTask('build-js', ['validate-js', 'current-version', 'concat', 'uglify', 'change-version']);
/*
* Create class documentation
*/
var docsPath = 'classes/docs.md';
grunt.registerTask('create-docs', 'Create markdown-formatted class documentation in '+docsPath, function() {
var bin = __dirname+'/vendor/victorjonsson/markdowndocs/bin/phpdoc-md',
done = this.async();
exec(bin+' generate --bootstrap=classes/tests/setup.php --ignore=mustache,tests classes > '+docsPath, function(err, stdout, stderr) {
if( err || stderr ) {
grunt.log.writeln('!! '+(err || stderr));
throw new Error('phpdocs-md is missing, please run $ composer update');
} else {
grunt.log.writeln('* Class docs written to '+docsPath);
}
done();
});
});
/*
* Run PHP-unit
*/
grunt.registerTask('phpunit', 'Run phpUnit tests', function() {
var done = this.async();
exec('phpunit', function(err, stdout, stderr) {
if( !handleProcessError(grunt, stderr, err, stdout) ) {
if( stdout.indexOf('<span style="') > -1 ) {
// h4ck.. in case of a php-error the output will not be sent
// to stderr...
handleProcessError(grunt, stdout);
} else if(stdout.indexOf('PHPUnit') > -1 ) {
grunt.log.writeln('* Successfully ran all unit tests ');
grunt.log.writeln(stdout);
} else {
handleProcessError(grunt, 'Unexpected output from phpunit: '+stdout);
}
}
done();
});
});
/*
* Localization
*/
grunt.registerTask('localization', 'Translate .pot-files', function() {
var done = this.async();
exec('msgfmt -o lang/arlima-sv_SE.mo lang/arlima.pot', function (error, stdout, stderr) {
if( !handleProcessError(grunt, stderr, error, stdout) ) {
grunt.log.writeln(stdout);
grunt.log.writeln('* Pot-files translated');
}
done();
});
});
/*
* Validate the readme file
*/
grunt.registerTask('validate-readme', 'Validate readme.txt', function() {
var faults = mval.validate('./readme.txt', mval.MANIFEST.WORDPRESS);
if( faults.length > 0 ) {
throw new Error('Validation of readme failed: \n'+faults.join('\n'));
}
});
/*
* Create release directory with copy of source code
*/
grunt.registerTask('create-release', 'Copy source code to release directory', function() {
if( !config.releaseVersion ) {
config.releaseVersion = getCurrentVersion();
}
var buildDir = 'release/v-'+config.releaseVersion;
// Create release directory
try {
var distStats = fs.statSync('release');
if( !distStats.isDirectory() ) {
fs.mkdirSync('release');
}
} catch(err) {
fs.mkdirSync('release');
}
config.excludeFromRelease.push('release');
// Copy files to build dir
wrench.copyDirSyncRecursive(__dirname, buildDir, {
forceDelete: true,
excludeHiddenUnix: true,
preserveFiles: false,
exclude: function( file ) {
return config.excludeFromRelease.indexOf(file) > -1;
}
});
});
/*
* Default task - creates a new release version
*/
var defaultTasks = [
'phpunit',
'validate-js',
'validate-readme',
'change-version',
'localization',
'create-docs',
'less',
'concat',
'uglify'
];
grunt.registerTask('default', defaultTasks);
};