Skip to content

Instantly share code, notes, and snippets.

@Naatan
Last active November 21, 2017 08:07
Show Gist options
  • Save Naatan/7375244 to your computer and use it in GitHub Desktop.
Save Naatan/7375244 to your computer and use it in GitHub Desktop.
Komodo Macro - Execute file/selection as PHP
new function()
{
/**
* Execute Macro
*
* @returns {void}
*/
this.exec = function()
{
// Ensure we're set up correcly
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus() };
// Get text to execute
var ke = komodo.editor;
if(!ke.selText)
{
// Use all text in file
var text = ko.views.manager.currentView.scimoz.text.trim()
}
else
{
// Use selected text
var 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);
// get PHP binary path
var phpBinary = Components.classes['@activestate.com/koPrefService;1']
.getService(Components.interfaces.koIPrefService)
.prefs.getStringPref('phpDefaultInterpreter');
// Run php
ko.run.runEncodedCommand(window, phpBinary + ' -d display_errors=true ' + file);
};
/**
* Write data to file
*
* @param {string} data Data to write to file
*
* @returns {string} File path
*/
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("krsp.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();
}
@Naatan
Copy link
Author

Naatan commented Jul 9, 2014

Try running the command manually from your command prompt

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