Skip to content
Merged
Next Next commit
Add more bootstrapper tests
Also merge DirectoryProxy and FileProxy to add path methods. The new proxy
is named a FileSystemProxy.
  • Loading branch information
vbfox committed Oct 27, 2016
commit 70b1258f99b62ee90a6c9281bc592f8ca8bbc387
30 changes: 18 additions & 12 deletions src/Paket.Bootstrapper/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Paket.Bootstrapper.HelperProxies;

namespace Paket.Bootstrapper
{
Expand Down Expand Up @@ -37,11 +37,11 @@ public static class EnvArgs
public const string PaketVersionEnv = "PAKET.VERSION";
}

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

var commandArgs = arguments.ToList();
var magicMode = GetIsMagicMode(fileSystemProxy);

ApplyAppSettings(appSettings, options);

Expand All @@ -51,7 +51,7 @@ public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<st
options.Silent = true;
options.Run = true;
options.RunArgs = commandArgs;
EvaluateDownloadOptions(options.DownloadArguments, new string[0], appSettings, envVariables, true);
EvaluateDownloadOptions(options.DownloadArguments, new string[0], appSettings, envVariables, true, fileSystemProxy);
return options;
}

Expand Down Expand Up @@ -83,12 +83,18 @@ public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<st
commandArgs.Remove(CommandArgs.Help);
}

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

options.UnprocessedCommandArgs = commandArgs;
return options;
}

static bool GetIsMagicMode(IFileSystemProxy fileSystemProxy)
{
var fileName = Path.GetFileName(fileSystemProxy.GetExecutingAssemblyPath());
return string.Equals(fileName, "paket.exe", StringComparison.OrdinalIgnoreCase);
}

private static void ApplyAppSettings(NameValueCollection appSettings, BootstrapperOptions options)
{
if (appSettings.IsTrue(AppSettingKeys.PreferNuget))
Expand All @@ -109,18 +115,18 @@ static string GetHash(string input)
}
}

static string GetMagicModeTarget()
private static string GetMagicModeTarget(IFileSystemProxy fileSystemProxy)
{
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
var assemblyLocation = fileSystemProxy.GetExecutingAssemblyPath();
var targetName = $"paket_{GetHash(assemblyLocation)}.exe";

return Path.Combine(Path.GetTempPath(), targetName);
return Path.Combine(fileSystemProxy.GetTempPath(), targetName);
}

