public void SelfUpdate(string latestVersion) { var executingAssembly = Assembly.GetExecutingAssembly(); string exePath = executingAssembly.Location; var localVersion = FileSystemProxy.GetLocalFileVersion(exePath); if (localVersion.StartsWith(latestVersion)) { ConsoleImpl.WriteDebug("Bootstrapper is up to date. Nothing to do."); return; } var url = String.Format("https://github.com/fsprojects/Paket/releases/download/{0}/paket.bootstrapper.exe", latestVersion); ConsoleImpl.WriteDebug("Starting download of bootstrapper from {0}", url); string renamedPath = BootstrapperHelper.GetTempFile("oldBootstrapper"); string tmpDownloadPath = BootstrapperHelper.GetTempFile("newBootstrapper"); WebRequestProxy.DownloadFile(url, tmpDownloadPath); try { 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."); FileSystemProxy.MoveFile(renamedPath, exePath); throw; } }
protected override void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashfile) { string url = String.Format(Constants.PaketNupkgDownloadUrlTemplate, latestVersion); ConsoleImpl.WriteInfo("Starting download from {0}", url); var tmpFile = BootstrapperHelper.GetTempFile("paketnupkg"); WebRequestProxy.DownloadFile(url, tmpFile); string packageName = Path.GetFileName(url); var randomFullPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); FileSystemProxy.CreateDirectory(randomFullPath); string packagePath = Path.Combine(randomFullPath, packageName); FileSystemProxy.CopyFile(tmpFile, packagePath, true); FileSystemProxy.DeleteFile(tmpFile); var installAsTool = new InstallKind.InstallAsTool(FileSystemProxy); installAsTool.Run(randomFullPath, target, latestVersion); FileSystemProxy.DeleteDirectory(randomFullPath, true); }
public void SelfUpdate() { //arrange var byteArray = Encoding.ASCII.GetBytes("paketExeContent"); var stream = new MemoryStream(byteArray); var tempFileNameNew = BootstrapperHelper.GetTempFile("newBootstrapper"); var tempFileNameOld = BootstrapperHelper.GetTempFile("oldBootstrapper"); mockWebProxy.Setup(x => x.DownloadFile(It.IsAny <string>(), It.IsAny <Stream>(), It.IsAny <int>())) .Callback <string, Stream, int>((url, streamIn, bufferIn) => stream.CopyTo(streamIn)).Verifiable(); var buffer = new byte[byteArray.Length]; mockFileProxy.Setup(x => x.Create(tempFileNameNew)).Returns(new MemoryStream(buffer)); mockFileProxy.Setup(x => x.GetLocalFileVersion(It.IsAny <string>())).Returns("2.52.1"); //act sut.SelfUpdate("2.57.1"); //assert mockWebProxy.Verify(); mockFileProxy.Verify(x => x.FileMove(Assembly.GetAssembly(typeof(GitHubDownloadStrategy)).Location, tempFileNameOld)); mockFileProxy.Verify(x => x.FileMove(tempFileNameNew, Assembly.GetAssembly(typeof(GitHubDownloadStrategy)).Location)); var text = Encoding.ASCII.GetString(buffer); Assert.That(text, Is.EqualTo("paketExeContent")); }
protected override void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashfile) { var url = String.Format(Constants.PaketExeDownloadUrlTemplate, latestVersion); ConsoleImpl.WriteInfo("Starting download from {0}", url); var tmpFile = BootstrapperHelper.GetTempFile("paket"); WebRequestProxy.DownloadFile(url, tmpFile); if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashfile, latestVersion, tmpFile)) { ConsoleImpl.WriteWarning("Hash of downloaded paket.exe is invalid, retrying once"); WebRequestProxy.DownloadFile(url, tmpFile); if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashfile, latestVersion, tmpFile)) { ConsoleImpl.WriteWarning("Hash of downloaded paket.exe still invalid (Using the file anyway)"); } else { ConsoleImpl.WriteTrace("Hash of downloaded file successfully found in {0}", hashfile); } } else { ConsoleImpl.WriteTrace("Hash of downloaded file successfully found in {0}", hashfile); } FileSystemProxy.CopyFile(tmpFile, target, true); FileSystemProxy.DeleteFile(tmpFile); }
protected override void DownloadHashFileCore(string latestVersion) { var url = String.Format(Constants.PaketCheckSumDownloadUrlTemplate, latestVersion); ConsoleImpl.WriteInfo("Starting download from {0}", url); var tmpFile = BootstrapperHelper.GetTempFile("paket-sha256.txt"); WebRequestProxy.DownloadFile(url, tmpFile); }
public void DownloadHashFile() { var expectedPath = BootstrapperHelper.GetTempFile("paket-sha256.txt"); var expectedUrl = string.Format(GitHubDownloadStrategy.Constants.PaketCheckSumDownloadUrlTemplate, "42.0"); var hashFilePath = sut.DownloadHashFile("42.0"); Assert.That(hashFilePath, Is.EqualTo(expectedPath)); mockWebProxy.Verify(x => x.DownloadFile(expectedUrl, expectedPath), Times.Once); }
public void DownloadVersion(string latestVersion, string target) { var url = String.Format(Constants.PaketExeDownloadUrlTemplate, latestVersion); ConsoleImpl.WriteDebug("Starting download from {0}", url); var tmpFile = BootstrapperHelper.GetTempFile("paket"); WebRequestProxy.DownloadFile(url, tmpFile); FileSystemProxy.CopyFile(tmpFile, target, true); FileSystemProxy.DeleteFile(tmpFile); }
public void DownloadVersion_HashFileNotFound() { //arrange var tempFileName = BootstrapperHelper.GetTempFile("paket"); //act sut.DownloadVersion("2.57.1", "paketExeLocation", null); //assert mockWebProxy.Verify(x => x.DownloadFile(It.IsAny <string>(), tempFileName), Times.Once); mockFileProxy.Verify(x => x.CopyFile(tempFileName, "paketExeLocation", true), Times.Once); mockFileProxy.Verify(x => x.DeleteFile(tempFileName), Times.Once); }
public void DownloadVersion() { //arrange var tempFileName = BootstrapperHelper.GetTempFile("paket"); mockWebProxy.Setup(x => x.DownloadFile(It.IsAny <string>(), It.IsAny <string>())).Verifiable(); //act sut.DownloadVersion("2.57.1", "paketExeLocation"); //assert mockWebProxy.Verify(x => x.DownloadFile(It.IsAny <string>(), tempFileName)); mockFileProxy.Verify(x => x.CopyFile(tempFileName, "paketExeLocation", true)); mockFileProxy.Verify(x => x.DeleteFile(tempFileName)); }
public void DownloadVersion(string latestVersion, string target) { var url = String.Format(Constants.PaketExeDownloadUrlTemplate, latestVersion); ConsoleImpl.WriteDebug("Starting download from {0}", url); var tmpFile = BootstrapperHelper.GetTempFile("paket"); using (var fileStream = FileProxy.Create(tmpFile)) { WebRequestProxy.DownloadFile(url, fileStream, HttpBufferSize); } FileProxy.Copy(tmpFile, target, true); FileProxy.Delete(tmpFile); }
public void SelfUpdate() { //arrange var tempFileNameNew = BootstrapperHelper.GetTempFile("newBootstrapper"); var tempFileNameOld = BootstrapperHelper.GetTempFile("oldBootstrapper"); mockWebProxy.Setup(x => x.DownloadFile(It.IsAny <string>(), It.IsAny <string>())).Verifiable(); mockFileProxy.Setup(x => x.GetLocalFileVersion(It.IsAny <string>())).Returns("2.52.1"); //act sut.SelfUpdate("2.57.1"); //assert mockWebProxy.Verify(x => x.DownloadFile(It.IsAny <string>(), tempFileNameNew)); mockFileProxy.Verify(x => x.MoveFile(Assembly.GetAssembly(typeof(GitHubDownloadStrategy)).Location, tempFileNameOld)); mockFileProxy.Verify(x => x.MoveFile(tempFileNameNew, Assembly.GetAssembly(typeof(GitHubDownloadStrategy)).Location)); }
public void DownloadVersion_InvalidHashRetryOnce() { //arrange var content = Guid.NewGuid().ToByteArray(); var sha = new SHA256Managed(); var checksum = sha.ComputeHash(new MemoryStream(content)); var hash = BitConverter.ToString(checksum).Replace("-", String.Empty); mockFileProxy.Setup(x => x.OpenRead(It.IsAny <string>())).Returns(() => new MemoryStream(Guid.NewGuid().ToByteArray())); var tempFileName = BootstrapperHelper.GetTempFile("paket"); //act sut.DownloadVersion("2.57.1", "paketExeLocation", new PaketHashFile(new List <string> { hash + " paket.exe" })); //assert mockWebProxy.Verify(x => x.DownloadFile(It.IsAny <string>(), tempFileName), Times.Exactly(2)); mockFileProxy.Verify(x => x.CopyFile(tempFileName, "paketExeLocation", true), Times.Once); mockFileProxy.Verify(x => x.DeleteFile(tempFileName), Times.Once); }
public void DownloadVersion() { //arrange var byteArray = Encoding.ASCII.GetBytes("paketExeContent"); var stream = new MemoryStream(byteArray); var tempFileName = BootstrapperHelper.GetTempFile("paket"); mockWebProxy.Setup(x => x.DownloadFile(It.IsAny <string>(), It.IsAny <Stream>(), It.IsAny <int>())) .Callback <string, Stream, int>((url, streamIn, bufferIn) => stream.CopyTo(streamIn)).Verifiable(); var buffer = new byte[byteArray.Length]; mockFileProxy.Setup(x => x.Create(tempFileName)).Returns(new MemoryStream(buffer)); //act sut.DownloadVersion("2.57.1", "paketExeLocation"); //assert mockWebProxy.Verify(); mockFileProxy.Verify(x => x.Copy(tempFileName, "paketExeLocation", true)); mockFileProxy.Verify(x => x.Delete(tempFileName)); var text = Encoding.ASCII.GetString(buffer); Assert.That(text, Is.EqualTo("paketExeContent")); }
public void SelfUpdate(string latestVersion) { string target = Assembly.GetExecutingAssembly().Location; var localVersion = FileProxy.GetLocalFileVersion(target); if (localVersion.StartsWith(latestVersion)) { ConsoleImpl.WriteDebug("Bootstrapper is up to date. Nothing to do."); return; } var apiHelper = new NugetApiHelper(PaketBootstrapperNugetPackageName, NugetSource); const string paketNupkgFile = "paket.bootstrapper.latest.nupkg"; const string paketNupkgFileTemplate = "paket.bootstrapper.{0}.nupkg"; var getLatestFromNugetUrl = apiHelper.GetLatestPackage(); var paketDownloadUrl = getLatestFromNugetUrl; var paketFile = paketNupkgFile; if (!String.IsNullOrWhiteSpace(latestVersion)) { paketDownloadUrl = apiHelper.GetSpecificPackageVersion(latestVersion); paketFile = String.Format(paketNupkgFileTemplate, latestVersion); } var randomFullPath = Path.Combine(Folder, Path.GetRandomFileName()); DirectoryProxy.CreateDirectory(randomFullPath); var paketPackageFile = Path.Combine(randomFullPath, paketFile); if (DirectoryProxy.Exists(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); } else { ConsoleImpl.WriteDebug("Starting download from {0}", paketDownloadUrl); WebRequestProxy.DownloadFile(paketDownloadUrl, paketPackageFile); } FileProxy.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); ConsoleImpl.WriteDebug("Self update of bootstrapper was successful."); } catch (Exception) { ConsoleImpl.WriteDebug("Self update failed. Resetting bootstrapper."); FileProxy.FileMove(renamedPath, target); throw; } DirectoryProxy.Delete(randomFullPath, true); }