src: new tool nitls
[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
33 tc.option_context.add_option(opt_keep, opt_recursive, opt_tree, opt_source, opt_project)
34
35 tc.process_options
36
37 var sum = opt_tree.value.to_i + opt_source.value.to_i + opt_project.value.to_i
38 if sum > 1 or tc.option_context.rest.is_empty or tc.opt_help.value then
39 print "Usage: nitls [OPTION].. [FILES]..."
40 print "List Nit source files"
41 tc.option_context.usage
42 exit 0
43 end
44
45 if sum == 0 then opt_project.value = true
46
47 var files
48 if opt_recursive.value then
49 files = new Array[String]
50 for d in tc.option_context.rest do
51 var pipe = new IProcess("find", d, "-name", "*.nit")
52 while not pipe.eof do
53 var l = pipe.read_line
54 if l == "" then break # last line
55 l = l.substring(0,l.length-1) # strip last oef
56 files.add l
57 end
58 pipe.close
59 pipe.wait
60 if pipe.status != 0 and not opt_keep.value then exit 1
61 end
62 else
63 files = tc.option_context.rest
64 end
65
66 for a in files do
67 var mp = mb.identify_file(a)
68 if mp == null then
69 if not opt_keep.value then tc.check_errors
70 end
71 end
72
73 if opt_tree.value then
74 var ot = new OrderedTree[Object]
75 for p in model.mprojects do
76 for g in p.mgroups do
77 ot.add(g.parent, g)
78 for mp in g.module_paths do
79 ot.add(g, mp)
80 end
81 end
82 end
83 ot.sort_with(new CachedAlphaComparator)
84 ot.write_to(stdout)
85 end
86
87 if opt_source.value then
88 var list = new Array[String]
89 for p in model.mprojects do
90 for g in p.mgroups do
91 for mp in g.module_paths do
92 list.add(mp.filepath)
93 end
94 end
95 end
96 alpha_comparator.sort(list)
97 for l in list do print l
98 end
99
100 if opt_project.value then
101 var list = new Array[String]
102 for p in model.mprojects do
103 list.add(p.root.filepath.as(not null))
104 end
105 alpha_comparator.sort(list)
106 for l in list do print l
107 end