neo_doxygen: Make the command user-friendly.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Sun, 2 Nov 2014 22:12:52 +0000 (17:12 -0500)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Tue, 4 Nov 2014 17:16:09 +0000 (12:16 -0500)
Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

contrib/neo_doxygen/gen-all.sh
contrib/neo_doxygen/gen-one.sh
contrib/neo_doxygen/src/doxml/language_specific.nit
contrib/neo_doxygen/src/neo_doxygen.nit

index be5ff21..b43ceab 100755 (executable)
@@ -14,7 +14,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# ./gen-all.sh directory
+# ./gen-all.sh <source_language> <directory>
 #
 # Document all projects in the specified directory.
 #
 NEO_DOXYGEN="${PWD}/bin/neo_doxygen"
 NX="${PWD}/../../bin/nx"
 
-for dir in "$1"/*; do
+for dir in "$2"/*; do
        if [ -d "$dir" ]; then
                if [ -f "$dir/.nx_config" ]; then
                        # Note: gen-one.sh already prints errors.
-                       ./gen-one.sh "$dir" || exit
+                       ./gen-one.sh "$1" "$dir" || exit
                fi
        fi
 done
index 90e7d01..218560f 100755 (executable)
@@ -14,7 +14,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# ./gen-one.sh directory
+# ./gen-one.sh <source_language> <directory>
 #
 # Document the project in the specified directory.
 #
 
 NEO_DOXYGEN="${PWD}/bin/neo_doxygen"
 NX="${PWD}/../../bin/nx"
-dir=$1
+dir=$2
 
 . sh-lib/errors.sh
 
 echo "$0: Documenting \"${dir##*/}\"..."
 pushd "$dir"
-try "$NEO_DOXYGEN" "${dir##*/}" "$dir/doxygen/xml" http://localhost:7474 > neo_doxygen.out
+try "$NEO_DOXYGEN" --src-lang "$1" --dest http://localhost:7474 -- "${dir##*/}" "$dir/doxygen/xml" > neo_doxygen.out
 try echo "$0: [done] neo_doxygen"
 try "$NX" neo doc "${dir##*/}"
 try echo "$0: [done] nx"
index b4faea9..bbc6410 100644 (file)
@@ -147,6 +147,13 @@ abstract class SourceLanguage
        end
 end
 
+# The default importation logics.
+#
+# Do nothing special.
+class DefaultSource
+       super SourceLanguage
+end
+
 # Importation logics for Java.
 class JavaSource
        super SourceLanguage
index 7738a4a..3952d28 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Doxygen XML to Neo4j
+# Doxygen XML to Neo4j.
 #
-# ## Synopsis
-#
-#     neo_doxygen project_name xml_output_dir [neo4j_url]
-#
-# ## Description
-#
-# Convert a Doxygen XML output into a model in Neo4j that is readable by the
+# Converts a Doxygen XML output into a model in Neo4j that is readable by the
 # `nx` tool.
-#
-# ## Arguments
-#
-# * project_name: The internal name of the project. Must the same name as the
-# one specified to the `nx` tool.
-#
-# * xml_output_dir: The directory where the XML documents generated by Doxygen
-# are located.
-#
-# * neo4j_url: The URL of the instance of Neo4j to use.
-# `http://localhost:7474` by default.
 module neo_doxygen
 
 import model
 import doxml
+import console
+import opts
 
 # An importation task.
-class NeoDoxygen
+class NeoDoxygenJob
        var client: Neo4jClient
        var model: ProjectGraph is noinit
 
@@ -52,10 +37,11 @@ class NeoDoxygen
        #
        # * `name`: project name.
        # * `dir`: Doxygen XML output directory path.
-       fun put_project(name: String, dir: String) do
+       # * `source`: The language-specific logics to use.
+       fun load_project(name: String, dir: String, source: SourceLanguage) do
                model = new ProjectGraph(name)
                # TODO Let the user select the language.
-               var reader = new CompoundFileReader(model, new JavaSource)
+               var reader = new CompoundFileReader(model, source)
                # Queue for sub-directories.
                var directories = new Array[String]
 
@@ -122,15 +108,172 @@ class NeoDoxygen
        end
 end
 
