Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
refactor ExtensionLoader.init
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonsanjose committed Jan 7, 2013
1 parent a8ff90f commit 1223fac
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
44 changes: 3 additions & 41 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,6 @@ define(function (require, exports, module) {
require("utils/ExtensionUtils");


function _initExtensions() {
// allow unit tests to override which plugin folder(s) to load
var paths = params.get("extensions");

if (!paths) {
paths = "default,dev," + ExtensionLoader.getUserExtensionPath();
}

return Async.doInParallel(paths.split(","), function (item) {
var extensionPath = item;

// If the item has "/" in it, assume it is a full path. Otherwise, load
// from our source path + "/extensions/".
if (item.indexOf("/") === -1) {
extensionPath = FileUtils.getNativeBracketsDirectoryPath() + "/extensions/" + item;
}

return ExtensionLoader.loadAllExtensionsInNativeDirectory(extensionPath);
});
}

function _initTest() {
// TODO: (issue #265) Make sure the "test" object is not included in final builds
// All modules that need to be tested from the context of the application
Expand Down Expand Up @@ -246,28 +225,11 @@ define(function (require, exports, module) {
LiveDevelopmentMain.init();

PerfUtils.addMeasurement("Application Startup");

// Load extensions before restoring the project

// Create a new DirectoryEntry and call getDirectory() on the user extension
// directory. If the directory doesn't exist, it will be created.
// Note that this is an async call and there are no success or failure functions passed
// in. If the directory *doesn't* exist, it will be created. Extension loading may happen
// before the directory is finished being created, but that is okay, since the extension
// loading will work correctly without this directory.
// If the directory *does* exist, nothing else needs to be done. It will be scanned normally
// during extension loading.
var extensionPath = ExtensionLoader.getUserExtensionPath();
new NativeFileSystem.DirectoryEntry().getDirectory(extensionPath,
{create: true});

// Create the extensions/disabled directory, too.
var disabledExtensionPath = extensionPath.replace(/\/user$/, "/disabled");
new NativeFileSystem.DirectoryEntry().getDirectory(disabledExtensionPath,
{create: true});

// Load all extensions
_initExtensions().always(function () {
var extensionLoaderPromimse = ExtensionLoader.init(params.get("extensions"));

This comment has been minimized.

Copy link
@DennisKehrig

DennisKehrig Jan 14, 2013

Contributor

That should be a promise, not a promimse :)


extensionLoaderPromimse.always(function () {
// Finish UI initialization
var initialProjectPath = ProjectManager.getInitialProjectPath();
ProjectManager.openProject(initialProjectPath).always(function () {
Expand Down
3 changes: 3 additions & 0 deletions src/project/ProjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,8 @@ define(function (require, exports, module) {
$(".jstree-rename-input").blur();
}

function init() {
}

// Initialize variables and listeners that depend on the HTML DOM
AppInit.htmlReady(function () {
Expand All @@ -1343,6 +1345,7 @@ define(function (require, exports, module) {
CommandManager.register(Strings.CMD_PROJECT_SETTINGS, Commands.FILE_PROJECT_SETTINGS, _projectSettings);

// Define public API
exports.init = init;
exports.getProjectRoot = getProjectRoot;
exports.getBaseUrl = getBaseUrl;
exports.setBaseUrl = setBaseUrl;
Expand Down
65 changes: 62 additions & 3 deletions src/utils/ExtensionLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ define(function (require, exports, module) {

var NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem,
FileUtils = require("file/FileUtils"),
Async = require("utils/Async"),
contexts = {},
srcPath = FileUtils.getNativeBracketsDirectoryPath();
Async = require("utils/Async");

var _init = false,
contexts = {},
srcPath = FileUtils.getNativeBracketsDirectoryPath();

// The native directory path ends with either "test" or "src". We need "src" to
// load the text and i18n modules.
Expand Down Expand Up @@ -238,6 +240,63 @@ define(function (require, exports, module) {
return _loadAll(directory, config, "unittests", testExtension);
}

/**
* Load extensions.
*
* @param {?string} A list containing references to extension source
* location. A source location may be either (a) a folder path
* relative to src/extensions or (b) an absolute path.
* @return {!$.Promise} A promise object that is resolved when all extensions complete loading.
*/
function init(paths) {
if (_init) {
// Only init once. Return a resolved promise.
return new $.Deferred().resolve().promise();
}

if (!paths) {
paths = "default,dev," + getUserExtensionPath();
}

// Load extensions before restoring the project

// Create a new DirectoryEntry and call getDirectory() on the user extension
// directory. If the directory doesn't exist, it will be created.
// Note that this is an async call and there are no success or failure functions passed
// in. If the directory *doesn't* exist, it will be created. Extension loading may happen
// before the directory is finished being created, but that is okay, since the extension
// loading will work correctly without this directory.
// If the directory *does* exist, nothing else needs to be done. It will be scanned normally
// during extension loading.
var extensionPath = getUserExtensionPath();
new NativeFileSystem.DirectoryEntry().getDirectory(extensionPath,
{create: true});

// Create the extensions/disabled directory, too.
var disabledExtensionPath = extensionPath.replace(/\/user$/, "/disabled");
new NativeFileSystem.DirectoryEntry().getDirectory(disabledExtensionPath,
{create: true});

var promise = Async.doInParallel(paths.split(","), function (item) {
var extensionPath = item;

// If the item has "/" in it, assume it is a full path. Otherwise, load
// from our source path + "/extensions/".
if (item.indexOf("/") === -1) {
extensionPath = FileUtils.getNativeBracketsDirectoryPath() + "/extensions/" + item;
}

return loadAllExtensionsInNativeDirectory(extensionPath);
});

promise.done(function () {

This comment has been minimized.

Copy link
@dangoor

dangoor Jan 10, 2013

Contributor

Should this be promise.always? Of course, if initialization fails all hope is likely lost anyhow and nothing else is going to be calling init, so this is more of a theoretical question than an important point :)

This comment has been minimized.

Copy link
@jasonsanjose

jasonsanjose Jan 10, 2013

Author Member

Yes. Fixed.

_init = true;
});

return promise;
}

exports.init = init;
exports.getUserExtensionPath = getUserExtensionPath;
exports.getRequireContextForExtension = getRequireContextForExtension;
exports.loadExtension = loadExtension;
Expand Down

0 comments on commit 1223fac

Please sign in to comment.