Skip to content

Commit dff687a

Browse files
committed
Merge pull request #1603 from Stift/bootstrapperRefactoring
Refactoring Bootstrapper to introduce better coverage and testing
2 parents b4dcaa5 + dc9e256 commit dff687a

35 files changed

Lines changed: 2248 additions & 656 deletions

‎docs/content/bootstrapper.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Options:
2727

2828
`-f`: Forces the bootstrapper to ignore any cached paket.exe versions and go directly to github.com or nuget.org based on other flags.
2929

30+
`--help`: Shows a help page with all possible options.
31+
3032
Environment Variables:
3133

3234
`PAKET.VERSION`: The requested version can also be set using this environment variable. Above options take precedence over the environment variable
Binary file not shown.

‎paket.dependencies‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ group Test
2424

2525
nuget NUnit.Runners
2626
nuget NUnit ~> 3
27+
nuget Moq
2728
nuget FSCheck
2829
github forki/FsUnit FsUnit.fs

‎paket.lock‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ NUGET
4545
FsCheck (2.3)
4646
FSharp.Core (>= 3.1.2.5)
4747
FSharp.Core (4.0.0.1)
48+
Moq (4.2.1510.2205)
4849
NUnit (3.2)
4950
NUnit.ConsoleRunner (3.2)
5051
NUnit.Extension.NUnitProjectLoader (3.2)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Collections.Specialized;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Reflection;
8+
9+
namespace Paket.Bootstrapper
10+
{
11+
public static class ArgumentParser
12+
{
13+
public static class CommandArgs
14+
{
15+
public const string Help = "--help";
16+
public const string PreferNuget = "--prefer-nuget";
17+
public const string ForceNuget = "--force-nuget";
18+
public const string Prerelease = "prerelease";
19+
public const string NugetSourceArgPrefix = "--nuget-source=";
20+
public const string SelfUpdate = "--self";
21+
public const string Silent = "-s";
22+
public const string IgnoreCache = "-f";
23+
}
24+
public static class AppSettingKeys
25+
{
26+
public const string PreferNugetAppSettingsKey = "PreferNuget";
27+
public const string ForceNugetAppSettingsKey = "ForceNuget";
28+
public const string PaketVersionAppSettingsKey = "PaketVersion";
29+
}
30+
public static class EnvArgs
31+
{
32+
public const string PaketVersionEnv = "PAKET.VERSION";
33+
}
34+
35+
public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<string> arguments, NameValueCollection appSettings, IDictionary envVariables)
36+
{
37+
var options = new BootstrapperOptions();
38+
39+
var commandArgs = arguments.ToList();
40+
41+
if (commandArgs.Contains(CommandArgs.PreferNuget) || appSettings.GetKey(AppSettingKeys.PreferNugetAppSettingsKey).ToLowerSafe() == "true")
42+
{
43+
options.PreferNuget = true;
44+
commandArgs.Remove(CommandArgs.PreferNuget);
45+
}
46+
if (commandArgs.Contains(CommandArgs.ForceNuget) || appSettings.GetKey(AppSettingKeys.ForceNugetAppSettingsKey).ToLowerSafe() == "true")
47+
{
48+
options.ForceNuget = true;
49+
commandArgs.Remove(CommandArgs.ForceNuget);
50+
}
51+
if (commandArgs.Contains(CommandArgs.Silent))
52+
{
53+
options.Silent = true;
54+
commandArgs.Remove(CommandArgs.Silent);
55+
}
56+
if (commandArgs.Contains(CommandArgs.Help))
57+
{
58+
options.ShowHelp = true;
59+
commandArgs.Remove(CommandArgs.Help);
60+
}
61+
62+
commandArgs = EvaluateDownloadOptions(options.DownloadArguments, commandArgs, appSettings, envVariables).ToList();
63+
64+
options.UnprocessedCommandArgs = commandArgs;
65+
return options;
66+
}
67+
68+
private static IEnumerable<string> EvaluateDownloadOptions(DownloadArguments downloadArguments, IEnumerable<string> args, NameValueCollection appSettings, IDictionary envVariables)
69+
{
70+
var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
71+
var target = Path.Combine(folder, "paket.exe");
72+
string nugetSource = null;
73+
74+
var latestVersion = appSettings.GetKey(AppSettingKeys.PaketVersionAppSettingsKey) ?? envVariables.GetKey(EnvArgs.PaketVersionEnv) ?? String.Empty;
75+
var ignorePrerelease = true;
76+
bool doSelfUpdate = false;
77+
var ignoreCache = false;
78+
var commandArgs = args.ToList();
79+
80+
if (commandArgs.Contains(CommandArgs.SelfUpdate))
81+
{
82+
commandArgs.Remove(CommandArgs.SelfUpdate);
83+
doSelfUpdate = true;
84+
}
85+
var nugetSourceArg = commandArgs.SingleOrDefault(x => x.StartsWith(CommandArgs.NugetSourceArgPrefix));
86+
if (nugetSourceArg != null)
87+
{
88+
commandArgs = commandArgs.Where(x => !x.StartsWith(CommandArgs.NugetSourceArgPrefix)).ToList();
89+
nugetSource = nugetSourceArg.Substring(CommandArgs.NugetSourceArgPrefix.Length);
90+
}
91+
if (commandArgs.Contains(CommandArgs.IgnoreCache))
92+
{
93+
commandArgs.Remove(CommandArgs.IgnoreCache);
94+
ignoreCache = true;
95+
}
96+
if (commandArgs.Count >= 1)
97+
{
98+
if (commandArgs[0] == CommandArgs.Prerelease)
99+
{
100+
ignorePrerelease = false;
101+
latestVersion = String.Empty;
102+
commandArgs.Remove(CommandArgs.Prerelease);
103+
}
104+
else
105+
{
106+
latestVersion = commandArgs[0];
107+
commandArgs.Remove(commandArgs[0]);
108+
}
109+
}
110+
111+
downloadArguments.LatestVersion = latestVersion;
112+
downloadArguments.IgnorePrerelease = ignorePrerelease;
113+
downloadArguments.IgnoreCache = ignoreCache;
114+
downloadArguments.NugetSource = nugetSource;
115+
downloadArguments.DoSelfUpdate = doSelfUpdate;
116+
downloadArguments.Target = target;
117+
downloadArguments.Folder = folder;
118+
return commandArgs;
119+
}
120+
121+
private static string GetKey(this NameValueCollection appSettings, string key)
122+
{
123+
if (appSettings != null && appSettings.AllKeys.Any(x => x == key))
124+
return appSettings.Get(key);
125+
return null;
126+
}
127+
128+
private static string GetKey(this IDictionary dictionary, string key)
129+
{
130+
if (dictionary != null && dictionary.Keys.Cast<string>().Any(x => x == key))
131+
return dictionary[key].ToString();
132+
return null;
133+
}
134+
135+
private static string ToLowerSafe(this string value)
136+
{
137+
if (value != null)
138+
return value.ToLower();
139+
return null;
140+
}
141+
}
142+
}

