Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added linux support #9

Merged
merged 1 commit into from

2 participants

@hadesgames

Added linux support using rb-inotify.

The Javascript is compiled using CoffeeScript 1.3.1

@lancejpollard lancejpollard merged commit f47841a into from
@lancejpollard

Thanks @hadesgames, I really appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 14, 2012
  1. @hadesgames

    Added linux support

    hadesgames authored
This page is out of date. Refresh to see the latest.
View
10 design.io.js
@@ -1,6 +1,8 @@
window.DesignIO = (function() {
+ DesignIO.name = 'DesignIO';
+
function DesignIO(namespace, options) {
options || (options = {});
this.callbacks = {};
@@ -32,7 +34,9 @@ window.DesignIO = (function() {
};
DesignIO.prototype.runCallback = function(name, data) {
- if (this.callbacks[name]) this.callbacks[name].call(this, data);
+ if (this.callbacks[name]) {
+ this.callbacks[name].call(this, data);
+ }
return true;
};
@@ -68,7 +72,9 @@ window.DesignIO = (function() {
if (watcher.id === data.id) {
watcher.path = data.path;
watcher.action = data.action;
- if (watcher.hasOwnProperty(data.action)) watcher[data.action](data);
+ if (watcher.hasOwnProperty(data.action)) {
+ watcher[data.action](data);
+ }
}
}
return this.runCallback(data.action, data);
View
4 lib/design.io.js
@@ -1,3 +1,5 @@
+// Generated by CoffeeScript 1.3.1
+(function() {
require('underscore.logger');
@@ -9,3 +11,5 @@
return require("./design.io/extensions/" + name).apply(this, Array.prototype.slice.call(arguments, 1, arguments.length));
}
};
+
+}).call(this);
View
20 lib/design.io/client.js
@@ -1,13 +1,17 @@
+// Generated by CoffeeScript 1.3.1
+(function() {
window.DesignIO = (function() {
+ DesignIO.name = 'DesignIO';
+
function DesignIO(namespace, options) {
options || (options = {});
this.callbacks = {};
this.watchers = [];
this.port = options.port || 4181;
this.namespace = namespace;
- this.url = options.url || ("" + window.location.protocol + "//" + window.location.hostname + ":" + this.port + "/" + namespace);
+ this.url = options.url || ("" + window.location.protocol + "//" + window.location.hostname + ":" + this.port);
this.socket = io.connect(this.url);
this.connect();
}
@@ -32,7 +36,9 @@
};
DesignIO.prototype.runCallback = function(name, data) {
- if (this.callbacks[name]) this.callbacks[name].call(this, data);
+ if (this.callbacks[name]) {
+ this.callbacks[name].call(this, data);
+ }
return true;
};
@@ -68,7 +74,9 @@
if (watcher.id === data.id) {
watcher.path = data.path;
watcher.action = data.action;
- if (watcher.hasOwnProperty(data.action)) watcher[data.action](data);
+ if (watcher.hasOwnProperty(data.action)) {
+ watcher[data.action](data);
+ }
}
}
return this.runCallback(data.action, data);
@@ -78,6 +86,7 @@
if (typeof data === "object") {
data.userAgent = window.navigator.userAgent;
data.url = window.location.href;
+ data.namespace = this.namespace;
}
return this.socket.emit('log', JSON.stringify(data, this.replacer));
};
@@ -85,7 +94,8 @@
DesignIO.prototype.userAgent = function() {
return {
userAgent: window.navigator.userAgent,
- url: window.location.href
+ url: window.location.href,
+ namespace: this.namespace
};
};
@@ -108,3 +118,5 @@
return DesignIO;
})();
+
+}).call(this);
View
1  lib/design.io/command.js
@@ -1,3 +1,4 @@
+// Generated by CoffeeScript 1.3.1
(function() {
var command, forever, fs;
View
11 lib/design.io/command/start.js
@@ -1,3 +1,4 @@
+// Generated by CoffeeScript 1.3.1
(function() {
var Hook, Project, app, command, connect, express, hook, io;
@@ -41,7 +42,7 @@
app.listen(command.port);
- app.use(express.static(__dirname + '/../..'));
+ app.use(express["static"](__dirname + '/../..'));
app.use(connect.bodyParser());
@@ -60,14 +61,18 @@
hook.on("*::*::watch", function(data, callback, event) {
var object;
- if (!event.name.match("design.io-watcher")) return;
+ if (!event.name.match("design.io-watcher")) {
+ return;
+ }
object = JSON.parse(data);
return io.sockets["in"](object.namespace).emit("watch", data);
});
hook.on("*::*::exec", function(data, callback, event) {
var object;
- if (!event.name.match("design.io-watcher")) return;
+ if (!event.name.match("design.io-watcher")) {
+ return;
+ }
object = JSON.parse(data);
return io.sockets["in"](object.namespace).emit("exec", data);
});
View
2  lib/design.io/command/stop.js
@@ -1,3 +1,4 @@
+// Generated by CoffeeScript 1.3.1
(function() {
var Hook, command, hook;
@@ -24,4 +25,5 @@
break
*/
+
}).call(this);
View
1  lib/design.io/command/watch.js
@@ -1,3 +1,4 @@
+// Generated by CoffeeScript 1.3.1
(function() {
var command, project;
View
4 lib/design.io/extensions/watchfile.js
@@ -1,3 +1,5 @@
+// Generated by CoffeeScript 1.3.1
+(function() {
module.exports = function() {
var project;
@@ -11,3 +13,5 @@
}
});
};
+
+}).call(this);
View
33 lib/design.io/listener.js
@@ -1,3 +1,4 @@
+// Generated by CoffeeScript 1.3.1
(function() {
var File, Listener, Pathfinder, _path;
@@ -9,6 +10,8 @@
Listener = (function() {
+ Listener.name = 'Listener';
+
function Listener(options, callback) {
var directories, files, initialized, path, paths, self, source, stat, _i, _len;
this.root = options.root;
@@ -20,7 +23,9 @@
initialized = [];
for (_i = 0, _len = paths.length; _i < _len; _i++) {
source = paths[_i];
- if (!File.exists(source)) continue;
+ if (!File.exists(source)) {
+ continue;
+ }
stat = File.stat(source);
path = _path.join(root, source.replace(root, ""));
if (!stat.isDirectory()) {
@@ -44,7 +49,9 @@
_ref = this.ignored;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
ignoredPath = _ref[_i];
- if (path.indexOf(ignoredPath) === 0) return true;
+ if (path.indexOf(ignoredPath) === 0) {
+ return true;
+ }
}
return false;
};
@@ -57,7 +64,9 @@
directories = this.directories;
files = this.files;
base = this.root;
- if (this.ignore(path)) return;
+ if (this.ignore(path)) {
+ return;
+ }
if (directories[path] && entries.length < directories[path].length) {
directories = this.directories;
action = "destroy";
@@ -76,13 +85,19 @@
_results = [];
for (_i = 0, _len = entries.length; _i < _len; _i++) {
entry = entries[_i];
- if (entry === '.' || entry === '..') continue;
+ if (entry === '.' || entry === '..') {
+ continue;
+ }
absolutePath = File.join(path, entry);
current = File.stat(absolutePath);
- if (current.isDirectory()) continue;
+ if (current.isDirectory()) {
+ continue;
+ }
previous = files[absolutePath];
changed = !(previous && current.size === previous.size && current.mtime.getTime() === previous.mtime.getTime());
- if (!changed) continue;
+ if (!changed) {
+ continue;
+ }
files[absolutePath] = current;
if (!previous) {
action || (action = "create");
@@ -102,7 +117,9 @@
Listener.prototype.log = function(path, options, callback) {
var name;
- if (options == null) options = {};
+ if (options == null) {
+ options = {};
+ }
name = options.action === "destroy" ? "deleted" : "" + options.action + "d";
_console.info("" + name + " " + path);
try {
@@ -122,6 +139,8 @@
require('./listener/windows');
+ require('./listener/linux');
+
module.exports = Listener;
}).call(this);
View
49 lib/design.io/listener/linux.js
@@ -0,0 +1,49 @@
+// Generated by CoffeeScript 1.3.1
+(function() {
+ var Linux,
+ __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; };
+
+ Linux = (function(_super) {
+
+ __extends(Linux, _super);
+
+ Linux.name = 'Linux';
+
+ function Linux(options, callback) {
+ var child, forever,
+ _this = this;
+ Linux.__super__.constructor.apply(this, arguments);
+ forever = require("forever");
+ child = forever.start(["ruby", "" + __dirname + "/linux.rb", this.root.replace(" ", "\\ ")], {
+ max: 10,
+ silent: true
+ });
+ child.on("stdout", function(data) {
+ var path, _i, _len, _results;
+ data = data.toString().trim();
+ try {
+ data = JSON.parse("[" + data.replace(/\]\[/g, ",").replace(/[\[\]]/g, "") + "]");
+ _results = [];
+ for (_i = 0, _len = data.length; _i < _len; _i++) {
+ path = data[_i];
+ _results.push(_this.changed(path.slice(0, -1), callback));
+ }
+ return _results;
+ } catch (error) {
+ return _console.error(error.toString());
+ }
+ });
+ child.on("stderr", function(data) {
+ return _console.error(data.toString().trim());
+ });
+ forever.startServer(child);
+ }
+
+ return Linux;
+
+ })(require('../listener'));
+
+ module.exports = Linux;
+
+}).call(this);
View
15 lib/design.io/listener/linux.rb
@@ -0,0 +1,15 @@
+require 'rubygems'
+require 'rb-inotify'
+
+notifier = INotify::Notifier.new
+STDOUT.sync = true
+io = STDOUT
+directory = ARGV[0]
+
+#directory = STDIN.read
+notifier.watch directory, :modify, :recursive do |event|
+ # Eliminate the file name
+ path = File.dirname(event.absolute_name) + '/'
+ io.write [path].inspect
+end
+notifier.run
View
18 lib/design.io/listener/mac.js
@@ -1,14 +1,18 @@
+// Generated by CoffeeScript 1.3.1
(function() {
- var Mac;
- var __hasProp = Object.prototype.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; };
+ var Mac,
+ __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; };
- Mac = (function() {
+ Mac = (function(_super) {
- __extends(Mac, require('../listener'));
+ __extends(Mac, _super);
+
+ Mac.name = 'Mac';
function Mac(options, callback) {
- var child, forever;
- var _this = this;
+ var child, forever,
+ _this = this;
Mac.__super__.constructor.apply(this, arguments);
forever = require("forever");
child = forever.start(["ruby", "" + __dirname + "/mac.rb", this.root.replace(" ", "\\ ")], {
@@ -38,7 +42,7 @@
return Mac;
- })();
+ })(require('../listener'));
module.exports = Mac;
View
16 lib/design.io/listener/polling.js
@@ -1,18 +1,22 @@
+// Generated by CoffeeScript 1.3.1
(function() {
- var Polling;
- var __hasProp = Object.prototype.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; };
+ var Polling,
+ __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; };
- Polling = (function() {
+ Polling = (function(_super) {
- __extends(Polling, require('../listener'));
+ __extends(Polling, _super);
+
+ Polling.name = 'Polling';
function Polling() {
- Polling.__super__.constructor.apply(this, arguments);
+ return Polling.__super__.constructor.apply(this, arguments);
}
return Polling;
- })();
+ })(require('../listener'));
module.exports = Polling;
View
16 lib/design.io/listener/windows.js
@@ -1,18 +1,22 @@
+// Generated by CoffeeScript 1.3.1
(function() {
- var Windows;
- var __hasProp = Object.prototype.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; };
+ var Windows,
+ __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; };
- Windows = (function() {
+ Windows = (function(_super) {
- __extends(Windows, require('../listener'));
+ __extends(Windows, _super);
+
+ Windows.name = 'Windows';
function Windows() {
- Windows.__super__.constructor.apply(this, arguments);
+ return Windows.__super__.constructor.apply(this, arguments);
}
return Windows;
- })();
+ })(require('../listener'));
module.exports = Windows;
View
64 lib/design.io/project.js
@@ -1,6 +1,7 @@
+// Generated by CoffeeScript 1.3.1
(function() {
- var File, Hook, Pathfinder, Project, Watcher, async, fs, mint;
- var __slice = Array.prototype.slice;
+ var File, Hook, Pathfinder, Project, Watcher, async, fs, mint,
+ __slice = [].slice;
Hook = require("hook.io").Hook;
@@ -18,6 +19,8 @@
Project = (function() {
+ Project.name = 'Project';
+
Project.store = function() {
return this._store || (this._store = {});
};
@@ -35,7 +38,9 @@
_ref = this.all();
for (namespace in _ref) {
project = _ref[namespace];
- if (project.root === cwd) return project;
+ if (project.root === cwd) {
+ return project;
+ }
}
return null;
}
@@ -59,9 +64,22 @@
}
};
+ Project.prototype.getListenerByPlatform = function() {
+ switch (process.platform) {
+ case "linux":
+ return require("./listener/linux");
+ case "darwin":
+ return require("./listener/mac");
+ default:
+ return require("./listener/mac");
+ }
+ };
+
function Project(options) {
var store;
- if (options == null) options = {};
+ if (options == null) {
+ options = {};
+ }
this.root = File.absolutePath(options.root);
this.namespace = options.namespace;
this.watchfile = File.absolutePath(options.watchfile);
@@ -70,8 +88,12 @@
this.watchers = [];
this.growl = options.growl !== false;
Watcher.pathfinder = this.pathfinder;
- if (!this.watchfile) throw new Error("You must specify the watchfile");
- if (!this.root) throw new Error("You must specify the directory to watch");
+ if (!this.watchfile) {
+ throw new Error("You must specify the watchfile");
+ }
+ if (!this.root) {
+ throw new Error("You must specify the directory to watch");
+ }
store = this.constructor.store();
if (store.hasOwnProperty(this.namespace)) {
throw new Error("Only one project per namespace");
@@ -79,19 +101,19 @@
store[this.namespace] = this;
this.hook = new Hook({
name: "design.io-watcher::" + this.namespace,
- debug: true,
- silent: false
+ debug: false,
+ silent: true
});
}
Project.prototype.watch = function() {
- var hook;
- var _this = this;
+ var hook,
+ _this = this;
hook = this.hook;
hook.on("hook::ready", function(data) {
hook.emit("ready", data);
_this.read(function() {
- return new (require('./listener/mac'))({
+ return new (_this.getListenerByPlatform())({
root: _this.root,
ignore: _this.ignoredPaths
}, function(path, options) {
@@ -123,9 +145,9 @@
Project.prototype.createWatcher = function() {
return this.watchers.push((function(func, args, ctor) {
ctor.prototype = func.prototype;
- var child = new ctor, result = func.apply(child, args);
- return typeof result === "object" ? result : child;
- })(Watcher, [this].concat(__slice.call(arguments)), function() {}));
+ var child = new ctor, result = func.apply(child, args), t = typeof result;
+ return t == "object" || t == "function" ? result || child : child;
+ })(Watcher, [this].concat(__slice.call(arguments)), function(){}));
};
Project.prototype.read = function(callback) {
@@ -144,13 +166,17 @@
result = "__project = require(\"design.io/lib/design.io/project\").find(\"" + _this.namespace + "\")\n\nignorePaths = ->\n __project.ignoredPaths ||= []\n __project.ignoredPaths = __project.ignoredPaths.concat Array.prototype.slice.call(arguments, 0, arguments.length)\n\nwatch = ->\n __project.createWatcher(arguments...)\n\n" + result;
return mint.coffee(result, {}, function(error, result) {
mainModule._compile(result, mainModule.filename);
- if (callback) return callback.call(_this);
+ if (callback) {
+ return callback.call(_this);
+ }
});
});
};
Project.prototype.changed = function(path, options) {
- if (options == null) options = {};
+ if (options == null) {
+ options = {};
+ }
return this.queue.push({
binding: this,
path: path,
@@ -197,7 +223,9 @@
Project.prototype.broadcast = function(action, data, callback) {
if (data.action === "initialize") {
- if (callback) callback.call(this, null, null);
+ if (callback) {
+ callback.call(this, null, null);
+ }
return;
}
data.namespace || (data.namespace = this.namespace);
@@ -233,6 +261,8 @@
Project.Watchfile = (function() {
+ Watchfile.name = 'Watchfile';
+
function Watchfile() {
Watcher._store = void 0;
}
View
51 lib/design.io/watcher.js
@@ -1,6 +1,7 @@
+// Generated by CoffeeScript 1.3.1
(function() {
- var File, Pathfinder, Project, Watcher, fs, path, uuid;
- var __slice = Array.prototype.slice;
+ var File, Pathfinder, Project, Watcher, fs, path, uuid,
+ __slice = [].slice;
fs = require('fs');
@@ -18,18 +19,26 @@
Watcher = (function() {
+ Watcher.name = 'Watcher';
+
function Watcher() {
var arg, args, key, methods, project, value, _i, _len;
project = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
this.project = project;
methods = args.pop();
- if (typeof methods === "function") methods = methods.call(this);
- if (args[0] instanceof Array) args = args[0];
+ if (typeof methods === "function") {
+ methods = methods.call(this);
+ }
+ if (args[0] instanceof Array) {
+ args = args[0];
+ }
this.ignore = null;
this.patterns = [];
for (_i = 0, _len = args.length; _i < _len; _i++) {
arg = args[_i];
- if (!arg) continue;
+ if (!arg) {
+ continue;
+ }
this.patterns.push(typeof arg === "string" ? new RegExp(arg) : arg);
}
for (key in methods) {
@@ -37,7 +46,9 @@
this[key] = value;
}
this.id || (this.id = uuid());
- if (this.hasOwnProperty("server")) this.server.watcher = this;
+ if (this.hasOwnProperty("server")) {
+ this.server.watcher = this;
+ }
}
Watcher.prototype.initialize = function(path, callback) {
@@ -51,7 +62,9 @@
Watcher.prototype.update = function(path, callback) {
var _this = this;
return fs.readFile(path, 'utf-8', function(error, result) {
- if (error) return _this.error(error);
+ if (error) {
+ return _this.error(error);
+ }
return _this.broadcast({
body: result
}, callback);
@@ -67,31 +80,39 @@
};
Watcher.prototype.error = function(error, callback) {
- if (this.action !== "initialize") console.log(error.stack || error);
+ if (this.action !== "initialize") {
+ console.log(error.stack || error);
+ }
if (this.project.growl) {
require("growl")(error.message, {
title: this.project.namespace,
sticky: false
});
}
- if (callback) callback();
+ if (callback) {
+ callback();
+ }
return false;
};
Watcher.prototype.match = function(path) {
var pattern, patterns, _i, _len;
- if (this.ignore && !!this.ignore.exec(path)) return false;
+ if (this.ignore && !!this.ignore.exec(path)) {
+ return false;
+ }
patterns = this.patterns;
for (_i = 0, _len = patterns.length; _i < _len; _i++) {
pattern = patterns[_i];
- if (!!pattern.exec(path)) return true;
+ if (!!pattern.exec(path)) {
+ return true;
+ }
}
return false;
};
Watcher.prototype.invoke = function(path, options, next) {
- var action, duration, timeout, timeoutError, timestamp, watcherCallback;
- var _this = this;
+ var action, duration, timeout, timeoutError, timestamp, watcherCallback,
+ _this = this;
action = options.action;
timestamp = options.timestamp;
duration = options.duration;
@@ -101,7 +122,9 @@
this.timestamp = timestamp;
watcherCallback = function(error) {
clearTimeout(timeout);
- if (error) console.log(error.stack || error);
+ if (error) {
+ console.log(error.stack || error);
+ }
return process.nextTick(next);
};
timeoutError = function() {
View
2  package.json
@@ -47,6 +47,6 @@
"hook.io": ">= 0.8.0"
},
"scripts": {
- "install": "gem install rb-fsevent --no-ri --no-rdoc"
+ "install": "gem install rb-fsevent rb-inotify --no-ri --no-rdoc"
}
}
View
1  src/design.io/listener.coffee
@@ -92,5 +92,6 @@ class Listener
require './listener/mac'
require './listener/polling'
require './listener/windows'
+require './listener/linux'
module.exports = Listener
View
26 src/design.io/listener/linux.coffee
@@ -0,0 +1,26 @@
+class Linux extends (require('../listener'))
+ constructor: (options, callback) ->
+ super
+
+ forever = require("forever")
+
+ child = forever.start ["ruby","#{__dirname}/linux.rb", @root.replace(" ", "\\ ")],
+ max: 10
+ silent: true
+
+ child.on "stdout", (data) =>
+ data = data.toString().trim()
+ try
+ data = JSON.parse("[" + data.replace(/\]\[/g, ",").replace(/[\[\]]/g, "") + "]")
+ # console.log(data.toString().trim())
+ for path in data
+ @changed(path[0..-2], callback)
+ catch error
+ _console.error error.toString()
+
+ child.on "stderr", (data) ->
+ _console.error data.toString().trim()
+
+ forever.startServer(child)
+
+module.exports = Linux
View
9 src/design.io/project.coffee
@@ -43,6 +43,13 @@ class Project
eval(value)
else
value
+
+ getListenerByPlatform: () ->
+ switch process.platform
+ when "linux" then return require("./listener/linux")
+ when "darwin" then return require("./listener/mac")
+ else return require("./listener/mac")
+
constructor: (options = {}) ->
@root = File.absolutePath(options.root)
@@ -71,7 +78,7 @@ class Project
hook.emit "ready", data
@read =>
- new (require('./listener/mac')) root: @root, ignore: @ignoredPaths, (path, options) =>
+ new (@getListenerByPlatform()) root: @root, ignore: @ignoredPaths, (path, options) =>
options.namespace = @namespace
options.paths = if path instanceof Array then path else [path]
for path in options.paths
Something went wrong with that request. Please try again.