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
23 # A Nit package, that encompass a product
27 # The name of the package
30 redef fun full_name
do return name
32 redef var c_name
= name
.to_cmangle
is lazy
34 # The model of the package
39 # The root of the group tree
40 var root
: nullable MGroup = null is writable
42 # The group tree, as a POSet
43 var mgroups
= new POSet[MGroup]
45 redef fun to_s
do return name
49 model
.mpackages
.add
(self)
50 model
.mpackage_by_name
.add_one
(name
, self)
53 # MPackage are always roots of the concerns hierarchy
54 redef fun parent_concern
do return null
56 redef fun mdoc_or_fallback
59 if mdoc
!= null then return mdoc
61 if root
!= null then return root
.mdoc_or_fallback
65 # Does `self` have a source file?
66 fun has_source
: Bool do return location
.file
!= null
69 fun package_path
: nullable String do
70 if not has_source
then return null
71 return location
.file
.as(not null).filename
74 # Is `self` in its own directory?
75 fun is_expanded
: Bool do
76 var path
= package_path
77 if path
== null then return false
78 return not path
.has_suffix
(".nit")
81 # The path to `self` ini file
82 fun ini_path
: nullable String do
83 var path
= package_path
84 if path
== null then return null
85 if is_expanded
then return path
/ "package.ini"
86 return path
.dirname
/ "{name}.ini"
89 # Does `self` have a ini file?
91 var ini_path
= self.ini_path
92 if ini_path
== null then return false
93 return ini_path
.file_exists
97 # A group of modules in a package
101 # The name of the group
102 # empty name for a default group in a single-module package
107 # The enclosing package
108 var mpackage
: MPackage
110 # The parent group if any
111 # see `in_nesting` for more
112 var parent
: nullable MGroup
114 # Fully qualified name.
115 # It includes each parent group separated by `>`.
116 # The full_name is terminated by `>` to avoid collision with other entities.
118 # E.g. `core>` and `core>collection>`
122 if p
== null then return "{name}>"
123 return "{p.full_name}{name}>"
126 # The group is the group tree on the package (`mpackage.mgroups`)
127 # nested groups (children) are smaller
128 # nesting group (see `parent`) is bigger
129 var in_nesting
: POSetElement[MGroup] is noinit
131 # Is `self` the root of its package?
132 fun is_root
: Bool do return mpackage
.root
== self
134 # The filepath (usually a directory) of the group, if any
136 # safe alias to `location.file.filename`
137 fun filepath
: nullable String do
139 res
= self.location
.file
140 if res
== null then return null
146 var tree
= mpackage
.mgroups
147 self.in_nesting
= tree
.add_node
(self)
148 var parent
= self.parent
149 if parent
!= null then
150 tree
.add_edge
(self, parent
)
154 redef fun model
do return mpackage
.model
156 redef fun parent_concern
do
157 if not is_root
then return parent
161 redef fun to_s
do return name
165 # packages of the model
166 var mpackages
= new Array[MPackage]
168 # Collections of package grouped by their names
169 private var mpackage_by_name
= new MultiHashMap[String, MPackage]
171 # Return all package named `name`
172 # If such a package is not yet loaded, null is returned (instead of an empty array)
173 fun get_mpackages_by_name
(name
: String): nullable Array[MPackage]
175 if mpackage_by_name
.has_key
(name
) then
176 return mpackage_by_name
[name
]