]> git.tdb.fi Git - builder.git/blob - source/config.cpp
Big rewrite for a more tool-centric approach
[builder.git] / source / config.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/io/file.h>
5 #include <msp/io/print.h>
6 #include <msp/time/utils.h>
7 #include "builder.h"
8 #include "config.h"
9 #include "sourcepackage.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 Config::Config(SourcePackage &p):
15         package(p),
16         freeze_mtime(false)
17 { }
18
19 void Config::add_option(const string &n, const string &v, const string &d)
20 {
21         options.insert(OptionMap::value_type(n, Option(n, v, d)));
22 }
23
24 const Config::Option &Config::get_option(const string &name) const
25 {
26         return get_item(options, name);
27 }
28
29 bool Config::is_option(const string &name) const
30 {
31         return options.count(name);
32 }
33
34 void Config::select_last_profile()
35 {
36         FS::Path profile_cache_fn = package.get_source()/".profile";
37         if(FS::exists(profile_cache_fn))
38         {
39                 IO::BufferedFile in(profile_cache_fn.str());
40                 string profile;
41                 in.getline(profile);
42                 set_option("profile", profile);
43         }
44
45         freeze_mtime = true;
46         package.get_builder().apply_profile_template(*this, get_option("profile").value);
47         freeze_mtime = false;
48
49         load();
50 }
51
52 void Config::select_profile(const string &profile)
53 {
54         set_option("profile", profile);
55
56         if(!package.get_builder().get_dry_run())
57         {
58                 IO::BufferedFile out((package.get_source()/".profile").str(), IO::M_WRITE);
59                 IO::print(out, "%s\n", profile);
60         }
61
62         freeze_mtime = true;
63         package.get_builder().apply_profile_template(*this, profile);
64         freeze_mtime = false;
65
66         load();
67 }
68
69 bool Config::update(const StringMap &opts)
70 {
71         bool changed = false;
72         for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
73         {
74                 if(set_option(i->first, i->second) && i->first!="profile")
75                         changed = true;
76         }
77
78         if(changed && !freeze_mtime)
79                 mtime = Time::now();
80
81         return changed;
82 }
83
84 void Config::finish()
85 {
86         for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
87                 i->second.value = package.expand_string(i->second.value);
88 }
89
90 void Config::save() const
91 {
92         FS::Path fn = package.get_source()/".options";
93
94         OptionMap::const_iterator i = options.find("profile");
95         if(i!=options.end())
96                 fn = package.get_source()/(".options."+i->second.value);
97
98         IO::BufferedFile out(fn.str(), IO::M_WRITE);
99
100         for(i=options.begin(); i!=options.end(); ++i)
101                 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
102 }
103
104 bool Config::set_option(const string &opt, const string &val)
105 {
106         bool result = false;
107
108         OptionMap::iterator i = options.find(opt);
109         if(i!=options.end())
110         {
111                 if(i->second.value!=val)
112                         result = true;
113                 i->second.value = val;
114         }
115
116         return result;
117 }
118
119 void Config::load()
120 {
121         FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
122
123         FS::Stat stat = FS::stat(fn);
124         if(stat)
125         {
126                 IO::BufferedFile in(fn.str());
127
128                 mtime = stat.get_modify_time();
129
130                 DataFile::Parser parser(in, fn.str());
131                 Loader loader(*this);
132                 loader.load(parser);
133         }
134 }
135
136
137 Config::Option::Option(const string &n, const string &v, const string &d):
138         name(n),
139         defv(v),
140         descr(d),
141         value(v)
142 { }
143
144
145 Config::Loader::Loader(Config &c):
146         conf(c)
147 {
148         add("option", &Loader::option);
149 }
150
151 void Config::Loader::option(const string &n, const string &v)
152 {
153         conf.set_option(n, v);
154 }