static void Main(string[] args) { executionWatch.Start(); Console.CancelKeyPress += CancelKeyPressed; var fileProxy = new FileSystemProxy(); var optionsBeforeDependenciesFile = ArgumentParser.ParseArgumentsAndConfigurations(args, ConfigurationManager.AppSettings, Environment.GetEnvironmentVariables(), fileProxy, Enumerable.Empty <string>()); ConsoleImpl.Verbosity = optionsBeforeDependenciesFile.Verbosity; var argumentsFromDependenciesFile = WindowsProcessArguments.Parse( PaketDependencies.GetBootstrapperArgsForFolder(fileProxy)); var options = ArgumentParser.ParseArgumentsAndConfigurations(args, ConfigurationManager.AppSettings, Environment.GetEnvironmentVariables(), fileProxy, argumentsFromDependenciesFile); if (options.ShowHelp) { ConsoleImpl.WriteAlways(BootstrapperHelper.HelpText); return; } ConsoleImpl.Verbosity = options.Verbosity; if (options.UnprocessedCommandArgs.Any()) { ConsoleImpl.WriteWarning("Ignoring the following unknown argument(s): {0}", String.Join(", ", options.UnprocessedCommandArgs)); } var effectiveStrategy = GetEffectiveDownloadStrategy(options.DownloadArguments, options.PreferNuget, options.ForceNuget); ConsoleImpl.WriteTrace("Using strategy: " + effectiveStrategy.Name); StartPaketBootstrapping(effectiveStrategy, options.DownloadArguments, fileProxy, () => OnSuccessfulDownload(options)); }
internal static string GetLocalFileVersion(string target, FileSystemProxy fileSystemProxy) { if (!File.Exists(target)) { ConsoleImpl.WriteTrace("File doesn't exists, no version information: {0}", target); return(""); } try { var bytes = new MemoryStream(); using (var stream = fileSystemProxy.OpenRead(target)) { stream.CopyTo(bytes); } var attr = Assembly.Load(bytes.ToArray()).GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false).Cast <AssemblyInformationalVersionAttribute>().FirstOrDefault(); if (attr == null) { ConsoleImpl.WriteWarning("No assembly version found in {0}", target); return(""); } return(attr.InformationalVersion); } catch (Exception exception) { ConsoleImpl.WriteWarning("Unable to get file version from {0}: {1}", target, exception); return(""); } }
public static bool ValidateHash(IFileSystemProxy fileSystem, PaketHashFile hashFile, string version, string paketFile) { if (hashFile == null) { ConsoleImpl.WriteTrace("No hashFile file expected, bypassing check."); return(true); } var dict = hashFile.Content .Select(i => i.Split(' ')) .ToDictionary(i => i[1], i => i[0]); string expectedHash; if (!dict.TryGetValue("paket.exe", out expectedHash)) { throw new InvalidDataException("Paket hashFile file is corrupted"); } using (var stream = fileSystem.OpenRead(paketFile)) using (var sha = SHA256.Create()) { byte[] checksum = sha.ComputeHash(stream); var hash = BitConverter.ToString(checksum).Replace("-", String.Empty); ConsoleImpl.WriteTrace("Expected hash = {0}", expectedHash); ConsoleImpl.WriteTrace("paket.exe hash = {0} ({1})", hash, paketFile); return(string.Equals(expectedHash, hash, StringComparison.OrdinalIgnoreCase)); } }
public static string GetBootstrapperArgsForFolder(IFileSystemProxy proxy) { try { var folder = proxy.GetCurrentDirectory(); var path = LocateDependenciesFile(proxy, new DirectoryInfo(folder)); if (path == null) { ConsoleImpl.WriteTrace("Dependencies file was not found."); return(null); } using (var fileStream = proxy.OpenRead(path)) { using (var reader = new StreamReader(fileStream)) { return(GetBootstrapperArgs(reader)); } } } catch (Exception e) { // ¯\_(ツ)_/¯ ConsoleImpl.WriteTrace("Error while retrieving arguments from paket.dependencies file: {0}", e); return(null); } }
public static bool ValidateHash(IFileSystemProxy fileSystem, string hashFile, string version, string paketFile) { if (hashFile == null) { ConsoleImpl.WriteTrace("No hash file expected, bypassing check."); return(true); } if (!fileSystem.FileExists(hashFile)) { ConsoleImpl.WriteInfo("No hash file of version {0} found.", version); return(true); } var dict = fileSystem.ReadAllLines(hashFile) .Select(i => i.Split(' ')) .ToDictionary(i => i[1], i => i[0]); string expectedHash; if (!dict.TryGetValue("paket.exe", out expectedHash)) { fileSystem.DeleteFile(hashFile); throw new InvalidDataException("Paket hash file is corrupted"); } using (var stream = fileSystem.OpenRead(paketFile)) using (var sha = SHA256.Create()) { byte[] checksum = sha.ComputeHash(stream); var hash = BitConverter.ToString(checksum).Replace("-", String.Empty); return(string.Equals(expectedHash, hash, StringComparison.OrdinalIgnoreCase)); } }
public static void StartPaketBootstrapping(IDownloadStrategy downloadStrategy, DownloadArguments dlArgs, IFileSystemProxy fileSystemProxy, Action onSuccess) { Action <Exception> handleException = exception => { #if DEBUG Environment.ExitCode = 1; ConsoleImpl.WriteError(String.Format("{0} ({1})", exception.ToString(), downloadStrategy.Name)); return; #else ConsoleImpl.WriteError(String.Format("{0} ({1})", exception.Message, downloadStrategy.Name)); if (!fileSystemProxy.FileExists(dlArgs.Target)) { Environment.ExitCode = 1; } else { fileSystemProxy.WaitForFileFinished(dlArgs.Target); onSuccess(); } #endif }; try { string versionRequested; if (!dlArgs.IgnorePrerelease) { versionRequested = "prerelease requested"; } else if (String.IsNullOrWhiteSpace(dlArgs.LatestVersion)) { versionRequested = "downloading latest stable"; } else { versionRequested = string.Format("version {0} requested", dlArgs.LatestVersion); } ConsoleImpl.WriteInfo("Checking Paket version ({0})...", versionRequested); ConsoleImpl.WriteTrace("Target path is {0}", dlArgs.Target); var localVersion = fileSystemProxy.GetLocalFileVersion(dlArgs.Target); ConsoleImpl.WriteTrace("File in target path version: v{0}", string.IsNullOrEmpty(localVersion) ? "UNKNOWN" : localVersion); var specificVersionRequested = true; var latestVersion = dlArgs.LatestVersion; if (latestVersion == string.Empty) { ConsoleImpl.WriteTrace("No version specified, checking online..."); var getLatestVersionWatch = Stopwatch.StartNew(); latestVersion = downloadStrategy.GetLatestVersion(dlArgs.IgnorePrerelease); getLatestVersionWatch.Stop(); ConsoleImpl.WriteTrace("Latest version check found v{0} in {1:0.##} second(s)", latestVersion, getLatestVersionWatch.Elapsed.TotalSeconds); specificVersionRequested = false; } if (dlArgs.DoSelfUpdate) { ConsoleImpl.WriteInfo("Trying self update"); downloadStrategy.SelfUpdate(latestVersion); } else { var currentSemVer = String.IsNullOrEmpty(localVersion) ? new SemVer() : SemVer.Create(localVersion); if (currentSemVer.PreRelease != null && dlArgs.IgnorePrerelease) { currentSemVer = new SemVer(); } var latestSemVer = SemVer.Create(latestVersion); var comparison = currentSemVer.CompareTo(latestSemVer); if ((comparison > 0 && specificVersionRequested) || comparison < 0) { PaketHashFile hashFile = null; if (downloadStrategy.CanDownloadHashFile) { ConsoleImpl.WriteTrace("Downloading hash for v{0} ...", latestVersion); var downloadHashWatch = Stopwatch.StartNew(); hashFile = downloadStrategy.DownloadHashFile(latestVersion); downloadHashWatch.Stop(); ConsoleImpl.WriteTrace("Hash download took {0:0.##} second(s)", downloadHashWatch.Elapsed.TotalSeconds); } ConsoleImpl.WriteTrace("Downloading v{0} ...", latestVersion); var downloadWatch = Stopwatch.StartNew(); downloadStrategy.DownloadVersion(latestVersion, dlArgs.Target, hashFile); downloadWatch.Stop(); ConsoleImpl.WriteTrace("Download took {0:0.##} second(s)", downloadWatch.Elapsed.TotalSeconds); ConsoleImpl.WriteInfo("Done in {0:0.##} second(s).", executionWatch.Elapsed.TotalSeconds); } else { ConsoleImpl.WriteInfo("Paket.exe {0} is up to date.", localVersion); } } executionWatch.Stop(); ConsoleImpl.WriteTrace("Paket Bootstrapping took {0:0.##} second(s)", executionWatch.Elapsed.TotalSeconds); onSuccess(); } catch (WebException exn) { var shouldHandleException = true; if (!fileSystemProxy.FileExists(dlArgs.Target)) { if (downloadStrategy.FallbackStrategy != null) { var fallbackStrategy = downloadStrategy.FallbackStrategy; ConsoleImpl.WriteInfo("'{0}' download failed. If using Mono, you may need to import trusted certificates using the 'mozroots' tool as none are contained by default. Trying fallback download from '{1}'.", downloadStrategy.Name, fallbackStrategy.Name); StartPaketBootstrapping(fallbackStrategy, dlArgs, fileSystemProxy, onSuccess); shouldHandleException = !fileSystemProxy.FileExists(dlArgs.Target); } } if (shouldHandleException) { handleException(exn); } } catch (Exception exn) { handleException(exn); } }
static void Main(string[] args) { AppContext.SetSwitch("Switch.System.Net.DontEnableSystemDefaultTlsVersions", false); executionWatch.Start(); Console.CancelKeyPress += CancelKeyPressed; IProxyProvider proxyProvider = new DefaultProxyProvider(); 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(), proxyProvider.FileSystemProxy, Enumerable.Empty <string>()); ConsoleImpl.Verbosity = optionsBeforeDependenciesFile.Verbosity; var argumentsFromDependenciesFile = WindowsProcessArguments.Parse( PaketDependencies.GetBootstrapperArgsForFolder(proxyProvider.FileSystemProxy)); var options = ArgumentParser.ParseArgumentsAndConfigurations(args, appSettings, Environment.GetEnvironmentVariables(), proxyProvider.FileSystemProxy, argumentsFromDependenciesFile); if (options.ShowHelp) { ConsoleImpl.WriteAlways(BootstrapperHelper.HelpText); return; } ConsoleImpl.Verbosity = options.Verbosity; if (options.UnprocessedCommandArgs.Any()) { ConsoleImpl.WriteWarning("Ignoring the following unknown argument(s): {0}", String.Join(", ", options.UnprocessedCommandArgs)); } #if PAKET_BOOTSTRAP_NO_CACHE ConsoleImpl.WriteTrace("Force ignore cache, because not implemented."); options.DownloadArguments.IgnoreCache = true; #endif var effectiveStrategy = GetEffectiveDownloadStrategy(options.DownloadArguments, options.PreferNuget, options.ForceNuget, proxyProvider); ConsoleImpl.WriteTrace("Using strategy: " + effectiveStrategy.Name); ConsoleImpl.WriteTrace("Using install kind: " + (options.DownloadArguments.AsTool? "tool": "exe")); StartPaketBootstrapping(effectiveStrategy, options.DownloadArguments, proxyProvider.FileSystemProxy, () => OnSuccessfulDownload(options)); }