# 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
#
# * `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]
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)