nitls: visit group for relevant outputs
[nit.git] / src / nitls.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Simple tool to list Nit source files
18 module nitls
19
20 import modelbuilder
21 intrude import loader
22 import ordered_tree
23 import console
24
25 class ProjTree
26 super OrderedTree[Object]
27
28 var opt_paths = false
29 var tc: ToolContext
30
31 redef fun display(o)
32 do
33 if o isa MGroup then
34 if opt_paths then
35 return o.filepath.as(not null)
36 else
37 var d = ""
38 if o.mdoc != null then
39 if tc.opt_no_color.value then
40 d = ": {o.mdoc.content.first}"
41 else
42 d = ": {o.mdoc.content.first.green}"
43 end
44 end
45 if tc.opt_no_color.value then
46 return "{o.name}{d} ({o.filepath.to_s})"
47 else
48 return "{o.name}{d} ({o.filepath.yellow})"
49 end
50 end
51 else if o isa ModulePath then
52 if opt_paths then
53 return o.filepath
54 else
55 var d = ""
56 var dd = ""
57 if o.mmodule != null and o.mmodule.mdoc != null then
58 if tc.opt_no_color.value then
59 d = ": {o.mmodule.mdoc.content.first}"
60 else
61 d = ": {o.mmodule.mdoc.content.first.green}"
62 end
63 end
64 if o.mmodule != null and not o.mmodule.in_importation.direct_greaters.is_empty then
65 var ms = new Array[String]
66 for m in o.mmodule.in_importation.direct_greaters do
67 if m.mgroup.mproject == o.mmodule.mgroup.mproject then
68 ms.add m.name
69 else
70 ms.add m.full_name
71 end
72 end
73 if tc.opt_no_color.value then
74 dd = " ({ms.join(" ")})"
75 else
76 dd = " ({ms.join(" ")})".light_gray
77 end
78 end
79 if tc.opt_no_color.value then
80 return "{o.name.bold}{d} ({o.filepath.to_s}){dd}"
81 else
82 return "{o.name.bold}{d} ({o.filepath.yellow}){dd}"
83 end
84 end
85 else
86 abort
87 end
88 end
89 end
90
91 class AlphaEntityComparator
92 super Comparator
93 fun nameof(a: COMPARED): String
94 do
95 if a isa MGroup then
96 return a.name
97 else if a isa ModulePath then
98 return a.name
99 else
100 abort
101 end
102 end
103 redef fun compare(a,b)
104 do
105 return nameof(a) <=> nameof(b)
106 end
107 end
108
109 var tc = new ToolContext
110
111 var opt_keep = new OptionBool("Ignore errors and files that are not a Nit source file", "-k", "--keep")
112 var opt_recursive = new OptionBool("Process directories recussively", "-r", "--recursive")
113 var opt_tree = new OptionBool("List source files in their groups and projects", "-t", "--tree")
114 var opt_source = new OptionBool("List source files", "-s", "--source")
115 var opt_project = new OptionBool("List projects paths (default)", "-P", "--project")
116 var opt_depends = new OptionBool("List dependencies of given modules", "-d", "--depends")
117 var opt_make = new OptionBool("List dependencies suitable for a rule in a Makefile. Alias for -d, -p and -s", "-M")
118 var opt_paths = new OptionBool("List only path (instead of name + path)", "-p", "--path")
119
120 tc.option_context.add_option(opt_keep, opt_recursive, opt_tree, opt_source, opt_project, opt_depends, opt_paths, opt_make)
121 tc.tooldescription = "Usage: nitls [OPTION]... <file.nit|directory>...\nLists the projects and/or paths of Nit sources files."
122 tc.accept_no_arguments = true
123 tc.process_options(args)
124
125 if opt_make.value then
126 opt_depends.value = true
127 opt_paths.value = true
128 opt_source.value = true
129 end
130
131 var sum = opt_tree.value.to_i + opt_source.value.to_i + opt_project.value.to_i
132 if sum > 1 then
133 print "Error: options --tree, --source, and --project are exclusives."
134 print tc.tooldescription
135 exit 1
136 end
137 if sum == 0 then opt_project.value = true
138 tc.keep_going = opt_keep.value
139
140 var model = new Model
141 var mb = new ModelBuilder(model, tc)
142
143 if tc.option_context.rest.is_empty then tc.option_context.rest.add "."
144 var files
145 if opt_recursive.value then
146 files = new Array[String]
147 for d in tc.option_context.rest do
148 var pipe = new IProcess("find", d, "-name", "*.nit")
149 while not pipe.eof do
150 var l = pipe.read_line
151 if l == "" then break # last line
152 l = l.substring(0,l.length-1) # strip last oef
153 files.add l
154 end
155 pipe.close
156 pipe.wait
157 if pipe.status != 0 and not opt_keep.value then exit 1
158 end
159 else
160 files = tc.option_context.rest
161 end
162
163 if sum == 0 then
164 # If one of the file is a group, default is `opt_tree` instead of `opt_project`
165 for a in files do
166 var g = mb.get_mgroup(a)
167 if g != null then
168 opt_tree.value = true
169 opt_project.value = false
170 break
171 end
172 end
173 end
174
175 # Identify all relevant files
176 for a in files do
177 var g = mb.get_mgroup(a)
178 var mp = mb.identify_file(a)
179 if g != null and not opt_project.value then
180 mb.visit_group(g)
181 end
182 end
183
184 # Load modules to get more informations
185 for mp in mb.identified_files.values do
186 if mp == null then continue
187 if not opt_paths.value or opt_depends.value then
188 var mm = mb.load_module(mp.filepath)
189 if mm != null and opt_depends.value then
190 mb.build_module_importation(mm)
191 end
192 end
193 end
194 #tc.check_errors
195
196
197 var ot = new ProjTree(tc)
198 var sorter = new AlphaEntityComparator
199 if opt_tree.value then
200 ot.opt_paths = opt_paths.value
201 for p in model.mprojects do
202 for g in p.mgroups do
203 var pa = g.parent
204 if g.is_interesting then
205 ot.add(pa, g)
206 pa = g
207 end
208 for mp in g.module_paths do
209 ot.add(pa, mp)
210 end
211 end
212 end
213 ot.sort_with(sorter)
214 ot.write_to(stdout)
215 end
216
217 if opt_source.value then
218 var list = new Array[ModulePath]
219 for p in model.mprojects do
220 for g in p.mgroups do
221 for mp in g.module_paths do
222 list.add mp
223 end
224 end
225 end
226 sorter.sort(list)
227 for mp in list do
228 if opt_paths.value then
229 print mp.filepath
230 else
231 print "{mp.mgroup.full_name}/{ot.display(mp)}"
232 end
233 end
234 end
235
236 if opt_project.value then
237 var list = new Array[MGroup]
238 for p in model.mprojects do
239 list.add p.root.as(not null)
240 end
241 sorter.sort(list)
242 for g in list do
243 var path = g.filepath.as(not null)
244 if opt_paths.value then
245 print path
246 else
247 var d = ""
248 var md = g.mdoc_or_fallback
249 if md != null then
250 if tc.opt_no_color.value then
251 d = ": {md.content.first}"
252 else
253 d = ": {md.content.first.green}"
254 end
255 end
256 if tc.opt_no_color.value then
257 print "{g.name}{d} ({path})"
258 else
259 print "{g.name}{d} ({path.yellow})"
260 end
261 end
262 end
263 end