mpackage: Add `mpackage` importation graph
[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 import graph::digraph
23
24 # A Nit package, that encompass a product
25 class MPackage
26 super MConcern
27
28 # The name of the package
29 redef var name
30
31 redef fun full_name do return name
32
33 redef var c_name = name.to_cmangle is lazy
34
35 # The model of the package
36 redef var model
37
38 redef var location
39
40 # The root of the group tree
41 var root: nullable MGroup = null is writable
42
43 # The group tree, as a POSet
44 var mgroups = new POSet[MGroup]
45
46 redef fun to_s do return name
47
48 init
49 do
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)
54 end
55
56 # MPackage are always roots of the concerns hierarchy
57 redef fun parent_concern do return null
58
59 redef fun mdoc_or_fallback
60 do
61 var mdoc = self.mdoc
62 if mdoc != null then return mdoc
63 var root = self.root
64 if root != null then return root.mdoc_or_fallback
65 return null
66 end
67
68 # Does `self` have a source file?
69 fun has_source: Bool do return location.file != null
70
71 # The path to `self`
72 fun package_path: nullable String do
73 if not has_source then return null
74 return location.file.as(not null).filename
75 end
76
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")
82 end
83
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"
90 end
91
92 # Does `self` have a ini file?
93 fun has_ini: Bool do
94 var ini_path = self.ini_path
95 if ini_path == null then return false
96 return ini_path.file_exists
97 end
98
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"
105 end
106
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
112 end
113 end
114
115 # A group of modules in a package
116 class MGroup
117 super MConcern
118
119 # The name of the group
120 # empty name for a default group in a single-module package
121 redef var name
122
123 redef var location
124
125 # The enclosing package
126 var mpackage: MPackage
127
128 # The parent group if any
129 # see `in_nesting` for more
130 var parent: nullable MGroup
131
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.
135 #
136 # E.g. `core>` and `core>collection>`
137 redef fun full_name
138 do
139 var p = parent
140 if p == null then return "{name}>"
141 return "{p.full_name}{name}>"
142 end
143
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
148
149 # Is `self` the root of its package?
150 fun is_root: Bool do return mpackage.root == self
151
152 # The filepath (usually a directory) of the group, if any
153 #
154 # safe alias to `location.file.filename`
155 fun filepath: nullable String do
156 var res
157 res = self.location.file
158 if res == null then return null
159 return res.filename
160 end
161
162 init
163 do
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)
169 end
170 end
171
172 redef fun model do return mpackage.model
173
174 redef fun parent_concern do
175 if not is_root then return parent
176 return mpackage
177 end
178
179 redef fun to_s do return name
180 end
181
182 redef class Model
183
184 # Full package importation graph
185 # Each package is in relation with itself
186 var mpackage_importation_graph = new HashDigraph[MPackage]
187
188 # packages of the model
189 var mpackages = new Array[MPackage]
190
191 # Collections of package grouped by their names
192 private var mpackage_by_name = new MultiHashMap[String, MPackage]
193
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]
197 do
198 if mpackage_by_name.has_key(name) then
199 return mpackage_by_name[name]
200 else
201 return null
202 end
203 end
204 end