1 module dco.readConfigs; 2 3 import std..string; 4 import std.exception; 5 import std.file; 6 import std.stdio; 7 import std.process:spawnProcess,tryWait; 8 9 import dco.globalInfo; 10 11 //ini 12 string configFile ="dco.ini",configFileLocal = "local.ini"; 13 string[string] configKeyValue; 14 15 bool readConfig(string _configFile) 16 { 17 try 18 { 19 string strConfigPath = "",targetType = "",targetName = ""; 20 bool isLocal = (_configFile != configFile); 21 22 if(!isLocal) 23 { 24 strConfigPath = thisExePath(); 25 strConfigPath = strConfigPath[0..strConfigPath.lastIndexOf(separator)].idup; 26 strConfigPath ~= separator ~ _configFile; 27 28 if(!enforce(exists(strConfigPath),"'FireWall' stop to access the '" ~ _configFile ~ "',please stop it.")) 29 { 30 writeln("dco not found " ~ _configFile ~ ", it will help you to create a init " ~ _configFile ~ " file ,but you should input something in it."); 31 initNewConfigFile(); 32 return false; 33 } 34 } 35 else 36 { 37 if("SpecialLib" in configKeyValue) 38 configKeyValue["SpecialLib"] = ""; 39 if("importPath" in configKeyValue) 40 configKeyValue["importPath"] = ""; 41 42 strConfigPath = getcwd() ~ separator ~ _configFile; 43 bGetLocal = exists(strConfigPath); 44 if(!bGetLocal) return true;//not exists,but can work. 45 //{ 46 // initNewConfigFile(); 47 // return false; 48 //} 49 } 50 auto file = File(strConfigPath); 51 scope(failure) file.close(); 52 auto range = file.byLine(); 53 foreach (line; range) 54 { 55 if (!line.init && line[0] != '#' && line[0] != ';' && line.indexOf("=") != -1) 56 { 57 ptrdiff_t i = line.indexOf("="); 58 ptrdiff_t j = line.indexOf(";"); 59 if(j == -1) 60 { 61 configKeyValue[line.strip()[0..i].idup] = line.strip()[i+1..$].idup; 62 } 63 else 64 { 65 configKeyValue[line.strip()[0..i].idup] = line.strip()[i+1..j].idup; 66 } 67 } 68 } 69 70 file.close(); 71 72 strDC = configKeyValue.get("DC",strDC); 73 74 strDCStandardEnvBin = configKeyValue.get("DCStandardEnvBin",strDCStandardEnvBin); 75 SpecialLib = configKeyValue.get("SpecialLib",""); 76 strImport = configKeyValue.get("importPath",""); 77 strLflags = configKeyValue.get("lflags",strConsole); 78 strDflags = configKeyValue.get("dflags",""); 79 strLibs = configKeyValue.get("libs",""); 80 strTargetPath = configKeyValue.get("targetPath",""); 81 strObjPath = configKeyValue.get("objPath",""); 82 83 if(isLocal) 84 { 85 switch(strLflags) 86 { 87 case "console": 88 strLflags = strConsole; 89 break; 90 case "win32": 91 strLflags = strWindows; 92 break; 93 case "win64": 94 strLflags = strWindows64; 95 break; 96 default: 97 break; 98 } 99 strTargetLflags = " " ~ strLflags; 100 101 if(SpecialLib == "dfl" || SpecialLib == "dgui") bUseSpecialLib = true; 102 103 targetType = configKeyValue.get("targetType","default"); 104 targetName = configKeyValue.get("targetName",""); 105 106 switch(targetType) 107 {//targetTypes {exe,lib,staticLib,dynamicLib,sourceLib,none}; 108 case "sourceLib": 109 case "none": 110 return false; 111 112 case "exe": 113 strTargetType ="exe"; 114 break; 115 case "lib": 116 case "staticLib": 117 strTargetTypeSwitch =" -lib"; 118 if(strDC.toLower().indexOf("dmd") != -1) 119 { 120 strTargetType = "lib"; 121 } 122 else if(strDC.toLower().indexOf("ldc") != -1) 123 { 124 strTargetType = "a"; 125 } 126 break; 127 case "dynamicLib": 128 strTargetTypeSwitch =" -shared"; 129 if(strDC.toLower().indexOf("dmd") != -1) 130 { 131 strTargetType = "dll"; 132 } 133 else if(strDC.toLower().indexOf("ldc") != -1) 134 { 135 strTargetType = "so"; 136 } 137 break; 138 default: 139 strTargetType ="exe"; 140 break; 141 } 142 if(targetName !="") 143 { 144 strTargetFileName = getcwd() ~ separator ~ targetName; 145 strTargetName = targetName; 146 if(targetName.indexOf(".") == -1) 147 { 148 strTargetName ~= "." ~ strTargetType; 149 } 150 strOtherArgs ~= " -of" ~ strTargetPath ~ separator ~ strTargetName; 151 bAssignTarget = true; 152 } 153 compileType = configKeyValue.get("compileType",""); 154 if(compileType !="" ) 155 { 156 strOtherArgs ~= " -m" ~ compileType; 157 } 158 159 buildMode = configKeyValue.get("buildMode",strDebugDefault); 160 if(buildMode.indexOf("-") == -1) 161 { 162 buildMode = " -" ~ buildMode; 163 } 164 } 165 return true; 166 } 167 catch(Exception e) 168 { 169 writeln(" Read ini file err,you should input something in ini file.",e.msg); 170 return false; 171 } 172 } 173 174 void initNewConfigFile() 175 { 176 string strConfig = configFileLocal; 177 auto ini = File(strConfig,"w"); 178 scope(failure) ini.close(); 179 ini.writeln("DC=" ~ strDC); 180 ini.writeln("DCStandardEnvBin=" ~ strDCStandardEnvBin); 181 182 if(SpecialLib =="") 183 { 184 ini.writeln(";SpecialLib=" ~ SpecialLib); 185 ini.writeln("SpecialLib="); 186 } 187 else 188 { 189 ini.writeln("SpecialLib=" ~ SpecialLib); 190 } 191 if(strImportDefault == "") 192 { 193 ini.writeln(";importPath=" ); 194 ini.writeln("importPath="); 195 } 196 else 197 { 198 ini.writeln("importPath=" ~ strImportDefault); 199 } 200 ini.writeln(";lflags=console"); 201 ini.writeln("lflags=win32"); 202 ini.writeln(";lflags=win64"); 203 204 ini.writeln(";dflags="); 205 ini.writeln(";libs="); 206 207 ini.writeln(";targetType=exe//lib//staticLib//dynamicLib//sourceLib//none"); 208 ini.writeln("targetType=exe"); 209 ini.writeln(";targetName=;// ;'null is auto'"); 210 ini.writeln("targetName="); 211 ini.writeln(";compileType=;//64//32mscoff"); 212 ini.writeln("compileType="); 213 ini.writeln(";buildMode=debug;//release"); 214 ini.writeln("buildMode=debug"); 215 ini.writeln("targetPath=Debug;// bin\\Debug"); 216 ini.writeln("objPath=Debug;// bin\\Debug"); 217 218 ini.close(); 219 220 auto pid = spawnProcess(["notepad.exe",strConfig]); 221 auto dmd = tryWait(pid); 222 if (dmd.terminated) 223 { 224 if (dmd.status == 0) 225 { 226 writeln("open "~ strConfig ~" succeeded!"); 227 } 228 else 229 { 230 writeln("open "~ strConfig ~" failed"); 231 } 232 } 233 else 234 { 235 writeln("Please add your Args..."); 236 } 237 }