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; 40 private import dxx.util.storage; 41 private import dxx.util.injector; 42 private import dxx.util.ini; 43 44 // Compile-time config 45 enum DXXConfig = IniConfig!("dxx.ini"); 46 47 // Runtime config 48 final class AppConfig { 49 static __gshared AppConfig _appconfig; 50 51 static void setRuntimeDefaults(ref Variant[string] properties) { 52 properties[DXXConfig.keys.packageVersion] = packageVersion; 53 properties[DXXConfig.keys.packageTimestamp] = packageTimestamp; 54 properties[DXXConfig.keys.packageTimestampISO] = packageTimestampISO; 55 properties[DXXConfig.keys.compilerName] = Constants.compilerName; 56 properties[DXXConfig.keys.compilerVersionMajor] = Constants.compilerVersionMajor.to!string; 57 properties[DXXConfig.keys.compilerVersionMinor] = Constants.compilerVersionMinor.to!string; 58 properties[DXXConfig.keys.currentDir] = getcwd; 59 properties[DXXConfig.keys.compileTimestamp] = Constants.compileTimestamp; 60 properties[DXXConfig.keys.appDir] = thisExePath; 61 //properties[DXXConfig.keys.applicationName] = 62 properties[DXXConfig.keys.commandLine] = Runtime.args.join(" "); 63 } 64 65 shared static this() { 66 Variant[string] properties; 67 //sharedLog.info("Config initialising."); 68 //MsgLog.info(DXXConfig.messages.MSG_CONFIG_INIT); 69 File f; 70 auto configFile = environment.get(DXXConfig.envKeys.configFile, 71 DXXConfig.app.configFile); 72 try { 73 //sharedLog.info("Loading default config file."); 74 //MsgLog.info(MsgText!(DXXConfig.messages.MSG_CONFIG_DEFAULT)(configFile)); 75 f = inputConfigFile!(DXXConfig.app)(configFile); 76 properties = readInjectorProperties(&f); 77 } catch(Exception e) { 78 // Create the default config file. 79 //sharedLog.info("Creating default config file."); 80 //MsgLog.info(MsgText!(DXXConfig.messages.MSG_CONFIG_INIT_DEFAULT)); 81 //auto of = outputConfigFile!(DXXConfig.app)(DXXConfig.app.configFile); 82 //of.write(import(DXXConfig.app.configDefaults)); 83 //of.flush; 84 //of.close; 85 //f = inputConfigFile!(DXXConfig.app)(DXXConfig.app.configFile); 86 //properties = readInjectorProperties(&f); 87 } 88 89 iterateValuesF!(DXXConfig.vars)( (string fqn,string k,string v) { 90 properties[k]=v; 91 } ); 92 93 version(Windows) { 94 static if(__traits(compiles,DXXConfig.windows.vars)) { 95 iterateValuesF!(DXXConfig.windows.vars)( (string fqn,string k,string v) { 96 properties[k]=v; 97 } ); 98 } 99 } 100 101 version(Win32) { 102 static if(__traits(compiles,DXXConfig.win32.vars)) { 103 iterateValuesF!(DXXConfig.win32.vars)( (string fqn,string k,string v) { 104 properties[k]=v; 105 } ); 106 } 107 } 108 109 version(Win64) { 110 static if(__traits(compiles,DXXConfig.win64.vars)) { 111 iterateValuesF!(DXXConfig.win64.vars)( (string fqn,string k,string v) { 112 properties[k]=v; 113 } ); 114 } 115 } 116 117 version(Posix) { 118 static if(__traits(compiles,DXXConfig.posix.vars)) { 119 iterateValuesF!(DXXConfig.posix.vars)( (string fqn,string k,string v) { 120 properties[k]=v; 121 } ); 122 } 123 } 124 125 version(Linux) { 126 static if(__traits(compiles,DXXConfig.linux.vars)) { 127 iterateValuesF!(DXXConfig.linux.vars)( (string fqn,string k,string v) { 128 properties[k]=v; 129 } ); 130 } 131 } 132 133 version(MacOS) { 134 static if(__traits(compiles,DXXConfig.macos.vars)) { 135 iterateValuesF!(DXXConfig.macos.vars)( (string fqn,string k,string v) { 136 properties[k]=v; 137 } ); 138 } 139 } 140 141 setRuntimeDefaults(properties); 142 registerInjectorProperties(properties); 143 144 if(_appconfig is null) { 145 _appconfig = new AppConfig; 146 } 147 } 148 auto static get(string s) { 149 return _appconfig.lookup(s); 150 } 151 auto lookup(string s) { 152 //return stringValues.get(s); 153 return ""; 154 } 155 }