‎src/Paket.Bootstrapper/BootstrapperHelper.cs‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ namespace Paket.Bootstrapper
88
{
99
internal static class BootstrapperHelper
1010
{
11+
public static string HelpText = @"The paket.bootstrapper downloads the latest version of paket.
12+
Usage for paket bootstrapper:
13+
paket.bootstrapper [OPTIONS] [prerelease|<version>]
14+
15+
Options:
16+
--help print this help
17+
--prefer-nuget prefer nuget as download source instead of github
18+
--force-nuget only use nuget as source
19+
--nuget-source=<NUGET_SOURCE> uses <NUGET_SOURCE> to download latest paket.
20+
NUGET_SOURCE can also be a filepath
21+
--self downloads and updates paket.bootstrapper
22+
-f don't use local cache; always downloads
23+
-s silent mode; no output";
1124
const string PaketBootstrapperUserAgent = "Paket.Bootstrapper";
1225

1326
internal static string GetLocalFileVersion(string target)
@@ -27,7 +40,7 @@ internal static string GetLocalFileVersion(string target)
2740
internal static string GetTempFile(string name)
2841
{
2942
var path = Path.GetTempPath();
30-
var fileName = Path.Combine(path, name + System.Diagnostics.Process.GetCurrentProcess().Id.ToString());
43+
var fileName = Path.Combine(path, name + System.Diagnostics.Process.GetCurrentProcess().Id);
3144
if (File.Exists(fileName))
3245
File.Delete(fileName);
3346
return fileName;
@@ -50,24 +63,6 @@ internal static HttpWebRequest PrepareWebRequest(string url)
5063
return request;
5164
}
5265

53-
internal static void WriteConsoleError(string message)
54-
{
55-
WriteConsole(message, ConsoleColor.Red);
56-
}
57-
58-
internal static void WriteConsoleInfo(string message)
59-
{
60-
WriteConsole(message, ConsoleColor.Yellow);
61-
}
62-
63-
private static void WriteConsole(string message, ConsoleColor consoleColor)
64-
{
65-
var oldColor = Console.ForegroundColor;
66-
Console.ForegroundColor = consoleColor;
67-
Console.WriteLine(message);
68-
Console.ForegroundColor = oldColor;
69-
}
70-
7166
internal static IWebProxy GetDefaultWebProxyFor(String url)
7267
{
7368
Uri uri = new Uri(url);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
namespace Paket.Bootstrapper
4+
{
5+
public class BootstrapperOptions
6+
{
7+
public BootstrapperOptions()
8+
{
9+
DownloadArguments = new DownloadArguments();
10+
}
11+
12+
public DownloadArguments DownloadArguments { get; set; }
13+
14+
public bool Silent { get; set; }
15+
public bool ForceNuget { get; set; }
16+
public bool PreferNuget { get; set; }
17+
public bool ShowHelp { get; set; }
18+
public IEnumerable<string> UnprocessedCommandArgs { get; set; }
19+
}
20+
}

‎src/Paket.Bootstrapper/CacheDownloadStrategy.cs‎

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)