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 # Basic catalog generator for Nit packages
17 # See: <http://nitlanguage.org/catalog/>
19 # The tool scans packages and generates the HTML files of a catalog.
23 # * [X] scan packages and their `.ini`
24 # * [X] generate lists of packages
25 # * [X] generate a page per package with the readme and most metadata
26 # * [ ] link/include/be included in the documentation
27 # * [ ] propose `related packages`
28 # * [X] show directory content (a la nitls)
29 # * [X] gather git information from the working directory
30 # * [ ] gather git information from the repository
31 # * [ ] gather package information from github
32 # * [ ] gather people information from github
34 # * [ ] separate information gathering from rendering
35 # * [ ] move up information gathering in (existing or new) service modules
36 # * [X] add command line options
37 # * [ ] harden HTML (escaping, path injection, etc)
38 # * [ ] nitcorn server with RESTful API
40 # ## Issues and limitations
42 # The tool works likee the other tools and expects to find valid Nit source code in the directories
44 # * cruft and temporary files will be collected
45 # * missing source file (e.g. not yet generated by nitcc) will make information
46 # incomplete (e.g. invalid module thus partial dependency and metrics)
48 # How to use the tool as the basis of a Nit code archive on the web usable with a package manager is not clear.
51 import loader
# Scan&load packages, groups and modules
52 import doc
::doc_down
# Display mdoc
53 import md5
# To get gravatar images
54 import counter
# For statistics
55 import modelize
# To process and count classes and methods
58 # Return the associated metadata from the `ini`, if any
59 fun metadata
(key
: String): nullable String
62 if ini
== null then return null
66 # The list of maintainers
67 var maintainers
= new Array[String]
69 # The list of contributors
70 var contributors
= new Array[String]
72 # The date of the most recent commit
73 var last_date
: nullable String = null
75 # The date of the oldest commit
76 var first_date
: nullable String = null
79 # A HTML page in a catalog
81 # This is just a template with the header pre-filled and the footer injected at rendering.
82 # Therefore, once instantiated, the content can just be added to it.
86 # Placeholder to include additional things before the `</head>`.
87 var more_head
= new Template
95 <meta charset="utf-8">
96 <link rel="stylesheet" media="all" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
97 <link rel="stylesheet" media="all" href="style.css">
104 <div class='container-fluid'>
106 <nav id='topmenu' class='navbar navbar-default navbar-fixed-top' role='navigation'>
107 <div class='container-fluid'>
108 <div class='navbar-header'>
109 <button type='button' class='navbar-toggle' data-toggle='collapse' data-target='#topmenu-collapse'>
110 <span class='sr-only'>Toggle menu</span>
111 <span class='icon-bar'></span>
112 <span class='icon-bar'></span>
113 <span class='icon-bar'></span>
115 <span class='navbar-brand'><a href="http://nitlanguage.org/">Nitlanguage.org</a></span>
117 <div class='collapse navbar-collapse' id='topmenu-collapse'>
118 <ul class='nav navbar-nav'>
119 <li><a href="index.html">Catalog</a></li>
131 </div> <!-- container-fluid -->
132 <script src='https://code.jquery.com/jquery-latest.min.js'></script>
133 <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js'></script>
134 <script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table-all.min.js'></script>
142 # Returns `log(self+1)`. Used to compute score of packages
143 fun score
: Float do return (self+1).to_f
.log
146 # The main class of the calatog generator that has the knowledge
150 # used to access the files and count source lines of code
151 var modelbuilder
: ModelBuilder
154 var tag2proj
= new MultiHashMap[String, MPackage]
156 # Packages by category
157 var cat2proj
= new MultiHashMap[String, MPackage]
159 # Packages by maintainer
160 var maint2proj
= new MultiHashMap[String, MPackage]
162 # Packages by contributors
163 var contrib2proj
= new MultiHashMap[String, MPackage]
165 # Dependency between packages
166 var deps
= new POSet[MPackage]
168 # Number of modules by package
169 var mmodules
= new Counter[MPackage]
171 # Number of classes by package
172 var mclasses
= new Counter[MPackage]
174 # Number of methods by package
175 var mmethods
= new Counter[MPackage]
177 # Number of line of code by package
178 var loc
= new Counter[MPackage]
180 # Number of commits by package
181 var commits
= new Counter[MPackage]
185 # The score is loosely computed using other metrics
186 var score
= new Counter[MPackage]
188 # Scan, register and add a contributor to a package
189 fun add_contrib
(person
: String, mpackage
: MPackage, res
: Template)
191 var projs
= contrib2proj
[person
]
192 if not projs
.has
(mpackage
) then projs
.add mpackage
197 # Regular expressions are broken, need to investigate.
200 #var re = "([^<(]*?)(<([^>]*?)>)?(\\((.*)\\))?".to_re
201 #var m = (person+" ").search(re)
202 #print "{person}: `{m or else "?"}` `{m[1] or else "?"}` `{m[3] or else "?"}` `{m[5] or else "?"}`"
204 var sp1
= person
.split_once_on
("<")
205 if sp1
.length
< 2 then
208 var sp2
= sp1
.last
.split_once_on
(">")
209 if sp2
.length
< 2 then
212 name
= sp1
.first
.trim
213 email
= sp2
.first
.trim
214 var sp3
= sp2
.last
.split_once_on
("(")
215 if sp3
.length
< 2 then
218 var sp4
= sp3
.last
.split_once_on
(")")
219 if sp4
.length
< 2 then
222 page
= sp4
.first
.trim
225 var e
= name
.html_escape
228 res
.add
"<a href=\"{page.html_escape}\
">"
230 if email
!= null then
231 # TODO get more things from github by using the email as a key
232 # "https://api.github.com/search/users?q={email}+in:email"
233 var md5
= email
.md5
.to_lower
234 res
.add
"<img src=\"https
://secure
.gravatar
.com
/avatar
/{md5}?size
=20&
;default
=retro\
"> "
237 if page
!= null then res
.add
"</a>"
241 # Recursively generate a level in the file tree of the *content* section
242 private fun gen_content_level
(ot
: OrderedTree[Object], os
: Array[Object], res
: Template)
250 if mdoc
!= null then d
= ": {mdoc.html_synopsis.write_to_string}"
251 res
.add
"<strong>{o.name}</strong>{d} ({o.filepath.to_s})"
252 else if o
isa ModulePath then
257 if mdoc
!= null then d
= ": {mdoc.html_synopsis.write_to_string}"
259 res
.add
"<strong>{o.name}</strong>{d} ({o.filepath.to_s})"
263 var subs
= ot
.sub
.get_or_null
(o
)
264 if subs
!= null then gen_content_level
(ot
, subs
, res
)
270 # Compute information and generate a full HTML page for a package
271 fun package_page
(mpackage
: MPackage): Writable
273 var res
= new CatalogPage
274 var score
= score
[mpackage
].to_f
275 var name
= mpackage
.name
.html_escape
276 res
.more_head
.add
"""<title>{{{name}}}</title>"""
279 <div class="content">
280 <h1 class="package-name">{{{name}}}</h1>
282 var mdoc
= mpackage
.mdoc_or_fallback
285 res
.add mdoc
.html_documentation
286 score
+= mdoc
.content
.length
.score
289 res
.add
"<h2>Content</h2>"
290 var ot
= new OrderedTree[Object]
291 for g
in mpackage
.mgroups
do
293 if g
.is_interesting
then
297 for mp
in g
.module_paths
do
301 ot
.sort_with
(alpha_comparator
)
302 gen_content_level
(ot
, ot
.roots
, res
)
307 <div class="sidebar">
310 var homepage
= mpackage
.metadata
("upstream.homepage")
311 if homepage
!= null then
313 var e
= homepage
.html_escape
314 res
.add
"<li><a href=\"{e}\
">{e}</a></li>\n"
316 var maintainer
= mpackage
.metadata
("package.maintainer")
317 if maintainer
!= null then
319 add_contrib
(maintainer
, mpackage
, res
)
320 mpackage
.maintainers
.add maintainer
321 var projs
= maint2proj
[maintainer
]
322 if not projs
.has
(mpackage
) then projs
.add mpackage
324 var license
= mpackage
.metadata
("package.license")
325 if license
!= null then
327 var e
= license
.html_escape
328 res
.add
"<li><a href=\"http
://opensource
.org
/licenses
/{e}\
">{e}</a> license</li>\n"
332 res
.add
"<h3>Source Code</h3>\n<ul class=\"box\
">\n"
333 var browse
= mpackage
.metadata
("upstream.browse")
334 if browse
!= null then
336 var e
= browse
.html_escape
337 res
.add
"<li><a href=\"{e}\
">{e}</a></li>\n"
339 var git
= mpackage
.metadata
("upstream.git")
341 var e
= git
.html_escape
342 res
.add
"<li><tt>{e}</tt></li>\n"
344 var last_date
= mpackage
.last_date
345 if last_date
!= null then
346 var e
= last_date
.html_escape
347 res
.add
"<li>most recent commit: {e}</li>\n"
349 var first_date
= mpackage
.first_date
350 if first_date
!= null then
351 var e
= first_date
.html_escape
352 res
.add
"<li>oldest commit: {e}</li>\n"
354 var commits
= commits
[mpackage
]
356 res
.add
"<li>{commits} commits</li>\n"
360 res
.add
"<h3>Tags</h3>\n"
361 var tags
= mpackage
.metadata
("package.tags")
362 var ts2
= new Array[String]
365 var ts
= tags
.split
(",")
368 if t
== "" then continue
369 if cat
== null then cat
= t
370 tag2proj
[t
].add mpackage
372 ts2
.add
"<a href=\"index
.html
#tag_{t}\">{t}</a>"
374 res
.add_list
(ts2
, ", ", ", ")
379 tag2proj
[t
].add mpackage
380 res
.add
"<a href=\"index
.html
#tag_{t}\">{t}</a>"
382 if cat
!= null then cat2proj
[cat
].add mpackage
383 score
+= ts2
.length
.score
385 if deps
.has
(mpackage
) then
386 var reqs
= deps
[mpackage
].greaters
.to_a
387 reqs
.remove
(mpackage
)
388 alpha_comparator
.sort
(reqs
)
389 res
.add
"<h3>Requirements</h3>\n"
390 if reqs
.is_empty
then
393 var list
= new Array[String]
395 var direct
= deps
.has_direct_edge
(mpackage
, r
)
396 var s
= "<a href=\"{r}.html\
">"
397 if direct
then s
+= "<strong>"
399 if direct
then s
+= "</strong>"
403 res
.add_list
(list
, ", ", " and ")
406 reqs
= deps
[mpackage
].smallers
.to_a
407 reqs
.remove
(mpackage
)
408 alpha_comparator
.sort
(reqs
)
409 res
.add
"<h3>Clients</h3>\n"
410 if reqs
.is_empty
then
413 var list
= new Array[String]
415 var direct
= deps
.has_direct_edge
(r
, mpackage
)
416 var s
= "<a href=\"{r}.html\
">"
417 if direct
then s
+= "<strong>"
419 if direct
then s
+= "</strong>"
423 res
.add_list
(list
, ", ", " and ")
426 score
+= deps
[mpackage
].greaters
.length
.score
427 score
+= deps
[mpackage
].direct_greaters
.length
.score
428 score
+= deps
[mpackage
].smallers
.length
.score
429 score
+= deps
[mpackage
].direct_smallers
.length
.score
432 var contributors
= mpackage
.contributors
433 if not contributors
.is_empty
then
434 res
.add
"<h3>Contributors</h3>\n<ul class=\"box\
">"
435 for c
in contributors
do
436 add_contrib
(c
, mpackage
, res
)
440 score
+= contributors
.length
.to_f
446 for g
in mpackage
.mgroups
do
447 mmodules
+= g
.module_paths
.length
448 for m
in g
.mmodules
do
449 var am
= modelbuilder
.mmodule2node
(m
)
451 var file
= am
.location
.file
453 loc
+= file
.line_starts
.length
- 1
456 for cd
in m
.mclassdefs
do
458 for pd
in cd
.mpropdefs
do
459 if not pd
isa MMethodDef then continue
465 self.mmodules
[mpackage
] = mmodules
466 self.mclasses
[mpackage
] = mclasses
467 self.mmethods
[mpackage
] = mmethods
468 self.loc
[mpackage
] = loc
470 #score += mmodules.score
471 score
+= mclasses
.score
472 score
+= mmethods
.score
478 <li>{{{mmodules}}} modules</li>
479 <li>{{{mclasses}}} classes</li>
480 <li>{{{mmethods}}} methods</li>
481 <li>{{{loc}}} lines of code</li>
488 self.score
[mpackage
] = score
.to_i
493 # Return a short HTML sequence for a package
495 # Intended to use in lists.
496 fun li_package
(p
: MPackage): String
499 var f
= "{p.name}.html"
500 res
+= "<a href=\"{f}\
">{p}</a>"
501 var d
= p
.mdoc_or_fallback
502 if d
!= null then res
+= " - {d.html_synopsis.write_to_string}"
506 # List packages by group.
508 # For each key of the `map` a `<h3>` is generated.
509 # Each package is then listed.
511 # The list of keys is generated first to allow fast access to the correct `<h3>`.
512 # `id_prefix` is used to give an id to the `<h3>` element.
513 fun list_by
(map
: MultiHashMap[String, MPackage], id_prefix
: String): Template
515 var res
= new Template
516 var keys
= map
.keys
.to_a
517 alpha_comparator
.sort
(keys
)
518 var list
= [for x
in keys
do "<a href=\"#{id_prefix}{x.html_escape}\">{x.html_escape}</a>"]
519 res
.add_list
(list
, ", ", " and ")
522 var projs
= map
[k
].to_a
523 alpha_comparator
.sort
(projs
)
524 var e
= k
.html_escape
525 res
.add
"<h3 id=\"{id_prefix}{e}\
">{e} ({projs.length})</h3>\n<ul>\n"
528 res
.add li_package
(p
)
536 # List the 10 best packages from `cpt`
537 fun list_best
(cpt
: Counter[MPackage]): Template
539 var res
= new Template
543 if i
> best
.length
then break
544 var p
= best
[best
.length-i
]
546 res
.add li_package
(p
)
547 # res.add " ({cpt[p]})"
554 # Collect more information on a package using the `git` tool.
555 fun git_info
(mpackage
: MPackage)
557 var ini
= mpackage
.ini
558 if ini
== null then return
560 # TODO use real git info
561 #var repo = ini.get_or_null("upstream.git")
562 #var branch = ini.get_or_null("upstream.git.branch")
563 #var directory = ini.get_or_null("upstream.git.directory")
565 var dirpath
= mpackage
.root
.filepath
566 if dirpath
== null then return
568 # Collect commits info
569 var res
= git_run
("log", "--no-merges", "--follow", "--pretty=tformat:%ad;%aN <%aE>", "--", dirpath
)
570 var contributors
= new Counter[String]
571 var commits
= res
.split
("\n")
572 if commits
.not_empty
and commits
.last
== "" then commits
.pop
573 self.commits
[mpackage
] = commits
.length
575 var s
= l
.split_once_on
(';')
576 if s
.length
!= 2 or s
.last
== "" then continue
578 # Collect date of last and first commit
579 if mpackage
.last_date
== null then mpackage
.last_date
= s
.first
580 mpackage
.first_date
= s
.first
583 contributors
.inc
(s
.last
)
585 for c
in contributors
.sort
.reverse_iterator
do
586 mpackage
.contributors
.add c
591 # Produce a HTML table containig information on the packages
593 # `package_page` must have been called before so that information is computed.
594 fun table_packages
(mpackages
: Array[MPackage]): Template
596 alpha_comparator
.sort
(mpackages
)
597 var res
= new Template
598 res
.add
"<table data-toggle=\"table\
" data-sort-name=\"name\
" data-sort-order=\"desc\
" width=\"100%\
">\n"
599 res
.add
"<thead><tr>\n"
600 res
.add
"<th data-field=\"name\
" data-sortable=\"true\
">name</th>\n"
601 res
.add
"<th data-field=\"maint\
" data-sortable=\"true\
">maint</th>\n"
602 res
.add
"<th data-field=\"contrib\
" data-sortable=\"true\
">contrib</th>\n"
603 if deps
.not_empty
then
604 res
.add
"<th data-field=\"reqs\
" data-sortable=\"true\
">reqs</th>\n"
605 res
.add
"<th data-field=\"dreqs\
" data-sortable=\"true\
">direct<br>reqs</th>\n"
606 res
.add
"<th data-field=\"cli\
" data-sortable=\"true\
">clients</th>\n"
607 res
.add
"<th data-field=\"dcli\
" data-sortable=\"true\
">direct<br>clients</th>\n"
609 res
.add
"<th data-field=\"mod\
" data-sortable=\"true\
">modules</th>\n"
610 res
.add
"<th data-field=\"cla\
" data-sortable=\"true\
">classes</th>\n"
611 res
.add
"<th data-field=\"met\
" data-sortable=\"true\
">methods</th>\n"
612 res
.add
"<th data-field=\"loc\
" data-sortable=\"true\
">lines</th>\n"
613 res
.add
"<th data-field=\"score\
" data-sortable=\"true\
">score</th>\n"
614 res
.add
"</tr></thead>"
615 for p
in mpackages
do
617 res
.add
"<td><a href=\"{p.name}.html\
">{p.name}</a></td>"
619 if p
.maintainers
.not_empty
then maint
= p
.maintainers
.first
620 res
.add
"<td>{maint}</td>"
621 res
.add
"<td>{p.contributors.length}</td>"
622 if deps
.not_empty
then
623 res
.add
"<td>{deps[p].greaters.length-1}</td>"
624 res
.add
"<td>{deps[p].direct_greaters.length}</td>"
625 res
.add
"<td>{deps[p].smallers.length-1}</td>"
626 res
.add
"<td>{deps[p].direct_smallers.length}</td>"
628 res
.add
"<td>{mmodules[p]}</td>"
629 res
.add
"<td>{mclasses[p]}</td>"
630 res
.add
"<td>{mmethods[p]}</td>"
631 res
.add
"<td>{loc[p]}</td>"
632 res
.add
"<td>{score[p]}</td>"
640 # Execute a git command and return the result
641 fun git_run
(command
: String...): String
643 # print "git {command.join(" ")}"
644 var p
= new ProcessReader("git", command
...)
651 var model
= new Model
652 var tc
= new ToolContext
654 var opt_dir
= new OptionString("Directory where the HTML files are generated", "-d", "--dir")
655 var opt_no_git
= new OptionBool("Do not gather git information from the working directory", "--no-git")
656 var opt_no_parse
= new OptionBool("Do not parse nit files (no importation information)", "--no-parse")
657 var opt_no_model
= new OptionBool("Do not analyse nit files (no class/method information)", "--no-model")
659 tc
.option_context
.add_option
(opt_dir
, opt_no_git
, opt_no_parse
, opt_no_model
)
661 tc
.process_options
(sys
.args
)
664 var modelbuilder
= new ModelBuilder(model
, tc
)
665 var catalog
= new Catalog(modelbuilder
)
667 # Get files or groups
668 for a
in tc
.option_context
.rest
do
669 modelbuilder
.get_mgroup
(a
)
670 modelbuilder
.identify_file
(a
)
673 # Scan packages and compute information
674 for p
in model
.mpackages
do
677 modelbuilder
.scan_group
(g
)
679 # Load the module to process importation information
680 if opt_no_parse
.value
then continue
681 modelbuilder
.parse_group
(g
)
683 catalog
.deps
.add_node
(p
)
684 for gg
in p
.mgroups
do for m
in gg
.mmodules
do
685 for im
in m
.in_importation
.direct_greaters
do
687 if ip
== null or ip
== p
then continue
688 catalog
.deps
.add_edge
(p
, ip
)
693 if not opt_no_git
.value
then for p
in model
.mpackages
do
697 # Run phases to modelize classes and properties (so we can count them)
698 if not opt_no_model
.value
then
699 modelbuilder
.run_phases
702 var out
= opt_dir
.value
or else "catalog.out"
705 # Generate the css (hard coded)
709 background-color: #f8f8f8;
714 text-decoration: none;
719 text-decoration: none;
731 border-bottom: solid 3px #CCC;
737 border-bottom: solid 1px #CCC;
741 list-style-type: square;
751 border: 1px solid #CCC;
752 font-family: Monospace;
754 background-color: rgb(250, 250, 250);
758 font-family: Monospace;
798 text-overflow: ellipsis;
800 border-bottom: 1px solid rgba(0,0,0,0.2);
803 css
.write_to_file
(out
/"style.css")
807 for p
in model
.mpackages
do
809 var f
= "{p.name}.html"
810 catalog
.package_page
(p
).write_to_file
(out
/f
)
815 var index
= new CatalogPage
816 index
.more_head
.add
"<title>Packages in Nit</title>"
819 <div class="content">
820 <h1>Packages in Nit</h1>
823 index
.add
"<h2>Highlighted Packages</h2>\n"
824 index
.add catalog
.list_best
(catalog
.score
)
826 if catalog
.deps
.not_empty
then
827 index
.add
"<h2>Most Required</h2>\n"
828 var reqs
= new Counter[MPackage]
829 for p
in model
.mpackages
do
830 reqs
[p
] = catalog
.deps
[p
].smallers
.length
- 1
832 index
.add catalog
.list_best
(reqs
)
835 index
.add
"<h2>By First Tag</h2>\n"
836 index
.add catalog
.list_by
(catalog
.cat2proj
, "cat_")
838 index
.add
"<h2>By Any Tag</h2>\n"
839 index
.add catalog
.list_by
(catalog
.tag2proj
, "tag_")
843 <div class="sidebar">
846 <li>{{{model.mpackages.length}}} packages</li>
847 <li>{{{catalog.maint2proj.length}}} maintainers</li>
848 <li>{{{catalog.contrib2proj.length}}} contributors</li>
849 <li>{{{catalog.tag2proj.length}}} tags</li>
850 <li>{{{catalog.mmodules.sum}}} modules</li>
851 <li>{{{catalog.mclasses.sum}}} classes</li>
852 <li>{{{catalog.mmethods.sum}}} methods</li>
853 <li>{{{catalog.loc.sum}}} lines of code</li>
858 index
.write_to_file
(out
/"index.html")
862 var page
= new CatalogPage
863 page
.more_head
.add
"<title>People of Nit</title>"
864 page
.add
"""<div class="content">\n<h1>People of Nit</h1>\n"""
865 page
.add
"<h2>By Maintainer</h2>\n"
866 page
.add catalog
.list_by
(catalog
.maint2proj
, "maint_")
867 page
.add
"<h2>By Contributor</h2>\n"
868 page
.add catalog
.list_by
(catalog
.contrib2proj
, "contrib_")
870 page
.write_to_file
(out
/"people.html")
874 page
= new CatalogPage
875 page
.more_head
.add
"<title>Projets of Nit</title>"
876 page
.add
"""<div class="content">\n<h1>People of Nit</h1>\n"""
877 page
.add
"<h2>Table of Projets</h2>\n"
878 page
.add catalog
.table_packages
(model
.mpackages
)
880 page
.write_to_file
(out
/"table.html")