Permalink
Browse files

refactor fully to using dir-level modules and new-style settings

  • Loading branch information...
1 parent 174b503 commit ff0728a7b353f698a00eb3fe5db50a6c5f70ea6d @chrisdickinson committed Oct 17, 2010
View
@@ -1,4 +1,18 @@
-var conf = require('./conf');
+var importModule = require('wilson/utils/module').importModule,
+ appInstances = {};
+
+var setApplicationInstance = function(app_name, app_instance) {
+ appInstances[app_name] = app_instance;
+};
+
+var getApplicationInstance = function(app_name) {
+ return appInstances[app_name];
+};
+
+var getApplicationInstances = function() {
+ return appInstances;
+};
+
var Application = function(opts) {
for(var i in opts) if(opts.hasOwnProperty(i)) {
this[i] = opts[i];
@@ -9,7 +23,6 @@ Application.prototype.instantiate = function(name) {
return new ApplicationInstance(name, this);
};
-
var ApplicationInstance = function(name, app) {
var self = this;
this.name = name;
@@ -32,7 +45,7 @@ ApplicationInstance.prototype.resolveAppDependency = function(dep_fn, app_dict)
};
ApplicationInstance.prototype.setupForwardDependencies = function(apps) {
- apps = apps || conf.getApplicationInstances();
+ apps = apps || getApplicationInstances();
var external_app_list = this.app.external_apps;
this.externals = {};
@@ -57,9 +70,8 @@ ApplicationInstance.prototype.setupForwardDependencies = function(apps) {
}
};
-
ApplicationInstance.prototype.resolveModels = function(apps) {
- apps = apps || conf.getApplicationInstances();
+ apps = apps || getApplicationInstances();
this.models = {};
for(var model_name in this.app.models) {
var model = this.app.models[model_name].instantiate(this, [this.name.toLowerCase(), model_name.toLowerCase()].join('_'));
@@ -102,5 +114,66 @@ exports.specific = function(app_name) {
};
};
+var AppDeclaration = function(loc, opts) {
+ this.opts = opts || {};
+ this.loc = loc;
+};
+
+AppDeclaration.prototype.load = function(name, options) {
+ var app = importModule(this.loc).app,
+ app_out = {};
+
+ if(options) {
+ Object.keys(app).forEach(function(item) {
+ app_out[item] = app[item];
+ });
+
+ Object.keys(options).forEach(function(item) {
+ app_out[item] = options[item];
+ });
+
+ app = new Application(app_out);
+ }
+
+ var app_instance = app.instantiate(name);
+ for(var option in this.opts) {
+ app_instance[option] = this.opts[option];
+ }
+
+ return app_instance;
+};
+
+exports.usePrimary = function(loc) {
+ return new AppDeclaration(loc, {
+ 'isPrimary':true
+ });
+};
+
+exports.use = function(loc) {
+ return new AppDeclaration(loc);
+};
+
+exports.loadApps = function(app_options) {
+ app_options = app_options || {};
+
+ var settings = require('wilson/conf').settings,
+ apps = settings.INSTALLED_APPS;
+
+ Object.keys(apps).forEach(function(app_name) {
+ appInstances[app_name] = apps[app_name].load(app_name, app_options[app_name]);
+ });
+
+ Object.keys(appInstances).forEach(function(app_name) {
+ appInstances[app_name].setupForwardDependencies();
+ });
+
+ Object.keys(appInstances).forEach(function(app_name) {
+ appInstances[app_name].resolveModels();
+ });
+};
+
exports.Application = Application;
exports.ApplicationInstance = ApplicationInstance;
+exports.setApplicationInstance = setApplicationInstance;
+exports.getApplicationInstance = getApplicationInstance;
+exports.getApplicationInstances = getApplicationInstances;
View
@@ -1,31 +1,34 @@
exports.runcommand = function() {
- var sys = require('sys');
-
- var settings = process.argv.slice(-1)[0],
+ var sys = require('sys'),
+ conf = require('wilson/conf'),
+ core = require('wilson/core/app'),
+ application = require('wilson/application'),
app_and_command = process.argv[2].split(':'),
- slice_until = -1;
- try {
- if(process.argv[2] !== settings) {
- require(settings);
- }
- } catch(err) {
- slice_until = undefined;
- }
-
- var wilson = require('wilson'),
- conf = wilson.conf,
app = app_and_command[0],
command = app_and_command[1],
- appInstance = conf.getApplicationInstance(app);
+ appInstance;
- if(appInstance === undefined) {
- appInstance = wilson.core.app.instantiate('core');
+ try {
+ application.loadApps(conf.settings.APP_OVERRIDES);
+ appInstance = application.getApplicationInstance(app);
+ } catch(err) {
+ conf.setSettings({
+ INSTALLED_APPS:{
+ core:application.use('wilson/core/app')
+ },
+ TEMPLATE_LOADERS:[
+ 'wilson/template/loaders.application',
+ 'wilson/template/loaders.filesystem',
+ ],
+ });
+ application.loadApps(conf.settings.APP_OVERRIDES);
+ appInstance = application.getApplicationInstance('core');
}
if(appInstance) {
var appCommand = appInstance.app.commands[command];
if(appCommand) {
- appCommand.apply(appInstance, process.argv.slice(3, slice_until));
+ appCommand.apply(appInstance, process.argv.slice(3));
} else {
sys.puts('Could not find the command '+app_and_command);
if(app === appInstance.name) {
@@ -35,7 +38,7 @@ exports.runcommand = function() {
}
} else {
sys.puts('All apps and commands:');
- var appInstances = conf.getApplicationInstances();
+ var appInstances = application.getApplicationInstances();
for(var app_name in appInstances) {
sys.puts('\t'+app_name+':');
for(var command in appInstances[app_name]) {
View
@@ -1,123 +1,52 @@
-var appInstances = {};
-var loadApps = function(app_options) {
- var settings = require('./settings'),
- apps = settings.get_value('apps');
- for(var app_name in apps) {
- appInstances[app_name] = apps[app_name].load(app_name, app_options[app_name]);
- }
-
- for(var app_name in appInstances) {
- appInstances[app_name].setupForwardDependencies();
- }
-
- for(var app_name in appInstances) {
- appInstances[app_name].resolveModels();
- }
-
- if(settings.get_value('use_app_template_directories')) {
- var jsdtl = require('jsdtl'),
- directories = jsdtl.settings.get_value('TEMPLATE_DIRS') || [];
- for(var instance_name in appInstances) {
- var instance = appInstances[instance_name],
- app_directories = instance.app.template_directories;
- if(app_directories) {
- for(var i = 0, len = app_directories.length; i < len; ++i) {
- if(directories.indexOf(app_directories[i]) === -1) {
- directories.push(app_directories[i]);
- }
- }
- }
- }
- }
-};
-
-var Settings = function () {
-
-};
-Settings.prototype.extend = function(literal) {
- for(var i in literal) if(literal.hasOwnProperty(i)) {
- var target = require(i).settings;
- this[i] = this[i] === undefined ? {'addons':{}, 'values':{} } : this[i];
- for(var value_name in literal[i].values) if(literal[i].values.hasOwnProperty(value_name)) {
- target.set_value(value_name, literal[i].values[value_name]);
- this[i].values[value_name] = literal[i].values[value_name];
- }
-
- for(var addon_name in literal[i].addons) if(literal[i].addons.hasOwnProperty(addon_name)) {
- target.set_addon(addon_name, literal[i].addons[addon_name]);
- this[i].addons[addon_name] = literal[i].addons[addon_name];
- }
- }
- loadApps(arguments[1] || {});
-};
-
-var setApplicationInstance = function(app_name, app_instance) {
- appInstances[app_name] = app_instance;
-};
-
-var getApplicationInstance = function(app_name) {
- return appInstances[app_name];
-};
-
-var getApplicationInstances = function() {
- return appInstances;
-};
-
-var AppDeclaration = function(loc, opts) {
- this.opts = opts || {};
- this.loc = loc;
-};
-
-AppDeclaration.prototype.load = function(name, options) {
- var application = require('./application'),
- isLocal = false;
- if(/^\.\//.test(this.loc)) {
- this.loc = this.loc.slice(2) + '/app';
- isLocal = true;
- }
- var app,
- app_out = {};
- if(isLocal) {
- app = require(this.loc).app;
- } else {
- var app_split = this.loc.split('.');
- app = require(app_split[0]);
- for(var i = 1, len = app_split.length; i < len; ++i) {
- app = app[app_split[i]];
+var Script = process.binding('evals').Script,
+ path = require('path'),
+ fs = require('fs');
+
+var Settings = function(fileName) {
+ this.fileName = fileName || process.env['WILSON_SETTINGS_FILE'];
+};
+
+Settings.prototype.load = function() {
+ var sandbox = {
+ require:require,
+ exports:{},
+ __filename:this.fileName,
+ __dirname:path.dirname(this.fileName),
+ module:{},
+ root:{}
+ },
+ content = fs.readFileSync(this.fileName, 'utf8'),
+ self = this;
+
+ sandbox.global = sandbox;
+ Script.runInNewContext(content, sandbox, this.fileName);
+ return sandbox.global;
+};
+
+var globalSettings = new Settings(path.join(__dirname, 'global_settings.js')),
+ settingsFile = 'settings.js',
+ loadSettings = function() {
+ if(arguments.callee._settings) {
+ return arguments.callee._settings;
}
- app = app.app;
- }
- if(options) {
- for(var item in app) if(app.hasOwnProperty(item)) {
- app_out[item] = app[item];
- }
- for(var item in options) if(options.hasOwnProperty(item)) {
- app_out[item] = options[item];
- }
- app = new application.Application(app_out);
- }
+ var settings = new Settings(settingsFile),
+ base_settings = globalSettings.load(),
+ extended_settings = settings.load();
+ Object.keys(extended_settings).forEach(function(key) {
+ base_settings[key] = extended_settings[key];
+ });
+ arguments.callee._settings = base_settings;
+ return base_settings;
+ };
- var app_instance = app.instantiate(name);
- for(var option in this.opts) {
- app_instance[option] = this.opts[option];
- }
-
- return app_instance;
+exports.Settings = Settings;
+exports.globalSettings = globalSettings,
+exports.setSettingsFile = function(file) {
+ settingsFile = file;
};
-
-exports.primary = function(loc) {
- return new AppDeclaration(loc, {
- 'isPrimary':true
- });
+exports.setSettings = function(incoming) {
+ loadSettings._settings = incoming;
};
-exports.app = function(loc) {
- return new AppDeclaration(loc);
-};
-
-exports.settings = new Settings();
-exports.setApplicationInstance = setApplicationInstance;
-exports.getApplicationInstance = getApplicationInstance;
-exports.getApplicationInstances = getApplicationInstances;
-
+exports.__defineGetter__('settings', loadSettings);
@@ -1,4 +1,4 @@
-var application = require('../../application'),
+var application = require('wilson/application'),
app = application.app,
primary = application.primary,
any = application.any,
@@ -8,21 +8,12 @@ var application = require('../../application'),
exports.app = app({
'provides':'auth', // a one-word description of what your app provides.
- 'models':require('./models'),
- 'urls':require('./urls').patterns,
+ 'models':require('wilson/contrib/auth/models'),
+ 'urls':require('wilson/contrib/auth/urls').patterns,
'external_apps':{
'sessions':primary('sessions'),
},
'middleware':{
- 'AuthenticationMiddleware':require('./middleware').AuthenticationMiddleware
- },
- 'template_directories':[
- path.join(path.dirname(fs.realpathSync(__filename)), 'templates')
- ],
+ 'AuthenticationMiddleware':require('wilson/contrib/auth/middleware').AuthenticationMiddleware
+ }
});
-
-exports.middleware = require('./middleware');
-exports.utils = require('./utils');
-exports.backends = {};
-exports.backends.base = require('./backends/base');
-exports.backends.model = require('./backends/model');
@@ -1,4 +1,5 @@
-var utils = require('./utils');
+var utils = require('wilson/contrib/auth/utils'),
+ import_module = require('wilson/utils/module').import_module;
var request_get_user = function(from) {
var users = this._users || {},
@@ -1,6 +1,4 @@
-// define your models here!
-
-var models = require('../../models');
+var models = require('wilson/models');
exports.User = models.model({
'username':models.CharField({'max_length':255}),
Oops, something went wrong.

0 comments on commit ff0728a

Please sign in to comment.