src/doc/commands: clean importation for `commands_base`
[nit.git] / src / doc / commands / commands_catalog.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Commands to retrieve Catalog related data
16 module commands_catalog
17
18 import commands_model
19 import catalog
20
21 # A DocCommand based on a Catalog
22 abstract class CmdCatalog
23 super DocCommand
24
25 autoinit(model, catalog, filter)
26
27 # Catalog to query at
28 var catalog: Catalog
29 end
30
31 # A CmdSearch command using a Catalog
32 class CmdCatalogSearch
33 super CmdCatalog
34 super CmdSearch
35
36 autoinit(model, catalog, filter, query, limit, page, count, max)
37
38 redef fun init_results do
39 if results != null then return new CmdSuccess
40
41 var res = super
42 if not res isa CmdSuccess then return res
43
44 var query = self.query
45 if query == null then return new ErrorNoQuery
46 sorter = null
47
48 var index = model.index
49
50 # lookup by name prefix
51 var matches = index.find_by_name_prefix(query).uniq.
52 sort(lname_sorter, name_sorter, kind_sorter)
53 matches = matches.rerank.sort(vis_sorter, score_sorter)
54
55 # lookup by tags
56 var malus = matches.length
57 if catalog.tag2proj.has_key(query) then
58 for mpackage in catalog.tag2proj[query] do
59 matches.add new IndexMatch(mpackage, malus)
60 malus += 1
61 end
62 matches = matches.uniq.rerank.sort(vis_sorter, score_sorter)
63 end
64
65 # lookup by full_name prefix
66 malus = matches.length
67 var full_matches = new IndexMatches
68 for match in index.find_by_full_name_prefix(query).
69 sort(lfname_sorter, fname_sorter) do
70 match.score += 1
71 full_matches.add match
72 end
73 matches = matches.uniq
74
75 # lookup by similarity
76 malus = matches.length
77 var sim_matches = new IndexMatches
78 for match in index.find_by_similarity(query).sort(score_sorter, lname_sorter, name_sorter) do
79 if match.score > query.length then break
80 match.score += 1
81 sim_matches.add match
82 end
83 matches.add_all sim_matches
84 matches = matches.uniq
85 results = matches.rerank.sort(vis_sorter, score_sorter).mentities
86 return res
87 end
88
89 private var score_sorter = new ScoreComparator
90 private var vis_sorter = new VisibilityComparator
91 private var name_sorter = new NameComparator
92 private var lname_sorter = new NameLengthComparator
93 private var fname_sorter = new FullNameComparator
94 private var lfname_sorter = new FullNameLengthComparator
95 private var kind_sorter = new MEntityComparator
96 end
97
98 # Retrieve the catalog metadata for a MPackage
99 class CmdMetadata
100 super CmdEntity
101
102 # MPackage metadata retrieved
103 var metadata: nullable MPackageMetadata = null is optional, writable
104
105 redef fun init_command do
106 if metadata != null then return new CmdSuccess
107
108 var res = super
109 if not res isa CmdSuccess then return res
110 var mentity = self.mentity.as(not null)
111
112 if mentity isa MPackage then
113 metadata = mentity.metadata
114 else
115 return new WarningNoMetadata(mentity)
116 end
117 return res
118 end
119 end
120
121 # No metadata for `mentity`
122 class WarningNoMetadata
123 super CmdWarning
124
125 # MEntity provided
126 var mentity: MEntity
127
128 redef fun to_s do return "No metadata for `{mentity.full_name}`"
129 end
130
131 # Retrieve the packages in the catalog
132 class CmdCatalogPackages
133 super CmdCatalog
134 super CmdEntities
135
136 autoinit(model, catalog, filter, limit, page, count, max)
137
138 redef var sorter = new CatalogScoreSorter(catalog) is lazy
139
140 redef fun init_results do
141 if results != null then return new CmdSuccess
142
143 var res = super
144 if not res isa CmdSuccess then return res
145
146 results = catalog.mpackages.values.to_a
147 return res
148 end
149 end
150
151 # Retrieve the catalog stats
152 class CmdCatalogStats
153 super CmdCatalog
154
155 # Retrieved catalog statistics
156 var stats: nullable CatalogStats = null is optional, writable
157
158 redef fun init_command do
159 super
160 self.stats = catalog.catalog_stats
161 return new CmdSuccess
162 end
163 end
164
165 # Retrieve the catalog tags list
166 class CmdCatalogTags
167 super CmdCatalog
168
169 # Sorter to sort tags alphabetically
170 var tags_sorter = new CatalogTagsSorter is optional, writable
171
172 # Count of packages by tag
173 var packages_count_by_tags: nullable ArrayMap[String, Int] = null is optional, writable
174
175 redef fun init_command do
176 super
177 var tags_to_projects = new ArrayMap[String, Int]
178 var tags = catalog.tag2proj.keys.to_a
179 tags_sorter.sort(tags)
180 for tag in tags do
181 if not catalog.tag2proj.has_key(tag) then continue
182 tags_to_projects[tag] = catalog.tag2proj[tag].length
183 end
184 packages_count_by_tags = tags_to_projects
185 return new CmdSuccess
186 end
187 end
188
189 # Retrieve the packages for a tag
190 class CmdCatalogTag
191 super CmdCatalogPackages
192
193 autoinit(model, catalog, filter, tag, limit, page, count, max)
194
195 # The tag to retrieve
196 var tag: nullable String = null is optional, writable
197
198 redef fun init_command do
199 var tag = self.tag
200 if tag == null then return new ErrorNoTag
201
202 if not catalog.tag2proj.has_key(tag) then return new ErrorTagNotFound(tag)
203 return super
204 end
205
206 redef fun init_results do
207 if results != null then return new CmdSuccess
208
209 var res = super
210 if not res isa CmdSuccess then return res
211
212 results = catalog.tag2proj[tag].to_a
213 return res
214 end
215 end
216
217 # No tag name provided
218 class ErrorNoTag
219 super CmdError
220
221 redef fun to_s do return "No tag name provided"
222 end
223
224 # No tag with this name in the catalog
225 class ErrorTagNotFound
226 super CmdError
227
228 # The tag that was not found
229 var tag: String
230
231 redef fun to_s do return "No tag found for `{tag}`"
232 end
233
234 # Retrieve a person from the catalog
235 class CmdCatalogPerson
236 super CmdCatalog
237
238 # Person to retrieve
239 #
240 # You can also pass a `person_name`.
241 var person: nullable Person = null is optional, writable
242
243 # Name of the person to retrieve
244 #
245 # You can also pass a `person` instance.
246 var person_name: nullable String = null is optional, writable
247
248 # Initialize the `person` result
249 fun init_person: CmdMessage do
250 var person = self.person
251 if person != null then
252 person_name = person.name
253 return new CmdSuccess
254 end
255
256 var name = self.person_name
257 if name == null then return new ErrorNoPerson
258 if not catalog.name2person.has_key(name) then return new ErrorPersonNotFound(name)
259 self.person = catalog.name2person[name]
260 return new CmdSuccess
261 end
262
263 redef fun init_command do
264 init_person
265 return super
266 end
267 end
268
269 # No person instance or name provided
270 class ErrorNoPerson
271 super CmdError
272
273 redef fun to_s do return "No person provided"
274 end
275
276 # No person found with this name
277 class ErrorPersonNotFound
278 super CmdError
279
280 # Name of the person that was not found
281 var name: String
282
283 redef fun to_s do return "No person found for `{name}`"
284 end
285
286 # Retrieve the packages maintained by a person
287 class CmdCatalogMaintaining
288 super CmdCatalogPerson
289 super CmdCatalogPackages
290
291 autoinit(model, catalog, filter, person, person_name, limit, page, count, max)
292
293 redef fun init_command do return super
294
295 redef fun init_results do
296 if results != null then return new CmdSuccess
297 var res = super
298 if not res isa CmdSuccess then return res
299 var person = self.person.as(not null)
300
301 if not catalog.maint2proj.has_key(person) then return res
302 results = catalog.maint2proj[person]
303 return res
304 end
305 end
306
307 # Retrieve the packages contributed by a person
308 class CmdCatalogContributing
309 super CmdCatalogPerson
310 super CmdCatalogPackages
311
312 autoinit(model, catalog, filter, person, person_name, limit, page, count, max)
313
314 # Include maintained packages?
315 #
316 # Default is `false`.
317 var maintaining = false is optional, writable
318
319 # FIXME linearization
320 redef fun init_command do return super
321
322 redef fun init_results do
323 if results != null then return new CmdSuccess
324
325 var res = super
326 if not res isa CmdSuccess then return res
327 var person = self.person.as(not null)
328
329 if not catalog.contrib2proj.has_key(person) then return res
330
331 var maint2proj = null
332 if catalog.maint2proj.has_key(person) then
333 maint2proj = catalog.maint2proj[person]
334 end
335
336 var results = new Array[MPackage]
337 for mpackage in catalog.contrib2proj[person] do
338 if not maintaining and maint2proj != null and maint2proj.has(mpackage) then continue
339 results.add mpackage
340 end
341 self.results = results
342 return res
343 end
344 end