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