Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Bootstrapper to introduce better coverage and testing #1603

Merged
merged 29 commits into from
Apr 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1d08ebc
cleanup of unused method
Stift Mar 31, 2016
9aec6af
create tests for CacheDownloadStrategy (bootstrapper)
Stift Mar 31, 2016
084a65b
rearranging files for better overview (bootstrapper)
Stift Mar 31, 2016
f488bcb
added tests for download strategy determination
Stift Apr 3, 2016
a3bc96a
move strategy test
Stift Apr 3, 2016
c128284
create tests for GitHubDownloadStrategy and refactored it
Stift Apr 3, 2016
1a26b19
added test for NugetDownloadStrategy
Stift Apr 4, 2016
6c64848
added tests for NugetApiHelper
Stift Apr 4, 2016
653ea62
added test for SemVer
Stift Apr 4, 2016
63133e9
refactor method with wrong responseStream handling
Stift Apr 4, 2016
2771ae9
Merge remote-tracking branch 'fsprojects/master' into bootstrapUpdate…
Stift Apr 7, 2016
fa39b9e
refactored argument parsing and created test for argument parsing
Stift Apr 7, 2016
f65ac2e
added tests for StartPaketBootstrapping
Stift Apr 8, 2016
54e3bd6
create some additional tests for ArgumentParser
Stift Apr 8, 2016
43bf687
removed fallback logic from CachedDownload strategy as this is alread…
Stift Apr 8, 2016
82fb98d
added test on Name of CacheDownloadStrategyTest
Stift Apr 8, 2016
7f9a01c
improve argument parsing
Stift Apr 11, 2016
4ef6965
Merge commit '0a917114f993b3e3b9917a208c768273b42ee87f' into bootstra…
Stift Apr 11, 2016
cbb9683
fix deprecated assert after nunit3 introduction
Stift Apr 11, 2016
19b601f
refactored console logging to use centrally one implementation and al…
Stift Apr 12, 2016
e00d077
Merge commit 'fbeb48803e5084216f7c4c6505d50ad7a5005d67' into bootstra…
Stift Apr 12, 2016
084d31b
fixing non-compiling tests
Stift Apr 12, 2016
57e0354
add help output to bootstrapper
Stift Apr 12, 2016
9dabe0d
fix bug when downloading latest but prerelease is already in directory
Stift Apr 12, 2016
da63dd9
optimize code
Stift Apr 12, 2016
84f421a
remove unused code
Stift Apr 12, 2016
5c821df
add test for non cached strategy resolving
Stift Apr 12, 2016
60cc032
add --help option to documentation
Stift Apr 12, 2016
dc9e256
removed code commented out and missed to remove
Apr 13, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/content/bootstrapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Options:

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

`--help`: Shows a help page with all possible options.

Environment Variables:

`PAKET.VERSION`: The requested version can also be set using this environment variable. Above options take precedence over the environment variable
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ group Test

