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