1cdde49c628ba95b142f99e1a0d7d3f260cc2bc4
[nit.git] / src / model / mpackage.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Modelisation of a Nit package
16 module mpackage
17
18 import model_base
19 private import more_collections
20 import poset
21 import mdoc
22
23 # A Nit package, that encompass a product
24 class MPackage
25 super MConcern
26
27 # The name of the package
28 redef var name
29
30 redef fun full_name do return name
31
32 redef var c_name = name.to_cmangle is lazy
33
34 # The model of the package
35 redef var model
36
37 redef var location
38
39 # The root of the group tree
40 var root: nullable MGroup = null is writable
41
42 # The group tree, as a POSet
43 var mgroups = new POSet[MGroup]
44
45 redef fun to_s do return name
46
47 init
48 do
49 model.mpackages.add(self)
50 model.mpackage_by_name.add_one(name, self)
51 end
52
53 # MPackage are always roots of the concerns hierarchy
54 redef fun parent_concern do return null
55
56 redef fun mdoc_or_fallback
57 do
58 var mdoc = self.mdoc
59 if mdoc != null then return mdoc
60 var root = self.root
61 if root != null then return root.mdoc_or_fallback
62 return null
63 end
64
65 # Does `self` have a source file?
66 fun has_source: Bool do return location.file != null
67
68 # The path to `self`
69 fun package_path: nullable String do
70 if not has_source then return null
71 return location.file.as(not null).filename
72 end
73
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")
79 end
80
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"
87 end
88
89 # Does `self` have a ini file?
90 fun has_ini: Bool do
91 var ini_path = self.ini_path
92 if ini_path == null then return false
93 return ini_path.file_exists
94 end
95
96 # The path to `self` README.md
97 fun readme_path: nullable String do
98 var path = package_path
99 if path == null then return null
100 if not is_expanded then return null
101 return path / "README.md"
102 end
103
104 # Does `self` have a README.md file?
105 fun has_readme: Bool do
106 var readme_path = self.readme_path
107 if readme_path == null then return false
108 return readme_path.file_exists
109 end
110 end
111
112 # A group of modules in a package
113 class MGroup
114 super MConcern
115
116 # The name of the group
117 # empty name for a default group in a single-module package
118 redef var name
119
120 redef var location
121
122 # The enclosing package
123 var mpackage: MPackage
124
125 # The parent group if any
126 # see `in_nesting` for more
127 var parent: nullable MGroup
128
129 # Fully qualified name.
130 # It includes each parent group separated by `>`.
131 # The full_name is terminated by `>` to avoid collision with other entities.
132 #
133 # E.g. `core>` and `core>collection>`
134 redef fun full_name
135 do
136 var p = parent
137 if p == null then return "{name}>"
138 return "{p.full_name}{name}>"
139 end
140
141 # The group is the group tree on the package (`mpackage.mgroups`)
142 # nested groups (children) are smaller
143 # nesting group (see `parent`) is bigger
144 var in_nesting: POSetElement[MGroup] is noinit
145
146 # Is `self` the root of its package?
147 fun is_root: Bool do return mpackage.root == self
148
149 # The filepath (usually a directory) of the group, if any
150 #
151 # safe alias to `location.file.filename`
152 fun filepath: nullable String do
153 var res
154 res = self.location.file
155 if res == null then return null
156 return res.filename
157 end
158
159 init
160 do
161 var tree = mpackage.mgroups
162 self.in_nesting = tree.add_node(self)
163 var parent = self.parent
164 if parent != null then
165 tree.add_edge(self, parent)
166 end
167 end
168
169 redef fun model do return mpackage.model
170
171 redef fun parent_concern do
172 if not is_root then return parent
173 return mpackage
174 end
175
176 redef fun to_s do return name
177 end
178
179 redef class Model
180 # packages of the model
181 var mpackages = new Array[MPackage]
182
183 # Collections of package grouped by their names
184 private var mpackage_by_name = new MultiHashMap[String, MPackage]
185
186 # Return all package named `name`
187 # If such a package is not yet loaded, null is returned (instead of an empty array)
188 fun get_mpackages_by_name(name: String): nullable Array[MPackage]
189 do
190 if mpackage_by_name.has_key(name) then
191 return mpackage_by_name[name]
192 else
193 return null
194 end
195 end
196 end