From 291f94da57695c0960952cca9d9364dc0a1bed45 Mon Sep 17 00:00:00 2001 From: Andrew Miner Date: Wed, 14 Jan 2015 15:57:27 -0800 Subject: [PATCH] Add the "verify" script In addition to the new script, this changes the V2 parser to be able to continue past the first error found so that as many errors as possible may be displayed. --- scripts/verify | 34 +++++++++++++++++++ src/scripts/models/mod_version_parser.coffee | 8 +++-- .../models/mod_version_parsers/v2.coffee | 33 +++++++++++++----- 3 files changed, 64 insertions(+), 11 deletions(-) create mode 100755 scripts/verify diff --git a/scripts/verify b/scripts/verify new file mode 100755 index 000000000..b421c06f0 --- /dev/null +++ b/scripts/verify @@ -0,0 +1,34 @@ +#!/usr/bin/env coffee + +global._ = require 'underscore' +global.Backbone = require 'backbone' + +fs = require 'fs' +Logger = require '../src/scripts/logger' +ModVersionParser = require '../src/scripts/models/mod_version_parser' + +require '../src/scripts/underscore_mixins' + +######################################################################################################################## + +if process.argv.length is 3 + sourceFileName = process.argv[2] +else + console.error """ + + USAGE: verify + + Verifies that a recipe book is in the proper format. If there is a problem, + then a message will be displayed. Otherwise, the command has no output. + + """ + process.exit -1 + +global.logger = new Logger level:Logger.WARNING + +try + text = fs.readFileSync sourceFileName, 'UTF-8' + parser = new ModVersionParser showAllErrors:true + modVersion = parser.parse text +catch e + console.error e.message diff --git a/src/scripts/models/mod_version_parser.coffee b/src/scripts/models/mod_version_parser.coffee index 724addd86..54c65bd81 100644 --- a/src/scripts/models/mod_version_parser.coffee +++ b/src/scripts/models/mod_version_parser.coffee @@ -15,10 +15,12 @@ module.exports = class ModVersionParser @CURRENT_VERSION = '2' - constructor: -> + constructor: (options={})-> + options.showAllErrors ?= false + @_parsers = - '1': new V1 - '2': new V2 + '1': new V1 options + '2': new V2 options parse: (data)-> if not data? then throw new Error 'mod description data is missing' diff --git a/src/scripts/models/mod_version_parsers/v2.coffee b/src/scripts/models/mod_version_parsers/v2.coffee index 989310c1b..0344ff189 100644 --- a/src/scripts/models/mod_version_parsers/v2.coffee +++ b/src/scripts/models/mod_version_parsers/v2.coffee @@ -15,6 +15,12 @@ StringBuilder = require '../string_builder' module.exports = class V2 + constructor: (options={})-> + options.showAllErrors ?= false + @showAllErrors = options.showAllErrors + + # Class Methods ################################################################################ + @COMMAND = /\ *([^:]*):?(.*)/ @COMMENT = /([^\\]?)#.*/ @@ -25,6 +31,8 @@ module.exports = class V2 @STACK = /^([0-9]+) +(.*)$/ + # Public Methods ############################################################################### + parse: (text)-> @_modVersionData = {} @_lineNumber = 1 @@ -36,7 +44,8 @@ module.exports = class V2 for command in commands @_execute command - return @_buildModVersion @_modVersionData + modVersion = @_handleErrors @_buildModVersion, @_modVersionData + return modVersion unparse: (modVersion)-> builder = new StringBuilder context:modVersion @@ -48,11 +57,8 @@ module.exports = class V2 _execute: (command)-> method = this["_command_#{command.name}"] if not method? then throw new Error "Unknown command: #{command.name}" - try - method.apply this, command.args - catch e - e.message = "line #{@_lineNumber}: #{e.message}" - throw e + + @_handleErrors method, command.args _parseLine: (line)-> line = line.replace V2.COMMENT, '$1' @@ -73,6 +79,17 @@ module.exports = class V2 return commands + _handleErrors: (callback, args...)-> + if args.length is 1 and _.isArray(args[0]) then args = args[0] + + try + callback.apply this, args + catch e + e.message = "line #{@_lineNumber}: #{e.message}" + if not @showAllErrors then throw e + console.error e.message + + # Command Methods ############################################################################## _command_description: (descriptionParts...)-> @@ -178,7 +195,7 @@ module.exports = class V2 modVersion = new ModVersion attributes for itemData in modVersionData.items - @_buildItem modVersion, itemData + @_handleErrors @_buildItem, modVersion, itemData return modVersion @@ -190,7 +207,7 @@ module.exports = class V2 item = new Item modVersion:modVersion, name:itemData.name, isGatherable:itemData.gatherable for recipeData in itemData.recipes - @_buildRecipe modVersion, item, recipeData + @_handleErrors @_buildRecipe, modVersion, item, recipeData return item