Skip to content

Commit

Permalink
add install/uninstall helper
Browse files Browse the repository at this point in the history
* install add the completer script to the according shell config file only
if not present yet.
* uninstall removes the completer script from the bashrc/zshrc file.

process.env.SHELL is used to know which file to read/write. Basically, just
helpers that does the same as completer completion >> ~/.bashrc
  • Loading branch information
mklabs committed Oct 23, 2011
1 parent 9ccd0d7 commit 6cfb0ee
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 8 deletions.
10 changes: 8 additions & 2 deletions bin/cake-complete
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ console.log([
'» You may want to setup the completion in your ~/.bashrc or ~/.zshrc file, if not already.',
'',
' cake-complete completion >> ~/.bashrc',
'Or',
' cake-complete completion >> ~/.zshrc'
'Or, using zsh:',
' cake-complete completion >> ~/.zshrc',
'',
'Or you may use the install command:',
' cake-complete completion install',
'',
'Simply run this command to uninstall',
' cake-complete completion uninstall'
].join('\n'));
8 changes: 7 additions & 1 deletion bin/play-complete
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ console.log([
'',
' play-complete completion >> ~/.bashrc',
'Or',
' play-complete completion >> ~/.zshrc'
' play-complete completion >> ~/.zshrc',
'',
'Or you may use the install command:',
' play-complete completion install',
'',
'Simply run this command to uninstall',
' play-complete completion uninstall'
].join('\n'));
8 changes: 7 additions & 1 deletion bin/rake-complete
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ console.log([
'',
' rake-complete completion >> ~/.bashrc',
'Or',
' rake-complete completion >> ~/.zshrc'
' rake-complete completion >> ~/.zshrc',
'',
'Or you may use the install command:',
' rake-complete completion install',
'',
'Simply run this command to uninstall',
' rake-complete completion uninstall'
].join('\n'));
80 changes: 76 additions & 4 deletions lib/completion.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

var fs = require('fs'),
path = require('path'),
exec = require('child_process').exec,
args = process.argv.slice(2),
completeCmd = args[0] === 'completion',
installCmd = completeCmd && args[1] === 'install',
Expand Down Expand Up @@ -28,8 +29,17 @@ exports.complete = function complete(name, completer, cb) {
// if not a complete command, return here.
if(!completeCmd) return cb();

// if install cmd, add complete script to either ~/.bashrc or ~/.zshrc
if(installCmd) return install(name + '', completer, console.log.bind(console));

// if install cmd, add complete script to either ~/.bashrc or ~/.zshrc
if(uninstallCmd) return uninstall(name, completer, console.log.bind(console));

// if the COMP_* are not in the env, then dump the install script.
if(!words || !point || !line) return script(name, completer, cb);
if(!words || !point || !line) return script(name, completer, function(err, content) {
if(err) return cb(err);
console.log(content);
});

var partial = line.substr(0, point),
last = line.split(' ').slice(-1).join(''),
Expand Down Expand Up @@ -95,16 +105,78 @@ function abbrev(o) { return function(it) {
// output the completion.sh script to the console for install instructions.
// This is actually a 'template' where the package name is used to setup
// the completion on the right command, and properly name the bash/zsh functions.
function script (name, completer, cb) {
function script(name, completer, cb) {
var p = path.resolve(__dirname, 'completion/completion.sh');

fs.readFile(p, 'utf8', function (er, d) {
if (er) return cb(er);
d = d
.replace(/\{pkgname\}/g, name)
.replace(/{completer}/g, completer);
console.log(d);
cb();
cb(null, d);
});
}

function install(name, completer, cb) {
console.log('Installing completion for ', name, ' using ', completer);
var markerIn = '###-begin-' + name + '-completion-###',
markerOut = '###-end-' + name + '-completion-###';

installed(markerIn, completer, function(isInstalled) {
if(isInstalled) {
return cb(' ✗ ', completer, 'has been already installed. Do nothing.');
}

// probably, will prompt user for installation here (y/n)
exec(completer + ' completion >> ~/.' + process.env.SHELL.match(/\/bin\/(\w+)/)[1] + 'rc', function(err) {
if(err) return cb(err);
return cb(' ✓ ', completer, 'installed.');
});
});
}

function uninstall(name, completer, cb) {
console.log('Uninstalling completion for ', name);

var markerIn = '###-begin-' + name + '-completion-###',
markerOut = '###-end-' + name + '-completion-###';

readRc(completer, function(err, file) {
if(err) return cb(err);

var part = file.split(markerIn)[1];
if(!part) return cb(' ✗ ', completer, 'does not appear in your .zshrc or .bashrc file. Do nothing.');

part = markerIn + part.split(markerOut)[0] + markerOut;
writeRc(file.replace(part, ''), function(err) {
if(err) return cb(err);
return cb(' ✓ ', completer, 'uninstalled.');
});
});
}

function readRc(completer, cb) {
var file = '.' + process.env.SHELL.match(/\/bin\/(\w+)/)[1] + 'rc',
filepath = path.join(process.env.HOME, file);
path.exists(filepath, function (state) {
if(!state) return cb(new Error("No " + file + " file. You'll have to run instead: " + completer + " completion >> ~/" + file));
fs.readFile(filepath, 'utf8', cb);
});
}

function writeRc(content, cb) {
var file = '.' + process.env.SHELL.match(/\/bin\/(\w+)/)[1] + 'rc',
filepath = path.join(process.env.HOME, file);
path.exists(filepath, function (state) {
if(!state) return cb(new Error("No " + file + " file. You'll have to run instead: " + completer + " completion >> ~/" + file));
fs.writeFile(filepath, content, cb);
});
}

function installed (marker, completer, cb) {
readRc(completer, function(err, file) {
if(err) return cb(err);
var installed = file.match(marker);
return cb(!!installed);
});
}

0 comments on commit 6cfb0ee

Please sign in to comment.