311580ae6697375f7719a94104100ed8fdf354dd
[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`, a command tool that displays useful data about Nit code
16 #
17 # Features:
18 #
19 # * Display documentation from name/namespace
20 # * Find type usage in parameters, returns and news.
21 # * Find usage (calls) of a specific property.
22 # * Find source code related to class/property by its name.
23 # * Display inheritance lists
24 module nitx
25
26 import frontend
27 import doc::term
28 import prompt
29
30 redef class ToolContext
31
32 # Used to shortcut the prompt and display directly the result in console.
33 var opt_command = new OptionString("Nitx command to perform", "-c", "--command")
34
35 # Compute and use the catalog
36 var opt_catalog = new OptionBool("Use catalog", "--catalog")
37
38 init do option_context.add_option(opt_command, opt_catalog)
39 end
40
41 # Nitx handles console queries
42 #
43 # Using `prompt`, the command line can be turned on an interactive tool.
44 class Nitx
45
46 # Model that contains the informations to display
47 var model: Model
48
49 # Mainmodule for class linearization
50 var mainmodule: MModule
51
52 # ModelBuilder to access AST nodes
53 var modelbuilder: ModelBuilder
54
55 # Catalog if any
56 var catalog: nullable Catalog = null is optional
57
58 # Do not use colors in the output
59 var no_color = false is optional
60
61 # Displays the welcome message and start prompt.
62 fun start do
63 welcome
64 prompt
65 end
66
67 # Displays the welcome message and the list of loaded modules.
68 fun welcome do
69 print "Welcome in the Nit Index!"
70 print ""
71 print "Loaded packages:\n"
72 var cmd = new CmdModelEntities(model, kind = "packages")
73 cmd.init_command
74 for mpackage in cmd.results.as(not null) do
75 print " * {mpackage.full_name}"
76 end
77 help
78 end
79
80 # Displays the list of available queries.
81 fun help do
82 # TODO automatize that
83 print "\nCommands:\n"
84 for usage, doc in parser.commands_usage do
85 var l = usage.length / 8
86 print "\t{usage}{"\t" * (3 - l)}{doc}"
87 end
88 print "\n"
89 print "\t:h\t\t\tdisplay this help message"
90 print "\t:q\t\t\tquit interactive mode"
91 print ""
92 end
93
94 # Prompts the user for a query.
95 fun prompt do
96 var line = sys.prompt(">> ", true)
97 if line != null then
98 do_command(line)
99 else
100 # EOF
101 exit 0
102 end
103 prompt
104 end
105
106 # Parser used to process doc commands
107 var parser = new CommandParser(model, mainmodule, modelbuilder, catalog) is lazy
108
109 # Processes the query string and performs it.
110 fun do_command(str: String) do
111 if str == ":q" then
112 exit 0
113 else if str == ":h" then
114 help
115 return
116 end
117 parser.execute(str, no_color)
118 end
119 end
120
121 redef class Catalog
122 # Build the catalog for Nitx
123 private fun build_catalog(model: Model, filter: nullable ModelFilter) do
124 # Compute the poset
125 for p in model.collect_mpackages(filter) do
126 var g = p.root
127 assert g != null
128 modelbuilder.scan_group(g)
129
130 deps.add_node(p)
131 for gg in p.mgroups do for m in gg.mmodules do
132 for im in m.in_importation.direct_greaters do
133 var ip = im.mpackage
134 if ip == null or ip == p then continue
135 deps.add_edge(p, ip)
136 end
137 end
138 end
139 # Build the catalog
140 for mpackage in model.collect_mpackages(filter) do
141 package_page(mpackage)
142 git_info(mpackage)
143 mpackage_stats(mpackage)
144 end
145 end
146 end
147
148 # build toolcontext
149 var toolcontext = new ToolContext
150 var tpl = new Template
151 tpl.add "Usage: nitx [OPTION]... <file.nit>... [query]\n"
152 tpl.add "Displays pieces of API information from Nit source files."
153 toolcontext.tooldescription = tpl.write_to_string
154
155 # process options
156 toolcontext.process_options(args)
157 var arguments = toolcontext.option_context.rest
158
159 # build model
160 var model = new Model
161 var mbuilder = new ModelBuilder(model, toolcontext)
162 var mmodules = mbuilder.parse_full(arguments)
163
164 # process
165 if mmodules.is_empty then return
166 mbuilder.run_phases
167 toolcontext.run_global_phases(mmodules)
168 var mainmodule = toolcontext.make_main_module(mmodules)
169
170 # build views
171 var catalog = null
172 if toolcontext.opt_catalog.value then
173 catalog = new Catalog(mbuilder)
174 catalog.build_catalog(model)
175 end
176
177 # start nitx
178 var nitx = new Nitx(model, mainmodule, mbuilder, catalog, toolcontext.opt_no_color.value)
179 var q = toolcontext.opt_command.value
180 if q != null then # shortcut prompt
181 print ""
182 nitx.do_command(q)
183 return
184 end
185 nitx.start