toolkit/devtools/gcli/commands/cmd.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const { Cc, Ci, Cu } = require("chrome");
michael@0 8
michael@0 9 const { Promise: promise } = require("resource://gre/modules/Promise.jsm");
michael@0 10
michael@0 11 const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
michael@0 12 const { TextEncoder, TextDecoder } = Cu.import('resource://gre/modules/commonjs/toolkit/loader.js', {});
michael@0 13 const gcli = require("gcli/index");
michael@0 14
michael@0 15 loader.lazyGetter(this, "prefBranch", function() {
michael@0 16 let prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
michael@0 17 return prefService.getBranch(null).QueryInterface(Ci.nsIPrefBranch2);
michael@0 18 });
michael@0 19
michael@0 20 loader.lazyGetter(this, "supportsString", function() {
michael@0 21 return Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
michael@0 22 });
michael@0 23
michael@0 24 loader.lazyImporter(this, "NetUtil", "resource://gre/modules/NetUtil.jsm");
michael@0 25
michael@0 26 const PREF_DIR = "devtools.commands.dir";
michael@0 27
michael@0 28 /**
michael@0 29 * Load all the .mozcmd files in the directory pointed to by PREF_DIR
michael@0 30 * @return A promise of an array of items suitable for gcli.addItems or
michael@0 31 * using in gcli.addItemsByModule
michael@0 32 */
michael@0 33 function loadItemsFromMozDir() {
michael@0 34 let dirName = prefBranch.getComplexValue(PREF_DIR,
michael@0 35 Ci.nsISupportsString).data.trim();
michael@0 36 if (dirName == "") {
michael@0 37 return promise.resolve([]);
michael@0 38 }
michael@0 39
michael@0 40 // replaces ~ with the home directory path in unix and windows
michael@0 41 if (dirName.indexOf("~") == 0) {
michael@0 42 let dirService = Cc["@mozilla.org/file/directory_service;1"]
michael@0 43 .getService(Ci.nsIProperties);
michael@0 44 let homeDirFile = dirService.get("Home", Ci.nsIFile);
michael@0 45 let homeDir = homeDirFile.path;
michael@0 46 dirName = dirName.substr(1);
michael@0 47 dirName = homeDir + dirName;
michael@0 48 }
michael@0 49
michael@0 50 // statPromise resolves to nothing if dirName is a directory, or it
michael@0 51 // rejects with an error message otherwise
michael@0 52 let statPromise = OS.File.stat(dirName);
michael@0 53 statPromise = statPromise.then(
michael@0 54 function onSuccess(stat) {
michael@0 55 if (!stat.isDir) {
michael@0 56 throw new Error("'" + dirName + "' is not a directory.");
michael@0 57 }
michael@0 58 },
michael@0 59 function onFailure(reason) {
michael@0 60 if (reason instanceof OS.File.Error && reason.becauseNoSuchFile) {
michael@0 61 throw new Error("'" + dirName + "' does not exist.");
michael@0 62 } else {
michael@0 63 throw reason;
michael@0 64 }
michael@0 65 }
michael@0 66 );
michael@0 67
michael@0 68 // We need to return (a promise of) an array of items from the *.mozcmd
michael@0 69 // files in dirName (which we can assume to be a valid directory now)
michael@0 70 return statPromise.then(() => {
michael@0 71 let itemPromises = [];
michael@0 72
michael@0 73 let iterator = new OS.File.DirectoryIterator(dirName);
michael@0 74 let iterPromise = iterator.forEach(entry => {
michael@0 75 if (entry.name.match(/.*\.mozcmd$/) && !entry.isDir) {
michael@0 76 itemPromises.push(loadCommandFile(entry));
michael@0 77 }
michael@0 78 });
michael@0 79
michael@0 80 return iterPromise.then(() => {
michael@0 81 iterator.close();
michael@0 82 return promise.all(itemPromises).then((itemsArray) => {
michael@0 83 return itemsArray.reduce((prev, curr) => {
michael@0 84 return prev.concat(curr);
michael@0 85 }, []);
michael@0 86 });
michael@0 87 }, reason => { iterator.close(); throw reason; });
michael@0 88 });
michael@0 89 }
michael@0 90
michael@0 91 exports.mozDirLoader = function(name) {
michael@0 92 return loadItemsFromMozDir().then(items => {
michael@0 93 return { items: items };
michael@0 94 });
michael@0 95 };
michael@0 96
michael@0 97 /**
michael@0 98 * Load the commands from a single file
michael@0 99 * @param OS.File.DirectoryIterator.Entry entry The DirectoryIterator
michael@0 100 * Entry of the file containing the commands that we should read
michael@0 101 */
michael@0 102 function loadCommandFile(entry) {
michael@0 103 let readPromise = OS.File.read(entry.path);
michael@0 104 return readPromise = readPromise.then(array => {
michael@0 105 let decoder = new TextDecoder();
michael@0 106 let source = decoder.decode(array);
michael@0 107 var principal = Cc["@mozilla.org/systemprincipal;1"]
michael@0 108 .createInstance(Ci.nsIPrincipal);
michael@0 109
michael@0 110 let sandbox = new Cu.Sandbox(principal, {
michael@0 111 sandboxName: entry.path
michael@0 112 });
michael@0 113 let data = Cu.evalInSandbox(source, sandbox, "1.8", entry.name, 1);
michael@0 114
michael@0 115 if (!Array.isArray(data)) {
michael@0 116 console.error("Command file '" + entry.name + "' does not have top level array.");
michael@0 117 return;
michael@0 118 }
michael@0 119
michael@0 120 return data;
michael@0 121 });
michael@0 122 }
michael@0 123
michael@0 124 exports.items = [
michael@0 125 {
michael@0 126 name: "cmd",
michael@0 127 get hidden() {
michael@0 128 return !prefBranch.prefHasUserValue(PREF_DIR);
michael@0 129 },
michael@0 130 description: gcli.lookup("cmdDesc")
michael@0 131 },
michael@0 132 {
michael@0 133 name: "cmd refresh",
michael@0 134 description: gcli.lookup("cmdRefreshDesc"),
michael@0 135 get hidden() {
michael@0 136 return !prefBranch.prefHasUserValue(PREF_DIR);
michael@0 137 },
michael@0 138 exec: function(args, context) {
michael@0 139 gcli.load();
michael@0 140
michael@0 141 let dirName = prefBranch.getComplexValue(PREF_DIR,
michael@0 142 Ci.nsISupportsString).data.trim();
michael@0 143 return gcli.lookupFormat("cmdStatus2", [ dirName ]);
michael@0 144 }
michael@0 145 },
michael@0 146 {
michael@0 147 name: "cmd setdir",
michael@0 148 description: gcli.lookup("cmdSetdirDesc"),
michael@0 149 params: [
michael@0 150 {
michael@0 151 name: "directory",
michael@0 152 description: gcli.lookup("cmdSetdirDirectoryDesc"),
michael@0 153 type: {
michael@0 154 name: "file",
michael@0 155 filetype: "directory",
michael@0 156 existing: "yes"
michael@0 157 },
michael@0 158 defaultValue: null
michael@0 159 }
michael@0 160 ],
michael@0 161 returnType: "string",
michael@0 162 get hidden() {
michael@0 163 return true; // !prefBranch.prefHasUserValue(PREF_DIR);
michael@0 164 },
michael@0 165 exec: function(args, context) {
michael@0 166 supportsString.data = args.directory;
michael@0 167 prefBranch.setComplexValue(PREF_DIR, Ci.nsISupportsString, supportsString);
michael@0 168
michael@0 169 gcli.load();
michael@0 170
michael@0 171 return gcli.lookupFormat("cmdStatus2", [ args.directory ]);
michael@0 172 }
michael@0 173 }
michael@0 174 ];

mercurial