Skip to content

Commit

Permalink
Merge branch 'release-0.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ethul committed Dec 15, 2012
2 parents 83a0d51 + afe3c03 commit a33e527
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
@@ -1,2 +1 @@
.*swp
.*un~
node_modules/
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2012 Eric Thul

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), 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.
111 changes: 111 additions & 0 deletions README.md
@@ -0,0 +1,111 @@
# Grunt Roy

Invokes the [Roy](http://roy.brianmckenna.org/) compiler through
[Grunt](http://gruntjs.com). [Roy](http://roy.brianmckenna.org/) is a
functional programming language that compiles to JavaScript.

## Getting Started
From the same directory as your project's `grunt.js` and `package.json`
file, install this plugin with the following command.

```bash
npm install grunt-roy --save-dev
```

Once that's done, add the following line to your project's `grunt.js`
file.

```js
grunt.loadNpmTasks('roy');
```

If the plugin has been installed correctly, running `grunt --help` at
the command line should list the newly-installed plugin's task or tasks.
In addition, the plugin should be listed in package.json as a
`devDependency`, which ensures that it will be installed whenever the
`npm install` command is run.

## The "roy" task

### Overview
In your project's Gruntfile, add a section named `roy` to the data
object passed into `grunt.initConfig()`.

```js
grunt.initConfig({
roy: {
app: {
src: ['lib/**/*.roy'],
dest: 'public/javascripts',
options: {
strict: true,
sourceMap: true
}
}
}
})
```

### Options

#### options.strict
Type: `Boolean`
Default value: `false`

A Boolean value that when true will insert `"use strict";` into the
compiled JavaScript.

#### options.sourceMap
Type: `Boolean`
Default value: `false`

A Boolean value that when true will generate a source map file
corresponding to each generated JavaScript file. Additionally, the
original Roy source file will be copied to the destination to be made
available for the source mapping.

Note that the source mapping URL will be appended to the compiled
JavaScript file. The value of this URL will be relative so that it may
reference the source map residing in the same directory. The reason this
works is because if you include the JavaScript file with a script tag,
the src attribute of the script tag will be used as the base path for
the source mapping URL.

#### options.nodejs
Type: `Boolean`
Default value: `false`

A Boolean value that when true will not wrap the compiled JavaScript in
an immediately-invoked function expression. Additionally, Roy's `export`
keyword will map to the Nodejs `exports` object. The following is an
example.

```roy
let A = {a: "a"}
export A
```

```js
var A = {
"a": "a"
};
exports["A"] = A;;
```

When the option value is set to false, an IIFE will wrap the compiled
JavaScript, and Roy's `export` keyword will map to the `this` object.
The following is an example.

```roy
let A = {a: "a"}
export A
```

```js
(function() {
var A = {
"a": "a"
};
this["A"] = A;;
})();
```
39 changes: 39 additions & 0 deletions grunt.js
@@ -0,0 +1,39 @@
module.exports = function(grunt){
'use strict';

grunt.initConfig({
test: {
files: ['test/**/*.js']
},
lint: {
files: [
'grunt.js',
'tasks/**/*.js',
'test/**/*.js'
]
},
jshint: {
options: {
bitwise: true,
curly: true,
eqeqeq: true,
forin: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
noempty: true,
nonew: true,
plusplus: true,
quotmark: true,
regexp: true,
undef: true,
trailing: true,
laxcomma: true,
node: true
}
}
});
grunt.loadTasks('tasks');
grunt.registerTask('default', 'lint test');
};
41 changes: 41 additions & 0 deletions package.json
@@ -0,0 +1,41 @@
{
"name": "grunt-roy",
"description": "Compiles Roy to JavaScript",
"version": "0.0.1",
"homepage": "https://github.com/ethul/grunt-roy",
"author": {
"name": "Eric",
"email": "thul.eric@gmail.com"
},
"repository": {
"type": "git",
"url": "git://github.com/ethul/grunt-roy.git"
},
"bugs": {
"url": "https://github.com/ethul/grunt-roy/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/ethul/grunt-roy/blob/master/LICENSE"
}
],
"main": "grunt.js",
"engines": {
"node": "~0.8.14"
},
"scripts": {
"postinstall": "(pushd node_modules/roy && make deps && make && popd) &>/dev/null"
},
"dependencies": {
"grunt": "~0.3.17",
"source-map": "~0.1.8",
"roy": "git://github.com/pufuwozu/roy.git#9ecd6e90bce762c13c591f7e54c714ab11e2a696"
},
"devDependencies": {
"sinon": "~1.5.2"
},
"keywords": [
"gruntplugin"
]
}
84 changes: 84 additions & 0 deletions tasks/roy.js
@@ -0,0 +1,84 @@
module.exports = function(grunt){
'use strict';

grunt.registerMultiTask('roy', 'compiles roy files', function(){
var src = this.file.src
, dest = this.file.dest
, options = this.data.options
;
return grunt.file.expandFiles(src).reduce(function(b, a){
var os = grunt.utils._.clone(options || {});
return b && compile(a, dest, os);
}, true);
});

function compile(srcpath, dest, options) {
var roy = require('roy')
, path = require('path')
, destpath = resolveDest(srcpath, dest)
, res = (function(){
try {
var out = writeSourceMap(srcpath, destpath, options, function(a){
return roy.compile(grunt.file.read(srcpath), {}, {}, {
filename: path.basename(srcpath),
sourceMap: a,
strict: options.strict,
nodejs: options.nodejs
}).output;
})
, _ = grunt.file.write(destpath, out)
;
return true;
}
catch(e) {
grunt.log.error('Compilation of ' + srcpath + ' has failed');
grunt.log.error(e);
return false;
}
}())
;
return res;
}

function resolveDest(srcpath, dest) {
var path = require('path')
, srcdirs = path.dirname(srcpath).split(path.sep).slice(1)
, srcdir = path.join.apply(path, srcdirs)
, destfile = path.basename(srcpath, '.roy') + '.js'
;
return path.join(dest, srcdir, destfile);
}

// The sources in the source map file are not specified as absolute
// urls, which means that they will be resolved relative to the
// location of the source map file.
//
// Also, we specify a relative path for the source mapping url, which
// causes the script source of the JS to be used as the source origin.
// Also, this url must adhere to RFC3986.
function writeSourceMap(srcpath, destpath, options, f) {
var path = require('path')
, SourceMapGenerator = require('source-map').SourceMapGenerator
, sourceMap = new SourceMapGenerator({file: path.basename(destpath)})
, sourceMapDest = destpath + '.map'
, sourceMappingUrl = encodeURIComponent(path.basename(sourceMapDest))
, res = f(sourceMap) + (options.sourceMap ?
'//@ sourceMappingURL=' + sourceMappingUrl + '\n' : ''
)
, _ = (function(){
if (options.sourceMap) {
grunt.file.write(sourceMapDest, sourceMap.toString());
grunt.file.copy(srcpath, path.join.apply(path, [
path.dirname(destpath),
path.basename(srcpath)
]));
}
}())
;
return res;
}

return {
compile: compile
};
};

0 comments on commit a33e527

Please sign in to comment.