private static IEnumerable<string> EvaluateDownloadOptions(DownloadArguments downloadArguments, IEnumerable<string> args, NameValueCollection appSettings, IDictionary envVariables, bool magicMode)
private static IEnumerable<string> EvaluateDownloadOptions(DownloadArguments downloadArguments, IEnumerable<string> args, NameValueCollection appSettings, IDictionary envVariables, bool magicMode, IFileSystemProxy fileSystemProxy)
{
var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var ? GetMagicModeTarget() : Path.Combine(folder, "paket.exe");
var folder = Path.GetDirectoryName(fileSystemProxy.GetExecutingAssemblyPath());
var ? GetMagicModeTarget(fileSystemProxy) : Path.Combine(folder, "paket.exe");
string nugetSource = downloadArguments.NugetSource;

var appSettingsVersion = appSettings.GetKey(AppSettingKeys.PaketVersion);
Expand Down Expand Up @@ -190,7 +196,7 @@ private static IEnumerable<string> EvaluateDownloadOptions(DownloadArguments dow
if (magicMode)
{
if (appSettingsRequestPrerelease || !String.IsNullOrWhiteSpace(appSettingsVersion))
downloadArguments.MaxFileAgeInMinutes = 0;
downloadArguments.MaxFileAgeInMinutes = null;
else
downloadArguments.MaxFileAgeInMinutes = 60 * 12;
}
Expand Down
22 changes: 10 additions & 12 deletions src/Paket.Bootstrapper/DownloadStrategies/CacheDownloadStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@ namespace Paket.Bootstrapper.DownloadStrategies
{
internal class CacheDownloadStrategy : IHaveEffectiveStrategy
{
public string Name { get { return String.Format("{0} - cached", EffectiveStrategy.Name); } }
public string Name => $"{EffectiveStrategy.Name} - cached";
public IDownloadStrategy FallbackStrategy { get; set; }

private readonly string _paketCacheDir =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NuGet", "Cache", "Paket");

public IDownloadStrategy EffectiveStrategy { get; set; }
public IDirectoryProxy DirectoryProxy { get; set; }
public IFileProxy FileProxy { get; set; }
public IFileSystemProxy FileSystemProxy { get; set; }

public CacheDownloadStrategy(IDownloadStrategy effectiveStrategy, IDirectoryProxy directoryProxy, IFileProxy fileProxy)
public CacheDownloadStrategy(IDownloadStrategy effectiveStrategy, IFileSystemProxy fileSystemProxy)
{
if (effectiveStrategy == null)
throw new ArgumentException("CacheDownloadStrategy needs a non-null effective strategy");
if (effectiveStrategy.FallbackStrategy != null)
throw new ArgumentException("CacheDownloadStrategy should not have a fallback strategy");

EffectiveStrategy = effectiveStrategy;
DirectoryProxy = directoryProxy;
FileProxy = fileProxy;
FileSystemProxy = fileSystemProxy;
}


Expand Down Expand Up @@ -55,19 +53,19 @@ public void DownloadVersion(string latestVersion, string target)
{
var cached = Path.Combine(_paketCacheDir, latestVersion, "paket.exe");

if (!FileProxy.Exists(cached))
if (!FileSystemProxy.FileExists(cached))
{
ConsoleImpl.WriteDebug("Version {0} not found in cache.", latestVersion);

EffectiveStrategy.DownloadVersion(latestVersion, target);
DirectoryProxy.CreateDirectory(Path.GetDirectoryName(cached));
FileProxy.Copy(target, cached);
FileSystemProxy.CreateDirectory(Path.GetDirectoryName(cached));
FileSystemProxy.CopyFile(target, cached);
}
else
{
ConsoleImpl.WriteDebug("Copying version {0} from cache.", latestVersion);

FileProxy.Copy(cached, target, true);
FileSystemProxy.CopyFile(cached, target, true);
}
}

Expand All @@ -78,10 +76,10 @@ public void SelfUpdate(string latestVersion)

private string GetLatestVersionInCache(bool ignorePrerelease)
{
DirectoryProxy.CreateDirectory(_paketCacheDir);
FileSystemProxy.CreateDirectory(_paketCacheDir);
var zero = new SemVer();

return DirectoryProxy.GetDirectories(_paketCacheDir)
return FileSystemProxy.GetDirectories(_paketCacheDir)
.Select(Path.GetFileName)
.OrderByDescending(x =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public static class Constants
}

private IWebRequestProxy WebRequestProxy { get; set; }
private IFileProxy FileProxy { get; set; }
private IFileSystemProxy FileSystemProxy { get; set; }
public string Name { get { return "Github"; } }
public IDownloadStrategy FallbackStrategy { get; set; }

public GitHubDownloadStrategy(IWebRequestProxy webRequestProxy, IFileProxy fileProxy)
public GitHubDownloadStrategy(IWebRequestProxy webRequestProxy, IFileSystemProxy fileSystemProxy)
{
WebRequestProxy = webRequestProxy;
FileProxy = fileProxy;
FileSystemProxy = fileSystemProxy;
}

public string GetLatestVersion(bool ignorePrerelease)
Expand Down Expand Up @@ -78,15 +78,15 @@ public void DownloadVersion(string latestVersion, string target)
var tmpFile = BootstrapperHelper.GetTempFile("paket");
WebRequestProxy.DownloadFile(url, tmpFile);

FileProxy.Copy(tmpFile, target, true);
FileProxy.Delete(tmpFile);
FileSystemProxy.CopyFile(tmpFile, target, true);
FileSystemProxy.DeleteFile(tmpFile);
}

public void SelfUpdate(string latestVersion)
{
var executingAssembly = Assembly.GetExecutingAssembly();
string exePath = executingAssembly.Location;
var localVersion = FileProxy.GetLocalFileVersion(exePath);
var localVersion = FileSystemProxy.GetLocalFileVersion(exePath);
if (localVersion.StartsWith(latestVersion))
{
ConsoleImpl.WriteDebug("Bootstrapper is up to date. Nothing to do.");
Expand All @@ -102,14 +102,14 @@ public void SelfUpdate(string latestVersion)

try
{
FileProxy.FileMove(exePath, renamedPath);
FileProxy.FileMove(tmpDownloadPath, exePath);
FileSystemProxy.MoveFile(exePath, renamedPath);
FileSystemProxy.MoveFile(tmpDownloadPath, exePath);
ConsoleImpl.WriteDebug("Self update of bootstrapper was successful.");
}
catch (Exception)
{
ConsoleImpl.WriteDebug("Self update failed. Resetting bootstrapper.");
FileProxy.FileMove(renamedPath, exePath);
FileSystemProxy.MoveFile(renamedPath, exePath);
throw;
}
}
Expand Down
42 changes: 20 additions & 22 deletions src/Paket.Bootstrapper/DownloadStrategies/NugetDownloadStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@ internal string GetSpecificPackageVersion(string version)
}

private IWebRequestProxy WebRequestProxy { get; set; }
private IDirectoryProxy DirectoryProxy { get; set; }
private IFileProxy FileProxy { get; set; }
private IFileSystemProxy FileSystemProxy { get; set; }
private string Folder { get; set; }
private string NugetSource { get; set; }
private const string PaketNugetPackageName = "Paket";
private const string PaketBootstrapperNugetPackageName = "Paket.Bootstrapper";

public NugetDownloadStrategy(IWebRequestProxy webRequestProxy, IDirectoryProxy directoryProxy, IFileProxy fileProxy, string folder, string nugetSource)
public NugetDownloadStrategy(IWebRequestProxy webRequestProxy, IFileSystemProxy fileSystemProxy, string folder, string nugetSource)
{
WebRequestProxy = webRequestProxy;
DirectoryProxy = directoryProxy;
FileProxy = fileProxy;
FileSystemProxy = fileSystemProxy;
Folder = folder;
NugetSource = nugetSource;
}
Expand All @@ -74,10 +72,10 @@ public string Name
public string GetLatestVersion(bool ignorePrerelease)
{
IEnumerable<string> allVersions = null;
if (DirectoryProxy.Exists(NugetSource))
if (FileSystemProxy.DirectoryExists(NugetSource))
{
var paketPrefix = "paket.";
allVersions = DirectoryProxy.
allVersions = FileSystemProxy.
EnumerateFiles(NugetSource, "paket.*.nupkg", SearchOption.TopDirectoryOnly).
Select(x => Path.GetFileNameWithoutExtension(x)).
// If the specified character isn't a digit, then the file
Expand Down Expand Up @@ -119,17 +117,17 @@ public void DownloadVersion(string latestVersion, string target)
}

var randomFullPath = Path.Combine(Folder, Path.GetRandomFileName());
DirectoryProxy.CreateDirectory(randomFullPath);
FileSystemProxy.CreateDirectory(randomFullPath);
var paketPackageFile = Path.Combine(randomFullPath, paketFile);

if (DirectoryProxy.Exists(NugetSource))
if (FileSystemProxy.DirectoryExists(NugetSource))
{
if (String.IsNullOrWhiteSpace(latestVersion)) latestVersion = GetLatestVersion(false);
var sourcePath = Path.Combine(NugetSource, String.Format(paketNupkgFileTemplate, latestVersion));

ConsoleImpl.WriteDebug("Starting download from {0}", sourcePath);

FileProxy.Copy(sourcePath, paketPackageFile);
FileSystemProxy.CopyFile(sourcePath, paketPackageFile);
}
else
{
Expand All @@ -138,16 +136,16 @@ public void DownloadVersion(string latestVersion, string target)
WebRequestProxy.DownloadFile(paketDownloadUrl, paketPackageFile);
}

FileProxy.ExtractToDirectory(paketPackageFile, randomFullPath);
FileSystemProxy.ExtractToDirectory(paketPackageFile, randomFullPath);
var paketSourceFile = Path.Combine(randomFullPath, "tools", "paket.exe");
FileProxy.Copy(paketSourceFile, target, true);
DirectoryProxy.Delete(randomFullPath, true);
FileSystemProxy.CopyFile(paketSourceFile, target, true);
FileSystemProxy.DeleteDirectory(randomFullPath, true);
}

public void SelfUpdate(string latestVersion)
{
string>
var localVersion = FileProxy.GetLocalFileVersion(target);
var localVersion = FileSystemProxy.GetLocalFileVersion(target);
if (localVersion.StartsWith(latestVersion))
{
ConsoleImpl.WriteDebug("Bootstrapper is up to date. Nothing to do.");
Expand All @@ -168,17 +166,17 @@ public void SelfUpdate(string latestVersion)
}

var randomFullPath = Path.Combine(Folder, Path.GetRandomFileName());
DirectoryProxy.CreateDirectory(randomFullPath);
FileSystemProxy.CreateDirectory(randomFullPath);
var paketPackageFile = Path.Combine(randomFullPath, paketFile);

if (DirectoryProxy.Exists(NugetSource))
if (FileSystemProxy.DirectoryExists(NugetSource))
{
if (String.IsNullOrWhiteSpace(latestVersion)) latestVersion = GetLatestVersion(false);
var sourcePath = Path.Combine(NugetSource, String.Format(paketNupkgFileTemplate, latestVersion));

ConsoleImpl.WriteDebug("Starting download from {0}", sourcePath);

FileProxy.Copy(sourcePath, paketPackageFile);
FileSystemProxy.CopyFile(sourcePath, paketPackageFile);
}
else
{
Expand All @@ -187,23 +185,23 @@ public void SelfUpdate(string latestVersion)
WebRequestProxy.DownloadFile(paketDownloadUrl, paketPackageFile);
}

FileProxy.ExtractToDirectory(paketPackageFile, randomFullPath);
FileSystemProxy.ExtractToDirectory(paketPackageFile, randomFullPath);

var paketSourceFile = Path.Combine(randomFullPath, "tools", "paket.bootstrapper.exe");
var renamedPath = BootstrapperHelper.GetTempFile("oldBootstrapper");
try
{
FileProxy.FileMove(target, renamedPath);
FileProxy.FileMove(paketSourceFile, target);
FileSystemProxy.MoveFile(target, renamedPath);
FileSystemProxy.MoveFile(paketSourceFile, target);
ConsoleImpl.WriteDebug("Self update of bootstrapper was successful.");
}
catch (Exception)
{
ConsoleImpl.WriteDebug("Self update failed. Resetting bootstrapper.");
FileProxy.FileMove(renamedPath, target);
FileSystemProxy.MoveFile(renamedPath, target);
throw;
}
DirectoryProxy.Delete(randomFullPath, true);
FileSystemProxy.DeleteDirectory(randomFullPath, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ internal class TemporarilyIgnoreUpdatesDownloadStrategy : IHaveEffectiveStrategy
{
private readonly int _maxFileAgeOfPaketExeInMinutes;
private readonly string _target;
private readonly IFileProxy _fileProxy;
private readonly IFileSystemProxy fileSystemProxy;

public TemporarilyIgnoreUpdatesDownloadStrategy(
IDownloadStrategy effectiveStrategy,
IFileProxy fileProxy,
IFileSystemProxy fileSystemProxy,
string target,
int maxFileAgeOfPaketExeInMinutes)
{
Expand All @@ -24,7 +24,7 @@ public TemporarilyIgnoreUpdatesDownloadStrategy(

_effectiveStrategy = effectiveStrategy;
_maxFileAgeOfPaketExeInMinutes = maxFileAgeOfPaketExeInMinutes;
_fileProxy = fileProxy;
this.fileSystemProxy = fileSystemProxy;

_target = target;
}
Expand All @@ -34,7 +34,7 @@ public string GetLatestVersion (bool ignorePrerelease)
var targetVersion = string.Empty;
try
{
targetVersion = _fileProxy.GetLocalFileVersion(_target);
targetVersion = fileSystemProxy.GetLocalFileVersion(_target);
}
catch (FileNotFoundException)
{
Expand Down Expand Up @@ -88,7 +88,7 @@ private bool IsOlderThanMaxFileAge()

try
{
var lastModification = _fileProxy.GetLastWriteTime(_target);
var lastModification = fileSystemProxy.GetLastWriteTime(_target);

return DateTimeProxy.Now > lastModification.AddMinutes(_maxFileAgeOfPaketExeInMinutes);
}
Expand All @@ -102,7 +102,7 @@ private void TouchTarget(string target)
{
try
{
_fileProxy.Touch(target);
fileSystemProxy.Touch(target);
}
catch (FileNotFoundException)
{
Expand Down
Loading