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