Skip to content

Instantly share code, notes, and snippets.

@sams
Created January 28, 2011 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sams/799599 to your computer and use it in GitHub Desktop.
Save sams/799599 to your computer and use it in GitHub Desktop.
(function() {
/*****************************************************************************
* Aims: (as yet untested) *
* Use sass-convert and add support for Scss *
* Dont rely on cygwin - option to use msysgit *
* *
* *
* EDIT CONSTANTS BELOW TO MATCH YOUR MACHINE SETUP *
* *
* Cygwin 1.7 and Ruby must be installed first (install using setup-1.7.exe) *
* Unix machines DO NOT need Cygwin. Instead use /usr/bin as CYGWIN_PATH *
* *
* To install Sass, follow instructions here first: *
* http://www.cygwin.com/ml/cygwin/2007-05/msg00439.html *
* Start Cygwin and run: *
* > gem install haml *
* *
* To install CoffeeScript, start Cygwin and run: *
* > gem install coffee-script *
* *
* java executable is recommended to be on PATH, however you can specify *
* a full path below. *
* *
* Remember to escape \ as \\ *
* *
****************************************************************************/
// Download from http://www.java.com/en/download/manual.jsp
const JAVA_PATH = 'C:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe';
// Download from http://yuilibrary.com/downloads/#yuicompressor
const YUI_PATH = 'C:\\Users\\sams\\yuicompressor-2.4.2.jar';
// Download from http://closure-compiler.googlecode.com/files/compiler-latest.zip
const CLOSURE_COMPILER_PATH = 'C:\\Users\\sams\\compiler.jar';
// use either msysgit or cygwin - MSYS ot CYG - then set the path below
const GIT_IS = 'MSYS';
// Download from http://cygwin.com/setup-1.7.exe
const CYGWIN_PATH = 'C:\\bin\\cygwin\\';
// Path to msysgit
const MSYSGIT_PATH = 'C:\\Program Files (x86)\\Git\\bin\\'
/** DON'T EDIT PAST THIS LINE ***********************************************/
var useGit = MSYSGIT_PATH;
var supportedCompilers = { 'yui-css': ['"' + JAVA_PATH + '" -jar "' + YUI_PATH + '" --charset utf-8 -v --type=css -o "{1}" "{0}"'],
'yui-js': ['"' + JAVA_PATH + '" -jar "' + YUI_PATH + '" --charset utf-8 -v --type=js -o "{1}" "{0}"'],
'closure-compiler-js': ['"' + JAVA_PATH + '" -jar "' + CLOSURE_COMPILER_PATH + '" --js "{0}" --js_output_file "{1}"'],
'sass': ['"sass-convert -F css -T sass "{0}" "{1}"',
''],
'scss': ['"sass-convert -F css -T scss " "{0}" "{1}"',
''],
'coffee': ['"' + MSYSGIT_PATH + 'bin\\coffee" --no-wrap --print "{0}" > "{1}"',
'PATH=' + MSYSGIT_PATH + 'bin;' + MSYSGIT_PATH + 'lib\nNODOSFILEWARNING=1'] };
var knownTypes = { '.uncompressed.css': ['yui-css', '.css'],
'.unprocessed.css': ['yui-css', '.css'],
'.uncompressed.js': ['closure-compiler-js', '.js'],
'.unprocessed.js': ['closure-compiler-js', '.js'],
'.jssrc': ['closure-compiler-js', '.js'],
'.sass': ['sass', '.css'],
'.scss': ['scss', '.css'],
'.coffee': ['coffee', '.uncompressed.js'] };
const Cc = Components.classes;
const Ci = Components.interfaces;
if (typeof (extensions) === 'undefined')
window.extensions = {};
if (typeof (window.extensions.compileObserver) === 'undefined')
window.extensions.compileObserver = null;
window.extensions.compileInputBaseName = null;
const INDEX_COMMAND_LINE = 0;
const INDEX_ENVIRONMENT = 1;
const INDEX_COMPILER = 0;
const INDEX_FILE_EXTENSION = 1;
var observerSvc = Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService);
function ProcessObserver(command, process, callback) {
this._command = command;
this._process = process;
this._callback = (callback || function() {});
observerSvc.addObserver(this, 'run_terminated', false);
try {
this._process.wait(0);
this.cleanUp();
} catch (exception) {};
};
ProcessObserver.prototype.observe = function(child, topic, command) {
if ('run_terminated' === topic &&
this._command === command) {
this.cleanUp();
this._process = null;
}
};
ProcessObserver.prototype.cleanUp = function() {
if (this._command) {
observerSvc.removeObserver(this, 'run_terminated');
this._command = null;
}
if (this._process) {
var processExitCode = this._process.wait(-1),
processOutput = (this._process.getStdout() || this._process.getStderr());
this._callback(processExitCode, processOutput, this._process);
this._process = null;
}
};
ProcessObserver.prototype.kill = function() {
if (this._command) {
observerSvc.removeObserver(this, 'run_terminated');
this._command = null;
}
if (this._process) {
this._process.kill(-1);
this._process = null;
}
};
function compile(inputFilePath, inputBaseName, inputDirectoryPath)
{
for (var knownExtension in knownTypes)
if (knownTypes.hasOwnProperty(knownExtension) &&
inputFilePath.indexOf(knownExtension) === inputFilePath.length - knownExtension.length) {
var outputFilePath = inputFilePath.substr(0, inputFilePath.length - knownExtension.length) + knownTypes[knownExtension][INDEX_FILE_EXTENSION],
outputBaseName = inputBaseName.substr(0, inputBaseName.length - knownExtension.length) + knownTypes[knownExtension][INDEX_FILE_EXTENSION],
compilerName = knownTypes[knownExtension][INDEX_COMPILER],
commandLine = supportedCompilers[compilerName][INDEX_COMMAND_LINE],
commandEnvironment = supportedCompilers[compilerName][INDEX_ENVIRONMENT];
commandLine = commandLine.replace('{0}', inputFilePath, 'g')
.replace('{1}', outputFilePath, 'g');
StatusBar_AddMessage("Compiling '" + inputBaseName + "' to '" + outputBaseName + "' using '" + compilerName + "'...", 'run_command', 12500, false);
var runSvc = Cc['@activestate.com/koRunService;1'].getService(Ci.koIRunService);
try {
if (window.extensions.compileObserver)
window.extensions.compileObserver.kill();
var process = runSvc.RunAndNotify(commandLine, inputDirectoryPath, commandEnvironment, null);
window.extensions.compileObserver = new ProcessObserver(commandLine, process, function(processExitCode, processOutput) {
if (processExitCode === 0) {
var continueChain = false;
for (knownExtension in knownTypes)
if (knownTypes.hasOwnProperty(knownExtension) &&
outputFilePath.indexOf(knownExtension) === outputFilePath.length - knownExtension.length) {
if ( ! window.extensions.compileInputBaseName)
window.extensions.compileInputBaseName = inputBaseName;
compile(outputFilePath, outputBaseName, inputDirectoryPath);
continueChain = true;
}
if ( ! continueChain)
StatusBar_AddMessage("Done compiling '" + (window.extensions.compileInputBaseName ? window.extensions.compileInputBaseName : inputBaseName) + "' to '" + outputBaseName + "'.", 'run_command', 1500, false);
} else {
StatusBar_AddMessage("Error compiling '" + inputBaseName + "' using '" + compilerName + "'.", 'run_command', 2500, true);
ko.dialogs.alert("Error compiling '" + inputBaseName + "' [" + processExitCode + "] using '" + compilerName + "'.",
"Output:\n"
+ processOutput
+ "\n________________________\n\n"
+ "Command line:\n" + commandLine);
}
});
} catch (exception) {
var lastErrorSvc = Cc['@activestate.com/koLastErrorService;1'].getService(Ci.koILastErrorService);
var errorMessage = lastErrorSvc.getLastErrorMessage();
if ( ! errorMessage)
errorMessage = exception;
ko.dialogs.alert('Whoops! Compile encountered an exception:', errorMessage);
throw exception;
}
}
}
if (ko.views.manager &&
ko.views.manager.currentView &&
ko.views.manager.currentView.getAttribute('type') === 'editor' &&
ko.views.manager.currentView.document) {
var view = ko.views.manager.currentView,
document = view.document,
inputFilePath, inputBaseName, inputDirectoryPath;
compile(inputFilePath = document.file.path,
inputBaseName = document.file.baseName,
inputDirectoryPath = inputFilePath.substr(0, inputFilePath.length - inputBaseName.length));
view.setFocus();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment