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[Object]
33 return o
.filepath
.as(not null)
36 if o
.mdoc
!= null then
37 d
= ": {o.mdoc.content.first}"
39 return "{o.name} ({o.filepath.to_s}){d}"
41 else if o
isa ModulePath then
44 else if o
.mmodule
!= null then
46 if o
.mmodule
.mdoc
!= null then
47 d
= ": {o.mmodule.mdoc.content.first}"
49 return "{o.name}{d} ({o.filepath})"
51 return "{o.name} ({o.filepath})"
59 var tc
= new ToolContext
61 var opt_keep
= new OptionBool("Ignore errors and files that are not a Nit source file", "-k", "--keep")
62 var opt_recursive
= new OptionBool("Process directories recussively", "-r", "--recursive")
63 var opt_tree
= new OptionBool("List source files in their groups and projects", "-t", "--tree")
64 var opt_source
= new OptionBool("List source files", "-s", "--source")
65 var opt_project
= new OptionBool("List projects paths (default)", "-P", "--project")
66 var opt_depends
= new OptionBool("List dependencies of given modules", "-d", "--depends")
67 var opt_make
= new OptionBool("List dependencies suitable for a rule in a Makefile. Alias for -d, -p and -s", "-M")
68 var opt_paths
= new OptionBool("List only path (instead of name + path)", "-p", "--path")
70 tc
.option_context
.add_option
(opt_keep
, opt_recursive
, opt_tree
, opt_source
, opt_project
, opt_depends
, opt_paths
, opt_make
)
71 tc
.tooldescription
= "Usage: nitls [OPTION]... <file.nit|directory>...\nLists the projects and/or paths of Nit sources files."
72 tc
.process_options
(args
)
74 if opt_make
.value
then
75 opt_depends
.value
= true
76 opt_paths
.value
= true
77 opt_source
.value
= true
80 var sum
= opt_tree
.value
.to_i
+ opt_source
.value
.to_i
+ opt_project
.value
.to_i
82 print
"Error: options --tree, --source, and --project are exclusives."
83 print tc
.tooldescription
87 tc
.keep_going
= opt_keep
.value
90 var mb
= new ModelBuilder(model
, tc
)
92 if opt_depends
.value
then
93 if opt_recursive
.value
then
94 print
"-M incompatible with -r"
98 mb
.parse
(tc
.option_context
.rest
)
101 if opt_recursive
.value
then
102 files
= new Array[String]
103 for d
in tc
.option_context
.rest
do
104 var pipe
= new IProcess("find", d
, "-name", "*.nit")
105 while not pipe
.eof
do
106 var l
= pipe
.read_line
107 if l
== "" then break # last line
108 l
= l
.substring
(0,l
.length-1
) # strip last oef
113 if pipe
.status
!= 0 and not opt_keep
.value
then exit
1
116 files
= tc
.option_context
.rest
120 var mp
= mb
.identify_file
(a
)
122 if mp
!= null and not opt_paths
.value
then
123 mb
.load_module
(mp
.filepath
)
128 if sum
== 0 then opt_project
.value
= true
130 var ot
= new ProjTree
131 if opt_tree
.value
then
132 ot
.opt_paths
= opt_paths
.value
133 for p
in model
.mprojects
do
134 for g
in p
.mgroups
do
136 for mp
in g
.module_paths
do
141 ot
.sort_with
(new CachedAlphaComparator)
145 if opt_source
.value
then
146 var list
= new Array[String]
147 for p
in model
.mprojects
do
148 for g
in p
.mgroups
do
149 for mp
in g
.module_paths
do
150 if opt_paths
.value
then
151 list
.add
(mp
.filepath
)
153 list
.add
("{g.full_name}/{ot.display(mp)}")
158 alpha_comparator
.sort
(list
)
159 for l
in list
do print l
162 if opt_project
.value
then
163 var list
= new Array[String]
164 for p
in model
.mprojects
do
165 var path
= p
.root
.filepath
.as(not null)
166 if opt_paths
.value
then
169 list
.add
("{p.name} ({path})")
172 alpha_comparator
.sort
(list
)
173 for l
in list
do print l