Mercurial > projects > dil
comparison src/SettingsLoader.d @ 839:4063da6f3edd default tip
Refactored the config file and how it is loaded.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Thu, 21 Aug 2008 17:51:04 +0200 |
parents | c60bd5cd61da |
children |
comparison
equal
deleted
inserted
replaced
838:1ecf05e680ba | 839:4063da6f3edd |
---|---|
14 import dil.Information; | 14 import dil.Information; |
15 import dil.Compilation; | 15 import dil.Compilation; |
16 import common; | 16 import common; |
17 | 17 |
18 import tango.io.FilePath; | 18 import tango.io.FilePath; |
19 import tango.sys.Environment; | |
20 import tango.text.Util : substitute; | |
19 | 21 |
20 /// Loads settings from a D module file. | 22 /// Loads settings from a D module file. |
21 class SettingsLoader | 23 abstract class SettingsLoader |
22 { | 24 { |
23 InfoManager infoMan; /// Collects error messages. | 25 InfoManager infoMan; /// Collects error messages. |
24 Module mod; /// Current module. | 26 Module mod; /// Current module. |
25 | 27 |
26 this(InfoManager infoMan) | 28 this(InfoManager infoMan) |
27 { | 29 { |
28 this.infoMan = infoMan; | 30 this.infoMan = infoMan; |
29 } | |
30 | |
31 static SettingsLoader opCall(InfoManager infoMan) | |
32 { | |
33 return new SettingsLoader(infoMan); | |
34 } | 31 } |
35 | 32 |
36 /// Creates an error report. | 33 /// Creates an error report. |
37 /// Params: | 34 /// Params: |
38 /// token = where the error occurred. | 35 /// token = where the error occurred. |
69 error(n.begin, "expression is not of type {}", type); | 66 error(n.begin, "expression is not of type {}", type); |
70 return n.Is!(T); | 67 return n.Is!(T); |
71 } | 68 } |
72 | 69 |
73 void load() | 70 void load() |
74 { | 71 {} |
75 scope execPath = new FilePath(GetExecutableFilePath()); | 72 } |
76 execPath = new FilePath(execPath.folder()); | 73 |
77 | 74 /// Loads the configuration file of dil. |
78 // Load config.d | 75 class ConfigLoader : SettingsLoader |
79 auto filePath = resolvePath(execPath, "config.d"); | 76 { |
77 static string configFileName = "config.d"; /// Name of the configuration file. | |
78 string executablePath; /// Absolute path to the executable of dil. | |
79 string executableDir; /// Absolte path to the directory of the executable of dil. | |
80 string dataDir; /// Absolute path to dil's data directory. | |
81 string homePath; /// Path to the home directory. | |
82 | |
83 this(InfoManager infoMan) | |
84 { | |
85 super(infoMan); | |
86 } | |
87 | |
88 static ConfigLoader opCall(InfoManager infoMan) | |
89 { | |
90 return new ConfigLoader(infoMan); | |
91 } | |
92 | |
93 string expandVariables(string val) | |
94 { | |
95 val = substitute(val, "${DATADIR}", dataDir); | |
96 val = substitute(val, "${HOME}", homePath); | |
97 val = substitute(val, "${EXECDIR}", executableDir); | |
98 return val; | |
99 } | |
100 | |
101 void load() | |
102 { | |
103 homePath = Environment.get("HOME"); | |
104 executablePath = GetExecutableFilePath(); | |
105 executableDir = (new FilePath(executablePath)).folder(); | |
106 | |
107 // Load the configuration file. | |
108 auto filePath = findConfigurationFilePath(); | |
109 if (filePath is null) | |
110 { | |
111 infoMan ~= new Error(new Location("",0), | |
112 "the configuration file "~configFileName~" could not be found."); | |
113 return; | |
114 } | |
80 mod = new Module(filePath, infoMan); | 115 mod = new Module(filePath, infoMan); |
81 mod.parse(); | 116 mod.parse(); |
82 | 117 |
83 if (mod.hasErrors) | 118 if (mod.hasErrors) |
84 return; | 119 return; |
85 | 120 |
86 auto context = new CompilationContext; | 121 auto context = new CompilationContext; |
87 auto pass1 = new SemanticPass1(mod, context); | 122 auto pass1 = new SemanticPass1(mod, context); |
88 pass1.run(); | 123 pass1.run(); |
89 | 124 |
90 if (auto array = getValue!(ArrayInitExpression)("version_ids")) | 125 // Initialize the dataDir member. |
126 if (auto val = getValue!(StringExpression)("DATADIR")) | |
127 dataDir = val.getString(); | |
128 dataDir = resolvePath(executableDir, dataDir); | |
129 GlobalSettings.dataDir = dataDir; | |
130 | |
131 if (auto array = getValue!(ArrayInitExpression)("VERSION_IDS")) | |
91 foreach (value; array.values) | 132 foreach (value; array.values) |
92 if (auto str = castTo!(StringExpression)(value)) | 133 if (auto val = castTo!(StringExpression)(value)) |
93 GlobalSettings.versionIds ~= str.getString(); | 134 GlobalSettings.versionIds ~= val.getString(); |
94 if (auto val = getValue!(StringExpression)("langfile")) | 135 if (auto val = getValue!(StringExpression)("LANG_FILE")) |
95 GlobalSettings.langFile = val.getString(); | 136 GlobalSettings.langFile = expandVariables(val.getString()); |
96 if (auto array = getValue!(ArrayInitExpression)("import_paths")) | 137 if (auto array = getValue!(ArrayInitExpression)("IMPORT_PATHS")) |
97 foreach (value; array.values) | 138 foreach (value; array.values) |
98 if (auto str = castTo!(StringExpression)(value)) | 139 if (auto val = castTo!(StringExpression)(value)) |
99 GlobalSettings.importPaths ~= str.getString(); | 140 GlobalSettings.importPaths ~= expandVariables(val.getString()); |
100 if (auto array = getValue!(ArrayInitExpression)("ddoc_files")) | 141 if (auto array = getValue!(ArrayInitExpression)("DDOC_FILES")) |
101 foreach (value; array.values) | 142 foreach (value; array.values) |
102 if (auto str = castTo!(StringExpression)(value)) | 143 if (auto val = castTo!(StringExpression)(value)) |
103 GlobalSettings.ddocFilePaths ~= resolvePath(execPath, str.getString()); | 144 GlobalSettings.ddocFilePaths ~= expandVariables(val.getString()); |
104 if (auto val = getValue!(StringExpression)("xml_map")) | 145 if (auto val = getValue!(StringExpression)("XML_MAP")) |
105 GlobalSettings.xmlMapFile = val.getString(); | 146 GlobalSettings.xmlMapFile = expandVariables(val.getString()); |
106 if (auto val = getValue!(StringExpression)("html_map")) | 147 if (auto val = getValue!(StringExpression)("HTML_MAP")) |
107 GlobalSettings.htmlMapFile = val.getString(); | 148 GlobalSettings.htmlMapFile = expandVariables(val.getString()); |
108 if (auto val = getValue!(StringExpression)("lexer_error")) | 149 if (auto val = getValue!(StringExpression)("LEXER_ERROR")) |
109 GlobalSettings.lexerErrorFormat = val.getString(); | 150 GlobalSettings.lexerErrorFormat = val.getString(); |
110 if (auto val = getValue!(StringExpression)("parser_error")) | 151 if (auto val = getValue!(StringExpression)("PARSER_ERROR")) |
111 GlobalSettings.parserErrorFormat = val.getString(); | 152 GlobalSettings.parserErrorFormat = val.getString(); |
112 if (auto val = getValue!(StringExpression)("semantic_error")) | 153 if (auto val = getValue!(StringExpression)("SEMANTIC_ERROR")) |
113 GlobalSettings.semanticErrorFormat = val.getString(); | 154 GlobalSettings.semanticErrorFormat = val.getString(); |
114 | 155 |
115 // Load language file. | 156 // Load language file. |
116 filePath = resolvePath(execPath, GlobalSettings.langFile); | 157 // TODO: create a separate class for this? |
158 filePath = expandVariables(GlobalSettings.langFile); | |
117 mod = new Module(filePath); | 159 mod = new Module(filePath); |
118 mod.parse(); | 160 mod.parse(); |
119 | 161 |
120 if (mod.hasErrors) | 162 if (mod.hasErrors) |
121 return; | 163 return; |
125 | 167 |
126 if (auto array = getValue!(ArrayInitExpression)("messages")) | 168 if (auto array = getValue!(ArrayInitExpression)("messages")) |
127 { | 169 { |
128 char[][] messages; | 170 char[][] messages; |
129 foreach (value; array.values) | 171 foreach (value; array.values) |
130 if (auto str = castTo!(StringExpression)(value)) | 172 if (auto val = castTo!(StringExpression)(value)) |
131 messages ~= str.getString(); | 173 messages ~= val.getString(); |
132 if (messages.length != MID.max+1) | 174 if (messages.length != MID.max+1) |
133 error(mod.firstToken, | 175 error(mod.firstToken, |
134 "messages table in {} must exactly have {} entries, but not {}.", | 176 "messages table in {} must exactly have {} entries, but not {}.", |
135 filePath, MID.max+1, messages.length); | 177 filePath, MID.max+1, messages.length); |
136 GlobalSettings.messages = messages; | 178 GlobalSettings.messages = messages; |
137 dil.Messages.SetMessages(messages); | 179 dil.Messages.SetMessages(messages); |
138 } | 180 } |
139 if (auto val = getValue!(StringExpression)("lang_code")) | 181 if (auto val = getValue!(StringExpression)("lang_code")) |
140 GlobalSettings.langCode = val.getString(); | 182 GlobalSettings.langCode = val.getString(); |
183 } | |
184 | |
185 /// Searches for the configuration file of dil. | |
186 /// Returns: the filePath or null if the file couldn't be found. | |
187 string findConfigurationFilePath() | |
188 { | |
189 // 1. Look in environment variable DILCONF. | |
190 auto filePath = new FilePath(Environment.get("DILCONF")); | |
191 if (filePath.exists()) | |
192 return filePath.toString(); | |
193 // 2. Look in the current working directory. | |
194 filePath.set(this.configFileName); | |
195 if (filePath.exists()) | |
196 return filePath.toString(); | |
197 // 3. Look in the directory set by HOME. | |
198 filePath.set(this.homePath); | |
199 filePath.append(this.configFileName); | |
200 if (filePath.exists()) | |
201 return filePath.toString(); | |
202 // 4. Look in the binary's directory. | |
203 filePath.set(this.executableDir); | |
204 filePath.append(this.configFileName); | |
205 if (filePath.exists()) | |
206 return filePath.toString(); | |
207 return null; | |
141 } | 208 } |
142 } | 209 } |
143 | 210 |
144 /// Loads an associative array from a D module file. | 211 /// Loads an associative array from a D module file. |
145 class TagMapLoader : SettingsLoader | 212 class TagMapLoader : SettingsLoader |
181 } | 248 } |
182 | 249 |
183 /// Resolves the path to a file from the executable's dir path | 250 /// Resolves the path to a file from the executable's dir path |
184 /// if it is relative. | 251 /// if it is relative. |
185 /// Returns: filePath if it is absolute or execPath + filePath. | 252 /// Returns: filePath if it is absolute or execPath + filePath. |
186 string resolvePath(FilePath execPath, string filePath) | 253 string resolvePath(string execPath, string filePath) |
187 { | 254 { |
188 if ((new FilePath(filePath)).isAbsolute()) | 255 scope path = new FilePath(filePath); |
256 if (path.isAbsolute()) | |
189 return filePath; | 257 return filePath; |
190 return execPath.dup.append(filePath).toString(); | 258 path.set(execPath).append(filePath); |
259 return path.toString(); | |
191 } | 260 } |
192 | 261 |
193 version(DDoc) | 262 version(DDoc) |
194 { | 263 { |
195 /// Returns the fully qualified path to this executable. | 264 /// Returns the fully qualified path to this executable. |