Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created July 31, 2013 21:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Gozala/6126308 to your computer and use it in GitHub Desktop.
execute command with GCLI
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: {}
})
@joewalker
Copy link

For what it's worth you should soon be able to do this:

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);

let gcliExec = (command) => {
  return requisition.exec(command).then(output => {
    if (output.error) throw output.data;
    else return output.data;
  });
};

gcliExec({
  command: "tilt toggle",
  args: {}
});

There are 3 changes that I need to make:

  • CommandUtils.createEnvironment should default to current window (not done yet)
  • gcli.jsm should export commonly used classes (not done yet)
  • requisition.exec should return the promise of an output not an output itself (done, not committed)

@joewalker
Copy link

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