nitls: add -M option do list dependencies
[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
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 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
43 exit 0
44 end
45
46 if opt_depends.value then
47 if opt_recursive.value then
48 print "-M incompatible with -r"
49 exit 1
50 end
51
52 mb.parse(tc.option_context.rest)
53 for x in model.mmodules do
54 print x.location.file.filename
55 end
56 end
57
58 if sum == 0 then opt_project.value = true
59
60 var files
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")
65 while not pipe.eof do
66 var l = pipe.read_line
67 if l == "" then break # last line
68 l = l.substring(0,l.length-1) # strip last oef
69 files.add l
70 end
71 pipe.close
72 pipe.wait
73 if pipe.status != 0 and not opt_keep.value then exit 1
74 end
75 else
76 files = tc.option_context.rest
77 end
78
79 for a in files do
80 var mp = mb.identify_file(a)
81 if mp == null then
82 if not opt_keep.value then tc.check_errors
83 end
84 end
85
86 if opt_tree.value then
87 var ot = new OrderedTree[Object]
88 for p in model.mprojects do
89 for g in p.mgroups do
90 ot.add(g.parent, g)
91 for mp in g.module_paths do
92 ot.add(g, mp)
93 end
94 end
95 end
96 ot.sort_with(new CachedAlphaComparator)
97 ot.write_to(stdout)
98 end
99
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)
106 end
107 end
108 end
109 alpha_comparator.sort(list)
110 for l in list do print l
111 end
112
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))
117 end
118 alpha_comparator.sort(list)
119 for l in list do print l
120 end