execute command with GCLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"] | |
.getService(Ci.nsIWindowMediator); | |
Cu.import("resource://gre/modules/devtools/gcli.jsm", {}); | |
let { CommandUtils } = Cu.import("resource:///modules/devtools/DeveloperToolbar.jsm", {}); | |
let { require } = Cu.import("resource://gre/modules/devtools/Require.jsm", {}); | |
let { Requisition } = require("gcli/cli"); | |
let { Promise: { defer } } = Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js", {}); | |
let gcliExec = (command) => { | |
let chromeWindow = command.chromeWindow || | |
windowMediator.getMostRecentWindow("navigator:browser"); | |
let contentWindow = command.contentWindow || | |
chromeWindow.gBrowser.selectedTab.linkedBrowser.contentWindow; | |
let environment = CommandUtils.createEnvironment(chromeWindow.document, | |
contentWindow.document); | |
let requisition = new Requisition(environment); | |
let output = requisition.exec(command); | |
return output.promise.then(function() { | |
return output.data | |
}, function() { | |
throw Error(output.data) | |
}) | |
} | |
gcliExec({ | |
command: "tilt toggle", | |
args: {} | |
}) |
Status of the changes outlined above:
- CommandUtils.createEnvironment should default to current window (done, not committed)
- gcli.jsm should export commonly used classes (done, not committed)
- requisition.exec should return the promise of an output not an output itself (committed)
A few extra notes worth mentioning:
I don't think it's a good idea to express command failure in the state of the promise for 2 reasons:
- command-line failure is another failure condition that we may need to convey, and conflating command failure (in which case we'd probably just display an error and continue) with command-line failure (in which case more drastic action may be needed) isn't helpful.
- The typical action of command success and command failure is typically similar: display the response for the user to see. Perhaps with differing styling in the error case
That given the code above becomes:
let { Requisition } = Cu.import("resource://gre/modules/devtools/gcli.jsm", {});
let { CommandUtils } = Cu.import("resource:///modules/devtools/DeveloperToolbar.jsm", {});
let environment = CommandUtils.createEnvironment();
let requisition = new Requisition(environment);
requisition.exec({
command: "tilt toggle",
args: {}
});
I am considering moving CommandUtils into gcli.jsm, in which case this would become:
let gcli = Cu.import("resource://gre/modules/devtools/gcli.jsm", {});
let environment = gcli.createEnvironment();
let requisition = new gcli.Requisition(environment);
requisition.exec({
command: "tilt toggle",
args: {}
});
I'm intending on taking further tweaks on a case by case basis because it's not totally clear what is beneficial to hide.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For what it's worth you should soon be able to do this:
There are 3 changes that I need to make: