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
23 var opt_expand
= new OptionBool("Move singleton packages to their own directory", "--expand")
26 var opt_check_ini
= new OptionBool("Check package.ini files", "--check-ini")
29 var opt_gen_ini
= new OptionBool("Generate package.ini files", "--gen-ini")
32 var opt_force
= new OptionBool("Force update of existing files", "-f", "--force")
35 var opt_check_makefile
= new OptionBool("Check Makefile files", "--check-makefile")
38 var opt_gen_makefile
= new OptionBool("Generate Makefile files", "--gen-makefile")
41 var nitpackage_phase
: Phase = new NitPackagePhase(self, null)
44 var opt_check_man
= new OptionBool("Check manpages files", "--check-man")
47 var opt_gen_man
= new OptionBool("Generate manpages files", "--gen-man")
51 option_context
.add_option
(opt_expand
, opt_force
)
52 option_context
.add_option
(opt_check_ini
, opt_gen_ini
)
53 option_context
.add_option
(opt_check_makefile
, opt_gen_makefile
)
54 option_context
.add_option
(opt_check_man
, opt_gen_man
)
58 private class NitPackagePhase
61 redef fun process_mainmodule
(mainmodule
, mmodules
) do
62 var mpackages
= extract_mpackages
(mmodules
)
63 for mpackage
in mpackages
do
65 # Fictive and buggy packages are ignored
66 if not mpackage
.has_source
then
67 toolcontext
.warning
(mpackage
.location
, "no-source",
68 "Warning: `{mpackage}` has no source file")
72 # Check package INI files
73 if toolcontext
.opt_check_ini
.value
then
74 mpackage
.check_ini
(toolcontext
)
78 # Check package Makefiles
79 if toolcontext
.opt_check_makefile
.value
then
80 mpackage
.check_makefile
(toolcontext
, mainmodule
)
85 if toolcontext
.opt_check_man
.value
then
86 mpackage
.check_man
(toolcontext
, mainmodule
)
91 if toolcontext
.opt_expand
.value
and not mpackage
.is_expanded
then
92 var path
= mpackage
.expand
93 toolcontext
.info
("{mpackage} moved to {path}", 0)
95 if not mpackage
.is_expanded
then
96 toolcontext
.warning
(mpackage
.location
, "no-dir",
97 "Warning: `{mpackage}` has no package directory")
102 if toolcontext
.opt_gen_ini
.value
then
103 if not mpackage
.has_ini
or toolcontext
.opt_force
.value
then
104 var path
= mpackage
.gen_ini
105 toolcontext
.info
("generated INI file `{path}`", 0)
110 if toolcontext
.opt_gen_makefile
.value
then
111 if not mpackage
.has_makefile
or toolcontext
.opt_force
.value
then
112 var path
= mpackage
.gen_makefile
(toolcontext
.modelbuilder
.model
, mainmodule
)
114 toolcontext
.info
("generated Makefile `{path}`", 0)
120 if toolcontext
.opt_gen_man
.value
then
121 mpackage
.gen_man
(toolcontext
, mainmodule
)
126 # Extract the list of packages from the mmodules passed as arguments
127 fun extract_mpackages
(mmodules
: Collection[MModule]): Collection[MPackage] do
128 var mpackages
= new ArraySet[MPackage]
129 for mmodule
in mmodules
do
130 var mpackage
= mmodule
.mpackage
131 if mpackage
== null then continue
132 mpackages
.add mpackage
134 return mpackages
.to_a
140 # Expand `self` in its own directory
141 private fun expand
: String do
142 assert not is_expanded
144 var ori_path
= package_path
.as(not null)
145 var new_path
= ori_path
.dirname
/ name
148 sys
.system
"mv {ori_path} {new_path / name}.nit"
150 var ini_file
= "{new_path}.ini"
151 if ini_file
.file_exists
then
152 sys
.system
"mv {new_path}.ini {new_path}/package.ini"
158 private var maintainer
: nullable String is lazy
do
159 return git_exec
("git shortlog -esn . | head -n 1 | sed 's/\\s*[0-9]*\\s*//'")
162 private var contributors
: Array[String] is lazy
do
163 var contribs
= git_exec
("git shortlog -esn . | head -n -1 | " +
164 "sed 's/\\s*[0-9]*\\s*//'")
165 if contribs
== null then return new Array[String]
166 return contribs
.split
("\n")
169 private var git_url
: nullable String is lazy
do
170 var git
= git_exec
("git remote get-url origin")
171 if git
== null then return null
172 git
= git
.replace
("git@github.com:", "https://github.com/")
173 git
= git
.replace
("git@gitlab.com:", "https://gitlab.com/")
177 private var git_dir
: nullable String is lazy
do
178 return git_exec
("git rev-parse --show-prefix")
181 private var browse_url
: nullable String is lazy
do
183 if git
== null then return null
184 var browse
= git
.replace
(".git", "")
186 if dir
== null or dir
.is_empty
then return browse
187 return "{browse}/tree/master/{dir}"
190 private var homepage_url
: nullable String is lazy
do
192 if git
== null then return null
193 # Special case for nit files
194 if git
.has_suffix
("/nit.git") then
195 return "http://nitlanguage.org"
197 return git
.replace
(".git", "")
200 private var issues_url
: nullable String is lazy
do
202 if git
== null then return null
203 return "{git.replace(".git", "")}/issues"
206 private var license
: nullable String is lazy
do
208 if git
== null then return null
209 # Special case for nit files
210 if git
.has_suffix
("/nit.git") then
216 private fun git_exec
(cmd
: String): nullable String do
217 var path
= package_path
218 if path
== null then return null
219 if not is_expanded
then path
= path
.dirname
220 with pr
= new ProcessReader("sh", "-c", "cd {path} && {cmd}") do
221 return pr
.read_all
.trim
225 private var allowed_ini_keys
= [
226 "package.name", "package.desc", "package.tags", "package.license",
227 "package.maintainer", "package.more_contributors",
228 "upstream.browse", "upstream.git", "upstream.git.directory",
229 "upstream.homepage", "upstream.issues", "upstream.apk", "upstream.tryit",
233 private fun check_ini
(toolcontext
: ToolContext) do
235 toolcontext
.error
(location
, "No `package.ini` file for `{name}`")
239 var pkg_path
= package_path
240 if pkg_path
== null then return
242 var ini_path
= ini_path
243 if ini_path
== null then return
245 var ini
= new ConfigTree(ini_path
)
247 ini
.check_key
(toolcontext
, self, "package.name", name
)
248 ini
.check_key
(toolcontext
, self, "package.desc")
249 ini
.check_key
(toolcontext
, self, "package.tags")
251 # FIXME since `git reflog --follow` seems bugged
252 ini
.check_key
(toolcontext
, self, "package.maintainer")
253 # var maint = mpackage.maintainer
254 # if maint != null then
255 # ini.check_key(toolcontext, self, "package.maintainer", maint)
258 # FIXME since `git reflog --follow` seems bugged
259 # var contribs = mpackage.contributors
260 # if contribs.not_empty then
261 # ini.check_key(toolcontext, self, "package.more_contributors", contribs.join(", "))
264 ini
.check_key
(toolcontext
, self, "package.license", license
)
265 ini
.check_key
(toolcontext
, self, "upstream.browse", browse_url
)
266 ini
.check_key
(toolcontext
, self, "upstream.git", git_url
)
267 ini
.check_key
(toolcontext
, self, "upstream.git.directory", git_dir
)
268 ini
.check_key
(toolcontext
, self, "upstream.homepage", homepage_url
)
269 ini
.check_key
(toolcontext
, self, "upstream.issues", issues_url
)
271 for key
in ini
.to_map
.keys
do
272 if not allowed_ini_keys
.has
(key
) then
273 toolcontext
.warning
(location
, "unknown-ini-key",
274 "Warning: ignoring unknown `{key}` key in `{ini.ini_file}`")
279 private fun gen_ini
: String do
280 var ini_path
= self.ini_path
.as(not null)
281 var ini
= new ConfigTree(ini_path
)
283 ini
.update_value
("package.name", name
)
284 ini
.update_value
("package.desc", "")
285 ini
.update_value
("package.tags", "")
286 ini
.update_value
("package.maintainer", maintainer
)
287 ini
.update_value
("package.more_contributors", contributors
.join
(","))
288 ini
.update_value
("package.license", license
or else "")
290 ini
.update_value
("upstream.browse", browse_url
)
291 ini
.update_value
("upstream.git", git_url
)
292 ini
.update_value
("upstream.git.directory", git_dir
)
293 ini
.update_value
("upstream.homepage", homepage_url
)
294 ini
.update_value
("upstream.issues", issues_url
)
302 # The path to `self` Makefile
303 fun makefile_path
: nullable String do
304 var path
= package_path
305 if path
== null then return null
306 if not is_expanded
then return null
307 return path
/ "Makefile"
310 # Does `self` have a Makefile?
311 fun has_makefile
: Bool do
312 var makefile_path
= self.makefile_path
313 if makefile_path
== null then return false
314 return makefile_path
.file_exists
317 private fun check_makefile
(toolcontext
: ToolContext, mainmodule
: MModule) do
318 var model
= toolcontext
.modelbuilder
.model
319 var filter
= new ModelFilter(accept_example
= false, accept_test
= false)
321 var cmd_bin
= new CmdMains(model
, filter
, mentity
= self)
322 var res_bin
= cmd_bin
.init_command
323 if not res_bin
isa CmdSuccess then return
325 for mmodule
in cmd_bin
.results
.as(not null) do
326 if not mmodule
isa MModule then continue
328 if mmodule
.makefile_path
== null then
329 toolcontext
.warning
(location
, "missing-makefile",
330 "Warning: no Makefile for executable module `{mmodule.full_name}`")
335 private fun gen_makefile
(model
: Model, mainmodule
: MModule): nullable String do
336 var filter
= new ModelFilter(accept_example
= false, accept_test
= false)
338 var pkg_path
= package_path
.as(not null)
339 var makefile_path
= makefile_path
.as(not null)
341 var bins
= new Array[String]
342 var cmd_bin
= new CmdMains(model
, filter
, mentity
= self)
343 var res_bin
= cmd_bin
.init_command
344 if res_bin
isa CmdSuccess then
345 for mmodule
in cmd_bin
.results
.as(not null) do
346 if not mmodule
isa MModule then continue
347 var mmodule_makefile
= mmodule
.makefile_path
348 if mmodule_makefile
!= null and mmodule_makefile
!= makefile_path
then continue
350 var file
= mmodule
.location
.file
351 if file
== null then continue
352 # Remove package path prefix
353 var bin_path
= file
.filename
354 if pkg_path
.has_suffix
("/") then
355 bin_path
= bin_path
.replace
(pkg_path
, "")
357 bin_path
= bin_path
.replace
("{pkg_path}/", "")
363 if bins
.is_empty
then return null
365 var make
= new NitMakefile(bins
)
366 make
.render
.write_to_file
(makefile_path
)
372 # The path to `self` manpage files
373 private fun man_path
: nullable String do
374 var path
= package_path
375 if path
== null then return null
376 if not is_expanded
then return null
380 # Does `self` have a manpage files?
381 private fun has_man
: Bool do
382 var man_path
= self.man_path
383 if man_path
== null then return false
384 return man_path
.file_exists
387 private fun check_man
(toolcontext
: ToolContext, mainmodule
: MModule) do
388 var model
= toolcontext
.modelbuilder
.model
389 var filter
= new ModelFilter(accept_example
= false, accept_test
= false)
390 var cmd
= new CmdMains(model
, filter
, mentity
= self)
391 var res
= cmd
.init_command
392 if not res
isa CmdSuccess then return
394 for mmodule
in cmd
.results
.as(not null) do
395 if not mmodule
isa MModule then continue
396 mmodule
.check_man
(toolcontext
)
400 private fun gen_man
(toolcontext
: ToolContext, mainmodule
: MModule) do
401 var model
= toolcontext
.modelbuilder
.model
402 var filter
= new ModelFilter(accept_example
= false, accept_test
= false)
403 var cmd
= new CmdMains(model
, filter
, mentity
= self)
404 var res
= cmd
.init_command
405 if not res
isa CmdSuccess then return
407 var pkg_man
= man_path
.as(not null)
408 for mmodule
in cmd
.results
.as(not null) do
409 if not mmodule
isa MModule then continue
410 if not has_man
then pkg_man
.mkdir
411 mmodule
.gen_man
(toolcontext
)
417 private fun makefile_path
: nullable String do
418 var file
= location
.file
419 if file
== null then return null
421 var dir
= file
.filename
.dirname
422 var makefile
= (dir
/ "Makefile")
423 if not makefile
.file_exists
then return null
425 for line
in makefile
.to_path
.read_lines
do
426 if line
.has_prefix
("{name}:") then return makefile
431 private fun man_path
: nullable String do
432 var mpackage
= self.mpackage
433 if mpackage
== null then return null
434 var path
= mpackage
.man_path
435 if path
== null then return null
436 return path
/ "{name}.man"
439 # Does `self` have a manpage?
440 private fun has_man
: Bool do
441 var man_path
= self.man_path
442 if man_path
== null then return false
443 return man_path
.file_exists
446 private fun make_module
(toolcontext
: ToolContext): Bool do
447 var mpackage
= self.mpackage
448 if mpackage
== null then return false
449 if not mpackage
.is_expanded
then return false
451 var pkg_path
= mpackage
.package_path
452 if pkg_path
== null then return false
454 var pr
= new ProcessReader("sh", "-c", "cd {pkg_path} && make -Bs bin/{name}")
455 var out
= pr
.read_all
.trim
458 if pr
.status
> 0 then
459 toolcontext
.error
(location
, "unable to compile `{name}`")
466 private fun stub_man
(toolcontext
: ToolContext): nullable String do
467 if not make_module
(toolcontext
) then return null
468 var mpackage
= self.mpackage
469 if mpackage
== null then return null
470 if not mpackage
.is_expanded
then return null
472 var pkg_path
= mpackage
.package_path
473 if pkg_path
== null then return null
475 var pr
= new ProcessReader("{pkg_path}/bin/{name}", "--stub-man")
476 var man
= pr
.read_all
.trim
479 if pr
.status
> 0 then
480 toolcontext
.error
(location
, "unable to run `{pkg_path}/bin/{name} --stub-man`")
487 private fun check_man
(toolcontext
: ToolContext) do
489 toolcontext
.error
(location
, "No manpage for bin {full_name}")
492 var man_path
= self.man_path
.as(not null)
493 var man
= stub_man
(toolcontext
)
494 if man
== null or man
.is_empty
then return
496 var old_man
= new ManPage.from_file
(self, man_path
)
497 var new_man
= new ManPage.from_string
(self, man
)
498 old_man
.diff
(toolcontext
, new_man
)
501 private fun gen_man
(toolcontext
: ToolContext) do
502 var man
= stub_man
(toolcontext
)
503 if man
== null or man
.is_empty
then return
504 var man_path
= self.man_path
505 if man_path
== null then return
506 man
.write_to_file
(man_path
)
507 toolcontext
.info
("created manpage `{man_path}`", 0)
511 redef class ConfigTree
512 private fun check_key
(toolcontext
: ToolContext, mpackage
: MPackage, key
: String, value
: nullable String) do
513 if not has_key
(key
) then
514 toolcontext
.warning
(mpackage
.location
, "missing-ini-key",
515 "Warning: missing `{key}` key in `{ini_file}`")
518 if self[key
].as(not null).is_empty
then
519 toolcontext
.warning
(mpackage
.location
, "missing-ini-value",
520 "Warning: empty `{key}` key in `{ini_file}`")
523 if value
!= null and self[key
] != value
then
524 toolcontext
.warning
(mpackage
.location
, "wrong-ini-value",
525 "Warning: wrong value for `{key}` in `{ini_file}`. " +
526 "Expected `{value}`, got `{self[key] or else ""}`")
530 private fun update_value
(key
: String, value
: nullable String) do
531 if value
== null then return
532 if not has_key
(key
) then
535 var old_value
= self[key
]
536 if not value
.is_empty
and old_value
!= value
then
543 # A Makefile for the Nit project
546 # Nit files to compile
547 var nit_files
: Array[String]
549 # List of rules to add in the Makefile
550 fun rules
: Array[MakeRule] do
551 var rules
= new Array[MakeRule]
553 var rule_all
= new MakeRule("all", is_phony
= true)
556 for file
in nit_files
do
557 var bin
= file
.basename
.strip_extension
559 rule_all
.deps
.add
"bin/{bin}"
561 var rule
= new MakeRule("bin/{bin}")
562 rule
.deps
.add
"$(shell $(NITLS) -M {file})"
563 rule
.lines
.add
"mkdir -p bin/"
564 rule
.lines
.add
"$(NITC) {file} -o bin/{bin}"
568 var rule_check
= new MakeRule("check", is_phony
= true)
569 rule_check
.lines
.add
"$(NITUNIT) ."
572 var rule_doc
= new MakeRule("doc", is_phony
= true)
573 rule_doc
.lines
.add
"$(NITDOC) . -o doc/"
576 var rule_clean
= new MakeRule("clean", is_phony
= true)
577 if nit_files
.not_empty
then
578 rule_clean
.lines
.add
"rm -rf bin/"
580 rule_clean
.lines
.add
"rm -rf doc/"
587 fun render
: Writable do
588 var tpl
= new Template
590 # This file is part of NIT ( http://www.nitlanguage.org ).
592 # Licensed under the Apache License, Version 2.0 (the "License");
593 # you may not use this file except in compliance with the License.
594 # You may obtain a copy of the License at
596 # http://www.apache.org/licenses/LICENSE-2.0
598 # Unless required by applicable law or agreed to in writing, software
599 # distributed under the License is distributed on an "AS IS" BASIS,
600 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
601 # See the License for the specific language governing permissions and
602 # limitations under the License.\n"""
604 if nit_files
.not_empty
then
605 tpl
.addn
"NITC ?= nitc"
606 tpl
.addn
"NITLS ?= nitls"
608 tpl
.addn
"NITUNIT ?= nitunit"
609 tpl
.addn
"NITDOC ?= nitdoc"
612 tpl
.add
"\n{rule.render.write_to_string}"
619 # A rule that goes into a Makefile
625 # Is this rule a `.PHONY` one?
626 var is_phony
: Bool = false is optional
629 var deps
= new Array[String]
632 var lines
= new Array[String]
635 fun render
: Writable do
636 var tpl
= new Template
638 tpl
.addn
".PHONY: {name}"
641 if deps
.not_empty
then
642 tpl
.add
" {deps.join(" ")}"
652 private class ManPage
654 var name
: nullable String is noinit
655 var synopsis
: nullable String is noinit
656 var options
= new HashMap[Array[String], String]
658 init from_file
(mmodule
: MModule, file
: String) do
659 from_lines
(mmodule
, file
.to_path
.read_lines
)
662 init from_string
(mmodule
: MModule, string
: String) do
663 from_lines
(mmodule
, string
.split
("\n"))
666 init from_lines
(mmodule
: MModule, lines
: Array[String]) do
670 for i
in [0..lines
.length
[ do
672 if line
.is_empty
then continue
674 if line
== "# NAME" then
678 if line
== "# SYNOPSIS" then
682 if line
== "# OPTIONS" then
687 if section
== "name" and name
== null then
690 if section
== "synopsis" and synopsis
== null then
693 if section
== "options" and line
.has_prefix
("###") then
694 var opts
= new Array[String]
695 for opt
in line
.substring
(3, line
.length
).trim
.replace
("`", "").split
(",") do
699 if i
< lines
.length
- 1 then
700 desc
= lines
[i
+ 1].trim
707 fun diff
(toolcontext
: ToolContext, ref
: ManPage) do
708 if name
!= ref
.name
then
709 toolcontext
.warning
(mmodule
.location
, "diff-man",
710 "Warning: outdated man description. " +
711 "Expected `{ref.name or else ""}` got `{name or else ""}`.")
713 if synopsis
!= ref
.synopsis
then
714 toolcontext
.warning
(mmodule
.location
, "diff-man",
715 "Warning: outdated man synopsis. " +
716 "Expected `{ref.synopsis or else ""}` got `{synopsis or else ""}`.")
718 for name
, desc
in options
do
719 if not ref
.options
.has_key
(name
) then
720 toolcontext
.warning
(mmodule
.location
, "diff-man",
721 "Warning: unknown man option `{name}`.`")
724 var ref_desc
= ref
.options
[name
]
725 if desc
!= ref_desc
then
726 toolcontext
.warning
(mmodule
.location
, "diff-man",
727 "Warning: outdated man option description. Expected `{ref_desc}` got `{desc}`.")
730 for ref_name
, ref_desc
in ref
.options
do
731 if not options
.has_key
(ref_name
) then
732 toolcontext
.warning
(mmodule
.location
, "diff-man",
733 "Warning: missing man option `{ref_name}`.`")
739 var tpl
= new Template
741 tpl
.addn name
or else ""
742 tpl
.addn
"# SYNOPSIS"
743 tpl
.addn synopsis
or else ""
745 for name
, desc
in options
do
746 tpl
.addn
" * {name}: {desc}"
748 return tpl
.write_to_string
753 var toolcontext
= new ToolContext
754 var tpl
= new Template
755 tpl
.add
"Usage: nitpackage [OPTION]... <file.nit>...\n"
756 tpl
.add
"Helpful features about packages."
757 toolcontext
.tooldescription
= tpl
.write_to_string
760 toolcontext
.process_options
(args
)
761 var arguments
= toolcontext
.option_context
.rest
764 var model
= new Model
765 var mbuilder
= new ModelBuilder(model
, toolcontext
)
766 var mmodules
= mbuilder
.parse_full
(arguments
)
769 if mmodules
.is_empty
then return
771 toolcontext
.run_global_phases
(mmodules
)