Skip to content

Instantly share code, notes, and snippets.

@DanielG
Created November 19, 2010 21:02
Show Gist options
  • Save DanielG/707163 to your computer and use it in GitHub Desktop.
Save DanielG/707163 to your computer and use it in GitHub Desktop.
Quick & dirty CMake repl
#! /usr/bin/env node
var fs = require("fs");
var rl = require('readline');
var util = require('util');
var spawn = require('child_process').spawn;
var tmpfiles = [];
var tmpfile = function(callback){
var tmpath = '/tmp/.xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx.cmake'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
tmpfiles.push(tmpath);
fs.open(tmpath, 'w', 0666, function(err, fd){
callback(err, fd, tmpath);
});
}
process.on('exit', function(){
tmpfiles.forEach(function(file){
fs.unlinkSync(file);
});
});
var stdin = process.openStdin();
var prompt = "cmake $ ";
var cmake_cwd = undefined;
var rli = rl.createInterface(stdin, function(text){
console.log('complete?');
return text;
});
rli.setPrompt(prompt);
rli.prompt();
stdin.on("data", function (chunk) {
rli.write(chunk);
});
rli.on('line', function (cmd) {
if(!cmd) {
rli.prompt();
return;
}
// if(cmd.indexOf('.') == 0) {
// return replCommand(cmd);
// }
cmd = trimWhitespace(cmd);
cmd = cmd.replace('&', '_CMAKE_repl_OUT__');
tmpfile(function(err, fd, tmpath){
var cmake_code = cmd + '\n'
+ 'foreach(i ${_CMAKE_repl_OUT__})\n'
+ ' message(STATUS ${i})\n'
+ 'endforeach()\n';
var buf = new Buffer(cmake_code);
fs.write(fd, buf, 0, buf.length, null, function(err, written){
if(err) throw err;
if(written < buf.length) throw new Error('write was not atomic, too lazy to implemente');
fs.close(fd, function(){
var cmake = spawn('/usr/bin/cmake', ['-P', tmpath]);
var out_data = '';
cmake.stdout.on('data', function(data){
out_data += data;
});
cmake.stderr.on('data', function(data){
util.print(data.toString());
});
cmake.on('exit', function(code){
console.log(out_data);
rli.prompt();
});
});
});
});
});
rli.on('close', function(callback){
stdin.destroy();
});
function trimWhitespace (cmd) {
var trimmer = /^\s*(.+)\s*$/m,
matches = trimmer.exec(cmd);
if (matches && matches.length === 2) {
return matches[1];
}
}
function regexpEscape(s) {
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment