Commits

Brian McKenna committed 367eeac

Gruntify build and fix lint errors

  • Participants
  • Parent commits e83ce09

Comments (0)

Files changed (18)

 examples/*.js.map
 lib/*.js
 roy.brianmckenna.org
-bundled-roy.js
+roy.js
+roy-min.js
 .DS_Store
 *~
 *.swp
 .PHONY: site
 
 all:
-	node src/typegrammar.js
-	node src/grammar.js
+	./node_modules/.bin/grunt
 
 deps:
 	npm install
 	npm prune
 
-bundle:
-	./node_modules/interleave/bin/interleave build interleaved-roy.js
-
 site: all bundle
 	[ -e roy.brianmckenna.org ] || mkdir roy.brianmckenna.org
 	cp -r site/* roy.brianmckenna.org
 	cp -r examples roy.brianmckenna.org
 	cp package.json roy.brianmckenna.org
-	$(MAKE) optimise-bundle DEST=roy.brianmckenna.org/
+	cp roy-min.js roy.brianmckenna.org/
 
 extension:
-	$(MAKE) optimise-bundle DEST=misc/chrome-extension/
-
-optimise-bundle:
-	closure --js dist/interleaved-roy.js --js_output_file $(DEST)bundled-roy.js 2>/dev/null || \
-		(echo "Closure not available - not minimising" && cp dist/interleaved-roy.js $(DEST)bundled-roy.js)
+	cp roy-min.js misc/chrome-extension/
 
 # Tests
 
-test: all
-	./node_modules/jasmine-node/bin/jasmine-node --verbose test
+test:
+	./node_modules/.bin/grunt jasmine
+module.exports = function(grunt) {
+    grunt.initConfig({
+        lint: {
+            src: [
+                './src/*.js'
+            ]
+        },
+        jison: {
+            './lib/typeparser.js': './src/typegrammar.js',
+            './lib/parser.js': './src/grammar.js'
+        },
+        rigger: {
+            'roy.js': 'rigger-roy.js'
+        },
+        jasmine: {
+            src: './test'
+        },
+        min: {
+            'roy-min.js': 'roy.js'
+        },
+        watch: {
+            parsers: {
+                files: './src/*grammar.js',
+                tasks: 'jison'
+            },
+            jasmine: {
+                files: ['./src/*.js', './test/*Spec.js'],
+                tasks: 'jasmine'
+            }
+        }
+    });
+
+    grunt.registerMultiTask('jison', 'Parser generator by jison.', function() {
+        var Parser = require('jison').Parser,
+            grammar = require(this.data).grammar;
+            parser = new Parser(grammar, {debug: true}),
+            fs = require('fs');
+
+        fs.writeFileSync(this.target, parser.generate());
+    });
+
+    grunt.registerMultiTask('rigger', 'File concatentation by rigger.', function() {
+        var rigger = require('rigger'),
+            fs = require('fs'),
+            done = this.async(),
+            target = this.target;
+
+        rigger(this.data, function(err, output) {
+            if(err) return grunt.log.error(err);
+            fs.writeFileSync(target, output);
+            done(true);
+        });
+    });
+
+    // Watching the task doesn't work. Sadly jasmine-node
+    // executeSpecsInFolder is not idempotent
+    grunt.registerMultiTask('jasmine', 'Testing by jasmine.', function() {
+        var path = require('path'),
+            specDir = this.file.src,
+            badCache = grunt.file.expand(specDir).concat([
+                path.dirname(require.resolve("jasmine-node")),
+            ]),
+            jasmine,
+            done = this.async(),
+            key;
+
+        // Would be nice to use grunt.file.clearRequireCache
+        grunt.utils._.each(require.cache, function(v, k) {
+            var isBad = grunt.utils._.any(badCache, function(dir) {
+                return k.indexOf(path.resolve(dir)) === 0;
+            });
+            if(!isBad) return;
+            delete require.cache[k];
+        });
+
+        jasmine = require("jasmine-node");
+
+        // Not nice (necessary for jasmine-node's asyncSpecWait, etc)
+        for(key in jasmine) if(jasmine[key] instanceof Function) global[key] = jasmine[key];
+
+        function onComplete(runner) {
+            if (runner.results().failedCount > 0) {
+                grunt.log.error();
+                return;
+            }
+            done(true);
+        };
+
+        jasmine.executeSpecsInFolder(specDir, onComplete, false, true);
+    });
+
+    grunt.registerTask('default', 'jison lint jasmine rigger min');
+};

interleaved-roy.js

-var roy = {};
-
-//= node_modules/underscore/underscore.js
-
-(function(){
-    var module = {"parent": true};
-    var modules = {"underscore": _};
-    var load = {};
-    var require = function(x){
-        if(!modules[x]) {
-            load[x](modules[x] = {})
-        }
-        return modules[x];
-    }
-
-    load["unicode-categories"] = function(exports) {
-        //= node_modules/unicode-categories/unicode-categories.js
-    };
-
-    load["./compile"] = function(exports) {
-        //= src/compile.js
-    };
-    load["./lexer"] = function(exports) {
-        //= src/lexer.js
-    };
-    load["./nodes"] = function(exports) {
-        //= src/nodes.js
-    };
-    load["./modules"] = function(exports) {
-        //= src/modules.js
-    };
-    load["./macroexpand"] = function(exports) {
-        //= src/macroexpand.js
-    };
-    load["./typeparser"] = function(exports) {
-        //= src/typeparser.js
-    };
-    load["./parser"] = function(exports) {
-        //= src/parser.js
-    };
-    load["./typeinference"] = function(exports) {
-        //= src/typeinference.js
-    };
-    load["./types"] = function(exports) {
-        //= src/types.js
-    };
-    load["./tarjan"] = function(exports) {
-        //= src/tarjan.js
-    };
-    load["./freeVariables"] = function(exports) {
-        //= src/freeVariables.js
-    };
-
-    roy.lexer = require("./lexer");
-    roy.compile = require("./compile").compile;
-})();

misc/chrome-extension/background.htm

 <!DOCTYPE html>
 <html>
   <head>
-    <script src="bundled-roy.js"></script>
+    <script src="roy-min.js"></script>
     <script>
       chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
         var js;
     "unicode-categories": "0.9.0"
   },
   "devDependencies": {
-    "interleave": "0.5.11",
+    "grunt": "0.3.15",
+    "rigger": "0.3.19",
     "jasmine-node": "1.0.26"
   }
 }
+var roy = {};
+
+//= node_modules/underscore/underscore.js
+
+(function(){
+    var module = {"parent": true};
+    var modules = {"underscore": _};
+    var load = {};
+    var require = function(x){
+        if(!modules[x]) {
+            load[x](modules[x] = {})
+        }
+        return modules[x];
+    }
+
+    load["unicode-categories"] = function(exports) {
+        //= node_modules/unicode-categories/unicode-categories.js
+    };
+
+    load["./compile"] = function(exports) {
+        //= src/compile.js
+    };
+    load["./lexer"] = function(exports) {
+        //= src/lexer.js
+    };
+    load["./nodes"] = function(exports) {
+        //= src/nodes.js
+    };
+    load["./modules"] = function(exports) {
+        //= src/modules.js
+    };
+    load["./macroexpand"] = function(exports) {
+        //= src/macroexpand.js
+    };
+    load["./typeparser"] = function(exports) {
+        //= lib/typeparser.js
+    };
+    load["./parser"] = function(exports) {
+        //= lib/parser.js
+    };
+    load["./typeinference"] = function(exports) {
+        //= src/typeinference.js
+    };
+    load["./types"] = function(exports) {
+        //= src/types.js
+    };
+    load["./tarjan"] = function(exports) {
+        //= src/tarjan.js
+    };
+    load["./freeVariables"] = function(exports) {
+        //= src/freeVariables.js
+    };
+
+    roy.lexer = require("./lexer");
+    roy.compile = require("./compile").compile;
+})();
     }
     </style>
     <script src="http://www.google-analytics.com/ga.js" async="async"></script>
-    <script src="bundled-roy.js"></script>
+    <script src="roy-min.js"></script>
     <script src="codemirror2/lib/codemirror.js"></script>
     <script src="codemirror2/lib/util/runmode.js"></script>
     <script src="codemirror2/mode/roy/roy.js"></script>
     types = require('./types'),
     nodeToType = require('./typeinference').nodeToType,
     nodes = require('./nodes').nodes,
-    parser = require('./parser').parser,
-    typeparser = require('./typeparser').parser,
     lexer = require('./lexer'),
+    parser = require('../lib/parser').parser,
+    typeparser = require('../lib/typeparser').parser,
     _ = require('underscore');
 
 // Assigning the nodes to `parser.yy` allows the grammar to access the nodes from
         }
     }
 
-    if(source == null) {
+    if(!source) {
         throw new Error("File(s) not found: " + filenames.join(", "));
     }
 
         return;
     case "-p":
         includePrelude = false;
+        /* falls through */
     case "-r":
         vm = require('vm');
         run = true;

