src: update tools to the new toolcontext helpers on option processing
[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 var tc = new ToolContext
24 var model = new Model
25 var mb = new ModelBuilder(model, tc)
26
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")
33
34 tc.option_context.add_option(opt_keep, opt_recursive, opt_tree, opt_source, opt_project, opt_depends)
35 tc.tooldescription = "Usage: nitls [OPTION]... <file.nit|directory>...\nLists the projects and/or paths of Nit sources files."
36 tc.process_options
37
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 then
40 print "Error: options --tree, --source, and --project are exclusives."
41 print tc.tooldescription
42 exit 1
43 end
44
45 if opt_depends.value then
46 if opt_recursive.value then
47 print "-M incompatible with -r"
48 exit 1
49 end
50
51 mb.parse(tc.option_context.rest)
52 for x in model.mmodules do
53 print x.location.file.filename
54 end
55 end
56
57 if sum == 0 then opt_project.value = true
58
59 var files
60 if opt_recursive.value then
61 files = new Array[String]
62 for d in tc.option_context.rest do
63 var pipe = new IProcess("find", d, "-name", "*.nit")
64 while not pipe.eof do
65 var l = pipe.read_line
66 if l == "" then break # last line
67 l = l.substring(0,l.length-1) # strip last oef
68 files.add l
69 end
70 pipe.close
71 pipe.wait
72 if pipe.status != 0 and not opt_keep.value then exit 1
73 end
74 else
75 files = tc.option_context.rest
76 end
77
78 for a in files do
79 var mp = mb.identify_file(a)
80 if mp == null then
81 if not opt_keep.value then tc.check_errors
82 end
83 end
84
85 if opt_tree.value then
86 var ot = new OrderedTree[Object]
87 for p in model.mprojects do
88 for g in p.mgroups do
89 ot.add(g.parent, g)
90 for mp in g.module_paths do
91 ot.add(g, mp)
92 end
93 end
94 end
95 ot.sort_with(new CachedAlphaComparator)
96 ot.write_to(stdout)
97 end
98
99 if opt_source.value then
100 var list = new Array[String]
101 for p in model.mprojects do
102 for g in p.mgroups do
103 for mp in g.module_paths do
104 list.add(mp.filepath)
105 end
106 end
107 end
108 alpha_comparator.sort(list)
109 for l in list do print l
110 end
111
112 if opt_project.value then
113 var list = new Array[String]
114 for p in model.mprojects do
115 list.add(p.root.filepath.as(not null))
116 end
117 alpha_comparator.sort(list)
118 for l in list do print l
119 end