nitls: bind the option --keep-going on ToolContext::keep_going
[nit.git] / src / nitls.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Simple tool to list Nit source files
18 module nitls
19
20 intrude import modelbuilder
21 import ordered_tree
22
23 class ProjTree
24 super OrderedTree[Object]
25
26 var opt_paths = false
27
28 redef fun display(o)
29 do
30 if o isa MGroup then
31 if opt_paths then
32 return o.filepath.as(not null)
33 else
34 return "{o.name} ({o.filepath.to_s})"
35 end
36 else if o isa ModulePath then
37 if opt_paths then
38 return o.filepath
39 else
40 return "{o.name} ({o.filepath})"
41 end
42 else
43 abort
44 end
45 end
46 end
47
48 var tc = new ToolContext
49
50 var opt_keep = new OptionBool("Ignore errors and files that are not a Nit source file", "-k", "--keep")
51 var opt_recursive = new OptionBool("Process directories recussively", "-r", "--recursive")
52 var opt_tree = new OptionBool("List source files in their groups and projects", "-t", "--tree")
53 var opt_source = new OptionBool("List source files", "-s", "--source")
54 var opt_project = new OptionBool("List projects paths (default)", "-P", "--project")
55 var opt_depends = new OptionBool("List dependencies of given modules", "-d", "--depends")
56 var opt_make = new OptionBool("List dependencies suitable for a rule in a Makefile. Alias for -d, -p and -s", "-M")
57 var opt_paths = new OptionBool("List only path (instead of name + path)", "-p", "--path")
58
59 tc.option_context.add_option(opt_keep, opt_recursive, opt_tree, opt_source, opt_project, opt_depends, opt_paths, opt_make)
60 tc.tooldescription = "Usage: nitls [OPTION]... <file.nit|directory>...\nLists the projects and/or paths of Nit sources files."
61 tc.process_options(args)
62
63 if opt_make.value then
64 opt_depends.value = true
65 opt_paths.value = true
66 opt_source.value = true
67 end
68
69 var sum = opt_tree.value.to_i + opt_source.value.to_i + opt_project.value.to_i
70 if sum > 1 then
71 print "Error: options --tree, --source, and --project are exclusives."
72 print tc.tooldescription
73 exit 1
74 end
75
76 tc.keep_going = opt_keep.value
77
78 var model = new Model
79 var mb = new ModelBuilder(model, tc)
80
81 if opt_depends.value then
82 if opt_recursive.value then
83 print "-M incompatible with -r"
84 exit 1
85 end
86
87 mb.parse(tc.option_context.rest)
88 else
89 var files
90 if opt_recursive.value then
91 files = new Array[String]
92 for d in tc.option_context.rest do
93 var pipe = new IProcess("find", d, "-name", "*.nit")
94 while not pipe.eof do
95 var l = pipe.read_line
96 if l == "" then break # last line
97 l = l.substring(0,l.length-1) # strip last oef
98 files.add l
99 end
100 pipe.close
101 pipe.wait
102 if pipe.status != 0 and not opt_keep.value then exit 1
103 end
104 else
105 files = tc.option_context.rest
106 end
107
108 for a in files do
109 var mp = mb.identify_file(a)
110 if mp == null then
111 tc.check_errors
112 end
113 end
114 end
115
116 if sum == 0 then opt_project.value = true
117
118 if opt_tree.value then
119 var ot = new ProjTree
120 ot.opt_paths = opt_paths.value
121 for p in model.mprojects do
122 for g in p.mgroups do
123 ot.add(g.parent, g)
124 for mp in g.module_paths do
125 ot.add(g, mp)
126 end
127 end
128 end
129 ot.sort_with(new CachedAlphaComparator)
130 ot.write_to(stdout)
131 end
132
133 if opt_source.value then
134 var list = new Array[String]
135 for p in model.mprojects do
136 for g in p.mgroups do
137 for mp in g.module_paths do
138 if opt_paths.value then
139 list.add(mp.filepath)
140 else
141 list.add("{g.full_name}/{mp.name} ({mp.filepath})")
142 end
143 end
144 end
145 end
146 alpha_comparator.sort(list)
147 for l in list do print l
148 end
149
150 if opt_project.value then
151 var list = new Array[String]
152 for p in model.mprojects do
153 var path = p.root.filepath.as(not null)
154 if opt_paths.value then
155 list.add(path)
156 else
157 list.add("{p.name} ({path})")
158 end
159 end
160 alpha_comparator.sort(list)
161 for l in list do print l
162 end