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