2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2013-02-06 06:13:10 +08:00
|
|
|
exports.init = function(grunt, phantomjs) {
|
2013-01-09 03:28:26 +08:00
|
|
|
// node api
|
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path');
|
|
|
|
|
|
|
|
// npm
|
|
|
|
var rimraf = require('rimraf');
|
|
|
|
|
|
|
|
var baseDir = '.',
|
|
|
|
tempDir = '.grunt/grunt-contrib-jasmine';
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.writeTempFile = function(dest, contents) {
|
|
|
|
var file = path.join(tempDir,dest);
|
|
|
|
grunt.file.write(file, contents);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.copyTempFile = function(src, dest) {
|
|
|
|
var file = path.join(tempDir,dest);
|
|
|
|
grunt.file.copy(src, file);
|
2012-11-02 13:03:09 +08:00
|
|
|
};
|
|
|
|
|
2013-01-09 03:28:26 +08:00
|
|
|
exports.cleanTemp = function() {
|
|
|
|
rimraf.sync(tempDir);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.buildSpecrunner = function (src, options){
|
|
|
|
|
|
|
|
exports.copyTempFile(__dirname + '/../jasmine/reporters/PhantomReporter.js', 'reporter.js');
|
2013-03-01 22:58:22 +08:00
|
|
|
exports.copyTempFile(__dirname + '/../../vendor/jasmine-' + options.version + '/jasmine.css', 'jasmine.css');
|
|
|
|
exports.copyTempFile(__dirname + '/../../vendor/jasmine-' + options.version + '/jasmine.js', 'jasmine.js');
|
|
|
|
exports.copyTempFile(__dirname + '/../../vendor/jasmine-' + options.version + '/jasmine-html.js', 'jasmine-html.js');
|
2013-01-09 03:28:26 +08:00
|
|
|
exports.copyTempFile(__dirname + '/../jasmine/jasmine-helper.js', 'jasmine-helper.js');
|
2013-06-16 00:38:39 +08:00
|
|
|
exports.copyTempFile(__dirname + '/../helpers/phantom-polyfill.js', 'phantom-polyfill.js');
|
2013-01-09 03:28:26 +08:00
|
|
|
|
|
|
|
var reporters = [
|
|
|
|
tempDir + '/reporter.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
var jasmineCss = [
|
|
|
|
tempDir + '/jasmine.css'
|
|
|
|
];
|
|
|
|
|
2013-06-14 11:29:09 +08:00
|
|
|
var source = '',
|
|
|
|
outfile = options.outfile,
|
|
|
|
specrunner = path.join(baseDir,outfile),
|
|
|
|
outdir = path.dirname(outfile);
|
|
|
|
|
|
|
|
jasmineCss = jasmineCss.concat(options.styles);
|
2013-03-11 13:15:29 +08:00
|
|
|
|
2013-06-16 00:38:39 +08:00
|
|
|
var polyfills = [
|
|
|
|
tempDir + '/phantom-polyfill.js'
|
|
|
|
];
|
|
|
|
|
2013-01-09 03:28:26 +08:00
|
|
|
var jasmineCore = [
|
|
|
|
tempDir + '/jasmine.js',
|
|
|
|
tempDir + '/jasmine-html.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
var jasmineHelper = tempDir + '/jasmine-helper.js';
|
|
|
|
|
|
|
|
var context = {
|
|
|
|
temp : tempDir,
|
2013-06-16 02:15:01 +08:00
|
|
|
css : exports.getRelativeFileList(outdir, jasmineCss, { nonull : true }),
|
2013-01-09 03:28:26 +08:00
|
|
|
scripts : {
|
2013-06-16 00:38:39 +08:00
|
|
|
polyfills : exports.getRelativeFileList(outdir, polyfills),
|
2013-06-14 11:29:09 +08:00
|
|
|
jasmine : exports.getRelativeFileList(outdir, jasmineCore),
|
2013-06-16 02:15:01 +08:00
|
|
|
helpers : exports.getRelativeFileList(outdir, options.helpers, { nonull : true }),
|
2013-06-14 11:29:09 +08:00
|
|
|
specs : exports.getRelativeFileList(outdir, options.specs),
|
2013-06-16 02:15:01 +08:00
|
|
|
src : exports.getRelativeFileList(outdir, src, { nonull : true }),
|
|
|
|
vendor : exports.getRelativeFileList(outdir, options.vendor, { nonull : true }),
|
2013-06-14 11:29:09 +08:00
|
|
|
reporters : exports.getRelativeFileList(outdir, reporters),
|
|
|
|
start : exports.getRelativeFileList(outdir, jasmineHelper)
|
2013-01-09 03:28:26 +08:00
|
|
|
},
|
|
|
|
options : options.templateOptions || {}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.template.process) {
|
2013-02-06 06:13:10 +08:00
|
|
|
var task = {
|
|
|
|
writeTempFile : exports.writeTempFile,
|
|
|
|
copyTempFile : exports.copyTempFile,
|
|
|
|
phantomjs : phantomjs
|
|
|
|
};
|
|
|
|
source = options.template.process(grunt, task, context);
|
2013-01-09 03:28:26 +08:00
|
|
|
grunt.file.write(specrunner, source);
|
|
|
|
} else {
|
|
|
|
grunt.file.copy(options.template, specrunner, {
|
|
|
|
process : function(src) {
|
|
|
|
source = grunt.util._.template(src, context);
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
});
|
2012-11-02 13:03:09 +08:00
|
|
|
}
|
2013-01-09 03:28:26 +08:00
|
|
|
|
|
|
|
return source;
|
|
|
|
};
|
|
|
|
|
2013-06-16 02:15:01 +08:00
|
|
|
exports.getRelativeFileList = function (outdir, patterns, options) {
|
2013-01-09 03:28:26 +08:00
|
|
|
var files = [];
|
2013-06-16 02:15:01 +08:00
|
|
|
patterns = patterns instanceof Array ? patterns : [ patterns ];
|
|
|
|
options = options || {};
|
|
|
|
patterns.forEach(function(listItem){
|
|
|
|
if (listItem) files = files.concat(grunt.file.expand(options, listItem));
|
2013-01-09 03:28:26 +08:00
|
|
|
});
|
2013-06-14 11:29:09 +08:00
|
|
|
|
2013-01-09 03:28:26 +08:00
|
|
|
files = grunt.util._(files).map(function(file){
|
2013-06-14 11:29:09 +08:00
|
|
|
return (/^https?:/).test(file) ? file : path.relative(outdir, file).replace(/\\/g, '/');
|
2013-01-09 03:28:26 +08:00
|
|
|
});
|
|
|
|
return files;
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
};
|
2012-11-02 13:03:09 +08:00
|
|
|
|