Skip to content

Instantly share code, notes, and snippets.

@lsloan
Forked from Naatan/executeAsPHP.js
Last active December 14, 2016 15:58
Show Gist options
  • Save lsloan/e18b424a1085c4e6352a to your computer and use it in GitHub Desktop.
Save lsloan/e18b424a1085c4e6352a to your computer and use it in GitHub Desktop.
JavaScript macro for Komodo Edit (or IDE) to run file/selection in PHP interpreter
Gist title: "Komodo: Run Current File or Selection with PHP"
Summary: Save this small JavaScript program to the Toolbox in Komodo Edit (or IDE), assign a key binding to it, and use it to execute the current file (or the selected code if there is any) in a PHP interpreter.
new function () {
/**
* Execute Macro
*
* @returns {void}
*/
this.exec = function () {
// Check for required macro API version and environment
komodo.assertMacroVersion(3);
if (komodo.view) {
komodo.view.setFocus()
};
// Get path to PHP interpreter
var phpInterpreter = Components.classes[
'@activestate.com/koPrefService;1']
.getService(Components.interfaces.koIPrefService)
.prefs.getStringPref('phpDefaultInterpreter');
// Alert if no PHP interpreter specified
if (phpInterpreter.length == 0) {
alert('A PHP interpreter must be specified in Komodo Preferences.');
return false;
}
// Get text to execute
var ke = komodo.editor;
var text;
if (!ke.selText) {
// Use all text in file
text = ko.views.manager.currentView.scimoz.text.trim()
} else {
// Use selected text
text = komodo.interpolate('%s').trim();
}
// Prepend PHP opening tag if necessary
if (text.substr(0, 2) != '<?') {
text = '<?php ' + text;
}
// Write temporary file
var file = this.writeFile(text);
// Run PHP interpreter on contents of temporary file
ko.run.runEncodedCommand(window, phpInterpreter +
' -d display_errors=true ' + file);
};
/**
* Write data to a temporary file
*
* @param {string} data Data to write to file
*
* @returns {string} Path to temporary file
*/
this.writeFile = function (data) {
// Get temp file path
var tmpFile = Components.classes[
'@mozilla.org/file/directory_service;1'].getService(Components.interfaces
.nsIProperties).get('TmpD', Components.interfaces.nsIFile);
tmpFile.append('komodo_code_selection.php');
// Initialize (create) file
var file = Components.classes['@mozilla.org/file/local;1'].createInstance(
Components.interfaces.nsILocalFile);
file.initWithPath(tmpFile.path);
// Open stream to file
var foStream = Components.classes[
'@mozilla.org/network/file-output-stream;1'].createInstance(
Components.interfaces.nsIFileOutputStream);
foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
// Use converter to ensure UTF-8 encoding
var converter = Components.classes[
'@mozilla.org/intl/converter-output-stream;1'].createInstance(
Components.interfaces.nsIConverterOutputStream);
// Write to file
converter.init(foStream, 'UTF-8', 0, 0);
converter.writeString(data);
converter.close();
return tmpFile.path;
};
this.exec();
}
@lsloan
Copy link
Author

lsloan commented Jun 17, 2014

I added a check for the path to the PHP interpreter binary which will display an error message in an alert when it's empty.

@Naatan
Copy link

Naatan commented Jun 26, 2014

Nice. Fwiw rather than indenting the whole else block you could just return false in the conditional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment