1 module dlangide.workspace.projectsettings;
2 
3 import dlangui.core.settings;
4 import dlangui.core.i18n;
5 
6 import dlangide.workspace.idesettings;
7 
8 import std..string;
9 import std.array;
10 import std.algorithm : equal;
11 
12 const AVAILABLE_TOOLCHAINS = ["default", "dmd", "ldc", "gdc"];
13 const AVAILABLE_ARCH = ["default", "x86", "x86_64", "arm", "arm64"];
14 
15 /// local settings for project (not supposed to put under source control)
16 class ProjectSettings : SettingsFile {
17 
18     this(string filename) {
19         super(filename);
20     }
21 
22     /// override to do something after loading - e.g. set defaults
23     override void afterLoad() {
24     }
25 
26     override void updateDefaults() {
27         Setting build = buildSettings();
28         build.setStringDef("toolchain", "default");
29         build.setStringDef("arch", "default");
30         build.setBooleanDef("verbose", false);
31         build.setStringDef("dub_additional_params", "");
32         Setting dbg = debugSettings();
33         dbg.setBooleanDef("external_console", true);
34     }
35 
36     @property Setting buildSettings() {
37         Setting res = _setting.objectByPath("build", true);
38         return res;
39     }
40 
41     @property Setting debugSettings() {
42         Setting res = _setting.objectByPath("debug", true);
43         return res;
44     }
45 
46     @property string projectConfiguration() {
47         return buildSettings.getString("configuration", "default");
48     }
49 
50     @property void projectConfiguration(string newConfiguration) {
51         buildSettings.setString("configuration", newConfiguration);
52     }
53 
54     @property bool buildVerbose() {
55         return buildSettings.getBoolean("verbose", false);
56     }
57 
58     string getToolchain(IDESettings idesettings) {
59         string cfg = buildSettings.getString("toolchain");
60         return idesettings.getToolchainCompilerExecutable(cfg);
61     }
62 
63     string getDubAdditionalParams(IDESettings idesettings) {
64         string cfg = buildSettings.getString("toolchain");
65         string globalparams = idesettings.dubAdditionalParams;
66         string globaltoolchainparams = idesettings.getToolchainAdditionalDubParams(cfg);
67         string projectparams = buildSettings.getString("dub_additional_params", "");
68         string verbosity = buildVerbose ? "-v" : null;
69         return joinParams(globalparams, globaltoolchainparams, projectparams, verbosity);
70     }
71 
72     string getArch(IDESettings idesettings) {
73         string cfg = buildSettings.getString("arch");
74         if (cfg.equal("default"))
75             return null;
76         return cfg;
77     }
78 
79     @property bool runInExternalConsole() {
80         return debugSettings.getBoolean("external_console", true);
81     }
82 }
83 
84 /// join parameter lists separating with space
85 string joinParams(string[] params...) pure {
86     char[] res;
87     foreach(param; params) {
88         string s = param.strip;
89         if (!s.empty) {
90             if (!res.empty)
91                 res ~= " ";
92             res ~= s;
93         }
94     }
95     if (res.empty)
96         return null;
97     return res.dup;
98 }