-if args.length != 2 and args.length != 3 then
-       stderr.write("Usage: {sys.program_name} project_name xml_output_dir [neo4j_url]\n")
-       exit(1)
+# The main class.
+class NeoDoxygenCommand
+
+       # Invalid arguments
+       var e_usage = 64
+
+       # Available options for `--src-lang`.
+       var sources = new HashMap[String, SourceLanguage]
+
+       # The synopsis.
+       var synopsis: String = "[--dest <url>] [--src-lang <lang>]\n" +
+                       "    [--] <project_name> <doxml_dir>"
+
+       # The synopsis for the help page.
+       var help_synopsis = "[-h|--help]"
+
+       # The default destination.
+       var default_dest = "http://localhost:7474"
+
+       # Processes the options.
+       var option_context = new OptionContext
+
+       # The `--src-lang` option.
+       var opt_src_lang: OptionEnum is noinit
+
+       # The `--dest` option.
+       var opt_dest: OptionString is noinit
+
+       # The `-h|--help` option.
+       var opt_help: OptionBool is noinit
+
+       init do
+               sources["any"] = new DefaultSource
+               sources["java"] = new JavaSource
+
+               var prefix = new OptionText("""
+{{{"NAME".bold}}}
+  {{{sys.program_name}}} — Doxygen XML to Neo4j.
+
+{{{"SYNOPSIS".bold}}}
+  {{{sys.program_name}}} {{{synopsis}}}
+  {{{sys.program_name}}} {{{help_synopsis}}}
+
+{{{"DESCRIPTION".bold}}}
+  Convert a Doxygen XML output into a model in Neo4j that is readable by the
+  `nx` tool.
+
+{{{"ARGUMENTS".bold}}}
+  <project_name>  The internal name of the project. Must the same name as the
+                  one specified to the `nx` tool. Must not begin by an upper
+                  case letter.
+
+  <doxml_dir>     The directory where the XML documents generated by Doxygen are
+                  located.
+
+{{{"OPTIONS".bold}}}
+""")
+               option_context.add_option(prefix)
+
+               opt_dest = new OptionString("The URL of the destination graph. `{default_dest}` by default.",
+                               "--dest")
+               opt_dest.default_value = default_dest
+               option_context.add_option(opt_dest)
+
+               var keys = new Array[String].from(sources.keys)
+               opt_src_lang = new OptionEnum(keys,
+                               "The programming language to assume when processing chunk in the declarations left as-is by Doxygen. Use `any` (the default) to disable any language-specific processing.",
+                               keys.index_of("any"), "--src-lang")
+               option_context.add_option(opt_src_lang)
+
+               opt_help = new OptionBool("Show the help (this page).",
+                               "-h", "--help")
+               option_context.add_option(opt_help)
+       end
+
+       # Start the application.
+       fun main: Int do
+               if args.is_empty then
+                       show_help
+                       return e_usage
+               end
+               option_context.parse(args)
+
+               var errors = option_context.get_errors
+               var rest = option_context.rest
+
+               if errors.is_empty and not opt_help.value and rest.length != 2 then
+                       errors.add "Unexpected number of additional arguments. Expecting 2; got {rest.length}."
+               end
+               if not errors.is_empty then
+                       for e in errors do print_error(e)
+                       show_usage
+                       return e_usage
+               end
+               if opt_help.value then
+                       show_help
+                       return 0
+               end
+
+               var source = sources[opt_src_lang.value_name]
+               var dest = opt_dest.value
+               var project_name = rest[0]
+               var dir = rest[1]
+               var neo = new NeoDoxygenJob(new Neo4jClient(dest or else default_dest))
+
+               neo.load_project(project_name, dir, source)
+               neo.save
+               return 0
+       end
+
+       # Show the help.
+       fun show_help do
+               option_context.usage
+       end
+
+       # Show the usage.
+       fun show_usage do
+               sys.stderr.write "Usage: {sys.program_name} {synopsis}\n"
+               sys.stderr.write "For details, run `{sys.program_name} --help`.\n"
+       end
+
+       # Print an error.
+       fun print_error(e: String) do
+               sys.stderr.write "{sys.program_name}: {e}\n"
+       end
 end
-var url = "http://localhost:7474"
-if args.length >= 3 then
-       url = args[2]
+
+# Add handling of multi-line descriptions.
+#
+# Note: The algorithm is naive and do not handle internationalisation and
+# escape sequences.
+redef class Option
+
+       redef fun pretty(off) do
+               var s = super
+
+               if s.length > 80 and off < 80 then
+                       var column_length = 80 - off
+                       var left = 0
+                       var right = 80
+                       var buf = new FlatBuffer
+                       var prefix = "\n{" " * off}"
+
+                       loop
+                               while right > left and s.chars[right] != ' ' do
+                                       right -= 1
+                               end
+                               if left == right then
+                                       buf.append s.substring(left, column_length)
+                                       right += column_length
+                               else
+                                       buf.append s.substring(left, right - left)
+                                       right += 1
+                               end
+                               buf.append prefix
+                               left = right
+                               right += column_length
+                               if right >= s.length then break
+                       end
+                       buf.append s.substring_from(left)
+                       buf.append "\n"
+                       return buf.to_s
+               else
+                       return "{s}\n"
+               end
+       end
 end
 
-var neo = new NeoDoxygen(new Neo4jClient(url))
-neo.put_project(args[0], args[1])
-neo.save
+exit((new NeoDoxygenCommand).main)