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 # Modelisation of a Nit package
19 private import more_collections
24 # A Nit package, that encompass a product
28 # The name of the package
31 redef fun full_name
do return name
33 redef var c_name
= name
.to_cmangle
is lazy
35 # The model of the package
40 # The root of the group tree
41 var root
: nullable MGroup = null is writable
43 # The group tree, as a POSet
44 var mgroups
= new POSet[MGroup]
46 redef fun to_s
do return name
50 model
.mpackages
.add
(self)
51 # Add `self` to the importation graph
52 model
.mpackage_importation_graph
.add_vertex
(self)
53 model
.mpackage_by_name
.add_one
(name
, self)
56 # MPackage are always roots of the concerns hierarchy
57 redef fun parent_concern
do return null
59 redef fun mdoc_or_fallback
62 if mdoc
!= null then return mdoc
64 if root
!= null then return root
.mdoc_or_fallback
68 # Does `self` have a source file?
69 fun has_source
: Bool do return location
.file
!= null
72 fun package_path
: nullable String do
73 if not has_source
then return null
74 return location
.file
.as(not null).filename
77 # Is `self` in its own directory?
78 fun is_expanded
: Bool do
79 var path
= package_path
80 if path
== null then return false
81 return not path
.has_suffix
(".nit")
84 # The path to `self` ini file
85 fun ini_path
: nullable String do
86 var path
= package_path
87 if path
== null then return null
88 if is_expanded
then return path
/ "package.ini"
89 return path
.dirname
/ "{name}.ini"
92 # Does `self` have a ini file?
94 var ini_path
= self.ini_path
95 if ini_path
== null then return false
96 return ini_path
.file_exists
99 # The path to `self` README.md
100 fun readme_path
: nullable String do
101 var path
= package_path
102 if path
== null then return null
103 if not is_expanded
then return null
104 return path
/ "README.md"
107 # Does `self` have a README.md file?
108 fun has_readme
: Bool do
109 var readme_path
= self.readme_path
110 if readme_path
== null then return false
111 return readme_path
.file_exists
115 # A group of modules in a package
119 # The name of the group
120 # empty name for a default group in a single-module package
125 # The enclosing package
126 var mpackage
: MPackage
128 # The parent group if any
129 # see `in_nesting` for more
130 var parent
: nullable MGroup
132 # Fully qualified name.
133 # It includes each parent group separated by `>`.
134 # The full_name is terminated by `>` to avoid collision with other entities.
136 # E.g. `core>` and `core>collection>`
140 if p
== null then return "{name}>"
141 return "{p.full_name}{name}>"
144 # The group is the group tree on the package (`mpackage.mgroups`)
145 # nested groups (children) are smaller
146 # nesting group (see `parent`) is bigger
147 var in_nesting
: POSetElement[MGroup] is noinit
149 # Is `self` the root of its package?
150 fun is_root
: Bool do return mpackage
.root
== self
152 # The filepath (usually a directory) of the group, if any
154 # safe alias to `location.file.filename`
155 fun filepath
: nullable String do
157 res
= self.location
.file
158 if res
== null then return null
164 var tree
= mpackage
.mgroups
165 self.in_nesting
= tree
.add_node
(self)
166 var parent
= self.parent
167 if parent
!= null then
168 tree
.add_edge
(self, parent
)
172 redef fun model
do return mpackage
.model
174 redef fun parent_concern
do
175 if not is_root
then return parent
179 redef fun to_s
do return name
184 # Full package importation graph
185 # Each package is in relation with itself
186 var mpackage_importation_graph
= new HashDigraph[MPackage]
188 # packages of the model
189 var mpackages
= new Array[MPackage]
191 # Collections of package grouped by their names
192 private var mpackage_by_name
= new MultiHashMap[String, MPackage]
194 # Return all package named `name`
195 # If such a package is not yet loaded, null is returned (instead of an empty array)
196 fun get_mpackages_by_name
(name
: String): nullable Array[MPackage]
198 if mpackage_by_name
.has_key
(name
) then
199 return mpackage_by_name
[name
]