Skip to content

Commit c9254be

Browse files
committed
add --config-file to bootstrapper
1 parent e8ff297 commit c9254be

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

src/Paket.Bootstrapper/ArgumentParser.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Security.Cryptography;
8+
using System.Xml.Linq;
89
using System.Text;
910
using Paket.Bootstrapper.HelperProxies;
1011

@@ -27,6 +28,7 @@ public static class CommandArgs
2728
public const string Run = "--run";
2829
public const string OutputDir = "--output-dir=";
2930
public const string AsTool = "--as-tool";
31+
public const string ConfigFile = "--config-file=";
3032
}
3133
public static class AppSettingKeys
3234
{
@@ -85,6 +87,15 @@ public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<st
8587
FillTarget(options.DownloadArguments, magicMode, fileSystem);
8688
}
8789

90+
var configFileArg = commandArgs.SingleOrDefault(x => x.StartsWith(CommandArgs.ConfigFile));
91+
if (configFileArg != null)
92+
{
93+
commandArgs.Remove(configFileArg);
94+
var configFilePath = configFileArg.Substring(CommandArgs.ConfigFile.Length);
95+
var newSettings = ReadSettings(configFilePath);
96+
appSettings = newSettings;
97+
}
98+
8899
// 1 - AppSettings
89100
FillOptionsFromAppSettings(options, appSettings);
90101

@@ -155,6 +166,43 @@ public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<st
155166
return options;
156167
}
157168

169+
private static NameValueCollection ReadSettings(string configFilePath)
170+
{
171+
var doc = XDocument.Load(configFilePath);
172+
173+
var nv = new NameValueCollection();
174+
175+
var appSettings = doc.Root.Element("appSettings");
176+
if (appSettings == null)
177+
throw new Exception(string.Format("appSettings element not found in file '{0}'", configFilePath));
178+
179+
var dic =
180+
appSettings
181+
.Elements("add")
182+
.Select(x => {
183+
var keyAttr = x.Attribute("key");
184+
var valueAttr = x.Attribute("value");
185+
if (keyAttr == null || valueAttr == null)
186+
return new KeyValuePair<string, string>(); ;
187+
188+
string key = keyAttr.Value;
189+
if (key == null)
190+
return new KeyValuePair<string, string>(); ;
191+
192+
string value = valueAttr.Value;
193+
return new KeyValuePair<string, string>(key, value);
194+
})
195+
.Where(kv => kv.Key != null)
196+
.ToArray();
197+
198+
foreach (var kv in dic)
199+
{
200+
nv.Add(kv.Key, kv.Value);
201+
}
202+
203+
return nv;
204+
}
205+
158206
private static void FillOptionsFromAppSettings(BootstrapperOptions options, NameValueCollection appSettings)
159207
{
160208
if (appSettings.IsTrue(AppSettingKeys.PreferNuget))

src/Paket.Bootstrapper/Paket.Bootstrapper.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Reference Include="System.Data" />
5353
<Reference Include="System.IO.Compression.FileSystem" />
5454
<Reference Include="System.Xml" />
55+
<Reference Include="System.Xml.Linq" />
5556
</ItemGroup>
5657
<ItemGroup>
5758
<Compile Include="ArgumentParser.cs" />

src/Paket.Bootstrapper/Program.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,23 @@ static void Main(string[] args)
3434

3535
var fileProxy = new FileSystemProxy();
3636

37-
var appSettings =
38-
ConfigurationManager.AppSettings;
37+
var appSettings = ConfigurationManager.AppSettings;
38+
39+
var appConfigInWorkingDir = Path.Combine(Environment.CurrentDirectory, "paket.bootstrapper.exe.config");
40+
if (File.Exists(appConfigInWorkingDir))
41+
{
42+
var exeInWorkingDir = Path.Combine(Environment.CurrentDirectory, "paket.bootstrapper.exe");
43+
var exeConf = ConfigurationManager.OpenExeConfiguration(null);
44+
if (exeConf != null)
45+
{
46+
var nv = new System.Collections.Specialized.NameValueCollection();
47+
foreach (KeyValueConfigurationElement kv in exeConf.AppSettings.Settings)
48+
{
49+
nv.Add(kv.Key, kv.Value);
50+
}
51+
appSettings = nv;
52+
}
53+
}
3954

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

0 commit comments

Comments
 (0)