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');
|
|
|
|
exports.copyTempFile(__dirname + '/../../vendor/jasmine-1.3.0/jasmine.css', 'jasmine.css');
|
|
|
|
exports.copyTempFile(__dirname + '/../../vendor/jasmine-1.3.0/jasmine.js', 'jasmine.js');
|
|
|
|
exports.copyTempFile(__dirname + '/../../vendor/jasmine-1.3.0/jasmine-html.js', 'jasmine-html.js');
|
|
|
|
exports.copyTempFile(__dirname + '/../jasmine/jasmine-helper.js', 'jasmine-helper.js');
|
|
|
|
|
|
|
|
var reporters = [
|
|
|
|
tempDir + '/reporter.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
var jasmineCss = [
|
|
|
|
tempDir + '/jasmine.css'
|
|
|
|
];
|
|
|
|
|
|
|
|
var jasmineCore = [
|
|
|
|
tempDir + '/jasmine.js',
|
|
|
|
tempDir + '/jasmine-html.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
var jasmineHelper = tempDir + '/jasmine-helper.js';
|
|
|
|
|
|
|
|
var context = {
|
|
|
|
temp : tempDir,
|
|
|
|
css : jasmineCss,
|
|
|
|
scripts : {
|
|
|
|
jasmine : exports.getRelativeFileList(jasmineCore),
|
|
|
|
helpers : exports.getRelativeFileList(options.helpers),
|
|
|
|
specs : exports.getRelativeFileList(options.specs),
|
|
|
|
src : exports.getRelativeFileList(src),
|
|
|
|
vendor : exports.getRelativeFileList(options.vendor),
|
|
|
|
reporters : exports.getRelativeFileList(reporters),
|
|
|
|
start : exports.getRelativeFileList(jasmineHelper)
|
|
|
|
},
|
|
|
|
options : options.templateOptions || {}
|
|
|
|
};
|
|
|
|
|
|
|
|
var source = '',
|
|
|
|
specrunner = path.join(baseDir,options.outfile);
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getRelativeFileList = function (/* args... */) {
|
|
|
|
|
|
|
|
var list = Array.prototype.slice.call(arguments);
|
|
|
|
var base = path.resolve(baseDir);
|
|
|
|
var files = [];
|
|
|
|
list.forEach(function(listItem){
|
2013-01-10 13:14:50 +08:00
|
|
|
if (listItem) files = files.concat(grunt.file.expand({filter: 'isFile'},listItem));
|
2013-01-09 03:28:26 +08:00
|
|
|
});
|
|
|
|
files = grunt.util._(files).map(function(file){
|
|
|
|
return path.resolve(file).replace(base,'.').replace(/\\/g,'/');
|
|
|
|
});
|
|
|
|
return files;
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
};
|
2012-11-02 13:03:09 +08:00
|
|
|
|