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