diff --git a/.gitignore b/.gitignore index 488f4e9..28ce95e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ .DS_Store -benchpress-build/ \ No newline at end of file +benchpress-build/ +build/ diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..4109657 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,18 @@ +var cli = require('./lib/cli'); + +/** + * This file is only for testing and demonstration of the included grunt tasks. + */ + +module.exports = function (grunt) { + grunt.initConfig({ + bp_build: { + options: { + buildPath: 'build/benchmarks', + benchmarksPath: 'grunt-benchmarks' + } + } + }); + + grunt.loadTasks('./tasks'); +}; diff --git a/grunt-benchmarks/sample-folder/bp.conf.js b/grunt-benchmarks/sample-folder/bp.conf.js new file mode 100644 index 0000000..b61439c --- /dev/null +++ b/grunt-benchmarks/sample-folder/bp.conf.js @@ -0,0 +1,3 @@ +module.exports = function(config) { + +} \ No newline at end of file diff --git a/grunt-benchmarks/sample-folder/main.html b/grunt-benchmarks/sample-folder/main.html new file mode 100644 index 0000000..2345e19 --- /dev/null +++ b/grunt-benchmarks/sample-folder/main.html @@ -0,0 +1 @@ +sample! \ No newline at end of file diff --git a/lib/cli.js b/lib/cli.js index a3a9252..20be1e8 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -2,8 +2,9 @@ var fs = require('fs'), path = require('path'), benchpressBase = path.resolve(module.filename, '../../'), rimraf = require('rimraf'), + mkdirp = require('mkdirp'), defaultBuildPath = path.resolve('benchpress-build'), - benchmarksPath = path.resolve('benchmarks'), + defaultBenchmarksPath = path.resolve('benchmarks'), underscorePath = path.resolve(benchpressBase, 'node_modules/underscore/underscore-min.js'), bootstrapPath = path.resolve(benchpressBase, 'node_modules/bootStrap/dist/css/bootstrap.min.css'), bpPath = path.resolve(benchpressBase, 'lib/bp.js'), @@ -11,19 +12,22 @@ var fs = require('fs'), httpServer = require('http-server'), minimist = require('minimist'); -exports.launchChrome = function launchChrome() { +exports.launchChrome = function launchChrome(done) { if (process.platform == 'linux') { require('child_process').exec('google-chrome -incognito --js-flags="--expose-gc"', function(err) { if (err) console.error('An error occurred trying to launch Chrome: ', err); + done && done(); }); } else if (process.platform === 'darwin') { require('child_process').exec('/Applications/Google\\ Chrome\\ Canary.app/Contents/MacOS/Google\\ Chrome\\ Canary --enable-memory-info --enable-precise-memory-info --enable-memory-benchmarking --js-flags="--expose-gc" -incognito', function(err) { if (err) console.error('An error occurred trying to launch Chrome: ', err); + done && done(); }); } else { console.log('Cannot launch chrome for your platform: ', process.platform); + done && done(); } }; @@ -40,15 +44,18 @@ exports.run = function run() { }); }; -exports.build = function build(buildPath) { - buildPath = buildPath || defaultBuildPath; +exports.build = function build(options, done) { + options = options || {}; + buildPath = options.buildPath || defaultBuildPath; + benchmarksPath = options.benchmarksPath || defaultBenchmarksPath; + //Start fresh rimraf(buildPath, buildIt); function buildIt (err) { var template, benchmarks; if (err) throw err; - fs.mkdirSync(buildPath); + mkdirp.sync(buildPath); //Get benchmark html template template = fs.readFileSync(path.resolve(libPath, 'template.html')); @@ -66,8 +73,9 @@ exports.build = function build(buildPath) { require(path.resolve(benchmarkPath, 'bp.conf.js'))(config); dependencies = fs.readdirSync(benchmarkPath); + dependencies.forEach(function(dependency) { - var dependencyPath = path.resolve('benchmarks', benchmark, dependency); + var dependencyPath = path.resolve(benchmarkPath, dependency); if (dependency === 'main.html') { //This is the main benchmark template main = fs.readFileSync(dependencyPath).toString(); @@ -83,6 +91,7 @@ exports.build = function build(buildPath) { main = main.replace('%%SCRIPTS%%', JSON.stringify(config.scripts)); fs.writeFileSync(path.resolve(buildPath, benchmark, 'index.html'), main); + done && done(); }); } @@ -102,7 +111,10 @@ exports.exec = function() { var args = minimist(process.argv.slice(2)); switch (process.argv[2]) { case 'build': - exports.build(args['build-path']); + exports.build({ + buildPath: args['build-path'], + benchmarksPath: args['benchmark-path'] + }); break; case 'run': exports.run(); diff --git a/package.json b/package.json index de8c1e9..9e28ce5 100644 --- a/package.json +++ b/package.json @@ -26,9 +26,11 @@ "homepage": "https://github.com/angular/benchpress", "dependencies": { "bootstrap": "^3.2.0", + "grunt": "^0.4.5", "http-server": "^0.6.1", "karma": "^0.12.21", "minimist": "^1.1.0", + "mkdirp": "^0.5.0", "rimraf": "^2.2.8", "underscore": "^1.6.0" } diff --git a/tasks/bp_build.js b/tasks/bp_build.js new file mode 100644 index 0000000..b04a72c --- /dev/null +++ b/tasks/bp_build.js @@ -0,0 +1,7 @@ +var cli = require('../lib/cli'); + +module.exports = function (grunt) { + grunt.registerTask('bp_build', 'build benchmarks for project', function() { + cli.build(this.options(), this.async()); + }); +}; \ No newline at end of file diff --git a/tasks/bp_launch_chrome.js b/tasks/bp_launch_chrome.js new file mode 100644 index 0000000..e0409f1 --- /dev/null +++ b/tasks/bp_launch_chrome.js @@ -0,0 +1,8 @@ +var cli = require('../lib/cli'); + +module.exports = function (grunt) { + grunt.registerTask('bp_launch_chrome', 'launch chrome canary in incognito with flags', + function() { + cli.launchChrome(this.async()); + }); +}; diff --git a/tasks/bp_run.js b/tasks/bp_run.js new file mode 100644 index 0000000..b9b9880 --- /dev/null +++ b/tasks/bp_run.js @@ -0,0 +1,7 @@ +var cli = require('../lib/cli'); + +module.exports = function (grunt) { + grunt.registerTask('bp_run', 'run a server from cwd', function() { + cli.run(this.async()); + }); +}; \ No newline at end of file