5c375ad0add1ac47f6069795e0b2aacf019588c5
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
20 intrude import modelbuilder
24 super OrderedTree[Object]
32 return o
.filepath
.as(not null)
34 return "{o.name} ({o.filepath})"
36 else if o
isa ModulePath then
40 return "{o.name} ({o.filepath})"
48 var tc
= new ToolContext
50 var mb
= new ModelBuilder(model
, tc
)
52 var opt_keep
= new OptionBool("Ignore errors and files that are not a Nit source file", "-k", "--keep")
53 var opt_recursive
= new OptionBool("Process directories recussively", "-r", "--recursive")
54 var opt_tree
= new OptionBool("List source files in their groups and projects", "-t", "--tree")
55 var opt_source
= new OptionBool("List source files", "-s", "--source")
56 var opt_project
= new OptionBool("List projects paths (default)", "-p", "--project")
57 var opt_depends
= new OptionBool("List dependencies of given modules", "-d", "--depends")
58 var opt_make
= new OptionBool("List dependencies suitable for a rule in a Makefile. Alias for -d, -p and -s", "-M")
59 var opt_paths
= new OptionBool("List only path (instead of name + path)", "-p", "--path")
61 tc
.option_context
.add_option
(opt_keep
, opt_recursive
, opt_tree
, opt_source
, opt_project
, opt_depends
, opt_paths
, opt_make
)
62 tc
.tooldescription
= "Usage: nitls [OPTION]... <file.nit|directory>...\nLists the projects and/or paths of Nit sources files."
63 tc
.process_options
(args
)
65 if opt_make
.value
then
66 opt_depends
.value
= true
67 opt_paths
.value
= true
68 opt_source
.value
= true
71 var sum
= opt_tree
.value
.to_i
+ opt_source
.value
.to_i
+ opt_project
.value
.to_i
73 print
"Error: options --tree, --source, and --project are exclusives."
74 print tc
.tooldescription
78 if opt_depends
.value
then
79 if opt_recursive
.value
then
80 print
"-M incompatible with -r"
84 mb
.parse
(tc
.option_context
.rest
)
87 if opt_recursive
.value
then
88 files
= new Array[String]
89 for d
in tc
.option_context
.rest
do
90 var pipe
= new IProcess("find", d
, "-name", "*.nit")
92 var l
= pipe
.read_line
93 if l
== "" then break # last line
94 l
= l
.substring
(0,l
.length-1
) # strip last oef
99 if pipe
.status
!= 0 and not opt_keep
.value
then exit
1
102 files
= tc
.option_context
.rest
106 var mp
= mb
.identify_file
(a
)
108 if not opt_keep
.value
then tc
.check_errors
113 if sum
== 0 then opt_project
.value
= true
115 if opt_tree
.value
then
116 var ot
= new ProjTree
117 ot
.opt_paths
= opt_paths
.value
118 for p
in model
.mprojects
do
119 for g
in p
.mgroups
do
121 for mp
in g
.module_paths
do
126 ot
.sort_with
(new CachedAlphaComparator)
130 if opt_source
.value
then
131 var list
= new Array[String]
132 for p
in model
.mprojects
do
133 for g
in p
.mgroups
do
134 for mp
in g
.module_paths
do
135 if opt_paths
.value
then
136 list
.add
(mp
.filepath
)
138 list
.add
("{g.full_name}/{mp.name} ({mp.filepath})")
143 alpha_comparator
.sort
(list
)
144 for l
in list
do print l
147 if opt_project
.value
then
148 var list
= new Array[String]
149 for p
in model
.mprojects
do
150 var path
= p
.root
.filepath
.as(not null)
151 if opt_paths
.value
then
154 list
.add
("{p.name} ({path})")
157 alpha_comparator
.sort
(list
)
158 for l
in list
do print l