1 module config.build; 2 3 import source.context; 4 5 import config.value; 6 7 auto parseAndExtendIfExist(C)(ref C config, Context context, string filename) { 8 import std.file; 9 if (!exists(filename)) { 10 return; 11 } 12 13 return config.parseAndExtend(context, filename); 14 } 15 16 auto parseAndExtend(C)(ref C config, Context context, string filename) { 17 import source.location; 18 auto base = context.registerFile(Location.init, filename, ""); 19 20 import source.jsonlexer; 21 auto lexer = lex(base, context); 22 23 // Workaround for https://issues.dlang.org/show_bug.cgi?id=22482 24 auto cfg = &config; 25 26 import config.jsonparser; 27 return cfg.extends(lexer.parseJSON()); 28 } 29 30 auto buildGlobalConfig(C)(ref C config, string name, Context context) { 31 // SDC's folder. 32 import std.file, std.path; 33 config.parseAndExtendIfExist(context, thisExePath.dirName().buildPath(name)); 34 35 // System wide configuration. 36 config.parseAndExtendIfExist(context, "/etc".buildPath(name)); 37 38 // User wide configuration. 39 import std.process; 40 if (auto home = environment.get("HOME", "")) { 41 config.parseAndExtendIfExist(context, home.buildPath('.' ~ name)); 42 } 43 } 44 45 auto buildLocalConfig(C)(ref C config, string name, Context context, string filename) { 46 // Prepend a dot once and for all. 47 name = '.' ~ name; 48 49 import std.path; 50 filename = filename.absolutePath() ~ name; 51 auto dir = filename; 52 53 string[] files; 54 55 while (true) { 56 import std.file; 57 if (exists(filename)) { 58 files ~= filename; 59 } 60 61 auto parentDir = dir.dirName(); 62 if (parentDir == dir) { 63 // We can't go any further. 64 break; 65 } 66 67 dir = parentDir; 68 filename = dir.buildPath(name); 69 } 70 71 import std.range; 72 foreach (file; files.retro()) { 73 config.parseAndExtend(context, file); 74 } 75 }