README: document nit_env.sh
[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 # * Display documentation page from Nitdoc in console
21 # * Find type usage in parameters, returns and news.
22 # * Find usage of a specific property.
23 # * Find source code related to class/property by its name.
24 module nitx
25
26 import modelbuilder
27 import doc::doc_phases::doc_console
28
29 redef class ToolContext
30
31 # Nittx generation phase.
32 var docx: Phase = new NitxPhase(self, null)
33
34 # Used to shortcut the prompt and display directly the result in console.
35 var opt_query = new OptionString("Nitx query to perform", "-q", "--query")
36
37 init do option_context.add_option opt_query
38 end
39
40 # Nitx phase explores the model and prepares the console rendering.
41 private class NitxPhase
42 super Phase
43 redef fun process_mainmodule(mainmodule, mmodules)
44 do
45 var doc = new DocModel(mainmodule.model, mainmodule)
46
47 var phases = [
48 new ExtractionPhase(toolcontext, doc),
49 new MakePagePhase(toolcontext, doc),
50 new ConcernsPhase(toolcontext, doc),
51 new StructurePhase(toolcontext, doc),
52 new POSetPhase(toolcontext, doc): DocPhase]
53
54 for phase in phases do
55 toolcontext.info("# {phase.class_name}", 1)
56 phase.apply
57 end
58
59 # start nitx
60 var nitx = new Nitx(toolcontext, doc)
61 var q = toolcontext.opt_query.value
62 if q != null then # shortcut prompt
63 print ""
64 nitx.do_query(q)
65 return
66 end
67 nitx.start
68 end
69 end
70
71 # build toolcontext
72 var toolcontext = new ToolContext
73 var tpl = new Template
74 tpl.add "Usage: nitx [OPTION]... <file.nit>... [query]\n"
75 tpl.add "Displays specific pieces of API information from Nit source files."
76 toolcontext.tooldescription = tpl.write_to_string
77
78 # process options
79 toolcontext.process_options(args)
80 var arguments = toolcontext.option_context.rest
81
82 # build model
83 var model = new Model
84 var mbuilder = new ModelBuilder(model, toolcontext)
85 var mmodules = mbuilder.parse_full(arguments)
86
87 # process
88 if mmodules.is_empty then return
89 mbuilder.run_phases
90 toolcontext.run_global_phases(mmodules)