cf7c609f372775e187b78b6bc5a4ba53ddd12444
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Simple tool to list Nit source files
25 super OrderedTree[MConcern]
34 return o
.filepath
.as(not null)
37 if o
.mdoc
!= null then
38 if tc
.opt_no_color
.value
then
39 d
= ": {o.mdoc.content.first}"
41 d
= ": {o.mdoc.content.first.green}"
44 if tc
.opt_no_color
.value
then
45 return "{o.name}{d} ({o.filepath.to_s})"
47 return "{o.name}{d} ({o.filepath.yellow})"
50 else if o
isa MModule then
52 return o
.filepath
.as(not null)
56 if o
.mdoc
!= null then
57 if tc
.opt_no_color
.value
then
58 d
= ": {o.mdoc.content.first}"
60 d
= ": {o.mdoc.content.first.green}"
63 if not o
.in_importation
.direct_greaters
.is_empty
then
64 var ms
= new Array[String]
65 for m
in o
.in_importation
.direct_greaters
do
66 if m
.mgroup
.mpackage
== o
.mgroup
.mpackage
then
72 if tc
.opt_no_color
.value
then
73 dd
= " ({ms.join(" ")})"
75 dd
= " ({ms.join(" ")})".light_gray
78 if tc
.opt_no_color
.value
then
79 return "{o.name.bold}{d} ({o.filepath.to_s}){dd}"
81 return "{o.name.bold}{d} ({o.filepath.yellow}){dd}"
90 class AlphaEntityComparator
92 fun nameof
(a
: COMPARED): String
96 else if a
isa ModulePath then
102 redef fun compare
(a
,b
)
104 return nameof
(a
) <=> nameof
(b
)
108 var tc
= new ToolContext
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 packages", "-t", "--tree")
113 var opt_source
= new OptionBool("List source files", "-s", "--source")
114 var opt_package
= new OptionBool("List packages paths (default)", "-P", "--package")
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")
119 tc
.option_context
.add_option
(opt_keep
, opt_recursive
, opt_tree
, opt_source
, opt_package
, opt_depends
, opt_paths
, opt_make
)
120 tc
.tooldescription
= "Usage: nitls [OPTION]... <file.nit|directory>...\nLists the packages and/or paths of Nit sources files."
121 tc
.accept_no_arguments
= true
122 tc
.process_options
(args
)
124 if opt_make
.value
then
125 opt_depends
.value
= true
126 opt_paths
.value
= true
127 opt_source
.value
= true
130 var sum
= opt_tree
.value
.to_i
+ opt_source
.value
.to_i
+ opt_package
.value
.to_i
132 print
"Error: options --tree, --source, and --package are exclusive."
133 print tc
.tooldescription
136 if sum
== 0 then opt_package
.value
= true
137 tc
.keep_going
= opt_keep
.value
139 var model
= new Model
140 var mb
= new ModelBuilder(model
, tc
)
142 if tc
.option_context
.rest
.is_empty
then tc
.option_context
.rest
.add
"."
144 if opt_recursive
.value
then
145 files
= new Array[String]
146 for d
in tc
.option_context
.rest
do
147 var pipe
= new ProcessReader("find", d
, "-name", "*.nit")
148 while not pipe
.eof
do
149 var l
= pipe
.read_line
150 if l
== "" then break # last line
155 if pipe
.status
!= 0 and not opt_keep
.value
then exit
1
158 files
= tc
.option_context
.rest
162 # If one of the file is a group, default is `opt_tree` instead of `opt_package`
164 var g
= mb
.identify_group
(a
)
166 opt_tree
.value
= true
167 opt_package
.value
= false
173 var mmodules
= mb
.scan_full
(files
)
175 # Load modules to get more informations
176 for mmodule
in mmodules
do
177 if not opt_paths
.value
or opt_depends
.value
then
178 var ast
= mmodule
.parse
(mb
)
179 if ast
!= null and opt_depends
.value
then
180 mb
.build_module_importation
(ast
)
186 if opt_depends
.value
then
187 # Extends the list of module with the loaded ones
188 mmodules
= mb
.parsed_modules
.to_a
191 var ot
= new ProjTree(tc
)
192 var sorter
= new AlphaEntityComparator
193 if opt_tree
.value
then
194 ot
.opt_paths
= opt_paths
.value
195 var mgroups
= new HashSet[MGroup]
196 for mp
in mmodules
do
198 while pa
!= null and not pa
.is_interesting
do pa
= pa
.parent
200 if pa
!= null then mgroups
.add pa
204 if g
.is_interesting
then
212 if opt_source
.value
then
213 sorter
.sort
(mmodules
)
214 for mp
in mmodules
do
215 if opt_paths
.value
then
216 print mp
.filepath
.as(not null)
218 print
"{mp.mgroup.full_name}/{ot.display(mp)}"
223 if opt_package
.value
then
224 var mpackages
= new Array[MPackage]
226 var p
= m
.mgroup
.mpackage
227 if mpackages
.has
(p
) then continue
231 sorter
.sort
(mpackages
)
232 for p
in mpackages
do
233 var g
= p
.root
.as(not null)
234 var path
= g
.filepath
.as(not null)
235 if opt_paths
.value
then
239 var md
= g
.mdoc_or_fallback
241 if tc
.opt_no_color
.value
then
242 d
= ": {md.content.first}"
244 d
= ": {md.content.first.green}"
247 if tc
.opt_no_color
.value
then
248 print
"{g.name}{d} ({path})"
250 print
"{g.name}{d} ({path.yellow})"