nitls: --tree do not show uninterresting groups
[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 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 tc.opt_no_color.value then
64 return "{o.name.bold}{d} ({o.filepath.to_s})"
65 else
66 return "{o.name.bold}{d} ({o.filepath.yellow})"
67 end
68 end
69 else
70 abort
71 end
72 end
73 end
74
75 var tc = new ToolContext
76
77 var opt_keep = new OptionBool("Ignore errors and files that are not a Nit source file", "-k", "--keep")
78 var opt_recursive = new OptionBool("Process directories recussively", "-r", "--recursive")
79 var opt_tree = new OptionBool("List source files in their groups and projects", "-t", "--tree")
80 var opt_source = new OptionBool("List source files", "-s", "--source")
81 var opt_project = new OptionBool("List projects paths (default)", "-P", "--project")
82 var opt_depends = new OptionBool("List dependencies of given modules", "-d", "--depends")
83 var opt_make = new OptionBool("List dependencies suitable for a rule in a Makefile. Alias for -d, -p and -s", "-M")
84 var opt_paths = new OptionBool("List only path (instead of name + path)", "-p", "--path")
85
86 tc.option_context.add_option(opt_keep, opt_recursive, opt_tree, opt_source, opt_project, opt_depends, opt_paths, opt_make)
87 tc.tooldescription = "Usage: nitls [OPTION]... <file.nit|directory>...\nLists the projects and/or paths of Nit sources files."
88 tc.process_options(args)
89
90 if opt_make.value then
91 opt_depends.value = true
92 opt_paths.value = true
93 opt_source.value = true
94 end
95
96 var sum = opt_tree.value.to_i + opt_source.value.to_i + opt_project.value.to_i
97 if sum > 1 then
98 print "Error: options --tree, --source, and --project are exclusives."
99 print tc.tooldescription
100 exit 1
101 end
102
103 tc.keep_going = opt_keep.value
104
105 var model = new Model
106 var mb = new ModelBuilder(model, tc)
107
108 var files
109 if opt_recursive.value then
110 files = new Array[String]
111 for d in tc.option_context.rest do
112 var pipe = new IProcess("find", d, "-name", "*.nit")
113 while not pipe.eof do
114 var l = pipe.read_line
115 if l == "" then break # last line
116 l = l.substring(0,l.length-1) # strip last oef
117 files.add l
118 end
119 pipe.close
120 pipe.wait
121 if pipe.status != 0 and not opt_keep.value then exit 1
122 end
123 else
124 files = tc.option_context.rest
125 end
126
127 for a in files do
128 var mp = mb.identify_file(a)
129 tc.check_errors
130 if mp != null and not opt_paths.value then
131 var mm = mb.load_module(mp.filepath)
132 if mm != null and opt_depends.value then
133 mb.build_module_importation(mm)
134 end
135 tc.check_errors
136 end
137 end
138
139 if sum == 0 then opt_project.value = true
140
141 var ot = new ProjTree(tc)
142 if opt_tree.value then
143 ot.opt_paths = opt_paths.value
144 for p in model.mprojects do
145 for g in p.mgroups do
146 var pa = g.parent
147 if g.is_interesting then
148 ot.add(pa, g)
149 pa = g
150 end
151 for mp in g.module_paths do
152 ot.add(pa, mp)
153 end
154 end
155 end
156 ot.sort_with(new CachedAlphaComparator)
157 ot.write_to(stdout)
158 end
159
160 if opt_source.value then
161 var list = new Array[String]
162 for p in model.mprojects do
163 for g in p.mgroups do
164 for mp in g.module_paths do
165 if opt_paths.value then
166 list.add(mp.filepath)
167 else
168 list.add("{g.full_name}/{ot.display(mp)}")
169 end
170 end
171 end
172 end
173 alpha_comparator.sort(list)
174 for l in list do print l
175 end
176
177 if opt_project.value then
178 var list = new Array[String]
179 for p in model.mprojects do
180 var path = p.root.filepath.as(not null)
181 if opt_paths.value then
182 list.add(path)
183 else
184 if tc.opt_no_color.value then
185 list.add("{p.name} ({path})")
186 else
187 list.add("{p.name} ({path.yellow})")
188 end
189 end
190 end
191 alpha_comparator.sort(list)
192 for l in list do print l
193 end