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
23 var tc
= new ToolContext
25 var mb
= new ModelBuilder(model
, tc
)
27 var opt_keep
= new OptionBool("Ignore errors and files that are not a Nit source file", "-k", "--keep")
28 var opt_recursive
= new OptionBool("Process directories recussively", "-r", "--recursive")
29 var opt_tree
= new OptionBool("List source files in their groups and projects", "-t", "--tree")
30 var opt_source
= new OptionBool("List source files", "-s", "--source")
31 var opt_project
= new OptionBool("List projects paths (default)", "-p", "--project")
32 var opt_depends
= new OptionBool("List dependencies of given modules", "-M", "--depends")
34 tc
.option_context
.add_option
(opt_keep
, opt_recursive
, opt_tree
, opt_source
, opt_project
, opt_depends
)
38 var sum
= opt_tree
.value
.to_i
+ opt_source
.value
.to_i
+ opt_project
.value
.to_i
+ opt_depends
.value
.to_i
39 if sum
> 1 or tc
.option_context
.rest
.is_empty
or tc
.opt_help
.value
then
40 print
"Usage: nitls [OPTION].. [FILES]..."
41 print
"List Nit source files"
42 tc
.option_context
.usage
46 if opt_depends
.value
then
47 if opt_recursive
.value
then
48 print
"-M incompatible with -r"
52 mb
.parse
(tc
.option_context
.rest
)
53 for x
in model
.mmodules
do
54 print x
.location
.file
.filename
58 if sum
== 0 then opt_project
.value
= true
61 if opt_recursive
.value
then
62 files
= new Array[String]
63 for d
in tc
.option_context
.rest
do
64 var pipe
= new IProcess("find", d
, "-name", "*.nit")
66 var l
= pipe
.read_line
67 if l
== "" then break # last line
68 l
= l
.substring
(0,l
.length-1
) # strip last oef
73 if pipe
.status
!= 0 and not opt_keep
.value
then exit
1
76 files
= tc
.option_context
.rest
80 var mp
= mb
.identify_file
(a
)
82 if not opt_keep
.value
then tc
.check_errors
86 if opt_tree
.value
then
87 var ot
= new OrderedTree[Object]
88 for p
in model
.mprojects
do
91 for mp
in g
.module_paths
do
96 ot
.sort_with
(new CachedAlphaComparator)
100 if opt_source
.value
then
101 var list
= new Array[String]
102 for p
in model
.mprojects
do
103 for g
in p
.mgroups
do
104 for mp
in g
.module_paths
do
105 list
.add
(mp
.filepath
)
109 alpha_comparator
.sort
(list
)
110 for l
in list
do print l
113 if opt_project
.value
then
114 var list
= new Array[String]
115 for p
in model
.mprojects
do
116 list
.add
(p
.root
.filepath
.as(not null))
118 alpha_comparator
.sort
(list
)
119 for l
in list
do print l