90020093acdbfecfae27991c9d42af2549de06bb
[nit.git] / src / model / mproject.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 project
16 module mproject
17
18 import model_base
19 private import more_collections
20 import poset
21 import mdoc
22
23 # A Nit project, that encompass a product
24 class MProject
25 super MConcern
26
27 # The name of the project
28 redef var name: String
29
30 # The model of the project
31 redef var model: Model
32
33 # The root of the group tree
34 var root: nullable MGroup = null is writable
35
36 # The group tree, as a POSet
37 var mgroups = new POSet[MGroup]
38
39 redef fun to_s do return name
40
41 init
42 do
43 model.mprojects.add(self)
44 model.mproject_by_name.add_one(name, self)
45 end
46
47 # MProject are always roots of the concerns hierarchy
48 redef fun parent_concern do return null
49
50 redef fun mdoc_or_fallback
51 do
52 if mdoc != null then return mdoc
53 return root.mdoc_or_fallback
54 end
55 end
56
57 # A group of modules in a project
58 class MGroup
59 super MConcern
60
61 # The name of the group
62 # empty name for a default group in a single-module project
63 redef var name: String
64
65 # The enclosing project
66 var mproject: MProject
67
68 # The parent group if any
69 # see `in_nesting` for more
70 var parent: nullable MGroup
71
72 # Fully qualified name.
73 # It includes each parent group separated by `/`
74 redef fun full_name
75 do
76 var p = parent
77 if p == null then return name
78 return "{p.full_name}/{name}"
79 end
80
81 # The group is the group tree on the project (`mproject.mgroups`)
82 # nested groups (children) are smaller
83 # nesting group (see `parent`) is bigger
84 var in_nesting: POSetElement[MGroup] is noinit
85
86 # Is `self` the root of its project?
87 fun is_root: Bool do return mproject.root == self
88
89 # The filepath (usually a directory) of the group, if any
90 var filepath: nullable String = null is writable
91
92 init
93 do
94 var tree = mproject.mgroups
95 self.in_nesting = tree.add_node(self)
96 var parent = self.parent
97 if parent != null then
98 tree.add_edge(self, parent)
99 end
100 end
101
102 redef fun model do return mproject.model
103
104 redef fun parent_concern do
105 if not is_root then return parent
106 return mproject
107 end
108
109 redef fun to_s do return name
110 end
111
112 redef class Model
113 # projects of the model
114 var mprojects = new Array[MProject]
115
116 # Collections of project grouped by their names
117 private var mproject_by_name = new MultiHashMap[String, MProject]
118
119 # Return all project named `name`
120 # If such a project is not yet loaded, null is returned (instead of an empty array)
121 fun get_mprojects_by_name(name: String): nullable Array[MProject]
122 do
123 if mproject_by_name.has_key(name) then
124 return mproject_by_name[name]
125 else
126 return null
127 end
128 end
129 end