/
ruby.plugin.js
56 lines (47 loc) · 2.29 KB
/
ruby.plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Generated by CoffeeScript 1.3.3
(function() {
var __hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
module.exports = function(BasePlugin) {
var RubyPlugin;
return RubyPlugin = (function(_super) {
__extends(RubyPlugin, _super);
function RubyPlugin() {
return RubyPlugin.__super__.constructor.apply(this, arguments);
}
RubyPlugin.prototype.name = 'ruby';
RubyPlugin.prototype.priority = 700;
RubyPlugin.prototype.render = function(opts, next) {
var content, data, errors, exec, file, inExtension, outExtension, result, ruby, source, spawn, templateData, _ref;
inExtension = opts.inExtension, outExtension = opts.outExtension, templateData = opts.templateData, content = opts.content, file = opts.file;
if (inExtension === 'rb' || inExtension === 'ruby' || inExtension === 'erb') {
_ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
data = JSON.stringify(file.getMeta().toJSON());
source = "content = <<-EOF\n" + (templateData.content || '') + "\nEOF\ndocument = Hash.new()\nrequire 'rubygems' unless defined? Gem\nrequire 'json'\ndocument = JSON.parse <<-EOF\n" + data + "\nEOF\n";
source += inExtension === 'erb' ? "require 'erb'\ntemplate = ERB.new <<-EOF\n" + content + "\nEOF\nputs template.result(binding)" : content;
result = '';
errors = '';
ruby = spawn('ruby');
ruby.stdout.on('data', function(data) {
return result += data.toString();
});
ruby.stderr.on('data', function(data) {
return errors += data.toString();
});
ruby.on('exit', function() {
if (errors) {
return next(new Error(errors));
}
opts.content = result;
return next();
});
ruby.stdin.write(source);
return ruby.stdin.end();
} else {
return next();
}
};
return RubyPlugin;
})(BasePlugin);
};
}).call(this);