diff options
author | Marc-André Lureau <marcandre.lureau@gmail.com> | 2013-01-08 18:42:31 +0100 |
---|---|---|
committer | Marc-André Lureau <marcandre.lureau@gmail.com> | 2013-01-08 18:42:31 +0100 |
commit | 0aa239cc22c9e6085a2d09c9cdc62da6ea54a2ae (patch) | |
tree | 6280c29e364214900c34e1914ef44629327df633 | |
parent | 56ea50cdbee2c0b065364522d6640424107b5c8d (diff) | |
download | msitools-0aa239cc22c9e6085a2d09c9cdc62da6ea54a2ae.tar.gz msitools-0aa239cc22c9e6085a2d09c9cdc62da6ea54a2ae.tar.xz msitools-0aa239cc22c9e6085a2d09c9cdc62da6ea54a2ae.zip |
wixl: add -D argument to define variables from command line
-rw-r--r-- | src/builder.vala | 11 | ||||
-rw-r--r-- | src/preprocessor.vala | 18 | ||||
-rw-r--r-- | src/wixl.vala | 11 |
3 files changed, 36 insertions, 4 deletions
diff --git a/src/builder.vala b/src/builder.vala index d2f5035..473464f 100644 --- a/src/builder.vala +++ b/src/builder.vala @@ -8,6 +8,15 @@ namespace Wixl { WixRoot root; MsiDatabase db; + HashTable<string, string> variables; + + construct { + variables = new HashTable<string, string> (str_hash, str_equal); + } + + public void define_variable (string name, string value) { + variables.insert (name, value); + } List<File> path; public void add_path (string p) { @@ -34,7 +43,7 @@ namespace Wixl { string data; FileUtils.get_contents (file.get_path (), out data); - var p = new Preprocessor (); + var p = new Preprocessor (variables); var doc = p.preprocess (data, file); if (preproc_only) { doc.dump_format (FileStream.fdopen (1, "w")); diff --git a/src/preprocessor.vala b/src/preprocessor.vala index 6953aa5..ff904cb 100644 --- a/src/preprocessor.vala +++ b/src/preprocessor.vala @@ -2,23 +2,35 @@ namespace Wixl { class Preprocessor: Object { + HashTable<string, string> globals; HashTable<string, string> variables; construct { variables = new HashTable<string, string> (str_hash, str_equal); } + public Preprocessor (HashTable<string, string> globals) { + this.globals = globals; + } + public void define_variable (string name, string value) { variables.insert (name, value); } - public string get_variable (string str, File? file) throws GLib.Error { + public string? lookup_variable (string name) { + return variables.lookup (name) ?? globals.lookup (name); + } + + public string eval_variable (string str, File? file) throws GLib.Error { var var = str.split (".", 2); if (var.length != 2) throw new Wixl.Error.FAILED ("invalid variable %s", str); switch (var[0]) { case "var": - return variables.lookup (var[1]); + var val = lookup_variable (var[1]); + if (val == null) + throw new Wixl.Error.FAILED ("Undefined variable %s", var[1]); + return val; case "env": return Environment.get_variable (var[1]); case "sys": @@ -55,7 +67,7 @@ namespace Wixl { var substring = remainder[1:closing]; if (substring.index_of ("(") != -1) throw new Wixl.Error.FIXME ("unsupported function"); - result += get_variable (substring, file); + result += eval_variable (substring, file); end += closing + 1; } } diff --git a/src/wixl.vala b/src/wixl.vala index be2be92..0f9eac5 100644 --- a/src/wixl.vala +++ b/src/wixl.vala @@ -8,11 +8,14 @@ namespace Wixl { static string output; [CCode (array_length = false, array_null_terminated = true)] static string[] files; + [CCode (array_length = false, array_null_terminated = true)] + static string[] defines; private const OptionEntry[] options = { { "version", 0, 0, OptionArg.NONE, ref version, N_("Display version number"), null }, { "verbose", 'v', 0, OptionArg.NONE, ref verbose, N_("Verbose output"), null }, { "output", 'o', 0, OptionArg.FILENAME, ref output, N_("Output file"), null }, + { "define", 'D', 0, OptionArg.STRING_ARRAY, ref defines, N_("Define variable"), null }, { "only-preproc", 'E', 0, OptionArg.NONE, ref preproc, N_("Stop after the preprocessing stage"), null }, { "", 0, 0, OptionArg.FILENAME_ARRAY, ref files, null, N_("INPUT_FILE...") }, { null } @@ -55,6 +58,14 @@ namespace Wixl { try { var builder = new WixBuilder (); + + foreach (var d in defines) { + var def = d.split ("=", 2); + var name = def[0]; + var value = def.length == 2 ? def[1] : "1"; + builder.define_variable (name, value); + } + foreach (var arg in files) { if (verbose) print ("Loading %s...\n", arg); |