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