nitweb: remove ModelView dependency
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 15 May 2018 01:48:24 +0000 (21:48 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 17 May 2018 21:16:29 +0000 (17:16 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/api/api_base.nit
src/doc/api/api_docdown.nit
src/doc/api/api_metrics.nit
src/doc/api/api_model.nit
src/nitweb.nit

index 08406a2..9b84a35 100644 (file)
@@ -40,42 +40,10 @@ class NitwebConfig
        var modelbuilder: ModelBuilder
 
        # The JSON API does not filter anything by default.
-       #
-       # So we can cache the model view.
-       var view: ModelView
+       var filter: nullable ModelFilter
 
        # Catalog to pass to handlers.
-       var catalog: Catalog is noinit
-
-       # Build the catalog
-       #
-       # This method should be called at nitweb startup.
-       # TODO move to nitweb
-       fun build_catalog do
-               var catalog = new Catalog(modelbuilder)
-               # Compute the poset
-               for p in view.mpackages do
-                       var g = p.root
-                       assert g != null
-                       modelbuilder.scan_group(g)
-
-                       catalog.deps.add_node(p)
-                       for gg in p.mgroups do for m in gg.mmodules do
-                               for im in m.in_importation.direct_greaters do
-                                       var ip = im.mpackage
-                                       if ip == null or ip == p then continue
-                                       catalog.deps.add_edge(p, ip)
-                               end
-                       end
-               end
-               # Build the catalog
-               for mpackage in view.mpackages do
-                       catalog.package_page(mpackage)
-                       catalog.git_info(mpackage)
-                       catalog.mpackage_stats(mpackage)
-               end
-               self.catalog = catalog
-       end
+       var catalog: Catalog
 end
 
 # Specific handler for the nitweb API.
@@ -86,11 +54,13 @@ abstract class APIHandler
        var config: NitwebConfig
 
        # Find the MEntity ` with `full_name`.
-       fun find_mentity(model: ModelView, full_name: nullable String): nullable MEntity do
+       fun find_mentity(full_name: nullable String): nullable MEntity do
                if full_name == null then return null
-               var mentity = model.mentity_by_full_name(full_name.from_percent_encoding)
+               var mentity = config.model.mentity_by_full_name(full_name.from_percent_encoding, config.filter)
                if mentity == null then return null
-               if config.view.accept_mentity(mentity) then return mentity
+
+               var filter = config.filter
+               if filter == null or filter.accept_mentity(mentity) then return mentity
                return null
        end
 
@@ -105,7 +75,7 @@ abstract class APIHandler
                        res.api_error(400, "Expected mentity full name")
                        return null
                end
-               var mentity = find_mentity(config.view, id)
+               var mentity = find_mentity(id)
                if mentity == null then
                        res.api_error(404, "MEntity `{id}` not found")
                end
index 0a23542..df16bd1 100644 (file)
@@ -21,9 +21,9 @@ import commands::commands_docdown
 redef class NitwebConfig
        # Specific Markdown processor to use within Nitweb
        var md_processor: MarkdownProcessor is lazy do
-               var parser = new CommandParser(view, modelbuilder)
+               var parser = new CommandParser(model, mainmodule, modelbuilder, catalog, filter)
                var proc = new CmdMarkdownProcessor(parser)
-               proc.decorator = new CmdDecorator(view)
+               proc.decorator = new CmdDecorator(model)
                return proc
        end
 end
index 4fcdb9e..2224150 100644 (file)
@@ -29,38 +29,38 @@ class APIStructuralMetrics
 
        private fun mclasses_metrics: MetricSet do
                var metrics = new MetricSet
-               metrics.register(new CNOA(config.view))
-               metrics.register(new CNOP(config.view))
-               metrics.register(new CNOC(config.view))
-               metrics.register(new CNOD(config.view))
-               metrics.register(new CNOAC(config.view))
-               metrics.register(new CNOAA(config.view))
-               metrics.register(new CNOAI(config.view))
-               metrics.register(new CDIT(config.view))
-               metrics.register(new CNBP(config.view))
-               metrics.register(new CNBA(config.view))
-               metrics.register(new CNBM(config.view))
-               metrics.register(new CNBI(config.view))
-               metrics.register(new CNBV(config.view))
-               metrics.register(new CNBIP(config.view))
-               metrics.register(new CNBRP(config.view))
-               metrics.register(new CNBHP(config.view))
-               metrics.register(new CNBLP(config.view))
+               metrics.register(new CNOA(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNOP(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNOC(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNOD(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNOAC(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNOAA(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNOAI(config.model, config.mainmodule, config.filter))
+               metrics.register(new CDIT(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBP(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBA(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBM(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBI(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBV(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBIP(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBRP(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBHP(config.model, config.mainmodule, config.filter))
+               metrics.register(new CNBLP(config.model, config.mainmodule, config.filter))
                return metrics
        end
 
        private fun mmodules_metrics: MetricSet do
                var metrics = new MetricSet
-               metrics.register(new MNOA(config.view))
-               metrics.register(new MNOP(config.view))
-               metrics.register(new MNOC(config.view))
-               metrics.register(new MNOD(config.view))
-               metrics.register(new MDIT(config.view))
-               metrics.register(new MNBD(config.view))
-               metrics.register(new MNBI(config.view))
-               metrics.register(new MNBR(config.view))
-               metrics.register(new MNBCC(config.view))
-               metrics.register(new MNBAC(config.view))
+               metrics.register(new MNOA(config.model, config.mainmodule, config.filter))
+               metrics.register(new MNOP(config.model, config.mainmodule, config.filter))
+               metrics.register(new MNOC(config.model, config.mainmodule, config.filter))
+               metrics.register(new MNOD(config.model, config.mainmodule, config.filter))
+               metrics.register(new MDIT(config.model, config.mainmodule, config.filter))
+               metrics.register(new MNBD(config.model, config.mainmodule, config.filter))
+               metrics.register(new MNBI(config.model, config.mainmodule, config.filter))
+               metrics.register(new MNBR(config.model, config.mainmodule, config.filter))
+               metrics.register(new MNBCC(config.model, config.mainmodule, config.filter))
+               metrics.register(new MNBAC(config.model, config.mainmodule, config.filter))
                return metrics
        end
 
index 5c92817..39d4305 100644 (file)
@@ -89,7 +89,7 @@ end
 class APIList
        super APICommand
 
-       redef fun command do return new CmdModelEntities(config.view)
+       redef fun command do return new CmdModelEntities(config.model)
 end
 
 # Return a random list of MEntities.
@@ -98,7 +98,7 @@ end
 class APIRandom
        super APICommand
 
-       redef fun command do return new CmdRandomEntities(config.view)
+       redef fun command do return new CmdRandomEntities(config.model)
 end
 
 # Search mentities from a cmd string.
@@ -107,7 +107,7 @@ end
 class APISearch
        super APIList
 
-       redef fun command do return new CmdCatalogSearch(config.view, config.catalog)
+       redef fun command do return new CmdCatalogSearch(config.model, config.catalog)
 end
 
 # CmdEntity
@@ -118,7 +118,7 @@ end
 class APIEntity
        super APICommand
 
-       redef fun command do return new CmdEntity(config.view)
+       redef fun command do return new CmdEntity(config.model)
 end
 
 # Return the full MDoc of a MEntity.
@@ -127,7 +127,7 @@ end
 class APIEntityDoc
        super APICommand
 
-       redef fun command do return new CmdComment(config.view)
+       redef fun command do return new CmdComment(config.model)
 end
 
 # List MEntity ancestors
@@ -136,7 +136,7 @@ end
 class APIEntityAncestors
        super APICommand
 
-       redef fun command do return new CmdAncestors(config.view)
+       redef fun command do return new CmdAncestors(config.model, config.mainmodule)
 end
 
 # List MEntity parents
@@ -145,7 +145,7 @@ end
 class APIEntityParents
        super APICommand
 
-       redef fun command do return new CmdParents(config.view)
+       redef fun command do return new CmdParents(config.model, config.mainmodule)
 end
 
 # List MEntity children
@@ -154,7 +154,7 @@ end
 class APIEntityChildren
        super APICommand
 
-       redef fun command do return new CmdChildren(config.view)
+       redef fun command do return new CmdChildren(config.model, config.mainmodule)
 end
 
 # List MEntity descendants
@@ -163,7 +163,7 @@ end
 class APIEntityDescendants
        super APICommand
 
-       redef fun command do return new CmdDescendants(config.view)
+       redef fun command do return new CmdDescendants(config.model, config.mainmodule)
 end
 
 # Linearize super definitions of a MClassDef or a MPropDef if any.
@@ -172,7 +172,7 @@ end
 class APIEntityLinearization
        super APICommand
 
-       redef fun command do return new CmdLinearization(config.view)
+       redef fun command do return new CmdLinearization(config.model, config.mainmodule)
 end
 
 # List definitions of a MEntity.
@@ -181,7 +181,7 @@ end
 class APIEntityDefs
        super APICommand
 
-       redef fun command do return new CmdFeatures(config.view)
+       redef fun command do return new CmdFeatures(config.model)
 end
 
 # List intro definitions of a MEntity.
@@ -190,7 +190,7 @@ end
 class APIEntityIntros
        super APICommand
 
-       redef fun command do return new CmdIntros(config.view)
+       redef fun command do return new CmdIntros(config.model, config.mainmodule)
 end
 
 # List redef definitions of a MEntity.
@@ -199,7 +199,7 @@ end
 class APIEntityRedefs
        super APICommand
 
-       redef fun command do return new CmdRedefs(config.view)
+       redef fun command do return new CmdRedefs(config.model, config.mainmodule)
 end
 
 # List all definitions accessible from a MEntity.
@@ -208,7 +208,7 @@ end
 class APIEntityAll
        super APICommand
 
-       redef fun command do return new CmdAllProps(config.view)
+       redef fun command do return new CmdAllProps(config.model, config.mainmodule)
 end
 
 # Return the source code of MEntity.
@@ -217,7 +217,7 @@ end
 class APIEntityCode
        super APICommand
 
-       redef fun command do return new CmdEntityCode(config.view, config.modelbuilder)
+       redef fun command do return new CmdEntityCode(config.model, config.modelbuilder)
 end
 
 # Return the UML diagram for MEntity.
@@ -226,7 +226,7 @@ end
 class APIEntityUML
        super APICommand
 
-       redef fun command do return new CmdUML(config.view)
+       redef fun command do return new CmdUML(config.model, config.mainmodule)
 end
 
 # Return the inheritance graph for MEntity.
@@ -235,7 +235,7 @@ end
 class APIInheritanceGraph
        super APICommand
 
-       redef fun command do return new CmdInheritanceGraph(config.view)
+       redef fun command do return new CmdInheritanceGraph(config.model, config.mainmodule)
 end
 
 # CmdCatalog
@@ -246,7 +246,7 @@ end
 class APICatalogPackages
        super APICommand
 
-       redef fun command do return new CmdCatalogPackages(config.view, config.catalog)
+       redef fun command do return new CmdCatalogPackages(config.model, config.catalog)
 end
 
 # Get the catalog statistics
@@ -255,7 +255,7 @@ end
 class APICatalogStats
        super APICommand
 
-       redef fun command do return new CmdCatalogStats(config.view, config.catalog)
+       redef fun command do return new CmdCatalogStats(config.model, config.catalog)
 end
 
 # Get the package metadata
@@ -264,7 +264,7 @@ end
 class APIEntityMetadata
        super APICommand
 
-       redef fun command do return new CmdMetadata(config.view)
+       redef fun command do return new CmdMetadata(config.model)
 end
 
 # Get all the tags from the catalog
@@ -273,7 +273,7 @@ end
 class APICatalogTags
        super APICommand
 
-       redef fun command do return new CmdCatalogTags(config.view, config.catalog)
+       redef fun command do return new CmdCatalogTags(config.model, config.catalog)
 end
 
 # Get the packages related to a tag
@@ -282,7 +282,7 @@ end
 class APICatalogTag
        super APICommand
 
-       redef fun command do return new CmdCatalogTag(config.view, config.catalog)
+       redef fun command do return new CmdCatalogTag(config.model, config.catalog)
 end
 
 # Get a person existing in the catalog
@@ -291,7 +291,7 @@ end
 class APICatalogPerson
        super APICommand
 
-       redef fun command do return new CmdCatalogPerson(config.view, config.catalog)
+       redef fun command do return new CmdCatalogPerson(config.model, config.catalog)
 end
 
 # Get the list of mpackages maintained by a person
@@ -300,7 +300,7 @@ end
 class APICatalogMaintaining
        super APICommand
 
-       redef fun command do return new CmdCatalogMaintaining(config.view, config.catalog)
+       redef fun command do return new CmdCatalogMaintaining(config.model, config.catalog)
 end
 
 # Get the list of mpackages contributed by a person
@@ -309,7 +309,7 @@ end
 class APICatalogContributing
        super APICommand
 
-       redef fun command do return new CmdCatalogContributing(config.view, config.catalog)
+       redef fun command do return new CmdCatalogContributing(config.model, config.catalog)
 end
 
 # CmdIni
@@ -320,7 +320,7 @@ end
 class APIIniDesc
        super APICommand
 
-       redef fun command do return new CmdIniDescription(config.view)
+       redef fun command do return new CmdIniDescription(config.model)
 end
 
 # Get the package Git URL from the ini file
@@ -329,7 +329,7 @@ end
 class APIIniGit
        super APICommand
 
-       redef fun command do return new CmdIniGitUrl(config.view)
+       redef fun command do return new CmdIniGitUrl(config.model)
 end
 
 # Get the package Git clone command from the ini file
@@ -338,7 +338,7 @@ end
 class APIIniClone
        super APICommand
 
-       redef fun command do return new CmdIniCloneCommand(config.view)
+       redef fun command do return new CmdIniCloneCommand(config.model)
 end
 
 # Get the package issues URL from the ini file
@@ -347,7 +347,7 @@ end
 class APIIniIssues
        super APICommand
 
-       redef fun command do return new CmdIniIssuesUrl(config.view)
+       redef fun command do return new CmdIniIssuesUrl(config.model)
 end
 
 # Get the package maintainer from the ini file
@@ -356,7 +356,7 @@ end
 class APIIniMaintainer
        super APICommand
 
-       redef fun command do return new CmdIniMaintainer(config.view)
+       redef fun command do return new CmdIniMaintainer(config.model)
 end
 
 # Get the package contributors from the ini file
@@ -365,7 +365,7 @@ end
 class APIIniContributors
        super APICommand
 
-       redef fun command do return new CmdIniContributors(config.view)
+       redef fun command do return new CmdIniContributors(config.model)
 end
 
 # Get the package license from the ini file
@@ -374,7 +374,7 @@ end
 class APIIniLicense
        super APICommand
 
-       redef fun command do return new CmdIniLicense(config.view)
+       redef fun command do return new CmdIniLicense(config.model)
 end
 
 # Get the package license file
@@ -383,7 +383,7 @@ end
 class APIIniLicenseFile
        super APICommand
 
-       redef fun command do return new CmdLicenseFile(config.view)
+       redef fun command do return new CmdLicenseFile(config.model)
 end
 
 # Get the package contrib file
@@ -392,7 +392,7 @@ end
 class APIIniContribFile
        super APICommand
 
-       redef fun command do return new CmdContribFile(config.view)
+       redef fun command do return new CmdContribFile(config.model)
 end
 
 # Get the package license file content
@@ -401,7 +401,7 @@ end
 class APIIniLicenseFileContent
        super APICommand
 
-       redef fun command do return new CmdLicenseFileContent(config.view)
+       redef fun command do return new CmdLicenseFileContent(config.model)
 end
 
 # Get the package contrib file content
@@ -410,5 +410,5 @@ end
 class APIIniContribFileContent
        super APICommand
 
-       redef fun command do return new CmdContribFileContent(config.view)
+       redef fun command do return new CmdContribFileContent(config.model)
 end
index 3617df4..ab16edc 100644 (file)
@@ -72,9 +72,9 @@ private class NitwebPhase
                        accept_attribute = not toolcontext.opt_no_attribute.value
                )
 
-               var view = new ModelView(model, mainmodule, filter)
+               var catalog = build_catalog(toolcontext.modelbuilder, filter)
 
-               var config = new NitwebConfig(model, mainmodule, toolcontext.modelbuilder, view)
+               var config = new NitwebConfig(model, mainmodule, toolcontext.modelbuilder, filter, catalog)
                var config_file = toolcontext.opt_config.value
                if config_file == null then config.default_config_file = "nitweb.ini"
                config.parse_options(args)
@@ -89,7 +89,6 @@ private class NitwebPhase
        do
                var config = build_config(toolcontext, mainmodule)
                config.model.nitdoc_md_processor = config.md_processor
-               config.build_catalog
 
                var app = new App
 
@@ -104,6 +103,37 @@ private class NitwebPhase
 
                app.listen(config.app_host, config.app_port)
        end
+
+       # Build the catalog
+       #
+       # This method should be called at nitweb startup.
+       fun build_catalog(modelbuilder: ModelBuilder, filter: nullable ModelFilter): Catalog do
+               var catalog = new Catalog(modelbuilder)
+               var mpackages = modelbuilder.model.collect_mpackages(filter)
+               # Compute the poset
+               for p in mpackages do
+                       var g = p.root
+                       assert g != null
+                       modelbuilder.scan_group(g)
+
+                       catalog.deps.add_node(p)
+                       for gg in p.mgroups do for m in gg.mmodules do
+                               for im in m.in_importation.direct_greaters do
+                                       var ip = im.mpackage
+                                       if ip == null or ip == p then continue
+                                       catalog.deps.add_edge(p, ip)
+                               end
+                       end
+               end
+               # Build the catalog
+               for mpackage in mpackages do
+                       catalog.package_page(mpackage)
+                       catalog.git_info(mpackage)
+                       catalog.mpackage_stats(mpackage)
+               end
+               return catalog
+       end
+
 end
 
 # build toolcontext