kopia lustrzana https://github.com/c9/core
remove old code
rodzic
e12789ecbb
commit
02d465b463
|
@ -81,30 +81,15 @@ module.exports = function(options) {
|
|||
// baseUrl: options.apiUrl
|
||||
// },
|
||||
"plugins/c9.core/util",
|
||||
{
|
||||
packagePath: "plugins/c9.ide.plugins/loader",
|
||||
plugins: options.plugins || [],
|
||||
loadFromDisk: options.standalone
|
||||
},
|
||||
{
|
||||
packagePath: "plugins/c9.ide.plugins/installer",
|
||||
updates: options.pluginUpdates || []
|
||||
},
|
||||
{
|
||||
packagePath: "plugins/c9.ide.plugins/manager",
|
||||
staticPrefix: staticPrefix + "/plugins/c9.ide.plugins",
|
||||
devel: devel
|
||||
},
|
||||
{
|
||||
packagePath: "plugins/c9.ide.plugins/debug"
|
||||
},
|
||||
{
|
||||
packagePath: "plugins/c9.ide.plugins/packages"
|
||||
},
|
||||
{
|
||||
packagePath: "plugins/c9.ide.plugins/test",
|
||||
staticPrefix: staticPrefix + "/plugins/c9.ide.plugins"
|
||||
},
|
||||
// {
|
||||
// packagePath: "plugins/c9.ide.plugins/test",
|
||||
// staticPrefix: staticPrefix + "/plugins/c9.ide.plugins"
|
||||
// },
|
||||
|
||||
// VFS
|
||||
"plugins/c9.vfs.client/vfs.ping",
|
||||
|
|
|
@ -1,554 +0,0 @@
|
|||
/*global requirejs*/
|
||||
define(function(require, exports, module) {
|
||||
main.consumes = [
|
||||
"Plugin", "vfs", "fs", "plugin.loader", "c9", "ext", "watcher",
|
||||
"dialog.notification", "dialog.info", "ui", "menus", "commands", "settings", "auth",
|
||||
"installer", "find", "util", "preferences.experimental"
|
||||
];
|
||||
main.provides = ["plugin.debug"];
|
||||
return main;
|
||||
|
||||
function main(options, imports, register) {
|
||||
var Plugin = imports.Plugin;
|
||||
var vfs = imports.vfs;
|
||||
var watcher = imports.watcher;
|
||||
var ext = imports.ext;
|
||||
var util = imports.util;
|
||||
var find = imports.find;
|
||||
var ui = imports.ui;
|
||||
var menus = imports.menus;
|
||||
var installer = imports.installer;
|
||||
var settings = imports.settings;
|
||||
var commands = imports.commands;
|
||||
var fs = imports.fs;
|
||||
var c9 = imports.c9;
|
||||
var auth = imports.auth;
|
||||
var loader = imports["plugin.loader"];
|
||||
var notify = imports["dialog.notification"].show;
|
||||
var experimental = imports["preferences.experimental"];
|
||||
var showInfo = imports["dialog.info"].show;
|
||||
|
||||
var dirname = require("path").dirname;
|
||||
var basename = require("path").basename;
|
||||
var join = require("path").join;
|
||||
var async = require("async");
|
||||
|
||||
var architect;
|
||||
|
||||
/***** Initialization *****/
|
||||
|
||||
var plugin = new Plugin("Ajax.org", main.consumes);
|
||||
var emit = plugin.getEmitter();
|
||||
|
||||
var plugins = [];
|
||||
|
||||
var ENABLED = c9.location.indexOf("debug=2") > -1;
|
||||
var HASSDK = ENABLED || experimental.addExperiment("sdk", false, "SDK/Load Custom Plugins");
|
||||
|
||||
var reParts = /^(builders|keymaps|modes|outline|runners|snippets|themes|templates)\/(.*)/;
|
||||
var reModule = /(?:_highlight_rules|_test|_worker|_fold|_behaviou?r)\.js$/;
|
||||
var jsExtRe = /\.js$/;
|
||||
|
||||
var loaded = false;
|
||||
function load() {
|
||||
if (loaded) return false;
|
||||
loaded = true;
|
||||
|
||||
if (!HASSDK) return;
|
||||
|
||||
menus.addItemByPath("Tools/~", new ui.divider(), 100000, plugin);
|
||||
menus.addItemByPath("Tools/Developer", null, 100100, plugin);
|
||||
|
||||
if (!ENABLED) {
|
||||
menus.addItemByPath("Tools/Developer/Start in Debug Mode", new ui.item({
|
||||
onclick: function() {
|
||||
var url = location.href + (location.href.indexOf("?") > -1
|
||||
? "&debug=2"
|
||||
: "?debug=2");
|
||||
window.open(url);
|
||||
}
|
||||
}), 900, plugin);
|
||||
}
|
||||
|
||||
if (!ENABLED) return;
|
||||
|
||||
notify("<div class='c9-readonly'>You are in <span style='color:rgb(245, 234, 15)'>Debug</span> Mode. "
|
||||
+ "Don't forget to open the browser's dev tools to see any errors.",
|
||||
false);
|
||||
|
||||
// Insert relevant LESS libraries
|
||||
var theme = settings.get("user/general/@skin");
|
||||
|
||||
ui.defineLessLibrary(require("text!../c9.ide.layout.classic/themes/default-" + theme + ".less"), plugin);
|
||||
ui.defineLessLibrary(require("text!../c9.ide.layout.classic/less/lesshat.less"), plugin);
|
||||
|
||||
fs.readdir("~/.c9/plugins", function(err, list) {
|
||||
if (err) return console.error(err);
|
||||
|
||||
var names = loader.plugins;
|
||||
var toLoad = [];
|
||||
|
||||
list.forEach(function(stat) {
|
||||
var name = stat.name;
|
||||
// If the plugin doesn't exist
|
||||
if (names.indexOf(name) == -1 && name.charAt(0) != "." && name.charAt(0) != "_")
|
||||
toLoad.push(name);
|
||||
});
|
||||
|
||||
loadPlugins(toLoad);
|
||||
});
|
||||
|
||||
commands.addCommand({
|
||||
name: "reloadCustomPlugin",
|
||||
group: "Plugins",
|
||||
bindKey: {
|
||||
mac: "Command-Enter",
|
||||
win: "Ctrl-Enter"
|
||||
},
|
||||
exec: function() {
|
||||
reloadPluginUI();
|
||||
}
|
||||
}, plugin);
|
||||
|
||||
menus.addItemByPath("Tools/Developer/Reload Custom Plugin", new ui.item({
|
||||
command: "reloadCustomPlugin"
|
||||
}), 1000, plugin);
|
||||
}
|
||||
|
||||
/***** Methods *****/
|
||||
|
||||
function loadPlugins(list) {
|
||||
if (!vfs.connected) {
|
||||
vfs.once("connect", loadPlugins.bind(this, config));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof list == "string")
|
||||
list = [list];
|
||||
|
||||
var config = [];
|
||||
var loadConfig = function() {
|
||||
architect.loadAdditionalPlugins(config, function(err) {
|
||||
if (err) console.error(err);
|
||||
});
|
||||
};
|
||||
|
||||
async.each(list, function(name, next) {
|
||||
var resourceHolder = new Plugin();
|
||||
var resourceVersion = "";
|
||||
|
||||
resourceHolder.on("load", function() {
|
||||
if (inited) load();
|
||||
});
|
||||
|
||||
resourceHolder.freezePublicAPI({
|
||||
get version() { return resourceVersion; },
|
||||
set version(v) { resourceVersion = v; }
|
||||
});
|
||||
|
||||
var inited = false;
|
||||
function load() {
|
||||
async.parallel([
|
||||
function(next) {
|
||||
// Fetch package.json
|
||||
fs.readFile("~/.c9/plugins/" + name + "/package.json", function(err, data) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
try {
|
||||
var options = JSON.parse(data);
|
||||
if (!options.plugins)
|
||||
throw new Error("Missing plugins property in package.json of " + name);
|
||||
}
|
||||
catch (e) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
var host = vfs.baseUrl + "/";
|
||||
var base = join(String(c9.projectId),
|
||||
"plugins", auth.accessToken);
|
||||
|
||||
// Configure Require.js
|
||||
var pathConfig = {};
|
||||
pathConfig["plugins/" + name] = host + join(base, name);
|
||||
requirejs.config({ paths: pathConfig });
|
||||
|
||||
// Add the plugin to the config
|
||||
Object.keys(options.plugins).forEach(function(path) {
|
||||
var pluginPath = name + "/" + path;
|
||||
|
||||
// Watch project path
|
||||
watch("~/.c9/plugins/" + pluginPath);
|
||||
var cfg = options.plugins[path];
|
||||
cfg.packagePath = "plugins/" + name + "/" + path;
|
||||
cfg.staticPrefix = host + join(base, name);
|
||||
cfg.apikey = "0000000000000000000000000000=";
|
||||
|
||||
// Set version for package manager
|
||||
cfg.version = options.version;
|
||||
|
||||
config.push(cfg);
|
||||
plugins.push(name + "/" + path);
|
||||
});
|
||||
|
||||
// Set version for package manager
|
||||
resourceHolder.version = options.version;
|
||||
|
||||
// Start the installer if one is included
|
||||
if (options.installer) {
|
||||
addStaticPlugin("installer", name, options.installer,
|
||||
null, resourceHolder);
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
},
|
||||
function(next) {
|
||||
var path = join("~/.c9/plugins", name);
|
||||
var rePath = new RegExp("^" + util.escapeRegExp(path.replace(/^~/, c9.home) + "/"), "gm");
|
||||
find.getFileList({
|
||||
path: path,
|
||||
nocache: true,
|
||||
buffer: true
|
||||
}, function(err, data) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
// Remove the base path
|
||||
data = data.replace(rePath, "");
|
||||
|
||||
if (data.match(/^__installed__.js/))
|
||||
return next("installed");
|
||||
|
||||
// Process all the submodules
|
||||
var parallel = processModules(path, data, resourceHolder);
|
||||
async.parallel(parallel, function(err, data) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
if (!inited)
|
||||
resourceHolder.load(name + ".bundle");
|
||||
|
||||
// Done
|
||||
next();
|
||||
});
|
||||
});
|
||||
}
|
||||
], function(err, results) {
|
||||
if (err) console.error(err);
|
||||
|
||||
if (!inited) {
|
||||
next();
|
||||
inited = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
load();
|
||||
}, function() {
|
||||
emit.sticky("ready");
|
||||
|
||||
if (!config.length) return;
|
||||
|
||||
// Load config
|
||||
if (installer.sessions.length) {
|
||||
installer.on("stop", function listen(err) {
|
||||
if (err)
|
||||
return console.error(err);
|
||||
|
||||
if (!installer.sessions.length) {
|
||||
loadConfig();
|
||||
installer.off("stop", listen);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
loadConfig();
|
||||
});
|
||||
}
|
||||
|
||||
function processModules(path, data, plugin) {
|
||||
var parallel = [];
|
||||
|
||||
data.split("\n").forEach(function(line) {
|
||||
if (!line.match(reParts)) return;
|
||||
|
||||
var type = RegExp.$1;
|
||||
var filename = RegExp.$2;
|
||||
if (filename.indexOf("/") > -1) return;
|
||||
|
||||
if (type == "modes" && (reModule.test(filename) || !jsExtRe.test(filename)))
|
||||
return;
|
||||
|
||||
if (type == "snippets") {
|
||||
if (jsExtRe.test(filename)) {
|
||||
var snippetPath = join("plugins", basename(path), type, filename).replace(jsExtRe, "");
|
||||
require([snippetPath], function(m) {
|
||||
architect.services["language.complete"].addSnippet(m, plugin);
|
||||
});
|
||||
}
|
||||
if (!/\.snippets$/.test(filename))
|
||||
return;
|
||||
}
|
||||
|
||||
parallel.push(function(next) {
|
||||
fs.readFile(join(path, type, filename), function(err, data) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return next(err);
|
||||
}
|
||||
|
||||
addStaticPlugin(type, basename(path), filename, data, plugin);
|
||||
|
||||
next();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return parallel;
|
||||
}
|
||||
|
||||
function addStaticPlugin(type, pluginName, filename, data, plugin) {
|
||||
var services = architect.services;
|
||||
var path = "plugins/" + pluginName + "/"
|
||||
+ (type == "installer" ? "" : type + "/")
|
||||
+ filename.replace(/\.js$/, "");
|
||||
|
||||
var bundleName = pluginName + ".bundle";
|
||||
if (!services[bundleName] && type !== "installer") {
|
||||
services[bundleName] = plugin;
|
||||
architect.lut["~/.c9/plugins/" + pluginName] = {
|
||||
provides: []
|
||||
};
|
||||
architect.pluginToPackage[bundleName] = {
|
||||
path: "~/.c9/plugins/" + pluginName,
|
||||
package: pluginName,
|
||||
version: plugin.version,
|
||||
isAdditionalMode: true
|
||||
};
|
||||
if (!architect.packages[pluginName])
|
||||
architect.packages[pluginName] = [];
|
||||
architect.packages[pluginName].push(name);
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case "builders":
|
||||
data = util.safeParseJson(data, function() {});
|
||||
if (!data) return;
|
||||
|
||||
services.build.addBuilder(filename, data, plugin);
|
||||
break;
|
||||
case "keymaps":
|
||||
data = util.safeParseJson(data, function() {});
|
||||
if (!data) return;
|
||||
|
||||
services["preferences.keybindings"].addCustomKeymap(filename, data, plugin);
|
||||
break;
|
||||
case "modes":
|
||||
var mode = {};
|
||||
var firstLine = data.split("\n", 1)[0].replace(/\/\*|\*\//g, "").trim();
|
||||
firstLine.split(";").forEach(function(n) {
|
||||
if (!n) return;
|
||||
var info = n.split(":");
|
||||
mode[info[0].trim()] = info[1].trim();
|
||||
});
|
||||
|
||||
services.ace.defineSyntax({
|
||||
name: path,
|
||||
caption: mode.caption,
|
||||
extensions: (mode.extensions || "").trim()
|
||||
.replace(/\s*,\s*/g, "|").replace(/(^|\|)\./g, "$1")
|
||||
});
|
||||
break;
|
||||
case "outline":
|
||||
if (!data) return;
|
||||
|
||||
services.outline.addOutlinePlugin(path, data, plugin);
|
||||
break;
|
||||
case "runners":
|
||||
data = util.safeParseJson(data, function() {});
|
||||
if (!data) return;
|
||||
|
||||
services.run.addRunner(data.caption || filename, data, plugin);
|
||||
break;
|
||||
case "snippets":
|
||||
services["language.complete"].addSnippet(data, plugin);
|
||||
break;
|
||||
case "themes":
|
||||
services.ace.addTheme(data, plugin);
|
||||
break;
|
||||
case "templates":
|
||||
services.newresource.addFileTemplate(data, plugin);
|
||||
break;
|
||||
case "installer":
|
||||
if (data) {
|
||||
installer.createSession(pluginName, data, function(v, o) {
|
||||
require([path], function(fn) {
|
||||
fn(v, o);
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
require([path], function(fn) {
|
||||
installer.createSession(pluginName, fn.version, function(v, o) {
|
||||
fn(v, o);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if require.s.contexts._ can help watching all dependencies
|
||||
function watch(path) {
|
||||
watcher.watch(path);
|
||||
|
||||
watcher.on("change", function(e) {
|
||||
if (e.path == path)
|
||||
reloadPackage(path.replace(/^~\/\.c9\//, ""));
|
||||
});
|
||||
watcher.on("delete", function(e) {
|
||||
if (e.path == path)
|
||||
reloadPackage(path.replace(/^~\/\.c9\//, ""));
|
||||
});
|
||||
watcher.on("failed", function(e) {
|
||||
if (e.path == path) {
|
||||
setTimeout(function() {
|
||||
watcher.watch(path); // Retries once after 1s
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function reloadPackage(path) {
|
||||
var unloaded = [];
|
||||
|
||||
function recurUnload(name) {
|
||||
var plugin = architect.services[name];
|
||||
unloaded.push(name);
|
||||
|
||||
// Find all the dependencies
|
||||
var deps = ext.getDependencies(plugin.name);
|
||||
|
||||
// Unload all the dependencies (and their deps)
|
||||
deps.forEach(function(name) {
|
||||
recurUnload(name);
|
||||
});
|
||||
|
||||
// Unload plugin
|
||||
plugin.unload();
|
||||
}
|
||||
|
||||
// Recursively unload plugin
|
||||
var p = architect.lut[path];
|
||||
if (p.provides) { // Plugin might not been initialized all the way
|
||||
p.provides.forEach(function(name) {
|
||||
recurUnload(name);
|
||||
});
|
||||
}
|
||||
|
||||
// create reverse lookup table
|
||||
var rlut = {};
|
||||
for (var packagePath in architect.lut) {
|
||||
var provides = architect.lut[packagePath].provides;
|
||||
if (provides) { // Plugin might not been initialized all the way
|
||||
provides.forEach(function(name) {
|
||||
rlut[name] = packagePath;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Build config of unloaded plugins
|
||||
var config = [], done = {};
|
||||
unloaded.forEach(function(name) {
|
||||
var packagePath = rlut[name];
|
||||
|
||||
// Make sure we include each plugin only once
|
||||
if (done[packagePath]) return;
|
||||
done[packagePath] = true;
|
||||
|
||||
var options = architect.lut[packagePath];
|
||||
delete options.provides;
|
||||
delete options.consumes;
|
||||
delete options.setup;
|
||||
|
||||
config.push(options);
|
||||
|
||||
// Clear require cache
|
||||
requirejs.undef(options.packagePath); // global
|
||||
});
|
||||
|
||||
// Load all plugins again
|
||||
architect.loadAdditionalPlugins(config, function(err) {
|
||||
if (err) console.error(err);
|
||||
});
|
||||
}
|
||||
|
||||
function reloadPluginUI() {
|
||||
var list = [];
|
||||
Object.keys(architect.pluginToPackage).forEach(function(name) {
|
||||
if (architect.pluginToPackage[name].isAdditionalMode)
|
||||
list.push(architect.pluginToPackage[name].path);
|
||||
});
|
||||
|
||||
var path = list[list.length - 1];
|
||||
reloadPackage(path.replace(/^~\/\.c9\//, ""));
|
||||
|
||||
// Avoid confusion with "Reload Last Plugin"
|
||||
var href = document.location.href.replace(/[?&]reload=[^&]+/, "");
|
||||
window.history.replaceState(window.history.state, null, href);
|
||||
showInfo("Reloaded " + path + ".", 1000);
|
||||
}
|
||||
|
||||
/***** Lifecycle *****/
|
||||
|
||||
plugin.on("load", function() {
|
||||
load();
|
||||
});
|
||||
plugin.on("unload", function() {
|
||||
loaded = false;
|
||||
});
|
||||
|
||||
/***** Register and define API *****/
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
plugin.freezePublicAPI({
|
||||
/**
|
||||
*
|
||||
*/
|
||||
get architect() { throw new Error(); },
|
||||
set architect(v) { architect = v; },
|
||||
/**
|
||||
*
|
||||
*/
|
||||
get plugins() { return plugins; },
|
||||
|
||||
_events: [
|
||||
/**
|
||||
* @event ready
|
||||
*/
|
||||
"ready"
|
||||
],
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
addStaticPlugin: addStaticPlugin,
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
reloadPackage: reloadPackage,
|
||||
/**
|
||||
*
|
||||
*/
|
||||
loadPackage: loadPlugins
|
||||
});
|
||||
|
||||
register(null, {
|
||||
"plugin.debug": plugin
|
||||
});
|
||||
}
|
||||
});
|
|
@ -1,196 +0,0 @@
|
|||
define(function(require, exports, module) {
|
||||
main.consumes = [
|
||||
"Plugin", "proc", "c9", "pubsub", "auth", "util", "installer",
|
||||
"preferences.experimental"
|
||||
];
|
||||
main.provides = ["plugin.installer"];
|
||||
return main;
|
||||
|
||||
function main(options, imports, register) {
|
||||
var Plugin = imports.Plugin;
|
||||
var c9 = imports.c9;
|
||||
var util = imports.util;
|
||||
var proc = imports.proc;
|
||||
var auth = imports.auth;
|
||||
var pubsub = imports.pubsub;
|
||||
var installer = imports.installer;
|
||||
var experimental = imports["preferences.experimental"];
|
||||
|
||||
var async = require("async");
|
||||
|
||||
var escapeShell = util.escapeShell;
|
||||
var updates = options.updates;
|
||||
var architect;
|
||||
|
||||
/***** Initialization *****/
|
||||
|
||||
var plugin = new Plugin("Ajax.org", main.consumes);
|
||||
var emit = plugin.getEmitter();
|
||||
|
||||
var DEBUG_MODE = c9.location.indexOf("debug=2") > -1;
|
||||
var HASSDK = DEBUG_MODE || experimental.addExperiment("sdk", false, "SDK/Load Custom Plugins");
|
||||
|
||||
var installing;
|
||||
|
||||
var loaded = false;
|
||||
function load() {
|
||||
if (loaded) return false;
|
||||
loaded = true;
|
||||
|
||||
if (!HASSDK) return;
|
||||
|
||||
pubsub.on("message", function(message) {
|
||||
if (message.type != "package")
|
||||
return;
|
||||
|
||||
console.log("PubSub package API message", message);
|
||||
var action = message.action;
|
||||
var body = message.body;
|
||||
|
||||
// Only accept packages that are installed for this project
|
||||
if (body.pid && body.pid != c9.projectId)
|
||||
return;
|
||||
|
||||
// Only accept packages that are installed for this user
|
||||
if (body.uid && body.uid != c9.uid)
|
||||
return;
|
||||
|
||||
if (action == "install") {
|
||||
installPlugins([body.config], function() {});
|
||||
}
|
||||
else if (action == "uninstall") {
|
||||
uninstallPlugin(body.name, function() {});
|
||||
}
|
||||
});
|
||||
|
||||
installPlugins(updates);
|
||||
}
|
||||
|
||||
/***** Methods *****/
|
||||
|
||||
function installPlugins(config, callback) {
|
||||
// if (!vfs.connected) {
|
||||
// vfs.once("connect", loadPackages.bind(this, config));
|
||||
// return;
|
||||
// }
|
||||
|
||||
if (!config.length)
|
||||
return callback && callback();
|
||||
|
||||
// Only run one installer at a time
|
||||
if (installing) {
|
||||
return plugin.once("finished", function() {
|
||||
installPlugins(config, callback);
|
||||
});
|
||||
}
|
||||
|
||||
installing = true;
|
||||
|
||||
var found = {}, packages = [];
|
||||
config.forEach(function(item) {
|
||||
if (typeof item === "string") {
|
||||
item = { name: item, version: null };
|
||||
}
|
||||
|
||||
if (found[item.name]) return;
|
||||
found[item.name] = true;
|
||||
|
||||
packages.push({ name: item.name, version: item.version });
|
||||
});
|
||||
|
||||
async.eachSeries(packages, function(pkg, next) {
|
||||
installPlugin(pkg.name, pkg.version, next);
|
||||
}, function(err) {
|
||||
installing = false;
|
||||
emit("finished");
|
||||
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
return callback && callback(err);
|
||||
}
|
||||
|
||||
architect.loadAdditionalPlugins(config, callback);
|
||||
});
|
||||
}
|
||||
|
||||
function installPlugin(name, version, callback) {
|
||||
// Headless installation of the plugin
|
||||
installer.createSession(name, version, function(session, options) {
|
||||
var cmd = [
|
||||
"c9",
|
||||
"install",
|
||||
"--local",
|
||||
"--force",
|
||||
"--accessToken=" + auth.accessToken,
|
||||
];
|
||||
|
||||
if (version == null)
|
||||
cmd.push(escapeShell(name));
|
||||
else
|
||||
cmd.push(escapeShell(name + "@" + version));
|
||||
|
||||
session.install({
|
||||
"bash": cmd.join(" ")
|
||||
});
|
||||
|
||||
// Force to start immediately
|
||||
session.start(callback, true);
|
||||
}, function() {}, 2); // Force to not be administered
|
||||
}
|
||||
|
||||
function uninstallPlugin(name, callback) {
|
||||
// Headless uninstallation of the plugin
|
||||
installer.createSession(name, -1, function(session, options) {
|
||||
session.install({
|
||||
"bash": "c9 remove --local --force --accessToken=" + auth.accessToken
|
||||
+ " " + escapeShell(name)
|
||||
});
|
||||
|
||||
// Force to start immediately
|
||||
session.start(callback, true);
|
||||
}, function() {}, 2); // Force to not be administered
|
||||
}
|
||||
|
||||
/***** Lifecycle *****/
|
||||
|
||||
plugin.on("load", function() {
|
||||
load();
|
||||
});
|
||||
plugin.on("unload", function() {
|
||||
loaded = false;
|
||||
installing = false;
|
||||
});
|
||||
|
||||
/***** Register and define API *****/
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
plugin.freezePublicAPI({
|
||||
/**
|
||||
*
|
||||
*/
|
||||
get architect() { throw new Error(); },
|
||||
set architect(v) { architect = v; },
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
installPlugins: installPlugins,
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
installPlugin: installPlugin,
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
uninstallPlugin: uninstallPlugin
|
||||
});
|
||||
|
||||
register(null, {
|
||||
"plugin.installer": plugin
|
||||
});
|
||||
}
|
||||
});
|
|
@ -1,340 +0,0 @@
|
|||
/*global requirejs*/
|
||||
define(function(require, exports, module) {
|
||||
main.consumes = [
|
||||
"Plugin", "vfs", "c9", "plugin.installer", "fs", "auth",
|
||||
"preferences.experimental"
|
||||
];
|
||||
main.provides = ["plugin.loader"];
|
||||
return main;
|
||||
|
||||
function main(options, imports, register) {
|
||||
var Plugin = imports.Plugin;
|
||||
var vfs = imports.vfs;
|
||||
var c9 = imports.c9;
|
||||
var fs = imports.fs;
|
||||
var auth = imports.auth;
|
||||
var installer = imports["plugin.installer"];
|
||||
var experimental = imports["preferences.experimental"];
|
||||
|
||||
var dirname = require("path").dirname;
|
||||
var join = require("path").join;
|
||||
var async = require("async");
|
||||
var _ = require("lodash");
|
||||
|
||||
var architect;
|
||||
|
||||
/***** Initialization *****/
|
||||
|
||||
var plugin = new Plugin("Ajax.org", main.consumes);
|
||||
// var emit = plugin.getEmitter();
|
||||
|
||||
var DEBUG_MODE = c9.location.indexOf("debug=2") > -1;
|
||||
var ENABLED = DEBUG_MODE || experimental.addExperiment("plugins", false, "SDK/Load Plugins From Workspace");
|
||||
var HASSDK = DEBUG_MODE || experimental.addExperiment("sdk", false, "SDK/Load Custom Plugins");
|
||||
|
||||
var plugins = options.plugins;
|
||||
var loadFromDisk = options.loadFromDisk;
|
||||
|
||||
var names = [];
|
||||
|
||||
function load() {
|
||||
if (!HASSDK) return;
|
||||
if (!ENABLED) return;
|
||||
|
||||
loadPlugins(plugins);
|
||||
}
|
||||
|
||||
/***** Methods *****/
|
||||
|
||||
// TODO the resolution alghoritm used here is very inefficient
|
||||
// ideally we will use a simpler method that doesn't need to scan directories
|
||||
function loadPlugins(loaderConfig) {
|
||||
if (!vfs.connected) {
|
||||
vfs.once("connect", loadPlugins.bind(this, loaderConfig));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!loaderConfig.length && !loadFromDisk)
|
||||
return;
|
||||
|
||||
listAllPackages(function(err, resolved) {
|
||||
if (err) return console.error(err);
|
||||
|
||||
var extraPackages = {};
|
||||
// convert old format from db to the new one
|
||||
loaderConfig.forEach(function(p) {
|
||||
if (!extraPackages[p.packageName]) {
|
||||
var path = "plugins/" + p.packageName;
|
||||
extraPackages[path] = {
|
||||
apikey: p.apikey,
|
||||
packagePath: path,
|
||||
version: p.version,
|
||||
name: p.packageName
|
||||
};
|
||||
}
|
||||
});
|
||||
if (!loadFromDisk) {
|
||||
// filter packages by config instead of loading
|
||||
// everything from disk
|
||||
resolved = resolved.filter(function(config) {
|
||||
if (extraPackages[config.packagePath])
|
||||
return true;
|
||||
|
||||
console.warn("[c9.ide.loader] Not loading package "
|
||||
+ config.path + " because it is not installed, "
|
||||
+ "according to the database");
|
||||
return false;
|
||||
});
|
||||
}
|
||||
resolved.filter(function(config) {
|
||||
if (extraPackages[config.packagePath]) {
|
||||
_.assign(config, extraPackages[config.packagePath], function(x, y) { return x || y; });
|
||||
delete extraPackages[config.packagePath];
|
||||
}
|
||||
});
|
||||
Object.keys(extraPackages).forEach(function(packagePath) {
|
||||
console.warn("[c9.ide.loader] Package "
|
||||
+ packagePath + " should be installed, according "
|
||||
+ "to the database, but was not found on the filesystem. "
|
||||
+ "Try reinstalling it.");
|
||||
});
|
||||
|
||||
async.each(resolved, loadPackage, function(err) {
|
||||
if (err) console.error(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* List all packages on disk by scanning `~/.c9` and resolve the
|
||||
* detected packages by order of override priority:
|
||||
*
|
||||
* - Developer plugins in `~/.c9/dev/plugins` have the highest
|
||||
* priority to allow local development of new functionality without
|
||||
* the risk of having your changes overwritten by any update
|
||||
* mechanism.
|
||||
*
|
||||
* - Managed plugins in `~/.c9/managed/plugins` are pre-installed and
|
||||
* updated by the Cloud9 system and have a higher priority that
|
||||
* possibly outdated or unknown packages installed by the user.
|
||||
*
|
||||
* - User-installed in `~/.c9/plugins` plugins installed plugins are
|
||||
* the default priority have the lowest priority.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @param {Error=} callback.err
|
||||
*/
|
||||
function listAllPackages(callback) {
|
||||
async.parallel({
|
||||
"plugins": async.apply(listPackages, "~/.c9/plugins"),
|
||||
"managed": async.apply(listPackages, "~/.c9/managed/plugins"),
|
||||
"dev": async.apply(listPackages, "~/.c9/dev/plugins"),
|
||||
}, function(err, packages) {
|
||||
if (err && err.code === "EDISCONNECT") {
|
||||
c9.once("connect", function() {
|
||||
listAllPackages(callback);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (err) return callback(err);
|
||||
|
||||
var resolved = {};
|
||||
|
||||
// default: ~/.c9/plugins
|
||||
packages.plugins.forEach(function(config) {
|
||||
if (!config) return;
|
||||
resolved[config.name] = config;
|
||||
});
|
||||
|
||||
// high: ~/.c9/managed/plugins
|
||||
packages.managed.forEach(function(config) {
|
||||
if (!config) return;
|
||||
resolved[config.name] = config;
|
||||
});
|
||||
|
||||
// higher: ~/.c9/dev/plugins
|
||||
packages.dev.forEach(function(config) {
|
||||
if (!config) return;
|
||||
resolved[config.name] = config;
|
||||
});
|
||||
|
||||
callback(null, _.values(resolved));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* List packages in the given directory
|
||||
*
|
||||
* @param {String} dirPath Path to the directory to scan
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @param {Error=} callback.err
|
||||
* @param {Object[]} callback.packages
|
||||
*/
|
||||
function listPackages(dirPath, callback) {
|
||||
fs.readdir(dirPath, function(err, stats) {
|
||||
if (err && err.code === "ENOENT")
|
||||
return callback(null, []);
|
||||
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
async.map(stats, function(stat, done) {
|
||||
// check for folder or symlink with folder target
|
||||
if (stat.mime !== "inode/directory"
|
||||
&& (stat.mime === "inode/symlink" && stat.linkStat.mime !== "inode/directory")
|
||||
) {
|
||||
return done();
|
||||
}
|
||||
|
||||
// check folers not prefixed with [._]
|
||||
if (stat.name[0] === "." || stat.name[0] === "_") {
|
||||
return done();
|
||||
}
|
||||
|
||||
// check and load package.json
|
||||
var config = {
|
||||
name: stat.name,
|
||||
path: absolutePath([dirPath, stat.name].join("/")),
|
||||
packagePath: ["plugins", stat.name].join("/"),
|
||||
staticPrefix: vfs.url([dirPath, stat.name].join("/")),
|
||||
};
|
||||
done(null, config);
|
||||
}, callback);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a the `package.json` metadata of the given package
|
||||
*
|
||||
* @param {Object} config The package configuration
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @param {Error=} callback.err
|
||||
* @param {Object} callback.metadata
|
||||
*/
|
||||
function loadPackageMetadata(config, callback) {
|
||||
fs.readfile([config.path, "package.json" ].join("/"), function(metadataStr) {
|
||||
var metadata;
|
||||
|
||||
try {
|
||||
metadata = JSON.parse(metadataStr);
|
||||
} catch (e) {
|
||||
return callback(e);
|
||||
}
|
||||
|
||||
callback(null, metadata);
|
||||
}, function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a the `__installed__.js` definition of the given package
|
||||
*
|
||||
* @param {Object} config The package configuration
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @param {Error=} callback.err
|
||||
* @param {Array<String|Object>} callback.installed
|
||||
*/
|
||||
function loadPackageInstalledJs(config, callback) {
|
||||
var paths = {};
|
||||
paths[config.packagePath] = config.staticPrefix;
|
||||
|
||||
requirejs.config({ paths: paths });
|
||||
requirejs.undef(config.packagePath, true);
|
||||
|
||||
require([[config.packagePath, "__installed__"].join("/")], function(installed) {
|
||||
callback(null, installed);
|
||||
}, function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the given package by checking its `__installed__.js` file or
|
||||
* its `package.json#plugins`, then call `Architect#loadAdditionalPlugins()`.
|
||||
*
|
||||
* @param {Object} config The package configuration
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @param {Error=} callback.err
|
||||
*/
|
||||
function loadPackage(config, callback) {
|
||||
loadPackageInstalledJs(config, function addToArchitect(err, installed) {
|
||||
var plugins = installed;
|
||||
if (err) {
|
||||
return callback(err);
|
||||
// TODO disbled since this doesn't handle bundles and breaks debug=2
|
||||
// loadPackageMetadata(config, function(err, metadata) {
|
||||
// if (err) return callback(err);
|
||||
// config.metadata = metadata;
|
||||
// var plugins = _.map(config.metadata.plugins, function(value, key) {
|
||||
// return [ "plugins", config.name, key ].join("/");
|
||||
// });
|
||||
// addToArchitect(err, plugins);
|
||||
// });
|
||||
}
|
||||
|
||||
var architectConfig = plugins.map(function(plugin) {
|
||||
if (typeof plugin == "string")
|
||||
plugin = { packagePath: plugin };
|
||||
|
||||
plugin.staticPrefix = config.staticPrefix;
|
||||
|
||||
plugin.packageName = config.name;
|
||||
plugin.packageMetadata = config.metadata;
|
||||
plugin.packageDir = config.path;
|
||||
|
||||
plugin.apikey = config.apikey;
|
||||
plugin.version = config.version;
|
||||
|
||||
return plugin;
|
||||
});
|
||||
|
||||
names.push(config.name);
|
||||
|
||||
architect.loadAdditionalPlugins(architectConfig, function(err) {
|
||||
callback(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function absolutePath(fromPath) {
|
||||
var toPath = fromPath.replace(/^~/, c9.home);
|
||||
return toPath;
|
||||
}
|
||||
|
||||
/***** Lifecycle *****/
|
||||
|
||||
plugin.on("load", function() {
|
||||
load();
|
||||
});
|
||||
plugin.on("unload", function() {
|
||||
});
|
||||
|
||||
/***** Register and define API *****/
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
plugin.freezePublicAPI({
|
||||
/**
|
||||
*
|
||||
*/
|
||||
get architect() { throw new Error(); },
|
||||
set architect(v) { architect = v; },
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
get plugins() { return names; }
|
||||
});
|
||||
|
||||
register(null, {
|
||||
"plugin.loader": plugin
|
||||
});
|
||||
}
|
||||
});
|
|
@ -1,202 +0,0 @@
|
|||
define(function(require, exports, module) {
|
||||
main.consumes = [
|
||||
"Editor", "editors", "ui", "commands", "menus", "layout",
|
||||
"tabManager", "util", "settings", "api", "c9",
|
||||
"preferences.experimental"
|
||||
];
|
||||
main.provides = ["plugin.packages"];
|
||||
return main;
|
||||
|
||||
function main(options, imports, register) {
|
||||
var Editor = imports.Editor;
|
||||
var editors = imports.editors;
|
||||
var tabs = imports.tabManager;
|
||||
var commands = imports.commands;
|
||||
var settings = imports.settings;
|
||||
var api = imports.api;
|
||||
var c9 = imports.c9;
|
||||
var menus = imports.menus;
|
||||
var util = imports.util;
|
||||
var layout = imports.layout;
|
||||
var ui = imports.ui;
|
||||
var experimental = imports["preferences.experimental"];
|
||||
|
||||
/***** Initialization *****/
|
||||
|
||||
var extensions = [];
|
||||
var packages = {};
|
||||
|
||||
var handle = editors.register("plugin.packages", "Package Browser",
|
||||
PackageBrowser, extensions);
|
||||
var emit = handle.getEmitter();
|
||||
emit.setMaxListeners(1000);
|
||||
|
||||
var DEBUG_MODE = c9.location.indexOf("debug=2") > -1;
|
||||
var HASSDK = DEBUG_MODE || experimental.addExperiment("sdk", false, "SDK/Load Custom Plugins");
|
||||
|
||||
function focusOpenPackages() {
|
||||
var pages = tabs.getTabs();
|
||||
for (var i = 0, tab = pages[i]; tab; tab = pages[i++]) {
|
||||
if (tab.editorType == "plugin.packages") {
|
||||
tabs.focusTab(tab);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handle.on("load", function() {
|
||||
if (!HASSDK) return;
|
||||
|
||||
settings.on("read", function() {
|
||||
settings.setDefaults("user/general", [["animateui", true]]);
|
||||
});
|
||||
|
||||
commands.addCommand({
|
||||
name: "openpackagebrowser",
|
||||
hint: "open the package browser",
|
||||
group: "General",
|
||||
// bindKey: { mac: "Command-,", win: "Ctrl-," },
|
||||
exec: function () {
|
||||
var tab = tabs.focussedTab;
|
||||
if (tab && tab.editor.type == "plugin.packages") {
|
||||
tab.close();
|
||||
return;
|
||||
}
|
||||
if (focusOpenPackages())
|
||||
return;
|
||||
|
||||
tabs.open({
|
||||
editorType: "plugin.packages",
|
||||
active: true
|
||||
}, function() {});
|
||||
}
|
||||
}, handle);
|
||||
|
||||
// menus.addItemByPath("Cloud9/~", new ui.divider(), 1000, handle);
|
||||
// menus.addItemByPath("Cloud9/Package Browser", new ui.item({
|
||||
// command: "openpackagebrowser"
|
||||
// }), 1100, handle);
|
||||
});
|
||||
|
||||
/***** Methods *****/
|
||||
|
||||
function installPlugin(name, target, callback) {
|
||||
var config = packages[name];
|
||||
if (!config)
|
||||
return callback(new Error("Could not find plugin: " + name));
|
||||
|
||||
if (target != "project" && target != "user")
|
||||
return callback(new Error("Invalid installation target: " + target));
|
||||
|
||||
api[target].put("install/" + name, function(err, state) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
function uninstallPlugin(name, target, callback) {
|
||||
var config = packages[name];
|
||||
if (!config)
|
||||
return callback(new Error("Could not find plugin: " + name));
|
||||
|
||||
if (target != "project" && target != "user")
|
||||
return callback(new Error("Invalid installation target: " + target));
|
||||
|
||||
api[target].put("uninstall/" + name, function(err, state) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
/***** Register and define API *****/
|
||||
|
||||
/**
|
||||
*
|
||||
* @extends Plugin
|
||||
* @singleton
|
||||
*/
|
||||
handle.freezePublicAPI({
|
||||
/**
|
||||
*
|
||||
*/
|
||||
installPlugin: installPlugin,
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
uninstallPlugin: uninstallPlugin
|
||||
});
|
||||
|
||||
/***** Editor *****/
|
||||
|
||||
function PackageBrowser() {
|
||||
var plugin = new Editor("Ajax.org", main.consumes, extensions);
|
||||
//var emit = plugin.getEmitter();
|
||||
var tab, iframe;
|
||||
|
||||
plugin.on("resize", function(e) {
|
||||
emit("resize", e);
|
||||
});
|
||||
|
||||
plugin.on("draw", function(e) {
|
||||
tab = e.tab;
|
||||
var htmlNode = e.htmlNode;
|
||||
|
||||
htmlNode.style.paddingTop = 0;
|
||||
|
||||
iframe = htmlNode.appendChild(document.createElement("iframe"));
|
||||
iframe.style.width = "100%";
|
||||
iframe.style.height = "100%";
|
||||
iframe.style.border = 0;
|
||||
iframe.style.backgroundColor = "#fbfbfb";
|
||||
|
||||
iframe.src = location.origin.replace("ide.", "") + "/profile/packages?nobar=1&pid=" + c9.projectId;
|
||||
});
|
||||
|
||||
plugin.on("getState", function(e) {
|
||||
});
|
||||
plugin.on("setState", function(e) {
|
||||
});
|
||||
|
||||
/***** Lifecycle *****/
|
||||
|
||||
plugin.on("documentLoad", function(e) {
|
||||
var doc = e.doc;
|
||||
doc.title = "Package Browser";
|
||||
|
||||
function setTheme() {
|
||||
var bg = "#fbfbfb";
|
||||
doc.tab.backgroundColor = bg;
|
||||
|
||||
if (util.shadeColor(bg, 1).isLight)
|
||||
doc.tab.classList.remove("dark");
|
||||
else
|
||||
doc.tab.classList.add("dark");
|
||||
}
|
||||
|
||||
layout.on("themeChange", setTheme, e.doc);
|
||||
setTheme();
|
||||
});
|
||||
|
||||
plugin.on("documentActivate", function(e) {
|
||||
|
||||
});
|
||||
|
||||
/***** Register and define API *****/
|
||||
|
||||
/**
|
||||
*
|
||||
* @extends Editor
|
||||
*/
|
||||
plugin.freezePublicAPI({
|
||||
|
||||
});
|
||||
|
||||
plugin.load(null, "plugin.packages");
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
register(null, {
|
||||
"plugin.packages": handle
|
||||
});
|
||||
}
|
||||
});
|
|
@ -1,417 +0,0 @@
|
|||
define(function(require, exports, module) {
|
||||
"use strict";
|
||||
|
||||
main.consumes = [
|
||||
"Plugin",
|
||||
"c9", "proc", "Dialog", "ui",
|
||||
];
|
||||
main.provides = ["plugin.updater.npm"];
|
||||
return main;
|
||||
|
||||
function main(options, imports, register) {
|
||||
var Plugin = imports["Plugin"];
|
||||
var c9 = imports["c9"];
|
||||
var proc = imports["proc"];
|
||||
var Dialog = imports["Dialog"];
|
||||
var ui = imports["ui"];
|
||||
|
||||
var async = require("async");
|
||||
var path = require("path");
|
||||
var semverCompare = require("semver-compare");
|
||||
|
||||
var NPM_MIN_VERSION = "2.6.0";
|
||||
|
||||
/***** Initialization *****/
|
||||
|
||||
var npmBin = options.npmBin || "/home/ubuntu/.nvm/nvm-exec";
|
||||
var managedPath = options.managedPath || "/home/ubuntu/.c9/managed";
|
||||
|
||||
var managedRcPath = [managedPath, ".npmrc"].join("/");
|
||||
var managedNpmPath = [managedPath, "npm"].join("/");
|
||||
var managedEtcPath = [managedNpmPath, "etc"].join("/");
|
||||
var managedCachePath = [managedPath, "npm", "cache"].join("/");
|
||||
var managedPluginsPath = [managedPath, "plugins"].join("/");
|
||||
var managedModulesPath = [managedPath, "node_modules"].join("/");
|
||||
|
||||
var plugin = new Plugin("Ajax.org", main.consumes);
|
||||
|
||||
function load() {
|
||||
ui.insertCss(require("text!./style.css"), false, plugin);
|
||||
|
||||
var pkgs = options.packages;
|
||||
|
||||
if (!pkgs) {
|
||||
console.warn("[plugin.updater.npm] no managed packages configured, not loading.");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: DRY error handling
|
||||
|
||||
fsMkdirs([ managedPath, managedEtcPath, managedModulesPath, managedPluginsPath ], function(err) {
|
||||
if (err) {
|
||||
console.error("[plugin.updater.npm]", err);
|
||||
showErrorDialog(err);
|
||||
return;
|
||||
}
|
||||
|
||||
fsWriteNpmrc(function(err) {
|
||||
if (err) {
|
||||
console.error("[plugin.updater.npm]", err);
|
||||
showErrorDialog(err);
|
||||
return;
|
||||
}
|
||||
|
||||
npmCheckVersion(function(err) {
|
||||
if (err) {
|
||||
console.error("[plugin.updater.npm]", err);
|
||||
showErrorDialog(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: clean up the flow for detecting and installing missing
|
||||
// packages. the nested functions are messy.
|
||||
|
||||
async.filter(pkgs, function(pkg, done) {
|
||||
npmExplorePath(pkg, function(err, path) {
|
||||
var isMissing = !!err;
|
||||
|
||||
if (isMissing)
|
||||
debug("missing package:", pkg, err);
|
||||
|
||||
done(isMissing);
|
||||
});
|
||||
}, function(missing) {
|
||||
if (missing.length) {
|
||||
console.info("[plugin.updater.npm] Installing missing plugins:", missing);
|
||||
showUpdateDialog();
|
||||
|
||||
npmInstall(missing, function(err) {
|
||||
if (err) {
|
||||
console.error("[plugin.updater.npm]", err);
|
||||
showErrorDialog(err);
|
||||
return;
|
||||
}
|
||||
|
||||
buildLinks(pkgs, function(err) {
|
||||
if (err) {
|
||||
console.error("[plugin.updater.npm]", err);
|
||||
showErrorDialog(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// reload browser
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
npmOutdated(pkgs, function(err, outdated) {
|
||||
if (err) {
|
||||
console.error("[plugin.updater.npm]", err);
|
||||
showErrorDialog(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!outdated) {
|
||||
debug("Plugins up-to-date.");
|
||||
return;
|
||||
}
|
||||
|
||||
console.info("[plugin.updater.npm] Updating outdated plugins:", outdated);
|
||||
showUpdateDialog();
|
||||
|
||||
buildLinks(pkgs, function(err) {
|
||||
if (err) {
|
||||
console.error("[plugin.updater.npm]", err);
|
||||
showErrorDialog(err);
|
||||
return;
|
||||
}
|
||||
|
||||
npmUpdate(outdated, function(err) {
|
||||
if (err) {
|
||||
console.error("[plugin.updater.npm]", err);
|
||||
alert("[plugin.updater.npm] Error: " + err.message);
|
||||
}
|
||||
|
||||
// reload browser
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function unload() {
|
||||
}
|
||||
|
||||
/* -- Helper Functions ----- */
|
||||
|
||||
function debug(format) {
|
||||
if (c9.debug) {
|
||||
var util = require("util");
|
||||
console.info(util.format.apply(null,
|
||||
Array.prototype.slice.call(arguments)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/* -- npm Management ----- */
|
||||
|
||||
/**
|
||||
* Check that the installed npm version matches the NPM_MIN_VERSION
|
||||
* required
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @param {Error=} callback.err An error if the version is lower than required
|
||||
*/
|
||||
function npmCheckVersion(callback) {
|
||||
npmExec("npm", ["-v"], function(err, stdout, stderr) {
|
||||
if (err) return callback(err, stdout, stderr);
|
||||
|
||||
var version = stdout;
|
||||
|
||||
debug("npm version", version);
|
||||
|
||||
if (semverCompare(version, NPM_MIN_VERSION) === -1) {
|
||||
var error = new Error("npm version " + NPM_MIN_VERSION + " or greater required");
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function npmExec(command, args, callback) {
|
||||
debug(npmBin, { args: [ "npm", command ].concat(args) });
|
||||
|
||||
proc.execFile(npmBin, {
|
||||
args: [ "npm", command ].concat(args),
|
||||
cwd: managedPath,
|
||||
env: {
|
||||
"npm_config_production": "true",
|
||||
"npm_config_depth": 0,
|
||||
"npm_config_userconfig": "/dev/null",
|
||||
"npm_config_prefix": managedNpmPath,
|
||||
"npm_config_cache": managedCachePath,
|
||||
},
|
||||
}, function(err, stdout, stderr) {
|
||||
debug([err, stdout, stderr]);
|
||||
|
||||
if (err) return callback(err, stdout, err.message);
|
||||
|
||||
stdout = stdout.trimRight();
|
||||
stderr = stderr.trimRight();
|
||||
|
||||
callback(null, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
/* -- Package Management ----- */
|
||||
|
||||
function npmOutdated(pkgs, callback) {
|
||||
npmExec("outdated", ["--json"], function(err, stdout, stderr) {
|
||||
if (err)
|
||||
return callback(err, null, stdout, stderr);
|
||||
|
||||
if (!stdout)
|
||||
return callback(null, null, stdout, stderr);
|
||||
|
||||
var outdated = JSON.parse(stdout);
|
||||
outdated = Object.keys(outdated);
|
||||
|
||||
callback(null, outdated, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
function npmInstall(pkgs, callback) {
|
||||
npmExec("install", ["--"].concat(pkgs), function(err, stdout, stderr) {
|
||||
callback(err, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
function npmUpdate(pkgs, callback) {
|
||||
npmExec("update", ["--"].concat(pkgs), function(err, stdout, stderr) {
|
||||
callback(err, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
function npmExplorePath(pkg, callback) {
|
||||
npmExec("explore", [pkg, "--", "pwd"], function(err, stdout, stderr) {
|
||||
if (err)
|
||||
return callback(err, null, stderr);
|
||||
|
||||
callback(null, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the symbolic links from `~/.c9/plugins` to the managed plugins.
|
||||
*
|
||||
* @param {String[]} pkgs A list of package names to link.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @param {Error=} callback.err
|
||||
*/
|
||||
function buildLinks(pkgs, callback) {
|
||||
async.each(pkgs, function(pkg, done) {
|
||||
npmExplorePath(pkg, function(err, pkgPath) {
|
||||
if (err) return done(err);
|
||||
fsLink(pkgPath, done);
|
||||
});
|
||||
}, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes symbolic links from the `~/.c9/managed/plugins` folder.
|
||||
*/
|
||||
function fsRmLinks(callback) {
|
||||
debug("find", { args: [ managedPluginsPath, "-maxdepth", "1", "-type", "l", "-exec", "rm", "{}", ";" ]});
|
||||
|
||||
// find . -maxdepth 1 -type l -exec rm {} \;
|
||||
|
||||
proc.execFile("find", {
|
||||
args: [
|
||||
managedPluginsPath,
|
||||
"-maxdepth", "1",
|
||||
"-type", "l",
|
||||
"-exec", "rm", "{}", ";"
|
||||
],
|
||||
}, function(err, stdout, stderr) {
|
||||
debug([err, stdout, stderr]);
|
||||
callback(err, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a symbolic link in `~/.c9/plugins` pointing to the given
|
||||
* plugin path.
|
||||
*
|
||||
* @param {String} pkgPath Path to the source package folder
|
||||
*/
|
||||
function fsLink(pkgPath, callback) {
|
||||
debug("ln", { args: [ "-s", "-f", pkgPath, [ managedPluginsPath, "." ].join("/") ]});
|
||||
|
||||
proc.execFile("ln", {
|
||||
args: [
|
||||
"-s", "-f",
|
||||
pkgPath,
|
||||
managedPluginsPath + "/.",
|
||||
],
|
||||
}, function(err, stdout, stderr) {
|
||||
debug([err, stdout, stderr]);
|
||||
callback(err, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
function fsMkdirs(dirPaths, callback) {
|
||||
debug("mkdir", { args: [ "-p", "--" ].concat(dirPaths) });
|
||||
|
||||
proc.execFile("mkdir", {
|
||||
args: [ "-p", "--" ].concat(dirPaths),
|
||||
}, function(err, stdout, stderr) {
|
||||
callback(err, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
function fsWriteNpmrc(callback) {
|
||||
var config = [
|
||||
"//registry.npmjs.org/:_authToken = a7c61f6e-5b10-41db-947f-8bc8f1f9468b",
|
||||
"production = true",
|
||||
"depth = 0",
|
||||
"userconfig = /dev/null",
|
||||
"prefix = " + managedNpmPath,
|
||||
"cache = " + managedCachePath,
|
||||
];
|
||||
|
||||
//
|
||||
// HACK: - fs.writeFile() does not always work? we are using echo
|
||||
// instead
|
||||
//
|
||||
// - config is not escaped
|
||||
//
|
||||
|
||||
debug("sh", { args: [ "-c", "echo '" + config.join("\\n") + "' > " + managedRcPath ]});
|
||||
|
||||
proc.execFile("sh", {
|
||||
args: [
|
||||
"-c",
|
||||
"echo '" + config.join("\\n") + "' > " + managedRcPath
|
||||
],
|
||||
}, function(err, stdout, stderr) {
|
||||
callback(err, stdout, stderr);
|
||||
});
|
||||
}
|
||||
|
||||
/* -- Interface ----- */
|
||||
|
||||
/**
|
||||
* Show a modal upgrade progress dialog, blocking the IDE interface
|
||||
* while we are updating the plugins.
|
||||
*/
|
||||
function showUpdateDialog() {
|
||||
var dialog = new Dialog("Ajax.org", [], {
|
||||
name: "plugin.updater.npm.dialog",
|
||||
class: "dialog-updater",
|
||||
allowClose: false,
|
||||
modal: true,
|
||||
elements: [
|
||||
],
|
||||
});
|
||||
|
||||
dialog.title = "Installing Updates";
|
||||
dialog.heading = "";
|
||||
dialog.body = "Your workspace will be updated to the newest version and reload automatically.";
|
||||
|
||||
dialog.show();
|
||||
|
||||
return dialog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show an upgrade error dialog, requesting to delete and recreate the
|
||||
* workspace. This is shown for critical update errors.
|
||||
*/
|
||||
function showErrorDialog(err) {
|
||||
var dialog = new Dialog("Ajax.org", [], {
|
||||
name: "plugin.updater.npm.error_dialog",
|
||||
allowClose: true,
|
||||
modal: true,
|
||||
elements: [
|
||||
],
|
||||
});
|
||||
|
||||
var errorMessage = (err && err.message) ? "" + err.message : err;
|
||||
|
||||
dialog.title = "Error installing updates";
|
||||
dialog.heading = "";
|
||||
dialog.body = "<strong>Important updates could not be installed on this workspace.</strong><br><br>"
|
||||
+ "Please delete this workspace and create a new one, in order to continue "
|
||||
+ "working in an up-to-date environment.<br><br>"
|
||||
+ "<div style='max-height: 100px; overflow: auto;'><small><code>" + errorMessage + "</code></small></div>";
|
||||
|
||||
dialog.show();
|
||||
|
||||
return dialog;
|
||||
}
|
||||
|
||||
/***** Register and define API *****/
|
||||
|
||||
plugin.on("load", load);
|
||||
plugin.on("unload", unload);
|
||||
|
||||
/**
|
||||
* @class salesforc.sync
|
||||
*/
|
||||
plugin.freezePublicAPI({
|
||||
});
|
||||
|
||||
register(null, {
|
||||
"plugin.updater.npm": plugin
|
||||
});
|
||||
}
|
||||
});
|
||||
|
Ładowanie…
Reference in New Issue