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
19 import templates
::md_commands
21 redef class CommandParser
23 fun execute
(query
: String, no_color
: nullable Bool) do
24 var cmd
= self.parse
(query
)
25 var error
= self.error
27 # Translate links to doc commands
28 if cmd
isa CmdEntityLink then
29 cmd
= new CmdComment(model
, mentity_name
= query
)
30 var opts
= new CmdOptions
31 var status
= cmd
.parser_init
(query
, opts
)
32 if not status
isa CmdSuccess then error
= status
35 if error
isa CmdError or error
isa CmdWarning then
36 error
.print_message
(no_color
)
39 if cmd
== null then return
44 redef class DocCommand
45 fun execute
(no_color
: nullable Bool) is abstract
48 redef class CmdMessage
49 fun print_message
(no_color
: nullable Bool) do print to_s
53 redef fun print_message
(no_color
) do
54 var res
= "Error: {to_s}"
55 if no_color
== null or not no_color
then res
= res
.bold
.red
60 redef class CmdWarning
61 redef fun print_message
(no_color
) do
62 var res
= "Warning: {to_s}"
63 if no_color
== null or not no_color
then res
= res
.bold
.yellow
70 redef class ErrorMEntityNotFound
71 redef fun print_message
(no_color
) do
72 if no_color
== null or not no_color
then
73 print
"No result found for `{mentity_name.blue}`...".bold
75 print
"No result found for `{mentity_name}`..."
78 if suggestions
.not_empty
then
81 for s
in suggestions
do
82 print s
.cs_list_item
(no_color
)
89 redef class ErrorMEntityConflict
90 redef fun print_message
(no_color
) do
91 if no_color
== null or not no_color
then
92 print
"Multiple results found for `{mentity_name.blue}`...".bold
94 print
"Multiple results found for `{mentity_name}`..."
97 if conflicts
.not_empty
then
100 for s
in conflicts
do
101 print s
.cs_list_item
(no_color
)
109 fun print_list
(list_title
: nullable String, list_items
: nullable Array[MEntity], no_color
: nullable Bool) do
110 if list_title
!= null then
111 if no_color
== null or not no_color
then
112 print list_title
.bold
118 if list_items
!= null and list_items
.not_empty
then
119 for mentity
in list_items
do
120 print mentity
.cs_list_item
(no_color
)
129 redef class CmdComment
130 redef fun execute
(no_color
) do
131 var mentity
= self.mentity
132 if mentity
== null then return
134 var full_name
= mentity
.cs_full_name
(no_color
)
135 if no_color
== null or not no_color
then
136 print
"Documentation for `{full_name}`:".bold
138 print
"Documentation for `{full_name}`:"
141 print
" {mentity.cs_icon(no_color)} {mentity.cs_full_name(no_color)}"
142 print
" {mentity.cs_declaration(no_color)}"
143 print
" {mentity.cs_location(no_color)}"
146 if mdoc
== null then return
148 print mdoc
.cs_comment
(no_color
, 3)
150 print
" {mdoc.cs_short_comment(no_color)}\n"
155 redef class CmdAncestors
156 redef fun execute
(no_color
) do
157 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
158 print_list
("Ancestors for `{full_name}`:", results
, no_color
)
162 redef class CmdParents
163 redef fun execute
(no_color
) do
164 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
165 print_list
("Parents for `{full_name}`:", results
, no_color
)
169 redef class CmdChildren
170 redef fun execute
(no_color
) do
171 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
172 print_list
("Children for `{full_name}`:", results
, no_color
)
176 redef class CmdDescendants
177 redef fun execute
(no_color
) do
178 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
179 print_list
("Descendants for `{full_name}`:", results
, no_color
)
183 redef class CmdLinearization
184 redef fun execute
(no_color
) do
185 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
186 print_list
("Linearization for `{full_name}`:", results
, no_color
)
190 redef class CmdSearch
191 redef fun execute
(no_color
) do
192 print_list
("Search results for `{query.as(not null)}`:", results
, no_color
)
196 redef class CmdModelEntities
197 redef fun execute
(no_color
) do
199 if no_color
!= null and not no_color
then kind
= kind
.blue
200 print_list
("MEntities for kind `{kind}`:", results
, no_color
)
204 redef class CmdFeatures
205 redef fun execute
(no_color
) do
206 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
207 print_list
("Features for `{full_name}`:", results
, no_color
)
211 redef class CmdEntityCode
213 redef var format
= "ansi" is optional
215 redef fun execute
(no_color
) do
216 var mentity
= self.mentity
217 if mentity
== null then return
219 var title
= "Code for `{mentity.cs_full_name(no_color)}`:"
220 if no_color
== null or not no_color
then
226 if (no_color
== null or not no_color
) and node
!= null then
227 var ansi
= render_code
(node
)
229 print ansi
.write_to_string
232 printn mentity
.cs_source_code
238 redef fun execute
(no_color
) do
240 var dot
= self.render
241 if dot
== null then return
242 var f
= new ProcessWriter("dot", "-Txlib")
243 f
.write dot
.write_to_string
252 redef fun execute
(no_color
) do
253 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
254 print_list
("Methods calling `{full_name}`:", results
, no_color
)
259 redef fun execute
(no_color
) do
260 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
261 print_list
("Methods intializing `{full_name}`:", results
, no_color
)
265 redef class CmdReturn
266 redef fun execute
(no_color
) do
267 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
268 print_list
("Methods returning `{full_name}`:", results
, no_color
)
273 redef fun execute
(no_color
) do
274 var full_name
= mentity
.as(not null).cs_full_name
(no_color
)
275 print_list
("Methods accepting `{full_name}` as parameter:", results
, no_color
)
281 redef class CmdCatalogPackages
282 redef fun execute
(no_color
) do
283 print_list
("Packages from catalog:", results
, no_color
)
287 redef class CmdCatalogStats
288 redef fun execute
(no_color
) do
289 if no_color
== null or not no_color
then
290 print
"Catalog statistics:".bold
292 print
"Catalog statistics:"
295 var stats
= self.stats
.as(not null)
296 print
" * {stats.packages} packages"
297 print
" * {stats.modules} modules"
298 print
" * {stats.methods} methods"
299 print
" * {stats.classes} classes"
300 print
" * {stats.loc} lines of code"
301 print
" * {stats.contributors} contributors"
302 print
" * {stats.maintainers} maintainers"
303 print
" * {stats.tags} tags"
307 redef class CmdCatalogTags
308 redef fun execute
(no_color
) do
309 if no_color
== null or not no_color
then
310 print
"Tags from catalog:".bold
312 print
"Tags from catalog:"
316 var counts
= self.packages_count_by_tags
.as(not null)
317 for tag
, count
in counts
do
318 if no_color
== null or not no_color
then
319 print
" * {tag.blue.bold}: {count} packages"
321 print
" * {tag}: {count} packages"
327 redef class CmdCatalogTag
328 redef fun execute
(no_color
) do
329 var tag
= self.tag
.as(not null)
330 if no_color
== null or not no_color
then tag
= tag
.blue
.bold
331 print_list
("Packages tagged with `{tag}`:", results
, no_color
)
335 redef class CmdCatalogMaintaining
336 redef fun execute
(no_color
) do
337 var name
= self.person_name
.as(not null)
338 if no_color
== null or not no_color
then name
= name
.blue
.bold
339 print_list
("Packages maintained by `{name}`:", results
, no_color
)
343 redef class CmdCatalogContributing
344 redef fun execute
(no_color
) do
345 var name
= self.person_name
.as(not null)
346 if no_color
== null or not no_color
then name
= name
.blue
.bold
347 print_list
("Packages contributed by `{name}`:", results
, no_color
)
355 fun print_ini
(title
: String, data
: nullable String, no_color
: nullable Bool) do
356 if data
== null then return
357 if no_color
== null or not no_color
then
367 redef class CmdIniDescription
368 redef fun execute
(no_color
) do
369 var title
= "Description from ini file:"
370 print_ini
(title
, desc
, no_color
)
374 redef class CmdIniGitUrl
375 redef fun execute
(no_color
) do
376 var title
= "Git URL from ini file:"
377 print_ini
(title
, url
, no_color
)
381 redef class CmdIniCloneCommand
382 redef fun execute
(no_color
) do
383 var title
= "Git clone command from ini file:"
384 print_ini
(title
, command
, no_color
)
388 redef class CmdIniIssuesUrl
389 redef fun execute
(no_color
) do
390 var title
= "Issues URL from ini file:"
391 print_ini
(title
, url
, no_color
)
395 redef class CmdIniMaintainer
396 redef fun execute
(no_color
) do
397 var title
= "Maintainer from ini file:"
398 print_ini
(title
, maintainer
, no_color
)
402 redef class CmdIniContributors
403 redef fun execute
(no_color
) do
404 var contributors
= self.contributors
405 if contributors
== null then return
406 var title
= "Contributors list from ini file:"
407 if no_color
== null or not no_color
then
413 for contributor
in contributors
do
414 print
" * {contributor}"
419 redef class CmdIniLicense
420 redef fun execute
(no_color
) do
421 var title
= "License from ini file:"
422 print_ini
(title
, license
, no_color
)
426 redef class CmdEntityFile
429 fun print_file
(title
: String, no_color
: nullable Bool) do
431 if file
== null then return
432 title
= "{title} `{file}`:"
433 if no_color
== null or not no_color
then
439 print file
.to_path
.read_all
444 redef class CmdLicenseFile
445 redef fun execute
(no_color
) do
446 print_file
("License from", no_color
)
450 redef class CmdContribFile
451 redef fun execute
(no_color
) do
452 print_file
("Contributing rules from", no_color
)
459 redef fun execute
(no_color
) do
460 var mentity
= self.mentity
.as(not null).full_name
461 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
462 print_list
("Mains in `{mentity}`:", results
, no_color
)
466 redef class CmdMainCompile
467 redef fun execute
(no_color
) do
468 var mentity
= self.mentity
.as(not null).full_name
469 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
470 var title
= "Compiling `{mentity}`:"
472 if no_color
== null or not no_color
then
479 var command
= self.command
480 if command
!= null then print command
484 redef class CmdTesting
485 redef fun execute
(no_color
) do
486 var mentity
= self.mentity
.as(not null).full_name
487 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
488 var title
= "Testing `{mentity}`:"
490 if no_color
== null or not no_color
then
497 var command
= self.command
498 if command
!= null then print command
502 redef class CmdManSynopsis
503 redef fun execute
(no_color
) do
504 var mentity
= self.mentity
.as(not null).full_name
505 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
506 var title
= "Synopsis for `{mentity}`:"
508 if no_color
== null or not no_color
then
515 var synopsis
= self.synopsis
516 if synopsis
!= null then print synopsis
520 redef class CmdManOptions
521 redef fun execute
(no_color
) do
522 var mentity
= self.mentity
.as(not null).full_name
523 if no_color
== null or not no_color
then mentity
= mentity
.blue
.bold
524 var title
= "Options for `{mentity}`:"
526 if no_color
== null or not no_color
then
533 var options
= self.options
.as(not null)
534 for opt
, desc
in options
do
535 if no_color
== null or not no_color
then
536 print
" * {opt.blue.bold}: {desc}"
538 print
" * {opt}: {desc}"