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
26 super OrderedTree[Object]
35 return o
.filepath
.as(not null)
38 if o
.mdoc
!= null then
39 if tc
.opt_no_color
.value
then
40 d
= ": {o.mdoc.content.first}"
42 d
= ": {o.mdoc.content.first.green}"
45 if tc
.opt_no_color
.value
then
46 return "{o.name}{d} ({o.filepath.to_s})"
48 return "{o.name}{d} ({o.filepath.yellow})"
51 else if o
isa ModulePath then
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}"
61 d
= ": {o.mmodule.mdoc.content.first.green}"
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
73 if tc
.opt_no_color
.value
then
74 dd
= " ({ms.join(" ")})"
76 dd
= " ({ms.join(" ")})".light_gray
79 if tc
.opt_no_color
.value
then
80 return "{o.name.bold}{d} ({o.filepath.to_s}){dd}"
82 return "{o.name.bold}{d} ({o.filepath.yellow}){dd}"
91 class AlphaEntityComparator
93 fun nameof
(a
: COMPARED): String
97 else if a
isa ModulePath then
103 redef fun compare
(a
,b
)
105 return nameof
(a
) <=> nameof
(b
)
109 var tc
= new ToolContext
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")
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
.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_project
.value
.to_i
132 print
"Error: options --tree, --source, and --project are exclusives."
133 print tc
.tooldescription
137 tc
.keep_going
= opt_keep
.value
139 var model
= new Model
140 var mb
= new ModelBuilder(model
, tc
)
143 if opt_recursive
.value
then
144 files
= new Array[String]
145 for d
in tc
.option_context
.rest
do
146 var pipe
= new IProcess("find", d
, "-name", "*.nit")
147 while not pipe
.eof
do
148 var l
= pipe
.read_line
149 if l
== "" then break # last line
150 l
= l
.substring
(0,l
.length-1
) # strip last oef
155 if pipe
.status
!= 0 and not opt_keep
.value
then exit
1
158 files
= tc
.option_context
.rest
162 var mp
= mb
.identify_file
(a
)
164 if mp
!= null and not opt_paths
.value
then
165 var mm
= mb
.load_module
(mp
.filepath
)
166 if mm
!= null and opt_depends
.value
then
167 mb
.build_module_importation
(mm
)
173 if sum
== 0 then opt_project
.value
= true
175 var ot
= new ProjTree(tc
)
176 if opt_tree
.value
then
177 ot
.opt_paths
= opt_paths
.value
178 for p
in model
.mprojects
do
179 for g
in p
.mgroups
do
181 if g
.is_interesting
then
185 for mp
in g
.module_paths
do
190 ot
.sort_with
(new AlphaEntityComparator)
194 if opt_source
.value
then
195 var list
= new Array[String]
196 for p
in model
.mprojects
do
197 for g
in p
.mgroups
do
198 for mp
in g
.module_paths
do
199 if opt_paths
.value
then
200 list
.add
(mp
.filepath
)
202 list
.add
("{g.full_name}/{ot.display(mp)}")
207 alpha_comparator
.sort
(list
)
208 for l
in list
do print l
211 if opt_project
.value
then
212 var list
= new Array[String]
213 for p
in model
.mprojects
do
214 var path
= p
.root
.filepath
.as(not null)
215 if opt_paths
.value
then
218 if tc
.opt_no_color
.value
then
219 list
.add
("{p.name} ({path})")
221 list
.add
("{p.name} ({path.yellow})")
225 alpha_comparator
.sort
(list
)
226 for l
in list
do print l