+/* $Id$
+
+This file is part of builder
+Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
#include <fstream>
-#include <msp/error.h>
+#include <msp/core/error.h>
#include <msp/path/utils.h>
#include <msp/time/utils.h>
+#include "builder.h"
#include "config.h"
+#include "package.h"
using namespace std;
using namespace Msp;
+Config::Config(Package &p):
+ package(p),
+ freeze_mtime(false)
+{ }
+
+/**
+Adds a configuration option.
+
+@param n Option name
+@param v Default value
+@param d Description
+*/
void Config::add_option(const string &n, const string &v, const string &d)
{
options.insert(OptionMap::value_type(n, Option(n, v, d)));
}
+/**
+Gets the given option from the configuration. If the option doesn't exist,
+an Exception is thrown.
+*/
const Config::Option &Config::get_option(const string &name) const
{
OptionMap::const_iterator i=options.find(name);
return i->second;
}
+/**
+Checks whether an option with the given name exists.
+*/
bool Config::is_option(const string &name) const
{
return options.count(name);
}
-bool Config::process(const RawOptionMap &opts)
+/**
+Selects the last profile used. If the profile cache file is not present, the
+default profile is assumed.
+*/
+void Config::select_last_profile()
+{
+ ifstream in((package.get_source()/".profile.cache").str().c_str());
+ if(in)
+ {
+ string profile;
+ getline(in, profile);
+ set_option("profile", profile);
+ }
+
+ freeze_mtime=true;
+ package.get_builder().apply_profile_template(*this, get_option("profile").value);
+ freeze_mtime=false;
+
+ load();
+}
+
+/**
+Selects the given profile. The profile cache file is updated as well, unless
+--dry-run was given.
+*/
+void Config::select_profile(const string &profile)
+{
+ set_option("profile", profile);
+
+ if(!package.get_builder().get_dry_run())
+ {
+ ofstream out((package.get_source()/".profile.cache").str().c_str());
+ if(out)
+ out<<profile<<'\n';
+ }
+
+ freeze_mtime=true;
+ package.get_builder().apply_profile_template(*this, profile);
+ freeze_mtime=false;
+
+ load();
+}
+
+/**
+Processes options from the given raw option map. Nonexistent options are
+ignored. If any options were changed, the mtime of the configuration is updated
+to the current time.
+
+@param opts A map to process options from
+
+@return Whether any option values were changed
+*/
+bool Config::update(const StringMap &opts)
{
bool changed=false;
- for(RawOptionMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
{
- OptionMap::iterator j=options.find(i->first);
- if(j!=options.end())
+ if(set_option(i->first, i->second) && i->first!="profile")
+ changed=true;
+ }
+
+ if(changed && !freeze_mtime)
+ mtime=Time::now();
+
+ return changed;
+}
+
+/**
+Expands any variable references in options.
+*/
+void Config::finish()
+{
+ for(unsigned n=0; n<20; ++n)
+ {
+ bool changed=false;
+ for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
{
- if(i->second!=j->second.value)
+ Option &opt=i->second;
+ unsigned dollar=0;
+ while((dollar=opt.value.find('$', dollar))!=string::npos)
+ {
+ unsigned end;
+ string var;
+ if(opt.value[dollar+1]=='{')
+ {
+ end=opt.value.find('}', dollar+2);
+ if(end==string::npos)
+ throw Exception("Unterminated variable reference");
+ var=opt.value.substr(dollar+2, end-dollar-2);
+ ++end;
+ }
+ else
+ {
+ for(end=dollar+1; (isalnum(opt.value[end]) && opt.value[end]!='_'); ++end);
+ var=opt.value.substr(dollar+1, end-dollar-1);
+ }
+
+ string value;
+ if(is_option(var))
+ value=get_option(var).value;
+ else
+ value=getenv(var.c_str());
+
+ opt.value.replace(dollar, end-dollar, value);
+
+ dollar+=value.size();
changed=true;
- j->second.value=i->second;
+ }
}
+
+ if(!changed)
+ break;
}
+}
- if(changed)
- mtime=Time::now();
+void Config::save() const
+{
+ Path::Path fn=package.get_source()/".options.cache";
- return changed;
+ OptionMap::const_iterator i=options.find("profile");
+ if(i!=options.end())
+ fn=package.get_source()/(".options."+i->second.value+".cache");
+
+ ofstream out(fn.str().c_str());
+ if(!out) return;
+
+ for(i=options.begin(); i!=options.end(); ++i)
+ out<<"option \""<<i->second.name<<"\" \""<<i->second.value<<"\";\n";
}
-void Config::load(const Path::Path &fn)
+bool Config::set_option(const string &opt, const string &val)
{
+ bool result=false;
+
+ OptionMap::iterator i=options.find(opt);
+ if(i!=options.end())
+ {
+ if(i->second.value!=val)
+ result=true;
+ i->second.value=val;
+ }
+
+ return result;
+}
+
+void Config::load()
+{
+ Path::Path fn=package.get_source()/".options.cache";
+
+ OptionMap::iterator i=options.find("profile");
+ if(i!=options.end())
+ fn=package.get_source()/(".options."+i->second.value+".cache");
+
ifstream in(fn.str().c_str());
if(!in) return;
Path::stat(fn, st);
mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
- Parser::Parser parser(in, fn.str());
+ DataFile::Parser parser(in, fn.str());
Loader loader(*this);
loader.load(parser);
}
-void Config::save(const Path::Path &fn) const
-{
- ofstream out(fn.str().c_str());
- if(!out) return;
-
- for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
- out<<"option \""<<i->second.name<<"\" \""<<i->second.value<<"\";\n";
-}
Config::Option::Option(const string &n, const string &v, const string &d):
name(n),
value(v)
{ }
+
Config::Loader::Loader(Config &c):
conf(c)
{
void Config::Loader::option(const string &n, const string &v)
{
- OptionMap::iterator i=conf.options.find(n);
- if(i!=conf.options.end())
- i->second.value=v;
+ conf.set_option(n, v);
}