Skip to content
Browse files

Initial release

  • Loading branch information...
1 parent 4e490e3 commit d19d56bb9aa7eff21cd6d3c8facdd1e76975f2ac @asciidisco committed Nov 22, 2012
Showing with 381 additions and 3 deletions.
  1. +2 −0 .gitignore
  2. +2 −0 .npmignore
  3. +6 −0 .travis.yml
  4. +22 −0 LICENSE-MIT
  5. +50 −3 README.md
  6. +2 −0 bin/grunt-backbonebuilder.js
  7. +70 −0 grunt.js
  8. +126 −0 lib/builder.js
  9. +40 −0 package.json
  10. +49 −0 tasks/backbonebuilder.js
  11. +12 −0 test/backbonebuilder_test.js
View
2 .gitignore
@@ -0,0 +1,2 @@
+/node_modules/
+.DS_Store
View
2 .npmignore
@@ -0,0 +1,2 @@
+/node_modules/
+.DS_Store
View
6 .travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+ - 0.8
+before_script:
+ - "export DISPLAY=:99.0"
+ - "npm install"
View
22 LICENSE-MIT
@@ -0,0 +1,22 @@
+Copyright (c) 2012 asciidisco
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
View
53 README.md
@@ -1,4 +1,51 @@
-grunt-backbonebuilder
-=====================
+# grunt-backbonebuilder
-Build your customized backbone experience with grunt
+Build your custom backbone experience with grunt.
+
+[![Build Status](https://secure.travis-ci.org/asciidisco/grunt-jquerybuilder.png?branch=master)](http://travis-ci.org/asciidisco/grunt-jquerybuilder)
+
+## Getting Started
+Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-backbonebuilder`
+
+Then add this line to your project's `grunt.js` gruntfile.
+
+```javascript
+grunt.loadNpmTasks('grunt-backbonebuilder');
+```
+
+### Resources
+
++ [grunt](https://github.com/cowboy/grunt)
++ [getting started](https://github.com/cowboy/grunt/blob/master/docs/getting_started.md)
++ [backbone.js](http://backbonejs.org)
+
+## Documentation
+Load the grunt-backbonebuilder task as described in 'Getting started' and add your backbone builder
+configuration to your grunt file:
+
+Example backbonebuilder grunt file config entry:
+
+```javascript
+// ... grunt file contents
+ backbone: {
+ // the parts you want to include in your build
+ // possible values ['Model', 'Collection', 'View', 'Events', 'Router']
+ include: ['Model', 'Collection', 'View', 'Events'],
+ // output location (relative to your grunt.js file location)
+ dest: 'build/backbone.custom.js',
+ // if you want to use a different backbone version than the build in one (0.9.2)
+ // then just a reference to your UNMINIFIED backbone version
+ src: 'vendor/mySpecialBackboneVersion.js'
+ },
+// ... even more grunt file contents
+```
+
+
+## Release History
+
+### 0.1.0
++ Initial Release
+
+## License
+Copyright (c) 2012 asciidisco
+Licensed under the MIT license.
View
2 bin/grunt-backbonebuilder.js
@@ -0,0 +1,2 @@
+#!/usr/bin/env node
+require('grunt').npmTasks('grunt-backbonebuilder').cli();
View
70 grunt.js
@@ -0,0 +1,70 @@
+'use strict';
+
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ pkg: '<json:package.json>',
+ meta: {
+ banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %>\n' +
+ '------------------------------\n' +
+ 'Build @ <%= grunt.template.today("yyyy-mm-dd") %>\n' +
+ 'Documentation and Full License Available at:\n' +
+ '<%= pkg.homepage %>\n' +
+ '<%= pkg.repository.url %>\n' +
+ 'Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>\n\n' +
+ 'Permission is hereby granted, free of charge, to any person obtaining a\n' +
+ 'copy of this software and associated documentation files (the "Software"),\n' +
+ 'to deal in the Software without restriction, including without limitation\n' +
+ 'the rights to use, copy, modify, merge, publish, distribute, sublicense,\n' +
+ 'and/or sell copies of the Software, and to permit persons to whom the\n\n' +
+ 'Software is furnished to do so, subject to the following conditions:\n' +
+ 'The above copyright notice and this permission notice shall be included in\n' +
+ 'all copies or substantial portions of the Software.\n\n' +
+ 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\n' +
+ 'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n' +
+ 'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n' +
+ 'IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n' +
+ 'DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\n' +
+ 'ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n' +
+ 'IN THE SOFTWARE.*/'
+ },
+
+ test: {
+ files: ['test/**/*.js']
+ },
+
+ lint: {
+ files: ['grunt.js', 'tasks/**/*.js', 'lib/**/*.js', 'test/**/*.js']
+ },
+
+ watch: {
+ files: '<config:lint.files>',
+ tasks: 'default'
+ },
+
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true,
+ es5: true
+ },
+ globals: {}
+ }
+ });
+
+ // Default task.
+ grunt.registerTask('default', 'lint test');
+
+ // Default task.
+ grunt.registerTask('travis', 'lint test');
+};
View
126 lib/builder.js
@@ -0,0 +1,126 @@
+/*global exports:true, require:true */
+
+/*
+ * grunt-backbonebuilder
+ * https://github.com/asciidisco/grunt-backbonebuilder
+ *
+ * Copyright (c) 2012 asciidisco
+ * Licensed under the MIT license.
+ */
+
+(function () {
+ 'use strict';
+
+ var fs = require('fs');
+ var _ = null;
+ var combinations = {
+ Collection: ['Header', 'Setup', 'Events', 'Model', 'Collection', 'sync', 'Helpers'],
+ Model: ['Header', 'Setup', 'Events', 'Model', 'sync', 'Helpers'],
+ View: ['Header', 'Setup', 'Events', 'View', 'Helpers'],
+ Router: ['Header', 'Setup', 'Events', 'Router', 'History', 'Helpers'],
+ Events: ['Header', 'Setup', 'Events', 'Helpers'],
+ All: ['Header', 'Setup', 'Events', 'Model', 'Collection', 'View', 'History', 'Helpers']
+ };
+
+ // extract a part by its character index
+ var extract = function (source, start, end) {
+ return source.substring(start, end);
+ };
+
+ // parts are seperated by annotations
+ var getBackboneParts = function (bbsource) {
+ var parts = {
+ Header: '',
+ Setup: '// Initial Setup',
+ Events: '// Backbone.Events',
+ Model: '// Backbone.Model',
+ Collection: '// Backbone.Collection',
+ Router: '// Backbone.Router',
+ History: '// Backbone.History',
+ View: '// Backbone.View',
+ sync: '// Backbone.sync',
+ Helpers: '// Helpers'
+ };
+
+ // index & module helper
+ var lastPos = 0;
+ var temp = 0;
+ var keys = Object.keys(parts);
+
+ // extract every module
+ keys.forEach(function (key, idx) {
+ temp = typeof keys[idx + 1] !== 'undefined' ? bbsource.search(parts[keys[idx + 1]]) : bbsource.length;
+ parts[key] = extract(bbsource, lastPos, temp);
+ lastPos = temp;
+ });
+
+ return parts;
+ };
+
+ // glues the backbone parts together again
+ var setUpNeededParts = function (combinations, parts, backboneSrc) {
+ var src = '';
+ var allCombinations = [];
+
+ parts.forEach(function (part) {
+ allCombinations.push(combinations[part]);
+ });
+
+ var neededParts = _.union.apply(_, allCombinations);
+ var backboneParts = getBackboneParts(backboneSrc);
+ var finalCombination = _.intersection(combinations.All, neededParts);
+
+ finalCombination.forEach(function (part) {
+ src += backboneParts[part];
+ });
+
+ return src;
+ };
+
+ exports.init = function(grunt) {
+ var exports = {};
+ _ = grunt.utils._ || grunt.util._;
+
+ var Transport = function (type, content) {
+ this.type = type;
+ this.content = content;
+ };
+
+ // cuts the backbone file & glues it together again
+ exports.build = function (options, contentCb) {
+ var backbonePath = require.resolve('backbone');
+ var fileContents = '';
+ var backboneSource = '';
+
+ // check if we should use a different backbone version
+ if (options.config.src) {
+ try {
+ backbonePath = options.config.src;
+ backboneSource = String(fs.readFileSync(backbonePath));
+ } catch (e) {
+ contentCb(new Transport('error', 'Could not load given backbone version at "' + options.config.src + '"'), options);
+ return null;
+ }
+ } else {
+ backboneSource = String(fs.readFileSync(backbonePath));
+ }
+
+ // check if the version is minified (and throw some error when it is)
+ if (backboneSource.length < 40000) {
+ contentCb(new Transport('error', 'Backbone builder needs an unminified version of Backbone to build ("' + options.config.src + '")'), options);
+ return null;
+ }
+
+ // get the custom backbone src
+ fileContents = setUpNeededParts(combinations, options.config.include || combinations.All, backbonePath);
+
+ // call callback with the transport object
+ contentCb(new Transport('content', fileContents), options);
+
+ return true;
+ };
+
+ return exports;
+ };
+
+}).call(this);
View
40 package.json
@@ -0,0 +1,40 @@
+{
+ "name": "grunt-backbonebuilder",
+ "description": "Backbone custom builds builder",
+ "version": "0.1.0",
+ "homepage": "https://github.com/asciidisco/grunt-backbonebuilder",
+ "author": {
+ "name": "asciidisco",
+ "email": "public@asciidisco.com"
+ },
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/asciidisco/grunt-backbonebuilder"
+ },
+ "bugs": {
+ "url": "https://github.com/asciidisco/grunt-backbonebuilder/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/asciidisco/grunt-backbonebuilder/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "grunt.js",
+ "bin": {
+ "grunt-backbonebuilder": "bin/grunt-backbonebuilder"
+ },
+ "engines": {
+ "node": ">=0.8.0"
+ },
+ "scripts": {
+ "test": "grunt travis --verbose"
+ },
+ "dependencies": {
+ "grunt": "0.3.x",
+ "backbone": "0.9.x"
+ },
+ "keywords": [
+ "gruntplugin", "backbone", "custom", "browser"
+ ]
+}
View
49 tasks/backbonebuilder.js
@@ -0,0 +1,49 @@
+/*global require: true */
+
+/*
+ * grunt-backbonebuilder
+ * https://github.com/asciidisco/grunt-backbonebuilder
+ *
+ * Copyright (c) 2012 asciidisco
+ * Licensed under the MIT license.
+ */
+
+module.exports = function (grunt) {
+ 'use strict';
+
+ // Internal lib
+ var builder = require('../lib/builder').init(grunt);
+
+ // ==========================================================================
+ // TASKS
+ // ==========================================================================
+
+ grunt.registerTask('backbone', 'Builds your custimized backbone experience', function(mode) {
+ var done = this.async(),
+ config = grunt.config.get('backbone');
+
+ var fileWriter = function (transport, options) {
+ // display error
+ if (transport.type === 'error') {
+ grunt.log.error(transport.content);
+ }
+
+ // proceed with file content
+ if (transport.type === 'content') {
+ grunt.log.write('Writing custom Backbone file');
+ grunt.file.write(options.config.dest, transport.content);
+ }
+
+ options.done();
+ };
+
+ // execute clear target helper
+ builder.build({
+ config: config,
+ done: done,
+ debug: !!config.debug
+ }, fileWriter);
+
+ });
+
+};
View
12 test/backbonebuilder_test.js
@@ -0,0 +1,12 @@
+'use strict';
+
+var grunt = require('grunt');
+// Load local tasks.
+grunt.loadTasks('tasks');
+
+exports['require'] = {
+ setUp: function(done) {
+ // setup here
+ done();
+ }
+};

0 comments on commit d19d56b

Please sign in to comment.
Something went wrong with that request. Please try again.