Changeset 839:4063da6f3edd

Show
Ignore:
Timestamp:
08/21/08 11:51:04 (5 months ago)
Author:
Aziz K?ksal <aziz.koeksal@gmail.com>
Tags:

tip

branch:
default
Message:

Refactored the config file and how it is loaded.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/Settings.d

    r806 r839  
    1010{ 
    1111static: 
     12  /// Path to the data directory. 
     13  string dataDir = "data/"; 
    1214  /// Predefined version identifiers. 
    1315  string[] versionIds; 
  • src/SettingsLoader.d

    r831 r839  
    1717 
    1818import tango.io.FilePath; 
     19import tango.sys.Environment; 
     20import tango.text.Util : substitute; 
    1921 
    2022/// Loads settings from a D module file. 
    21 class SettingsLoader 
     23abstract class SettingsLoader 
    2224{ 
    2325  InfoManager infoMan; /// Collects error messages. 
     
    2729  { 
    2830    this.infoMan = infoMan; 
    29   } 
    30  
    31   static SettingsLoader opCall(InfoManager infoMan) 
    32   { 
    33     return new SettingsLoader(infoMan); 
    3431  } 
    3532 
     
    7269 
    7370  void load() 
    74   { 
    75     scope execPath = new FilePath(GetExecutableFilePath()); 
    76     execPath = new FilePath(execPath.folder()); 
    77  
    78     // Load config.d 
    79     auto filePath = resolvePath(execPath, "config.d"); 
     71  {} 
     72
     73 
     74/// Loads the configuration file of dil. 
     75class ConfigLoader : SettingsLoader 
     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    } 
    80115    mod = new Module(filePath, infoMan); 
    81116    mod.parse(); 
     
    88123    pass1.run(); 
    89124 
    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")) 
    91132      foreach (value; array.values) 
    92         if (auto str = castTo!(StringExpression)(value)) 
    93           GlobalSettings.versionIds ~= str.getString(); 
    94     if (auto val = getValue!(StringExpression)("langfile")) 
    95       GlobalSettings.langFile = val.getString(); 
    96     if (auto array = getValue!(ArrayInitExpression)("import_paths")) 
     133        if (auto val = castTo!(StringExpression)(value)) 
     134          GlobalSettings.versionIds ~= val.getString(); 
     135    if (auto val = getValue!(StringExpression)("LANG_FILE")) 
     136      GlobalSettings.langFile = expandVariables(val.getString()); 
     137    if (auto array = getValue!(ArrayInitExpression)("IMPORT_PATHS")) 
    97138      foreach (value; array.values) 
    98         if (auto str = castTo!(StringExpression)(value)) 
    99           GlobalSettings.importPaths ~= str.getString(); 
    100     if (auto array = getValue!(ArrayInitExpression)("ddoc_files")) 
     139        if (auto val = castTo!(StringExpression)(value)) 
     140          GlobalSettings.importPaths ~= expandVariables(val.getString()); 
     141    if (auto array = getValue!(ArrayInitExpression)("DDOC_FILES")) 
    101142      foreach (value; array.values) 
    102         if (auto str = castTo!(StringExpression)(value)) 
    103           GlobalSettings.ddocFilePaths ~= resolvePath(execPath, str.getString()); 
    104     if (auto val = getValue!(StringExpression)("xml_map")) 
    105       GlobalSettings.xmlMapFile = val.getString(); 
    106     if (auto val = getValue!(StringExpression)("html_map")) 
    107       GlobalSettings.htmlMapFile = val.getString(); 
    108     if (auto val = getValue!(StringExpression)("lexer_error")) 
     143        if (auto val = castTo!(StringExpression)(value)) 
     144          GlobalSettings.ddocFilePaths ~= expandVariables(val.getString()); 
     145    if (auto val = getValue!(StringExpression)("XML_MAP")) 
     146      GlobalSettings.xmlMapFile = expandVariables(val.getString()); 
     147    if (auto val = getValue!(StringExpression)("HTML_MAP")) 
     148      GlobalSettings.htmlMapFile = expandVariables(val.getString()); 
     149    if (auto val = getValue!(StringExpression)("LEXER_ERROR")) 
    109150      GlobalSettings.lexerErrorFormat = val.getString(); 
    110     if (auto val = getValue!(StringExpression)("parser_error")) 
     151    if (auto val = getValue!(StringExpression)("PARSER_ERROR")) 
    111152      GlobalSettings.parserErrorFormat = val.getString(); 
    112     if (auto val = getValue!(StringExpression)("semantic_error")) 
     153    if (auto val = getValue!(StringExpression)("SEMANTIC_ERROR")) 
    113154      GlobalSettings.semanticErrorFormat = val.getString(); 
    114155 
    115156    // Load language file. 
    116     filePath = resolvePath(execPath, GlobalSettings.langFile); 
     157    // TODO: create a separate class for this? 
     158    filePath = expandVariables(GlobalSettings.langFile); 
    117159    mod = new Module(filePath); 
    118160    mod.parse(); 
     
    128170      char[][] messages; 
    129171      foreach (value; array.values) 
    130         if (auto str = castTo!(StringExpression)(value)) 
    131           messages ~= str.getString(); 
     172        if (auto val = castTo!(StringExpression)(value)) 
     173          messages ~= val.getString(); 
    132174      if (messages.length != MID.max+1) 
    133175        error(mod.firstToken, 
     
    139181    if (auto val = getValue!(StringExpression)("lang_code")) 
    140182      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; 
    141208  } 
    142209} 
     
    184251/// if it is relative. 
    185252/// Returns: filePath if it is absolute or execPath + filePath. 
    186 string resolvePath(FilePath execPath, string filePath) 
    187 
    188   if ((new FilePath(filePath)).isAbsolute()) 
     253string resolvePath(string execPath, string filePath) 
     254
     255  scope path = new FilePath(filePath); 
     256  if (path.isAbsolute()) 
    189257    return filePath; 
    190   return execPath.dup.append(filePath).toString(); 
     258  path.set(execPath).append(filePath); 
     259  return path.toString(); 
    191260} 
    192261 
  • src/config.d

    r806 r839  
    11/// The configuration file of dil. 
    22/// 
    3 /// Relative paths are resolved from the directory of the executable. 
     3/// The file is searched for in the following order: 
     4/// $(OL 
     5///   $(LI The environment variable DILCONF.) 
     6///   $(LI The current working directory.) 
     7///   $(LI The directory set in the environment variable HOME.) 
     8///   $(LI The executable's directory.) 
     9/// ) 
     10/// The program will fail with an error msg if the file couldn't be found.$(BR) 
     11/// 
     12/// The following variables are expanded inside strings (only where paths are expected): 
     13/// $(UL 
     14///   $(LI ${DATADIR} -> the data directory of dil (e.g. /home/user/dil/bin/data or C:\dil\bin\data).) 
     15///   $(LI ${HOME} -> the home directory (e.g. /home/name or C:\Documents and Settings\name).) 
     16///   $(LI ${EXECDIR} -> the absolute path to the directory of dil's executable (e.g. /home/name/dil/bin).) 
     17/// ) 
    418module config; 
    519 
     20/// Files needed by dil are located in this directory. 
     21/// 
     22/// A relative path is resolved from the directory of dil's executable. 
     23var DATADIR = "data/"; 
     24 
    625/// Predefined version identifiers. 
    7 var version_ids = ["X86", "linux", "LittleEndian"]; 
     26var VERSION_IDS = ["X86", "linux", "LittleEndian"]; 
    827// "X86_64", "Windows", "Win32", "Win64", "BigEndian" 
    928 
    10 /// Path to the language file. 
    11 var langfile = "lang_en.d"; 
    12  
    1329/// An array of import paths to look for modules. 
    14 var import_paths = []; /// E.g.: ["src/", "import/"] 
     30/// 
     31/// Relative paths are resolved from the current working directory. 
     32var IMPORT_PATHS = []; /// E.g.: ["src/", "import/"] 
    1533 
    1634/// DDoc macro file paths. 
    1735/// 
    18 /// Macro definitions in ddoc_files[n] override the ones in ddoc_files[n-1]. 
    19 var ddoc_files = ["predefined.ddoc"]; /// E.g.: ["src/mymacros.ddoc", "othermacros.ddoc"] 
     36/// Macro definitions in ddoc_files[n] override the ones in ddoc_files[n-1].$(BR) 
     37/// Relative paths are resolved from the current working directory. 
     38var DDOC_FILES = ["${DATADIR}/predefined.ddoc"]; /// E.g.: ["src/mymacros.ddoc", "othermacros.ddoc"] 
    2039 
    21 var xml_map = "xml_map.d"; 
    22 var html_map = "html_map.d"; 
     40/// Path to the language file. 
     41var LANG_FILE = "${DATADIR}/lang_en.d"; 
     42/// Path to the xml map. 
     43var XML_MAP = "${DATADIR}/xml_map.d"; 
     44/// Path to the html map. 
     45var HTML_MAP = "${DATADIR}/html_map.d"; 
    2346 
    2447/// Customizable formats for error messages. 
    2548/// 
    26 /// <ul> 
    27 ///   <li>0: file path to the source text.</li> 
    28 ///   <li>1: line number.</li> 
    29 ///   <li>2: column number.</li> 
    30 ///   <li>3: error message.</li> 
    31 /// </ul> 
    32 var lexer_error = "{0}({1},{2})L: {3}"; 
    33 var parser_error = "{0}({1},{2})P: {3}"; /// ditto 
    34 var semantic_error = "{0}({1},{2})S: {3}"; /// ditto 
     49/// $(UL 
     50///   $(LI 0: file path to the source text.) 
     51///   $(LI 1: line number.) 
     52///   $(LI 2: column number.) 
     53///   $(LI 3: error message.) 
     54/// ) 
     55var LEXER_ERROR = "{0}({1},{2})L: {3}"; 
     56var PARSER_ERROR = "{0}({1},{2})P: {3}"; /// ditto 
     57var SEMANTIC_ERROR = "{0}({1},{2})S: {3}"; /// ditto 
  • src/main.d

    r832 r839  
    4747{ 
    4848  auto infoMan = new InfoManager(); 
    49   SettingsLoader.SettingsLoader(infoMan).load(); 
     49  ConfigLoader(infoMan).load(); 
    5050  if (infoMan.hasInfo) 
    5151    return printErrors(infoMan); 
     
    392392    else if (info.classinfo is Warning.classinfo) 
    393393      errorFormat = "{0}: Warning: {3}"; 
     394    else if (info.classinfo is dil.Information.Error.classinfo) 
     395      errorFormat = "Error: {3}"; 
    394396    else 
    395397      continue;