From 748b8f4f89201a50a70aebf39b06ee6b5eefadd9 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 16 Oct 2008 22:18:07 -0400 Subject: [PATCH] tweak some command line stuff, still a lot to do --- src/CommandLineInterface.hx | 116 +++++++++++++++++++++++++++------- src/FunctionTokenProcessor.hx | 3 + src/MyTests.hx | 1 + 3 files changed, 96 insertions(+), 24 deletions(-) diff --git a/src/CommandLineInterface.hx b/src/CommandLineInterface.hx index ffe1f65..673c0b7 100644 --- a/src/CommandLineInterface.hx +++ b/src/CommandLineInterface.hx @@ -1,43 +1,111 @@ +/** + The Neko command line interface to Harmonious Code. This is *far* from complete, but + can, at the moment, function as a very simple way to integrate a version check into + a build or test script. +**/ class CommandLineInterface { #if neko static public function main() { var arguments = neko.Sys.args(); + var usage_string = "Usage: ./command_line.sh [ --php-version ] [ --file ] \n"; + if (arguments.length > 0) { - if (neko.FileSystem.exists(arguments[0])) { - var code = neko.io.File.getContent(arguments[0]); + var mapped_arguments = parse_arguments(arguments); - var parser = new CodeParser(); - parser.loadProcessorsFromDisk(); + if (!mapped_arguments.exists("file")) { + neko.Lib.print(usage_string); + neko.Sys.exit(1); + } - var results = parser.parse(code); + var filepath = mapped_arguments.get("file"); + if (!neko.FileSystem.exists(filepath)) { + neko.Lib.print("The specified file does not exist: " + filepath + "\n"); + neko.Sys.exit(1); + } - var version_info = new CodeVersionInformation(results); + var code = neko.io.File.getContent(filepath); - neko.Lib.print("Your code in " + arguments[0] + " requires the following minimum PHP & PECL module versions:\n"); + var parser = new CodeParser(); + parser.load_all_processors_from_disk(); - var minimum = version_info.final_versions.get("minimum"); + var results = parser.parse(code); - for (module in minimum.keys()) { - neko.Lib.print("* " + module + ": " + minimum.get(module) + "\n"); + var version_info = new CodeVersionInformation(results); + + neko.Lib.print("Your code in " + arguments[0] + " requires the following minimum PHP & PECL module versions:\n"); + + var minimum = version_info.final_versions.get("minimum"); + + for (module in minimum.keys()) { + neko.Lib.print("* " + module + ": " + minimum.get(module) + "\n"); + } + + var maximum = version_info.final_versions.get("maximum"); + var printed_message = false; + + for (module in maximum.keys()) { + if (!printed_message) { + neko.Lib.print("Your code also can't use PHP or PECL modules newer than:\n"); + printed_message = true; } + neko.Lib.print("* " + module + ": " + maximum.get(module) + "\n"); - var maximum = version_info.final_versions.get("maximum"); - var printed_message = false; - - for (module in maximum.keys()) { - if (!printed_message) { - neko.Lib.print("Your code also can't use PHP or PECL modules newer than:\n"); - printed_message = true; - } - neko.Lib.print("* " + module + ": " + maximum.get(module) + "\n"); - - if (!version_info.is_valid()) { - neko.Lib.print("This code may not run!\n"); - } + if (!version_info.is_valid()) { + neko.Lib.print("This code may not run!\n"); + neko.Sys.exit(1); } } + + if (mapped_arguments.exists("php-version")) { + var minimum_specified_php_version = mapped_arguments.get("php-version"); + if (CodeVersionInformation.version_compare(minimum_specified_php_version, minimum.get("PHP")) == -1) { + neko.Lib.print("Your code requires a version higher than the minimum version you specified, " + minimum_specified_php_version + "!\n"); + neko.Sys.exit(1); + } + } + + neko.Sys.exit(0); + } + neko.Lib.print(usage_string); + neko.Sys.exit(1); + } + #end + + /** + Parse a series of arguments fed via the command line. + **/ + public static function parse_arguments(arguments : Array) : Hash { + var mapped_arguments = new Hash(); + + var capturable_options = [ "file", "php-version" ]; + var valid_options_hash = new Hash(); + + for (option in capturable_options) { + valid_options_hash.set(option, true); + } + + var current_option : String = null; + for (argument in arguments) { + if (argument.indexOf("--") == 0) { + var option = argument.substr(2); + if (valid_options_hash.exists(option)) { + if (valid_options_hash.get(option)) { + current_option = option; + } else { + mapped_arguments.set(option, "true"); + } + } + } else { + if (current_option != null) { + mapped_arguments.set(current_option, argument); + current_option = null; + } else { + mapped_arguments.set("file", argument); + } } } - #end + + return mapped_arguments; + } } \ No newline at end of file diff --git a/src/FunctionTokenProcessor.hx b/src/FunctionTokenProcessor.hx index 862bec7..51ec840 100644 --- a/src/FunctionTokenProcessor.hx +++ b/src/FunctionTokenProcessor.hx @@ -1,3 +1,6 @@ +/** + Process the PHP documentation's function definition file. +**/ class FunctionTokenProcessor extends TokenProcessor { public static var source_path : String = "../data/phpdoc_function_versions.xml"; override public function get_default_token_type() { return FunctionToken; } diff --git a/src/MyTests.hx b/src/MyTests.hx index 0545254..34a178a 100644 --- a/src/MyTests.hx +++ b/src/MyTests.hx @@ -11,6 +11,7 @@ class MyTests { r.add(new TestCodeVersionInformation()); r.add(new TestResult()); r.add(new TestJavaScriptTarget()); + r.add(new TestCommandLineInterface()); r.run(); } } \ No newline at end of file