1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Doxygen XML to Neo4j.
17 # Converts a Doxygen XML output into a model in Neo4j that is readable by the
28 # An importation task.
31 # The storage medium to use.
34 # The loaded project graph.
35 var model
: ProjectGraph is noinit
37 # Escape control sequence to save the cursor position.
38 private var term_save_cursor
: String = (new TermSaveCursor).to_s
40 # Escape control sequence to rewind to the last saved cursor position.
41 private var term_rewind
: String = "{new TermRestoreCursor}{new TermEraseDisplayDown}"
43 # Generate a graph from the specified project model.
47 # * `name`: project name.
48 # * `dir`: Doxygen XML output directory path.
49 # * `source`: The language-specific logics to use.
50 fun load_project
(name
: String, dir
: String, source
: SourceLanguage) do
52 model
= new ProjectGraph(name
)
53 var reader
= new CompoundFileReader(model
, source
)
54 # Queue for sub-directories.
55 var directories
= new Array[String]
59 printn
"Reading the current directory... "
61 printn
"Reading {dir}... "
65 for f
in list_files
(dir
) do
67 if path
.file_stat
.is_dir
then
68 directories
.push
(path
)
69 else if f
.has_suffix
(".xml") and f
!= "index.xml" then
74 if directories
.length
<= 0 then break
77 model
.add_global_modules
79 if file_count
< 2 then
80 print
"{file_count} file read."
82 print
"{file_count} files read."
87 # List files in a directory.
89 # This method may be redefined to force the order in which the files
90 # are read by `load_project`.
91 protected fun list_files
(dir
: String): Collection[String] do
95 # Check the project’s name.
96 private fun check_name
(name
: String) do
97 assert name_valid
: not name
.chars
.first
.is_upper
else
98 sys
.stderr
.write
("{sys.program_name}: The project’s name must not" +
99 " begin with an upper case letter. Got `{name}`.\n")
101 assert name_unused
: not store
.has_node_label
(name
) else
102 sys
.stderr
.write
("{sys.program_name}: The label `{name}` is already" +
103 " used in the specified graph.\n")
109 sys
.stdout
.write
"Linking nodes...{term_save_cursor} "
112 print
"{term_rewind} Done."
113 var nodes
= model
.all_nodes
114 sys
.stdout
.write
"Saving {nodes.length} nodes..."
115 store
.save_all
(nodes
)
116 var edges
= model
.all_edges
117 sys
.stdout
.write
"Saving {edges.length} edges..."
118 store
.save_all
(edges
)
123 class NeoDoxygenCommand
128 # Available options for `--src-lang`.
129 var sources
= new HashMap[String, SourceLanguage]
132 var synopsis
: String = "[--dest <url>] [--src-lang <lang>]\n" +
133 " [--] <project_name> <doxml_dir>"
135 # The synopsis for the help page.
136 var help_synopsis
= "[-h|--help]"
138 # The default destination.
139 var default_dest
= "http://localhost:7474"
141 # Processes the options.
142 var option_context
= new OptionContext
144 # The `--src-lang` option.
145 var opt_src_lang
: OptionEnum is noinit
147 # The `--dest` option.
148 var opt_dest
: OptionString is noinit
150 # The `-h|--help` option.
151 var opt_help
: OptionBool is noinit
154 sources
["any"] = new DefaultSource
155 sources
["java"] = new JavaSource
157 var prefix
= new OptionText("""
159 {{{sys.program_name}}} — Doxygen XML to Neo4j.
161 {{{"SYNOPSIS".bold}}}
162 {{{sys.program_name}}} {{{synopsis}}}
163 {{{sys.program_name}}} {{{help_synopsis}}}
165 {{{"DESCRIPTION".bold}}}
166 Convert a Doxygen XML output into a model in Neo4j that is readable by the
169 {{{"ARGUMENTS".bold}}}
170 <project_name> The internal name of the project. Must the same name as the
171 one specified to the `nx` tool. Must not begin by an upper
174 <doxml_dir> The directory where the XML documents generated by Doxygen are
179 option_context
.add_option
(prefix
)
181 opt_dest
= new OptionString("The URL of the destination graph. `{default_dest}` by default.",
183 opt_dest
.default_value
= default_dest
184 option_context
.add_option
(opt_dest
)
186 opt_help
= new OptionBool("Show the help (this page).",
188 option_context
.add_option
(opt_help
)
190 var keys
= new Array[String].from
(sources
.keys
)
191 opt_src_lang
= new OptionEnum(keys
,
192 "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.",
193 keys
.index_of
("any"), "--src-lang")
194 option_context
.add_option
(opt_src_lang
)
197 # Start the application.
199 if args
.is_empty
then
203 option_context
.parse
(args
)
205 var errors
= option_context
.get_errors
206 var rest
= option_context
.rest
208 if errors
.is_empty
and not opt_help
.value
and rest
.length
!= 2 then
209 errors
.add
"Unexpected number of additional arguments. Expecting 2; got {rest.length}."
211 if not errors
.is_empty
then
212 for e
in errors
do print_error
(e
)
216 if opt_help
.value
then
221 var source
= sources
[opt_src_lang
.value_name
]
222 var dest
= opt_dest
.value
223 var project_name
= rest
[0]
225 var neo
= new NeoDoxygenJob(create_store
(dest
or else default_dest
))
227 neo
.load_project
(project_name
, dir
, source
)
232 # Create an instance of `GraphStore` for the specified destination.
233 protected fun create_store
(dest
: String): GraphStore do
234 return new Neo4jStore(new Neo4jClient(dest
))
244 sys
.stderr
.write
"Usage: {sys.program_name} {synopsis}\n"
245 sys
.stderr
.write
"For details, run `{sys.program_name} --help`.\n"
249 fun print_error
(e
: String) do
250 sys
.stderr
.write
"{sys.program_name}: {e}\n"
254 # Add handling of multi-line descriptions.
256 # Note: The algorithm is naive and do not handle internationalisation and
260 redef fun pretty
(off
) do
263 if s
.length
> 80 and off
< 80 then
264 var column_length
= 80 - off
267 var buf
= new FlatBuffer
268 var prefix
= "\n{" " * off}"
271 while right
> left
and s
.chars
[right
] != ' ' do
274 if left
== right
then
275 buf
.append s
.substring
(left
, column_length
)
276 right
+= column_length
278 buf
.append s
.substring
(left
, right
- left
)
283 right
+= column_length
284 if right
>= s
.length
then break
286 buf
.append s
.substring_from
(left
)
295 exit
((new NeoDoxygenCommand).main
)