Skip to content

Commit

Permalink
add --config-file to bootstrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
enricosada committed Sep 14, 2018
1 parent e8ff297 commit c9254be
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/Paket.Bootstrapper/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Xml.Linq;
using System.Text;
using Paket.Bootstrapper.HelperProxies;

Expand All @@ -27,6 +28,7 @@ public static class CommandArgs
public const string Run = "--run";
public const string OutputDir = "--output-dir=";
public const string AsTool = "--as-tool";
public const string ConfigFile = "--config-file=";
}
public static class AppSettingKeys
{
Expand Down Expand Up @@ -85,6 +87,15 @@ public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<st
FillTarget(options.DownloadArguments, magicMode, fileSystem);
}

var configFileArg = commandArgs.SingleOrDefault(x => x.StartsWith(CommandArgs.ConfigFile));
if (configFileArg != null)
{
commandArgs.Remove(configFileArg);
var configFilePath = configFileArg.Substring(CommandArgs.ConfigFile.Length);
var newSettings = ReadSettings(configFilePath);
appSettings = newSettings;
}

// 1 - AppSettings
FillOptionsFromAppSettings(options, appSettings);

Expand Down Expand Up @@ -155,6 +166,43 @@ public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<st
return options;
}

private static NameValueCollection ReadSettings(string configFilePath)
{
var doc = XDocument.Load(configFilePath);

var nv = new NameValueCollection();

var appSettings = doc.Root.Element("appSettings");
if (appSettings == null)
throw new Exception(string.Format("appSettings element not found in file '{0}'", configFilePath));

var dic =
appSettings
.Elements("add")
.Select(x => {
var keyAttr = x.Attribute("key");
var valueAttr = x.Attribute("value");
if (keyAttr == null || valueAttr == null)
return new KeyValuePair<string, string>(); ;

string key = keyAttr.Value;
if (key == null)
return new KeyValuePair<string, string>(); ;

string value = valueAttr.Value;
return new KeyValuePair<string, string>(key, value);
})
.Where(kv => kv.Key != null)
.ToArray();

foreach (var kv in dic)
{
nv.Add(kv.Key, kv.Value);
}

return nv;
}

private static void FillOptionsFromAppSettings(BootstrapperOptions options, NameValueCollection appSettings)
{
if (appSettings.IsTrue(AppSettingKeys.PreferNuget))
Expand Down
1 change: 1 addition & 0 deletions src/Paket.Bootstrapper/Paket.Bootstrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Reference Include="System.Data" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgumentParser.cs" />
Expand Down
19 changes: 17 additions & 2 deletions src/Paket.Bootstrapper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ static void Main(string[] args)

var fileProxy = new FileSystemProxy();

var appSettings =
ConfigurationManager.AppSettings;
var appSettings = ConfigurationManager.AppSettings;

var appConfigInWorkingDir = Path.Combine(Environment.CurrentDirectory, "paket.bootstrapper.exe.config");
if (File.Exists(appConfigInWorkingDir))
{
var exeInWorkingDir = Path.Combine(Environment.CurrentDirectory, "paket.bootstrapper.exe");
var exeConf = ConfigurationManager.OpenExeConfiguration(null);
if (exeConf != null)
{
var nv = new System.Collections.Specialized.NameValueCollection();
foreach (KeyValueConfigurationElement kv in exeConf.AppSettings.Settings)
{
nv.Add(kv.Key, kv.Value);
}
appSettings = nv;
}
}

var optionsBeforeDependenciesFile = ArgumentParser.ParseArgumentsAndConfigurations(args, appSettings,
Environment.GetEnvironmentVariables(), fileProxy, Enumerable.Empty<string>());
Expand Down

0 comments on commit c9254be

Please sign in to comment.