d6539691c71ce50014311a5acc7e071829019597
1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 import commands
::commands_parser
18 import templates
::term_model
20 redef class CommandParser
22 fun execute
(query
: String, no_color
: nullable Bool) do
23 var cmd
= self.parse
(query
)
24 var error
= self.error
26 # Translate links to doc commands
27 if cmd
isa CmdEntityLink then
28 cmd
= new CmdComment(model
, filter
, mentity_name
= query
)
29 var opts
= new HashMap[String, String]
30 var status
= cmd
.parser_init
(query
, opts
)
31 if not status
isa CmdSuccess then error
= status
34 if error
isa CmdError or error
isa CmdWarning then
35 error
.print_message
(no_color
)
38 if cmd
== null then return
43 redef class DocCommand
44 fun execute
(no_color
: nullable Bool) is abstract
47 redef class CmdMessage
48 fun print_message
(no_color
: nullable Bool) do print to_s
52 redef fun print_message
(no_color
) do
53 var res
= "Error: {to_s}"
54 if no_color
== null or not no_color
then res
= res
.bold
.red
59 redef class CmdWarning
60 redef fun print_message
(no_color
) do
61 var res
= "Warning: {to_s}"
62 if no_color
== null or not no_color
then res
= res
.bold
.yellow
69 redef class ErrorMEntityNotFound
70 redef fun print_message
(no_color
) do
71 if no_color
== null or not no_color
then
72 print
"No result found for `{mentity_name.blue}`...".bold
74 print
"No result found for `{mentity_name}`..."
77 if suggestions
.not_empty
then
80 for s
in suggestions
do
81 print s
.cs_list_item
(no_color
)
88 redef class ErrorMEntityConflict
89 redef fun print_message
(no_color
) do
90 if no_color
== null or not no_color
then
91 print
"Multiple results found for `{mentity_name.blue}`...".bold
93 print
"Multiple results found for `{mentity_name}`..."
96 if conflicts
.not_empty
then
100 print s
.cs_list_item
(no_color
)
108 fun print_list
(list_title
: nullable String, list_items
: nullable Array[MEntity], no_color
: nullable Bool) do
109 if list_title
!= null then
110 if no_color
== null or not no_color
then
111 print list_title
.bold
117 if list_items
!= null and list_items
.not_empty
then
118 for mentity
in list_items
do
119 print mentity
.cs_list_item
(no_color
)
128 redef class CmdComment
129 redef fun execute
(no_color
) do
130 var mentity
= self.mentity
131 if mentity
== null then return
133 var full_name
= mentity
.cs_full_name
(no_color
)
134 if no_color
== null or not no_color
then
135 print
"Documentation for `{full_name}`:".bold
137 print
"Documentation for `{full_name}`:"
140 print
" {mentity.cs_icon(no_color)} {mentity.cs_full_name(no_color)}"
141 print
" {mentity.cs_declaration(no_color)}"
142 print
" {mentity.cs_location(no_color)}"
145 if mdoc
== null then return
147 print mdoc
.cs_comment
(no_color
, 3)
149 print
" {mdoc.cs_short_comment(no_color)}\n"
154 redef class CmdAncestors
155 redef fun execute
(no_color
) do
156 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
157 print_list
("Ancestors for `{full_name}`:", results
, no_color
)
161 redef class CmdParents
162 redef fun execute
(no_color
) do
163 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
164 print_list
("Parents for `{full_name}`:", results
, no_color
)
168 redef class CmdChildren
169 redef fun execute
(no_color
) do
170 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
171 print_list
("Children for `{full_name}`:", results
, no_color
)
175 redef class CmdDescendants
176 redef fun execute
(no_color
) do
177 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
178 print_list
("Descendants for `{full_name}`:", results
, no_color
)
182 redef class CmdLinearization
183 redef fun execute
(no_color
) do
184 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
185 print_list
("Linearization for `{full_name}`:", results
, no_color
)
189 redef class CmdSearch
190 redef fun execute
(no_color
) do
191 print_list
("Search results for `{query.as(not null)}`:", results
, no_color
)
195 redef class CmdModelEntities
196 redef fun execute
(no_color
) do
198 if no_color
!= null and not no_color
then kind
= kind
.blue
199 print_list
("MEntities for kind `{kind}`:", results
, no_color
)
203 redef class CmdFeatures
204 redef fun execute
(no_color
) do
205 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
206 print_list
("Features for `{full_name}`:", results
, no_color
)
210 redef class CmdEntityCode
212 redef var format
= "ansi" is optional
214 redef fun execute
(no_color
) do
215 var mentity
= self.mentity
216 if mentity
== null then return
218 var title
= "Code for `{mentity.cs_full_name(no_color)}`:"
219 if no_color
== null or not no_color
then
225 if (no_color
== null or not no_color
) and node
!= null then
226 var ansi
= render_code
(node
)
228 print ansi
.write_to_string
231 printn mentity
.cs_source_code
237 redef fun execute
(no_color
) do
239 var dot
= self.render
240 if dot
== null then return
241 var f
= new ProcessWriter("dot", "-Txlib")
242 f
.write dot
.write_to_string
251 redef fun execute
(no_color
) do
252 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
253 print_list
("Methods calling `{full_name}`:", results
, no_color
)
258 redef fun execute
(no_color
) do
259 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
260 print_list
("Methods intializing `{full_name}`:", results
, no_color
)
264 redef class CmdReturn
265 redef fun execute
(no_color
) do
266 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
267 print_list
("Methods returning `{full_name}`:", results
, no_color
)
272 redef fun execute
(no_color
) do
273 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
274 print_list
("Methods accepting `{full_name}` as parameter:", results
, no_color
)
280 redef class CmdCatalogPackages
281 redef fun execute
(no_color
) do
282 print_list
("Packages from catalog:", results
, no_color
)
286 redef class CmdCatalogStats
287 redef fun execute
(no_color
) do
288 if no_color
== null or not no_color
then
289 print
"Catalog statistics:".bold
291 print
"Catalog statistics:"
294 var stats
= self.stats
.as(not null)
295 print
" * {stats.packages} packages"
296 print
" * {stats.modules} modules"
297 print
" * {stats.methods} methods"
298 print
" * {stats.classes} classes"
299 print
" * {stats.loc} lines of code"
300 print
" * {stats.contributors} contributors"
301 print
" * {stats.maintainers} maintainers"
302 print
" * {stats.tags} tags"
306 redef class CmdCatalogTags
307 redef fun execute
(no_color
) do
308 if no_color
== null or not no_color
then
309 print
"Tags from catalog:".bold
311 print
"Tags from catalog:"
315 var counts
= self.packages_count_by_tags
.as(not null)
316 for tag
, count
in counts
do
317 if no_color
== null or not no_color
then
318 print
" * {tag.blue.bold}: {count} packages"
320 print
" * {tag}: {count} packages"
326 redef class CmdCatalogTag
327 redef fun execute
(no_color
) do
328 var tag
= self.tag
.as(not null)
329 if no_color
== null or not no_color
then tag
= tag
.blue
.bold
330 print_list
("Packages tagged with `{tag}`:", results
, no_color
)
334 redef class CmdCatalogMaintaining
335 redef fun execute
(no_color
) do
336 var name
= self.person_name
.as(not null)
337 if no_color
== null or not no_color
then name
= name
.blue
.bold
338 print_list
("Packages maintained by `{name}`:", results
, no_color
)
342 redef class CmdCatalogContributing
343 redef fun execute
(no_color
) do
344 var name
= self.person_name
.as(not null)
345 if no_color
== null or not no_color
then name
= name
.blue
.bold
346 print_list
("Packages contributed by `{name}`:", results
, no_color
)
354 fun print_ini
(title
: String, data
: nullable String, no_color
: nullable Bool) do
355 if data
== null then return
356 if no_color
== null or not no_color
then
366 redef class CmdIniDescription
367 redef fun execute
(no_color
) do
368 var title
= "Description from ini file:"
369 print_ini
(title
, desc
, no_color
)
373 redef class CmdIniGitUrl
374 redef fun execute
(no_color
) do
375 var title
= "Git URL from ini file:"
376 print_ini
(title
, url
, no_color
)
380 redef class CmdIniCloneCommand
381 redef fun execute
(no_color
) do
382 var title
= "Git clone command from ini file:"
383 print_ini
(title
, command
, no_color
)
387 redef class CmdIniIssuesUrl
388 redef fun execute
(no_color
) do
389 var title
= "Issues URL from ini file:"
390 print_ini
(title
, url
, no_color
)
394 redef class CmdIniMaintainer
395 redef fun execute
(no_color
) do
396 var title
= "Maintainer from ini file:"
397 print_ini
(title
, maintainer
, no_color
)
401 redef class CmdIniContributors
402 redef fun execute
(no_color
) do
403 var contributors
= self.contributors
404 if contributors
== null then return
405 var title
= "Contributors list from ini file:"
406 if no_color
== null or not no_color
then
412 for contributor
in contributors
do
413 print
" * {contributor}"
418 redef class CmdIniLicense
419 redef fun execute
(no_color
) do
420 var title
= "License from ini file:"
421 print_ini
(title
, license
, no_color
)
425 redef class CmdEntityFile
428 fun print_file
(title
: String, no_color
: nullable Bool) do
430 if file
== null then return
431 title
= "{title} `{file}`:"
432 if no_color
== null or not no_color
then
438 print file
.to_path
.read_all
443 redef class CmdLicenseFile
444 redef fun execute
(no_color
) do
445 print_file
("License from", no_color
)
449 redef class CmdContribFile
450 redef fun execute
(no_color
) do
451 print_file
("Contributing rules from", no_color
)
458 redef fun execute
(no_color
) do
459 var mentity
= self.mentity
.as(not null).full_name
460 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
461 print_list
("Mains in `{mentity}`:", results
, no_color
)
465 redef class CmdMainCompile
466 redef fun execute
(no_color
) do
467 var mentity
= self.mentity
.as(not null).full_name
468 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
469 var title
= "Compiling `{mentity}`:"
471 if no_color
== null or not no_color
then
478 var command
= self.command
479 if command
!= null then print command
483 redef class CmdTesting
484 redef fun execute
(no_color
) do
485 var mentity
= self.mentity
.as(not null).full_name
486 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
487 var title
= "Testing `{mentity}`:"
489 if no_color
== null or not no_color
then
496 var command
= self.command
497 if command
!= null then print command
501 redef class CmdManSynopsis
502 redef fun execute
(no_color
) do
503 var mentity
= self.mentity
.as(not null).full_name
504 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
505 var title
= "Synopsis for `{mentity}`:"
507 if no_color
== null or not no_color
then
514 var synopsis
= self.synopsis
515 if synopsis
!= null then print synopsis
519 redef class CmdManOptions
520 redef fun execute
(no_color
) do
521 var mentity
= self.mentity
.as(not null).full_name
522 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
523 var title
= "Options for `{mentity}`:"
525 if no_color
== null or not no_color
then
532 var options
= self.options
.as(not null)
533 for opt
, desc
in options
do
534 if no_color
== null or not no_color
then
535 print
" * {opt.blue.bold}: {desc}"
537 print
" * {opt}: {desc}"