nuget NUnit.Runners
nuget NUnit ~> 3
nuget Moq
nuget FSCheck
github forki/FsUnit FsUnit.fs
1 change: 1 addition & 0 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ NUGET
FsCheck (2.3)
FSharp.Core (>= 3.1.2.5)
FSharp.Core (4.0.0.1)
Moq (4.2.1510.2205)
NUnit (3.2)
NUnit.ConsoleRunner (3.2)
NUnit.Extension.NUnitProjectLoader (3.2)
Expand Down
142 changes: 142 additions & 0 deletions src/Paket.Bootstrapper/ArgumentParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Paket.Bootstrapper
{
public static class ArgumentParser
{
public static class CommandArgs
{
public const string Help = "--help";
public const string PreferNuget = "--prefer-nuget";
public const string ForceNuget = "--force-nuget";
public const string Prerelease = "prerelease";
public const string NugetSourceArgPrefix = "--nuget-source=";
public const string SelfUpdate = "--self";
public const string Silent = "-s";
public const string IgnoreCache = "-f";
}
public static class AppSettingKeys
{
public const string PreferNugetAppSettingsKey = "PreferNuget";
public const string ForceNugetAppSettingsKey = "ForceNuget";
public const string PaketVersionAppSettingsKey = "PaketVersion";
}
public static class EnvArgs
{
public const string PaketVersionEnv = "PAKET.VERSION";
}

public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<string> arguments, NameValueCollection appSettings, IDictionary envVariables)
{
var options = new BootstrapperOptions();

var commandArgs = arguments.ToList();

if (commandArgs.Contains(CommandArgs.PreferNuget) || appSettings.GetKey(AppSettingKeys.PreferNugetAppSettingsKey).ToLowerSafe() == "true")
{
options.PreferNuget = true;
commandArgs.Remove(CommandArgs.PreferNuget);
}
if (commandArgs.Contains(CommandArgs.ForceNuget) || appSettings.GetKey(AppSettingKeys.ForceNugetAppSettingsKey).ToLowerSafe() == "true")
{
options.ForceNuget = true;
commandArgs.Remove(CommandArgs.ForceNuget);
}
if (commandArgs.Contains(CommandArgs.Silent))
{
options.Silent = true;
commandArgs.Remove(CommandArgs.Silent);
}
if (commandArgs.Contains(CommandArgs.Help))
{
options.ShowHelp = true;
commandArgs.Remove(CommandArgs.Help);
}

commandArgs = EvaluateDownloadOptions(options.DownloadArguments, commandArgs, appSettings, envVariables).ToList();

options.UnprocessedCommandArgs = commandArgs;
return options;
}

private static IEnumerable<string> EvaluateDownloadOptions(DownloadArguments downloadArguments, IEnumerable<string> args, NameValueCollection appSettings, IDictionary envVariables)
{
var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var target = Path.Combine(folder, "paket.exe");
string nugetSource = null;

var latestVersion = appSettings.GetKey(AppSettingKeys.PaketVersionAppSettingsKey) ?? envVariables.GetKey(EnvArgs.PaketVersionEnv) ?? String.Empty;
var ignorePrerelease = true;
bool doSelfUpdate = false;
var ignoreCache = false;
var commandArgs = args.ToList();

if (commandArgs.Contains(CommandArgs.SelfUpdate))
{
commandArgs.Remove(CommandArgs.SelfUpdate);
doSelfUpdate = true;
}
var nugetSourceArg = commandArgs.SingleOrDefault(x => x.StartsWith(CommandArgs.NugetSourceArgPrefix));
if (nugetSourceArg != null)
{
commandArgs = commandArgs.Where(x => !x.StartsWith(CommandArgs.NugetSourceArgPrefix)).ToList();
nugetSource = nugetSourceArg.Substring(CommandArgs.NugetSourceArgPrefix.Length);
}
if (commandArgs.Contains(CommandArgs.IgnoreCache))
{
commandArgs.Remove(CommandArgs.IgnoreCache);
ignoreCache = true;
}
if (commandArgs.Count >= 1)
{
if (commandArgs[0] == CommandArgs.Prerelease)
{
ignorePrerelease = false;
latestVersion = String.Empty;
commandArgs.Remove(CommandArgs.Prerelease);
}
else
{
latestVersion = commandArgs[0];
commandArgs.Remove(commandArgs[0]);
}
}

downloadArguments.LatestVersion = latestVersion;
downloadArguments.IgnorePrerelease = ignorePrerelease;
downloadArguments.IgnoreCache = ignoreCache;
downloadArguments.NugetSource = nugetSource;
downloadArguments.DoSelfUpdate = doSelfUpdate;
downloadArguments.Target = target;
downloadArguments.Folder = folder;
return commandArgs;
}

private static string GetKey(this NameValueCollection appSettings, string key)
{
if (appSettings != null && appSettings.AllKeys.Any(x => x == key))
return appSettings.Get(key);
return null;
}

private static string GetKey(this IDictionary dictionary, string key)
{
if (dictionary != null && dictionary.Keys.Cast<string>().Any(x => x == key))
return dictionary[key].ToString();
return null;
}

private static string ToLowerSafe(this string value)
{
if (value != null)
return value.ToLower();
return null;
}
}
}
33 changes: 14 additions & 19 deletions src/Paket.Bootstrapper/BootstrapperHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ namespace Paket.Bootstrapper
{
internal static class BootstrapperHelper
{
public static string HelpText = @"The paket.bootstrapper downloads the latest version of paket.
Usage for paket bootstrapper:
paket.bootstrapper [OPTIONS] [prerelease|<version>]

Options:
--help print this help
--prefer-nuget prefer nuget as download source instead of github
--force-nuget only use nuget as source
--nuget-source=<NUGET_SOURCE> uses <NUGET_SOURCE> to download latest paket.
NUGET_SOURCE can also be a filepath
--self downloads and updates paket.bootstrapper
-f don't use local cache; always downloads
-s silent mode; no output";
const string PaketBootstrapperUserAgent = "Paket.Bootstrapper";

internal static string GetLocalFileVersion(string target)
Expand All @@ -27,7 +40,7 @@ internal static string GetLocalFileVersion(string target)
internal static string GetTempFile(string name)
{
var path = Path.GetTempPath();
var fileName = Path.Combine(path, name + System.Diagnostics.Process.GetCurrentProcess().Id.ToString());
var fileName = Path.Combine(path, name + System.Diagnostics.Process.GetCurrentProcess().Id);
if (File.Exists(fileName))
File.Delete(fileName);
return fileName;
Expand All @@ -50,24 +63,6 @@ internal static HttpWebRequest PrepareWebRequest(string url)
return request;
}

internal static void WriteConsoleError(string message)
{
WriteConsole(message, ConsoleColor.Red);
}

internal static void WriteConsoleInfo(string message)
{
WriteConsole(message, ConsoleColor.Yellow);
}

private static void WriteConsole(string message, ConsoleColor consoleColor)
{
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = consoleColor;
Console.WriteLine(message);
Console.ForegroundColor = oldColor;
}

internal static IWebProxy GetDefaultWebProxyFor(String url)
{
Uri uri = new Uri(url);
Expand Down
20 changes: 20 additions & 0 deletions src/Paket.Bootstrapper/BootstrapperOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;

namespace Paket.Bootstrapper
{
public class BootstrapperOptions
{
public BootstrapperOptions()
{
DownloadArguments = new DownloadArguments();
}

public DownloadArguments DownloadArguments { get; set; }

public bool Silent { get; set; }
public bool ForceNuget { get; set; }
public bool PreferNuget { get; set; }
public bool ShowHelp { get; set; }
public IEnumerable<string> UnprocessedCommandArgs { get; set; }
}
}
111 changes: 0 additions & 111 deletions src/Paket.Bootstrapper/CacheDownloadStrategy.cs

This file was deleted.

Loading