1 module mage.config;
2 import mage;
3 import mage.util.properties;
4 
5 __gshared static auto G = Environment("GlobalEnv");
6 static auto C = Environment("GlobalContext");
7 
8 
9 private class GlobalEnvironment
10 {
11   auto globals = Properties("globals");
12   auto defaults = Properties("globalDefaults");
13 }
14 
15 private __gshared static auto _G = new GlobalEnvironment();
16 
17 class MagicContext
18 {
19   Environment*[] envStack;
20 
21   @property ref inout(Environment) activeEnv() inout
22   {
23     assert(!this.envStack.empty, "Magic context has no environment on the stack!");
24     return *envStack[$-1];
25   }
26 
27   void setActive(ref Environment env)
28   {
29     this.envStack ~= &env;
30   }
31 
32   void popActive()
33   {
34     if(!this.envStack.empty) {
35       this.envStack.length -= 1;
36     }
37   }
38 
39   void opIndexAssign(T)(auto ref T value, string key) {
40     this.activeEnv[key] = value;
41   }
42 
43   ref inout(Property) opIndex(string key) inout {
44     return *this.activeEnv.first(key);
45   }
46 }
47 
48 
49 shared static this()
50 {
51   import mage.target : Config;
52   
53   _G.globals["sourceRootPath"] = Path();
54   _G.globals["genRootPath"] = Path();
55   G.env ~= &_G.globals;
56 
57   // Default configurations if targets don't set any.
58   auto dbg = Config("Debug", "x86");
59   dbg["debugSymbols"] = true;
60 
61   auto rel = Config("Release", "x86");
62   
63   _G.defaults["configurations"] = [ dbg, rel ];
64   _G.defaults["language"] = "none";
65   _G.defaults["type"] = "none";
66   G.env ~= &_G.defaults;
67 }
68 
69 unittest
70 {
71   assert(G.first("configurations").get!(Config[])[0].name == "Debug");
72   assert(G.first("configurations").get!(Config[])[0].architecture == "x86");
73   assert(G.first("configurations").get!(Config[])[0]["name"].get!string() == "Debug");
74   assert(G.first("configurations").get!(Config[])[0]["architecture"].get!string() == "x86");
75   assert(G.first("configurations").get!(Config[])[0]["debugSymbols"].get!bool() == true);
76 
77   assert(G.first("configurations").get!(Config[])[1].name == "Release");
78   assert(G.first("configurations").get!(Config[])[1].architecture == "x86");
79   assert(G.first("configurations").get!(Config[])[1]["name"].get!string() == "Release");
80   assert(G.first("configurations").get!(Config[])[1]["architecture"].get!string() == "x86");
81 
82   assert(G.first("language").get!string() == "none");
83   assert(G.first("type").get!string() == "none");
84 }
85 
86 @property Path sourceRootPath()
87 {
88   return G["sourceRootPath"].get!Path();
89 }
90 
91 @property Path genRootPath()
92 {
93   return G["genRootPath"].get!Path();
94 }