YAYYYyyy giatn refactor

This commit is contained in:
Peter Cottle 2012-12-27 14:48:04 -08:00
parent 171e4c8351
commit d497bea70c
7 changed files with 654 additions and 451 deletions

View file

@ -6,7 +6,7 @@ var Errors = require('../util/errors');
var GitCommands = require('../git/commands');
var GitOptionParser = GitCommands.GitOptionParser;
var sandboxInstantCommands = require('../level/sandboxCommands').sandboxInstantCommands;
var ParseWaterfall = require('../level/parseWaterfall').ParseWaterfall;
var CommandProcessError = Errors.CommandProcessError;
var GitError = Errors.GitError;
@ -18,38 +18,47 @@ var Command = Backbone.Model.extend({
status: 'inqueue',
rawStr: null,
result: '',
createTime: null,
error: null,
warnings: null,
parseWaterfall: new ParseWaterfall(),
generalArgs: null,
supportedMap: null,
options: null,
method: null,
method: null
createTime: null
},
validateAtInit: function() {
// weird things happen with defaults if you dont
// make new objects
this.set('generalArgs', []);
this.set('supportedMap', {});
this.set('warnings', []);
if (this.get('rawStr') === null) {
throw new Error('Give me a string!');
}
if (!this.get('createTime')) {
this.set('createTime', new Date().toString());
}
initialize: function(options) {
this.initDefaults();
this.validateAtInit();
this.on('change:error', this.errorChanged, this);
// catch errors on init
if (this.get('error')) {
this.errorChanged();
}
this.parseOrCatch();
},
initDefaults: function() {
// weird things happen with defaults if you dont
// make new objects
this.set('generalArgs', []);
this.set('supportedMap', {});
this.set('warnings', []);
},
validateAtInit: function() {
if (this.get('rawStr') === null) {
throw new Error('Give me a string!');
}
if (!this.get('createTime')) {
this.set('createTime', new Date().toString());
}
},
setResult: function(msg) {
@ -71,19 +80,26 @@ var Command = Backbone.Model.extend({
return '<p>' + i + this.get('warnings').join('</p><p>' + i) + '</p>';
},
initialize: function() {
this.validateAtInit();
this.parseOrCatch();
},
parseOrCatch: function() {
this.expandShortcuts(this.get('rawStr'));
try {
this.parse();
this.processInstants();
} catch (err) {
Errors.filterError(err);
// errorChanged() will handle status and all of that
this.set('error', err);
}
if (this.parseAll()) {
// something in our parse waterfall succeeded
return;
}
// if we reach here, this command is not supported :-/
this.set('error', new CommandProcessError({
msg: 'The command "' + this.get('rawStr') + '" isn\'t supported, sorry!'
})
);
},
errorChanged: function() {
@ -103,65 +119,35 @@ var Command = Backbone.Model.extend({
this.set('result', this.get('error').toResult());
},
parse: function() {
expandShortcuts: function(str) {
str = this.get('parseWaterfall').expandAllShortcuts(str);
this.set('rawStr', str);
},
processInstants: function() {
var str = this.get('rawStr');
// first if the string is empty, they just want a blank line
if (!str.length) {
throw new CommandResult({msg: ""});
}
str = GitCommands.expandShortcut(str);
this.set('rawStr', str);
// then check if it's one of our sandbox commands
_.each(sandboxInstantCommands, function(tuple) {
var regex = tuple[0];
var results = regex.exec(str);
if (results) {
// this will throw a result
tuple[1](results);
}
});
// see if begins with git
if (str.slice(0,3) !== 'git') {
throw new CommandProcessError({
msg: 'That command is not supported, sorry!'
});
}
// ok, we have a (probably) valid command. actually parse it
this.gitParse(str);
// then instant commands that will throw
this.get('parseWaterfall').processAllInstants(str);
},
gitParse: function(str) {
// now slice off command part
var fullCommand = str.slice('git '.length);
parseAll: function() {
var str = this.get('rawStr');
var results = this.get('parseWaterfall').parseAll(str);
// see if we support this particular command
_.each(GitCommands.getRegexMap(), function(regex, method) {
if (regex.exec(fullCommand)) {
this.set('options', fullCommand.slice(method.length + 1));
this.set('method', method);
// we should stop iterating, but the regex will only match
// one command in practice. we could stop iterating if we used
// jqeurys for each but im using underscore (for no real reason other
// than style)
}
}, this);
if (!this.get('method')) {
throw new CommandProcessError({
msg: "Sorry, this demo does not support that git command: " + fullCommand
});
if (!results) {
// nothing parsed successfully
return false;
}
// parse off the options and assemble the map / general args
var options = new GitOptionParser(this.get('method'), this.get('options'));
// steal these away so we can be completely JSON
this.set('generalArgs', options.generalArgs);
this.set('supportedMap', options.supportedMap);
_.each(results.toSet, function(obj, key) {
this.set(key, obj);
}, this);
return true;
}
});