1 | var AppCfg = require('appcfg'),
|
2 | Config = require('./config/Config'),
|
3 | ShellStrategy = require('./shell/Strategy.js'),
|
4 | ShellPrompt = require('./shell/Prompt.js'),
|
5 | ShellProcess = require('./shell/Process.js'),
|
6 | io = require('atma-io');
|
7 |
|
8 |
|
9 | io.settings({
|
10 | extensions: {
|
11 | 'yml': [
|
12 | 'atma-io-middleware-yml:read'
|
13 | ]
|
14 | }
|
15 | });
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | Class.Await.TIMEOUT = 20000;
|
21 |
|
22 | var Application = Class({
|
23 | Extends: [Class.EventEmitter, Class.Deferred, ShellPrompt],
|
24 |
|
25 | config: null,
|
26 |
|
27 | Construct: function() {
|
28 |
|
29 | global.app = this;
|
30 |
|
31 | this.config = AppCfg
|
32 | .fetch([
|
33 | Config.Utils,
|
34 | {
|
35 | path: '%APP%/globals/actions.js'
|
36 | },
|
37 | {
|
38 | path: '%APP%/globals/config.yml'
|
39 | },
|
40 | {
|
41 | path: '%APPDATA%/.atma/config.yml',
|
42 | writable: true,
|
43 | optional: true
|
44 | },
|
45 | {
|
46 | path: 'package.json',
|
47 | getterProperty: 'atma',
|
48 | optional: true,
|
49 | lookupAncestors: true
|
50 | },
|
51 | Config.Projects,
|
52 | Config.Plugins,
|
53 | Config.Tasks,
|
54 | Config.Settings
|
55 | ]);
|
56 |
|
57 | this
|
58 | .config
|
59 | .done(function() {
|
60 |
|
61 | if (app.config.$cli.params.help) {
|
62 | app.run({ action: 'help' });
|
63 | return;
|
64 | }
|
65 |
|
66 | app.resolve(app);
|
67 | });
|
68 |
|
69 | },
|
70 |
|
71 | run: function(taskConfigs) {
|
72 |
|
73 | app.worker = new Class.Deferred();
|
74 | app.errors = [];
|
75 |
|
76 | if (taskConfigs != null) {
|
77 | if (Array.isArray(taskConfigs) === false) {
|
78 | taskConfigs = [taskConfigs];
|
79 | }
|
80 |
|
81 | this.config.tasks = taskConfigs;
|
82 | } else {
|
83 |
|
84 | taskConfigs = this.config.tasks;
|
85 | }
|
86 | if (Array.isArray(taskConfigs) === false || taskConfigs.length === 0) {
|
87 |
|
88 | return app
|
89 | .worker
|
90 | .reject('<app:run> tasks are invalid');
|
91 | }
|
92 |
|
93 | this.process(taskConfigs.shift());
|
94 |
|
95 | return app.worker;
|
96 | },
|
97 |
|
98 |
|
99 |
|
100 | process: function(taskConfig) {
|
101 |
|
102 | var app = this;
|
103 |
|
104 | this.current = taskConfig;
|
105 | this
|
106 | .findAction(taskConfig.action)
|
107 | .fail(function(error) {
|
108 | logger.error('<app.action>', error);
|
109 | next();
|
110 | })
|
111 | .done(function(handler) {
|
112 |
|
113 |
|
114 | setTimeout(run);
|
115 |
|
116 | function run() {
|
117 | if (handler.strategy) {
|
118 |
|
119 | var strategy = new ShellStrategy(handler.strategy),
|
120 | path = process.argv.slice(3).join(' '),
|
121 | cmd = ruta.$utils.pathFromCLI(path);
|
122 |
|
123 | strategy.process(cmd, taskConfig, callback);
|
124 | return;
|
125 | }
|
126 | if (handler.process) {
|
127 | handler.process(taskConfig, callback);
|
128 | return;
|
129 | }
|
130 | app.errors.push('<fail> ' +
|
131 | taskConfig.action +
|
132 | ':' +
|
133 | ' No `strategy` object, no `process` function'
|
134 | );
|
135 | }
|
136 |
|
137 |
|
138 | function callback(error) {
|
139 |
|
140 | if (error)
|
141 | app.errors.push('<fail> ' + taskConfig.action + ':' + error);
|
142 | next();
|
143 | }
|
144 | });
|
145 |
|
146 | function next() {
|
147 | var taskConfig = app.config.tasks.shift();
|
148 |
|
149 | if (taskConfig == null) {
|
150 | app
|
151 | .worker
|
152 | .resolve();
|
153 | return;
|
154 | }
|
155 |
|
156 | app.process(taskConfig);
|
157 | }
|
158 |
|
159 | return this;
|
160 | },
|
161 |
|
162 | findAction: function(action) {
|
163 | var dfr = new Class.Deferred(),
|
164 | mix = this.config.actions[action];
|
165 |
|
166 | if (mix != null && typeof mix === 'object') {
|
167 | return dfr.resolve(mix);
|
168 | }
|
169 |
|
170 | var path = mix;
|
171 | if (path == null)
|
172 | path = '/src/action/' + action + '.js';
|
173 |
|
174 | var base = io.env.applicationDir.toString();
|
175 | if (path[0] === '/')
|
176 | path = net.Uri.combine(base, path);
|
177 |
|
178 | include
|
179 | .instance(base)
|
180 | .setBase(base)
|
181 | .js(path + '::Action')
|
182 | .done(function(resp) {
|
183 |
|
184 |
|
185 | if (resp == null || resp.Action == null) {
|
186 | dfr.reject('Action not found: ' + action);
|
187 | return;
|
188 | }
|
189 |
|
190 |
|
191 | dfr.resolve(resp.Action);
|
192 | });
|
193 |
|
194 | return dfr;
|
195 | },
|
196 |
|
197 | findActions: function() {
|
198 | var actions = Array.prototype.slice.call(arguments),
|
199 | fns = [],
|
200 | dfr = new Class.Deferred(),
|
201 | app = this;
|
202 |
|
203 | function next() {
|
204 | if (actions.length === 0) {
|
205 | dfr.resolve.apply(dfr, fns);
|
206 | return;
|
207 | }
|
208 | app
|
209 | .findAction(actions.shift())
|
210 | .done(function(fn) {
|
211 | fns.push(fn);
|
212 | next();
|
213 | })
|
214 | .fail(function(error) {
|
215 | dfr.reject(error);
|
216 | })
|
217 | }
|
218 |
|
219 | next();
|
220 | return dfr;
|
221 | },
|
222 |
|
223 | runAction: function(action, config, done) {
|
224 | this
|
225 | .findAction(action)
|
226 | .done(function(action) {
|
227 | action.process(config, done);
|
228 | })
|
229 | .fail(function() {
|
230 | done('<Atma.Toolkit::Action - 404> ' + action);
|
231 | })
|
232 | }
|
233 | });
|
234 |
|
235 |
|
236 |
|
237 | module.exports = new Application; |
\ | No newline at end of file |