nitx: migrate to new Nitdoc architecture
[nit.git] / src / nitx.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # `nitx`, is a command tool that displays useful informations about the code.
16 #
17 # Features:
18 #
19 # * Display comment from name/namespace
20 module nitx
21
22 import modelbuilder
23 import doc::doc_phases::doc_console
24
25 redef class ToolContext
26
27 # Nittx generation phase.
28 var docx: Phase = new NitxPhase(self, null)
29
30 # Used to shortcut the prompt and display directly the result in console.
31 var opt_query = new OptionString("Nitx query to perform", "-q", "--query")
32
33 init do option_context.add_option opt_query
34 end
35
36 # Nitx phase explores the model and prepares the console rendering.
37 private class NitxPhase
38 super Phase
39 redef fun process_mainmodule(mainmodule, mmodules)
40 do
41 var doc = new DocModel(mainmodule.model, mainmodule)
42
43 var phases = [
44 new ExtractionPhase(toolcontext, doc),
45 new MakePagePhase(toolcontext, doc),
46 new ConcernsPhase(toolcontext, doc),
47 new StructurePhase(toolcontext, doc): DocPhase]
48
49 for phase in phases do
50 toolcontext.info("# {phase.class_name}", 1)
51 phase.apply
52 end
53
54 # start nitx
55 var nitx = new Nitx(toolcontext, doc)
56 var q = toolcontext.opt_query.value
57 if q != null then # shortcut prompt
58 print ""
59 nitx.do_query(q)
60 return
61 end
62 nitx.start
63 end
64 end
65
66 # build toolcontext
67 var toolcontext = new ToolContext
68 var tpl = new Template
69 tpl.add "Usage: nitx [OPTION]... <file.nit>... [query]\n"
70 tpl.add "Displays specific pieces of API information from Nit source files."
71 toolcontext.tooldescription = tpl.write_to_string
72
73 # process options
74 toolcontext.process_options(args)
75 var arguments = toolcontext.option_context.rest
76
77 # build model
78 var model = new Model
79 var mbuilder = new ModelBuilder(model, toolcontext)
80 var mmodules = mbuilder.parse_full(arguments)
81
82 # process
83 if mmodules.is_empty then return
84 mbuilder.run_phases
85 toolcontext.run_global_phases(mmodules)