1 /**
2 Copyright 2018 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 **/
22 module dxx.util.config;
23 
24 private import ctini.ctini;
25 
26 private import std.format;
27 private import std.array : appender,join;
28 private import std.stdio;
29 private import std.experimental.logger;
30 private import std.conv;
31 private import std.traits;
32 private import std.compiler;
33 private import std.process : environment;
34 private import std.file : getcwd,thisExePath;
35 private import std.variant;
36 
37 private import core.runtime;
38 
39 private import dxx.packageVersion;
40 private import dxx.sys.constants;
41 private import dxx.util.storage;
42 //private import dxx.util.injector;
43 private import dxx.util.ini;
44 
45 // Compile-time config
46 enum DXXConfig = IniConfig!("dxx.ini");
47 
48 // Runtime config
49 final class AppConfig {
50     static __gshared AppConfig _appconfig;
51 
52     static void setRuntimeDefaults(ref Variant[string] properties) {
53         properties[DXXConfig.keys.packageVersion] = packageVersion;
54         properties[DXXConfig.keys.packageTimestamp] = packageTimestamp;
55         properties[DXXConfig.keys.packageTimestampISO] = packageTimestampISO;
56         properties[DXXConfig.keys.compilerName] = Constants.compilerName;
57         properties[DXXConfig.keys.compilerVersionMajor] = Constants.compilerVersionMajor.to!string;
58         properties[DXXConfig.keys.compilerVersionMinor] = Constants.compilerVersionMinor.to!string;
59         properties[DXXConfig.keys.currentDir] = getcwd;
60         properties[DXXConfig.keys.compileTimestamp] = Constants.compileTimestamp;
61         properties[DXXConfig.keys.appDir] = RTConstants.constants.appDir;
62         //properties[DXXConfig.keys.applicationName] =
63         properties[DXXConfig.keys.commandLine] =  RTConstants.constants.argString;
64     }
65 
66     shared static this() {
67         Variant[string] properties;
68         //sharedLog.info("Config initialising.");
69         //MsgLog.info(DXXConfig.messages.MSG_CONFIG_INIT);
70         File f;
71         auto configFile = environment.get(DXXConfig.envKeys.configFile,
72                 DXXConfig.app.configFile);
73         try {
74             //sharedLog.info("Loading default config file.");
75             //MsgLog.info(MsgText!(DXXConfig.messages.MSG_CONFIG_DEFAULT)(configFile));
76             f = inputConfigFile!(DXXConfig.app)(configFile);
77             //properties = readInjectorProperties(&f);
78         } catch(Exception e) {
79             // Create the default config file.
80             //sharedLog.info("Creating default config file.");
81             //MsgLog.info(MsgText!(DXXConfig.messages.MSG_CONFIG_INIT_DEFAULT));
82             //auto of = outputConfigFile!(DXXConfig.app)(DXXConfig.app.configFile);
83             //of.write(import(DXXConfig.app.configDefaults));
84             //of.flush;
85             //of.close;
86             //f = inputConfigFile!(DXXConfig.app)(DXXConfig.app.configFile);
87             //properties = readInjectorProperties(&f);
88         }
89 
90         iterateValuesF!(DXXConfig.vars)( (string fqn,string k,string v) {
91             properties[k]=v;
92         } );
93 
94         version(Windows) {
95             static if(__traits(compiles,DXXConfig.windows.vars)) {
96                 iterateValuesF!(DXXConfig.windows.vars)( (string fqn,string k,string v) {
97                     properties[k]=v;
98                 } );
99             }
100         }
101 
102         version(Win32) {
103             static if(__traits(compiles,DXXConfig.win32.vars)) {
104                 iterateValuesF!(DXXConfig.win32.vars)( (string fqn,string k,string v) {
105                     properties[k]=v;
106                 } );
107             }
108         }
109 
110         version(Win64) {
111             static if(__traits(compiles,DXXConfig.win64.vars)) {
112                 iterateValuesF!(DXXConfig.win64.vars)( (string fqn,string k,string v) {
113                     properties[k]=v;
114                 } );
115             }
116         }
117 
118         version(Posix) {
119             static if(__traits(compiles,DXXConfig.posix.vars)) {
120                 iterateValuesF!(DXXConfig.posix.vars)( (string fqn,string k,string v) {
121                     properties[k]=v;
122                 } );
123             }
124         }
125 
126         version(Linux) {
127             static if(__traits(compiles,DXXConfig.linux.vars)) {
128                 iterateValuesF!(DXXConfig.linux.vars)( (string fqn,string k,string v) {
129                     properties[k]=v;
130                 } );
131             }
132         }
133 
134         version(MacOS) {
135             static if(__traits(compiles,DXXConfig.macos.vars)) {
136                 iterateValuesF!(DXXConfig.macos.vars)( (string fqn,string k,string v) {
137                     properties[k]=v;
138                 } );
139             }
140         }
141 
142         //setRuntimeDefaults(properties);
143         //registerInjectorProperties(properties);
144 
145         if(_appconfig is null) {
146             _appconfig = new AppConfig;
147         }
148     }
149     auto static get(string s) {
150         return _appconfig.lookup(s);
151     }
152     auto lookup(string s) {
153         //return stringValues.get(s);
154         return "";
155     }
156 }