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.
15 # Helpful features about packages
19 import doc
::commands
::commands_main
21 redef class ToolContext
24 var nitpackage_phase
: Phase = new NitPackagePhase(self, null)
27 var opt_expand
= new OptionBool("Move singleton packages to their own directory", "--expand")
30 var opt_check_ini
= new OptionBool("Check package.ini files", "--check-ini")
33 var opt_gen_ini
= new OptionBool("Generate package.ini files", "--gen-ini")
36 var opt_force
= new OptionBool("Force update of existing files", "-f", "--force")
39 var opt_check_makefile
= new OptionBool("Check Makefile files", "--check-makefile")
42 var opt_gen_makefile
= new OptionBool("Generate Makefile files", "--gen-makefile")
45 var opt_check_man
= new OptionBool("Check manpages files", "--check-man")
48 var opt_gen_man
= new OptionBool("Generate manpages files", "--gen-man")
51 var opt_check_readme
= new OptionBool("Check README.md files", "--check-readme")
55 option_context
.add_option
(opt_expand
, opt_force
)
56 option_context
.add_option
(opt_check_ini
, opt_gen_ini
)
57 option_context
.add_option
(opt_check_makefile
, opt_gen_makefile
)
58 option_context
.add_option
(opt_check_man
, opt_gen_man
)
59 option_context
.add_option
(opt_check_readme
)
63 private class NitPackagePhase
66 redef fun process_mainmodule
(mainmodule
, mmodules
) do
67 var mpackages
= extract_mpackages
(mmodules
)
68 for mpackage
in mpackages
do
70 # Fictive and buggy packages are ignored
71 if not mpackage
.has_source
then
72 toolcontext
.warning
(mpackage
.location
, "no-source",
73 "Warning: `{mpackage}` has no source file")
77 # Check package INI files
78 if toolcontext
.opt_check_ini
.value
then
79 mpackage
.check_ini
(toolcontext
)
83 # Check package Makefiles
84 if toolcontext
.opt_check_makefile
.value
then
85 mpackage
.check_makefile
(toolcontext
, mainmodule
)
90 if toolcontext
.opt_check_man
.value
then
91 mpackage
.check_man
(toolcontext
, mainmodule
)
96 if toolcontext
.opt_check_readme
.value
then
97 mpackage
.check_readme
(toolcontext
)
102 if toolcontext
.opt_expand
.value
and not mpackage
.is_expanded
then
103 var path
= mpackage
.expand
104 toolcontext
.info
("{mpackage} moved to {path}", 0)
106 if not mpackage
.is_expanded
then
107 toolcontext
.warning
(mpackage
.location
, "no-dir",
108 "Warning: `{mpackage}` has no package directory")
113 if toolcontext
.opt_gen_ini
.value
then
114 if not mpackage
.has_ini
or toolcontext
.opt_force
.value
then
115 var path
= mpackage
.gen_ini
116 toolcontext
.info
("generated INI file `{path}`", 0)
121 if toolcontext
.opt_gen_makefile
.value
then
122 if not mpackage
.has_makefile
or toolcontext
.opt_force
.value
then
123 var path
= mpackage
.gen_makefile
(toolcontext
.modelbuilder
.model
, mainmodule
)
125 toolcontext
.info
("generated Makefile `{path}`", 0)
131 if toolcontext
.opt_gen_man
.value
then
132 mpackage
.gen_man
(toolcontext
, mainmodule
)
137 # Extract the list of packages from the mmodules passed as arguments
138 fun extract_mpackages
(mmodules
: Collection[MModule]): Collection[MPackage] do
139 var mpackages
= new ArraySet[MPackage]
140 for mmodule
in mmodules
do
141 var mpackage
= mmodule
.mpackage
142 if mpackage
== null then continue
143 mpackages
.add mpackage
145 return mpackages
.to_a
151 # Expand `self` in its own directory
152 private fun expand
: String do
153 assert not is_expanded
155 var ori_path
= package_path
.as(not null)
156 var new_path
= ori_path
.dirname
/ name
159 sys
.system
"mv {ori_path} {new_path / name}.nit"
161 var ini_file
= "{new_path}.ini"
162 if ini_file
.file_exists
then
163 sys
.system
"mv {new_path}.ini {new_path}/package.ini"
169 private var maintainer
: nullable String is lazy
do
170 return git_exec
("git shortlog -esn . | head -n 1 | sed 's/\\s*[0-9]*\\s*//'")
173 private var contributors
: Array[String] is lazy
do
174 var contribs
= git_exec
("git shortlog -esn . | head -n -1 | " +
175 "sed 's/\\s*[0-9]*\\s*//'")
176 if contribs
== null then return new Array[String]
177 return contribs
.split
("\n")
180 private var git_url
: nullable String is lazy
do
181 var git
= git_exec
("git remote get-url origin")
182 if git
== null then return null
183 git
= git
.replace
("git@github.com:", "https://github.com/")
184 git
= git
.replace
("git@gitlab.com:", "https://gitlab.com/")
188 private var git_dir
: nullable String is lazy
do
189 return git_exec
("git rev-parse --show-prefix")
192 private var browse_url
: nullable String is lazy
do
194 if git
== null then return null
195 var browse
= git
.replace
(".git", "")
197 if dir
== null or dir
.is_empty
then return browse
198 return "{browse}/tree/master/{dir}"
201 private var homepage_url
: nullable String is lazy
do
203 if git
== null then return null
204 # Special case for nit files
205 if git
.has_suffix
("/nit.git") then
206 return "http://nitlanguage.org"
208 return git
.replace
(".git", "")
211 private var issues_url
: nullable String is lazy
do
213 if git
== null then return null
214 return "{git.replace(".git", "")}/issues"
217 private var license
: nullable String is lazy
do
219 if git
== null then return null
220 # Special case for nit files
221 if git
.has_suffix
("/nit.git") then
227 private fun git_exec
(cmd
: String): nullable String do
228 var path
= package_path
229 if path
== null then return null
230 if not is_expanded
then path
= path
.dirname
231 with pr
= new ProcessReader("sh", "-c", "cd {path} && {cmd}") do
232 return pr
.read_all
.trim
236 private var allowed_ini_keys
= [
237 "package.name", "package.desc", "package.tags", "package.license",
238 "package.maintainer", "package.more_contributors",
239 "upstream.browse", "upstream.git", "upstream.git.directory",
240 "upstream.homepage", "upstream.issues", "upstream.apk", "upstream.tryit",
244 private fun check_ini
(toolcontext
: ToolContext) do
246 toolcontext
.error
(location
, "No `package.ini` file for `{name}`")
250 var pkg_path
= package_path
251 if pkg_path
== null then return
253 var ini_path
= ini_path
254 if ini_path
== null then return
256 var ini
= new ConfigTree(ini_path
)
258 ini
.check_key
(toolcontext
, self, "package.name", name
)
259 ini
.check_key
(toolcontext
, self, "package.desc")
260 ini
.check_key
(toolcontext
, self, "package.tags")
262 # FIXME since `git reflog --follow` seems bugged
263 ini
.check_key
(toolcontext
, self, "package.maintainer")
264 # var maint = mpackage.maintainer
265 # if maint != null then
266 # ini.check_key(toolcontext, self, "package.maintainer", maint)
269 # FIXME since `git reflog --follow` seems bugged
270 # var contribs = mpackage.contributors
271 # if contribs.not_empty then
272 # ini.check_key(toolcontext, self, "package.more_contributors", contribs.join(", "))
275 ini
.check_key
(toolcontext
, self, "package.license", license
)
276 ini
.check_key
(toolcontext
, self, "upstream.browse", browse_url
)
277 ini
.check_key
(toolcontext
, self, "upstream.git", git_url
)
278 ini
.check_key
(toolcontext
, self, "upstream.git.directory", git_dir
)
279 ini
.check_key
(toolcontext
, self, "upstream.homepage", homepage_url
)
280 ini
.check_key
(toolcontext
, self, "upstream.issues", issues_url
)
282 for key
in ini
.to_map
.keys
do
283 if not allowed_ini_keys
.has
(key
) then
284 toolcontext
.warning
(location
, "unknown-ini-key",
285 "Warning: ignoring unknown `{key}` key in `{ini.ini_file}`")
290 private fun gen_ini
: String do
291 var ini_path
= self.ini_path
.as(not null)
292 var ini
= new ConfigTree(ini_path
)
294 ini
.update_value
("package.name", name
)
295 ini
.update_value
("package.desc", "")
296 ini
.update_value
("package.tags", "")
297 ini
.update_value
("package.maintainer", maintainer
)
298 ini
.update_value
("package.more_contributors", contributors
.join
(","))
299 ini
.update_value
("package.license", license
or else "")
301 ini
.update_value
("upstream.browse", browse_url
)
302 ini
.update_value
("upstream.git", git_url
)
303 ini
.update_value
("upstream.git.directory", git_dir
)
304 ini
.update_value
("upstream.homepage", homepage_url
)
305 ini
.update_value
("upstream.issues", issues_url
)
313 # The path to `self` Makefile
314 fun makefile_path
: nullable String do
315 var path
= package_path
316 if path
== null then return null
317 if not is_expanded
then return null
318 return path
/ "Makefile"
321 # Does `self` have a Makefile?
322 fun has_makefile
: Bool do
323 var makefile_path
= self.makefile_path
324 if makefile_path
== null then return false
325 return makefile_path
.file_exists
328 private fun check_makefile
(toolcontext
: ToolContext, mainmodule
: MModule) do
329 var model
= toolcontext
.modelbuilder
.model
330 var filter
= new ModelFilter(accept_example
= false, accept_test
= false)
332 var cmd_bin
= new CmdMains(model
, filter
, mentity
= self)
333 var res_bin
= cmd_bin
.init_command
334 if not res_bin
isa CmdSuccess then return
336 for mmodule
in cmd_bin
.results
.as(not null) do
337 if not mmodule
isa MModule then continue
339 if mmodule
.makefile_path
== null then
340 toolcontext
.warning
(location
, "missing-makefile",
341 "Warning: no Makefile for executable module `{mmodule.full_name}`")
346 private fun gen_makefile
(model
: Model, mainmodule
: MModule): nullable String do
347 var filter
= new ModelFilter(accept_example
= false, accept_test
= false)
349 var pkg_path
= package_path
.as(not null)
350 var makefile_path
= makefile_path
.as(not null)
352 var bins
= new Array[String]
353 var cmd_bin
= new CmdMains(model
, filter
, mentity
= self)
354 var res_bin
= cmd_bin
.init_command
355 if res_bin
isa CmdSuccess then
356 for mmodule
in cmd_bin
.results
.as(not null) do
357 if not mmodule
isa MModule then continue
358 var mmodule_makefile
= mmodule
.makefile_path
359 if mmodule_makefile
!= null and mmodule_makefile
!= makefile_path
then continue
361 var file
= mmodule
.location
.file
362 if file
== null then continue
363 # Remove package path prefix
364 var bin_path
= file
.filename
365 if pkg_path
.has_suffix
("/") then
366 bin_path
= bin_path
.replace
(pkg_path
, "")
368 bin_path
= bin_path
.replace
("{pkg_path}/", "")
374 if bins
.is_empty
then return null
376 var make
= new NitMakefile(bins
)
377 make
.render
.write_to_file
(makefile_path
)
383 # The path to `self` manpage files
384 private fun man_path
: nullable String do
385 var path
= package_path
386 if path
== null then return null
387 if not is_expanded
then return null
391 # Does `self` have a manpage files?
392 private fun has_man
: Bool do
393 var man_path
= self.man_path
394 if man_path
== null then return false
395 return man_path
.file_exists
398 private fun check_man
(toolcontext
: ToolContext, mainmodule
: MModule) do
399 var model
= toolcontext
.modelbuilder
.model
400 var filter
= new ModelFilter(accept_example
= false, accept_test
= false)
401 var cmd
= new CmdMains(model
, filter
, mentity
= self)
402 var res
= cmd
.init_command
403 if not res
isa CmdSuccess then return
405 for mmodule
in cmd
.results
.as(not null) do
406 if not mmodule
isa MModule then continue
407 mmodule
.check_man
(toolcontext
)
411 private fun gen_man
(toolcontext
: ToolContext, mainmodule
: MModule) do
412 var model
= toolcontext
.modelbuilder
.model
413 var filter
= new ModelFilter(accept_example
= false, accept_test
= false)
414 var cmd
= new CmdMains(model
, filter
, mentity
= self)
415 var res
= cmd
.init_command
416 if not res
isa CmdSuccess then return
418 var pkg_man
= man_path
.as(not null)
419 for mmodule
in cmd
.results
.as(not null) do
420 if not mmodule
isa MModule then continue
421 if not has_man
then pkg_man
.mkdir
422 mmodule
.gen_man
(toolcontext
)
428 private fun check_readme
(toolcontext
: ToolContext) do
429 if not has_readme
then
430 toolcontext
.error
(location
, "No `README.md` file for `{name}`")
437 private fun makefile_path
: nullable String do
438 var file
= location
.file
439 if file
== null then return null
441 var dir
= file
.filename
.dirname
442 var makefile
= (dir
/ "Makefile")
443 if not makefile
.file_exists
then return null
445 for line
in makefile
.to_path
.read_lines
do
446 if line
.has_prefix
("{name}:") then return makefile
451 private fun man_path
: nullable String do
452 var mpackage
= self.mpackage
453 if mpackage
== null then return null
454 var path
= mpackage
.man_path
455 if path
== null then return null
456 return path
/ "{name}.man"
459 # Does `self` have a manpage?
460 private fun has_man
: Bool do
461 var man_path
= self.man_path
462 if man_path
== null then return false
463 return man_path
.file_exists
466 private fun make_module
(toolcontext
: ToolContext): Bool do
467 var mpackage
= self.mpackage
468 if mpackage
== null then return false
469 if not mpackage
.is_expanded
then return false
471 var pkg_path
= mpackage
.package_path
472 if pkg_path
== null then return false
474 var pr
= new ProcessReader("sh", "-c", "cd {pkg_path} && make -Bs bin/{name}")
475 var out
= pr
.read_all
.trim
478 if pr
.status
> 0 then
479 toolcontext
.error
(location
, "unable to compile `{name}`")
486 private fun stub_man
(toolcontext
: ToolContext): nullable String do
487 if not make_module
(toolcontext
) then return null
488 var mpackage
= self.mpackage
489 if mpackage
== null then return null
490 if not mpackage
.is_expanded
then return null
492 var pkg_path
= mpackage
.package_path
493 if pkg_path
== null then return null
495 var pr
= new ProcessReader("{pkg_path}/bin/{name}", "--stub-man")
496 var man
= pr
.read_all
.trim
499 if pr
.status
> 0 then
500 toolcontext
.error
(location
, "unable to run `{pkg_path}/bin/{name} --stub-man`")
507 private fun check_man
(toolcontext
: ToolContext) do
509 toolcontext
.error
(location
, "No manpage for bin {full_name}")
512 var man_path
= self.man_path
.as(not null)
513 var man
= stub_man
(toolcontext
)
514 if man
== null or man
.is_empty
then return
516 var old_man
= new ManPage.from_file
(self, man_path
)
517 var new_man
= new ManPage.from_string
(self, man
)
518 old_man
.diff
(toolcontext
, new_man
)
521 private fun gen_man
(toolcontext
: ToolContext) do
522 var man
= stub_man
(toolcontext
)
523 if man
== null or man
.is_empty
then return
524 var man_path
= self.man_path
525 if man_path
== null then return
526 man
.write_to_file
(man_path
)
527 toolcontext
.info
("created manpage `{man_path}`", 0)
531 redef class ConfigTree
532 private fun check_key
(toolcontext
: ToolContext, mpackage
: MPackage, key
: String, value
: nullable String) do
533 if not has_key
(key
) then
534 toolcontext
.warning
(mpackage
.location
, "missing-ini-key",
535 "Warning: missing `{key}` key in `{ini_file}`")
538 if self[key
].as(not null).is_empty
then
539 toolcontext
.warning
(mpackage
.location
, "missing-ini-value",
540 "Warning: empty `{key}` key in `{ini_file}`")
543 if value
!= null and self[key
] != value
then
544 toolcontext
.warning
(mpackage
.location
, "wrong-ini-value",
545 "Warning: wrong value for `{key}` in `{ini_file}`. " +
546 "Expected `{value}`, got `{self[key] or else ""}`")
550 private fun update_value
(key
: String, value
: nullable String) do
551 if value
== null then return
552 if not has_key
(key
) then
555 var old_value
= self[key
]
556 if not value
.is_empty
and old_value
!= value
then
563 # A Makefile for the Nit project
566 # Nit files to compile
567 var nit_files
: Array[String]
569 # List of rules to add in the Makefile
570 fun rules
: Array[MakeRule] do
571 var rules
= new Array[MakeRule]
573 var rule_all
= new MakeRule("all", is_phony
= true)
576 for file
in nit_files
do
577 var bin
= file
.basename
.strip_extension
579 rule_all
.deps
.add
"bin/{bin}"
581 var rule
= new MakeRule("bin/{bin}")
582 rule
.deps
.add
"$(shell $(NITLS) -M {file})"
583 rule
.lines
.add
"mkdir -p bin/"
584 rule
.lines
.add
"$(NITC) {file} -o bin/{bin}"
588 var rule_check
= new MakeRule("check", is_phony
= true)
589 rule_check
.lines
.add
"$(NITUNIT) ."
592 var rule_doc
= new MakeRule("doc", is_phony
= true)
593 rule_doc
.lines
.add
"$(NITDOC) . -o doc/"
596 var rule_clean
= new MakeRule("clean", is_phony
= true)
597 if nit_files
.not_empty
then
598 rule_clean
.lines
.add
"rm -rf bin/"
600 rule_clean
.lines
.add
"rm -rf doc/"
607 fun render
: Writable do
608 var tpl
= new Template
610 # This file is part of NIT ( http://www.nitlanguage.org ).
612 # Licensed under the Apache License, Version 2.0 (the "License");
613 # you may not use this file except in compliance with the License.
614 # You may obtain a copy of the License at
616 # http://www.apache.org/licenses/LICENSE-2.0
618 # Unless required by applicable law or agreed to in writing, software
619 # distributed under the License is distributed on an "AS IS" BASIS,
620 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
621 # See the License for the specific language governing permissions and
622 # limitations under the License.\n"""
624 if nit_files
.not_empty
then
625 tpl
.addn
"NITC ?= nitc"
626 tpl
.addn
"NITLS ?= nitls"
628 tpl
.addn
"NITUNIT ?= nitunit"
629 tpl
.addn
"NITDOC ?= nitdoc"
632 tpl
.add
"\n{rule.render.write_to_string}"
639 # A rule that goes into a Makefile
645 # Is this rule a `.PHONY` one?
646 var is_phony
: Bool = false is optional
649 var deps
= new Array[String]
652 var lines
= new Array[String]
655 fun render
: Writable do
656 var tpl
= new Template
658 tpl
.addn
".PHONY: {name}"
661 if deps
.not_empty
then
662 tpl
.add
" {deps.join(" ")}"
672 private class ManPage
674 var name
: nullable String is noinit
675 var synopsis
: nullable String is noinit
676 var options
= new HashMap[Array[String], String]
678 init from_file
(mmodule
: MModule, file
: String) do
679 from_lines
(mmodule
, file
.to_path
.read_lines
)
682 init from_string
(mmodule
: MModule, string
: String) do
683 from_lines
(mmodule
, string
.split
("\n"))
686 init from_lines
(mmodule
: MModule, lines
: Array[String]) do
690 for i
in [0..lines
.length
[ do
692 if line
.is_empty
then continue
694 if line
== "# NAME" then
698 if line
== "# SYNOPSIS" then
702 if line
== "# OPTIONS" then
707 if section
== "name" and name
== null then
710 if section
== "synopsis" and synopsis
== null then
713 if section
== "options" and line
.has_prefix
("###") then
714 var opts
= new Array[String]
715 for opt
in line
.substring
(3, line
.length
).trim
.replace
("`", "").split
(",") do
719 if i
< lines
.length
- 1 then
720 desc
= lines
[i
+ 1].trim
727 fun diff
(toolcontext
: ToolContext, ref
: ManPage) do
728 if name
!= ref
.name
then
729 toolcontext
.warning
(mmodule
.location
, "diff-man",
730 "Warning: outdated man description. " +
731 "Expected `{ref.name or else ""}` got `{name or else ""}`.")
733 if synopsis
!= ref
.synopsis
then
734 toolcontext
.warning
(mmodule
.location
, "diff-man",
735 "Warning: outdated man synopsis. " +
736 "Expected `{ref.synopsis or else ""}` got `{synopsis or else ""}`.")
738 for name
, desc
in options
do
739 if not ref
.options
.has_key
(name
) then
740 toolcontext
.warning
(mmodule
.location
, "diff-man",
741 "Warning: unknown man option `{name}`.`")
744 var ref_desc
= ref
.options
[name
]
745 if desc
!= ref_desc
then
746 toolcontext
.warning
(mmodule
.location
, "diff-man",
747 "Warning: outdated man option description. Expected `{ref_desc}` got `{desc}`.")
750 for ref_name
, ref_desc
in ref
.options
do
751 if not options
.has_key
(ref_name
) then
752 toolcontext
.warning
(mmodule
.location
, "diff-man",
753 "Warning: missing man option `{ref_name}`.`")
759 var tpl
= new Template
761 tpl
.addn name
or else ""
762 tpl
.addn
"# SYNOPSIS"
763 tpl
.addn synopsis
or else ""
765 for name
, desc
in options
do
766 tpl
.addn
" * {name}: {desc}"
768 return tpl
.write_to_string
773 var toolcontext
= new ToolContext
774 var tpl
= new Template
775 tpl
.add
"Usage: nitpackage [OPTION]... <file.nit>...\n"
776 tpl
.add
"Helpful features about packages."
777 toolcontext
.tooldescription
= tpl
.write_to_string
780 toolcontext
.process_options
(args
)
781 var arguments
= toolcontext
.option_context
.rest
784 var model
= new Model
785 var mbuilder
= new ModelBuilder(model
, toolcontext
)
786 var mmodules
= mbuilder
.parse_full
(arguments
)
789 if mmodules
.is_empty
then return
791 toolcontext
.run_global_phases
(mmodules
)