src/freeVariables.js

+/*jshint expr:true*/
 var _ = require('underscore');
 var nodes = require('./nodes');
 
-var sys = require('sys'),
-    Parser = require('jison').Parser,
-    typegrammar = require('./typegrammar').bnf;
+var typegrammar = require('./typegrammar').bnf;
 
 var n = function(s) {
     return s + "$$.lineno = yylineno;";
         ]
     }
 };
-
-var parser = new Parser(grammar, {debug: true});
-
-var fs = require('fs');
-fs.writeFile('src/parser.js', parser.generate());
+exports.grammar = grammar;
 };
 
 exports.tokenise = function(source) {
+    /*jshint boss:true*/
     indent = 0;
     indents = [];
     tokens = [];

src/macroexpand.js

                 macros[n.name] = code;
             },
             visitCall: function() {
+                /*jshint evil:true*/
                 if(!macros[n.func.value]) return;
 
                 var f = new Function('var nodes = this.nodes; ' + macros[n.func.value]);
 var lexer = require('./lexer'),
-    typeparser = require('./typeparser').parser,
+    typeparser = require('../lib/typeparser').parser,
     nodes = require('./nodes').nodes,
     types = require('./types'),
     _ = require('underscore');
     // node.js uses a few prefixes to decide where to load from:
     // http://nodejs.org/docs/latest/api/all.html#loading_from_node_modules_Folders
     var relative = _.any(['/', './', '../'], function(e) {
-        return moduleName.indexOf(e) == 0;
+        return moduleName.indexOf(e) === 0;
     });
 
     if(relative) {

src/prettyprint.js

 var prettyPrint = function(n) {
     return n.accept({
         visitFunction: function() {
-            return "\\" + _.map(n.args, prettyPrint).join(" ") + " -> " + _.map(n.body, prettyPrint)
+            return "\\" + _.map(n.args, prettyPrint).join(" ") + " -> " + _.map(n.body, prettyPrint);
         },
         visitArg: function() {
             return n.name;
                });
 
         if (smallestReachableIndex[vertex.id] === indices[vertex.id]) {
-            var currentComponent = [];
+            var currentComponent = [],
+                popped;
 
             do {
-                var popped = stack.pop();
+                popped = stack.pop();
 
                 isInStack[popped.id] = false;
 
                 currentComponent.push(popped);
-            } while (vertex.id != popped.id)
+            } while (vertex.id != popped.id);
 
             components.push(currentComponent);
         }

src/typegrammar.js

-var Parser = require('jison').Parser;
-
 var bnf = {
     // For type annotations
     "type": [
     ],
     "dataParamList": [
         ["IDENTIFIER", "$$ = [new yy.Arg($1)];"],
-        ["dataParamList IDENTIFIER", "$$ = $1; $1.push(new yy.Arg($2));"],
+        ["dataParamList IDENTIFIER", "$$ = $1; $1.push(new yy.Arg($2));"]
     ],
     "optDataParamList": [
         ["", "$$ = [];"],
         ["IDENTIFIER", "$$ = $1;"]
     ]
 };
-
 exports.bnf = bnf;
 
 var grammar = {
         "keywordOrIdentifier": bnf.keywordOrIdentifier
     }
 };
-
-if(exports && !module.parent) {
-    var parser = new Parser(grammar, {debug: true});
-
-    var fs = require('fs');
-    fs.writeFile('src/typeparser.js', parser.generate());
-}
+exports.grammar = grammar;

test/TypeInferenceSpec.js

     var typeinference = require('../src/typeinference'),
         types = require('../src/types');
         lexer = require('../src/lexer');
-        parser = require('../src/parser');
+        parser = require('../lib/parser');
 
     beforeEach(function() {
         this.addMatchers({