private void DownloadAndPlaceInCache(string latestVersion, string target, string cached, PaketHashFile hashFile) { FileSystemProxy.CreateDirectory(Path.GetDirectoryName(cached)); var tempFile = Path.Combine(FileSystemProxy.GetTempPath(), Guid.NewGuid().ToString()); EffectiveStrategy.DownloadVersion(latestVersion, tempFile, hashFile); if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashFile, latestVersion, tempFile)) { throw new InvalidOperationException( string.Format("paket.exe was corrupted after download by {0}: Invalid hash", EffectiveStrategy.Name)); } ConsoleImpl.WriteTrace("Caching version {0} for later, hash is ok", latestVersion); using (var targetStream = FileSystemProxy.CreateExclusive(target)) using (var cachedStream = FileSystemProxy.CreateExclusive(cached)) { using (var tempStream = FileSystemProxy.OpenRead(tempFile)) { tempStream.CopyTo(targetStream); tempStream.Seek(0, SeekOrigin.Begin); tempStream.CopyTo(cachedStream); } } FileSystemProxy.DeleteFile(tempFile); }
protected override string DownloadHashFileCore(string latestVersion) { if (!EffectiveStrategy.CanDownloadHashFile) { return(null); } var cached = GetHashFilePathInCache(latestVersion); if (!FileSystemProxy.FileExists(cached)) { ConsoleImpl.WriteInfo("Hash file of version {0} not found in cache.", latestVersion); var effectivePath = EffectiveStrategy.DownloadHashFile(latestVersion); if (effectivePath == null) { // 'EffectiveStrategy.CanDownloadHashFile' should have returned false... return(null); } ConsoleImpl.WriteTrace("Copying hash file in cache."); ConsoleImpl.WriteTrace("{0} -> {1}", effectivePath, cached); FileSystemProxy.CreateDirectory(Path.GetDirectoryName(cached)); FileSystemProxy.CopyFile(effectivePath, cached, true); } return(cached); }
protected override void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashFile) { var cached = Path.Combine(PaketCacheDir, latestVersion, "paket.exe"); if (!FileSystemProxy.FileExists(cached)) { ConsoleImpl.WriteInfo("Version {0} not found in cache.", latestVersion); DownloadAndPlaceInCache(latestVersion, target, cached, hashFile); return; } FileSystemProxy.WaitForFileFinished(cached); if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashFile, latestVersion, cached)) { ConsoleImpl.WriteWarning("Version {0} found in cache but it's hashFile isn't valid.", latestVersion); DownloadAndPlaceInCache(latestVersion, target, cached, hashFile); return; } ConsoleImpl.WriteInfo("Copying version {0} from cache.", latestVersion); ConsoleImpl.WriteTrace("{0} -> {1}", cached, target); using (var targetStream = FileSystemProxy.CreateExclusive(target)) using (var cachedStream = FileSystemProxy.OpenRead(cached)) { cachedStream.CopyTo(targetStream); } }
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 string GetLatestVersionCore(bool ignorePrerelease) { var targetVersion = string.Empty; try { targetVersion = fileSystemProxy.GetLocalFileVersion(_target); } catch (FileNotFoundException) { targetVersion = string.Empty; } if (!IsOlderThanMaxFileAge()) { ConsoleImpl.WriteInfo("Don't look for new version, as last version is not older than {0} minutes", _maxFileAgeOfPaketExeInMinutes); return(targetVersion); } ConsoleImpl.WriteTrace("Target file is older than {0} minutes or not found.", _maxFileAgeOfPaketExeInMinutes); var latestVersion = _effectiveStrategy.GetLatestVersion(ignorePrerelease); if (latestVersion == targetVersion) { ConsoleImpl.WriteTrace("Target file version is already the latest version (v{0})", latestVersion); TouchTarget(_target); } return(latestVersion); }
private void DownloadAndPlaceInCache(string latestVersion, string target, string cached, string hashfile) { EffectiveStrategy.DownloadVersion(latestVersion, target, hashfile); ConsoleImpl.WriteTrace("Caching version {0} for later", latestVersion); FileSystemProxy.CreateDirectory(Path.GetDirectoryName(cached)); FileSystemProxy.CopyFile(target, cached); }
protected override PaketHashFile DownloadHashFileCore(string latestVersion) { if (!EffectiveStrategy.CanDownloadHashFile) { return(null); } var cachedPath = GetHashFilePathInCache(latestVersion); if (FileSystemProxy.FileExists(cachedPath)) { // Maybe there's another bootstraper process running // We trust it to close the file with the correct content FileSystemProxy.WaitForFileFinished(cachedPath); ConsoleImpl.WriteInfo("Hash file of version {0} found in cache.", latestVersion); } else { FileSystemProxy.CreateDirectory(Path.GetDirectoryName(cachedPath)); try { ConsoleImpl.WriteInfo("Hash file of version {0} not found in cache.", latestVersion); var hashFile = EffectiveStrategy.DownloadHashFile(latestVersion); Debug.Assert(hashFile != null, "'EffectiveStrategy.CanDownloadHashFile' but DownloadHashFile returned null"); ConsoleImpl.WriteTrace("Writing hashFile file in cache."); ConsoleImpl.WriteTrace("hashFile -> {0}", cachedPath); using (var finalStream = FileSystemProxy.CreateExclusive(cachedPath)) { hashFile.WriteToStream(finalStream); } return(hashFile); } catch (IOException ex) { if (ex.HResult == HelperProxies.FileSystemProxy.HRESULT_ERROR_SHARING_VIOLATION) { ConsoleImpl.WriteTrace("Can't lock hashFile file, another instance might be writing it. Waiting."); // Same as before let's trust other bootstraper processes FileSystemProxy.WaitForFileFinished(cachedPath); } else { throw; } } } return(PaketHashFile.FromStrings(FileSystemProxy.ReadAllLines(cachedPath))); }
private bool IsOlderThanMaxFileAge() { if (_maxFileAgeOfPaketExeInMinutes <= 0) return true; try { var lastModification = fileSystemProxy.GetLastWriteTime(_target); ConsoleImpl.WriteTrace("Target file last modification: {0}", lastModification); return DateTimeProxy.Now > lastModification.AddMinutes(_maxFileAgeOfPaketExeInMinutes); } catch (FileNotFoundException) { return true; } }
protected override void DownloadVersionCore(string latestVersion, string target) { var cached = Path.Combine(_paketCacheDir, latestVersion, "paket.exe"); if (!FileSystemProxy.FileExists(cached)) { ConsoleImpl.WriteInfo("Version {0} not found in cache.", latestVersion); EffectiveStrategy.DownloadVersion(latestVersion, target); ConsoleImpl.WriteTrace("Caching version {0} for later", latestVersion); FileSystemProxy.CreateDirectory(Path.GetDirectoryName(cached)); FileSystemProxy.CopyFile(target, cached); } else { ConsoleImpl.WriteInfo("Copying version {0} from cache.", latestVersion); ConsoleImpl.WriteTrace("{0} -> {1}", cached, target); FileSystemProxy.CopyFile(cached, target, true); } }
private string CreateNugetConfigForBootstrapper(string paketToolNupkgDir) { string path = Path.GetTempFileName(); ConsoleImpl.WriteTrace(string.Format("Create nuget config for dotnet install in '{0}'", path)); ConsoleImpl.WriteTrace(string.Format("Path of local feed '{0}'", paketToolNupkgDir)); var text = new[] { "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "<configuration>", "<packageSources>", " <!--To inherit the global NuGet package sources remove the <clear/> line below -->", " <clear />", string.Format(" <add key=\"download_paket_tool\" value=\"{0}\" />", paketToolNupkgDir), "</packageSources>", "</configuration>" }; File.WriteAllText(path, string.Join(System.Environment.NewLine, text)); return(path); }
private TResult Wrap <TResult>(Func <TResult> func, string actionName) { if (!ConsoleImpl.IsTraceEnabled) { return(func()); } ConsoleImpl.WriteTrace("[{0}] {1}...", Name, actionName); var watch = Stopwatch.StartNew(); try { var result = func(); watch.Stop(); ConsoleImpl.WriteTrace("[{0}] {1} took {2:0.##} second(s) and returned {3}.", Name, actionName, watch.Elapsed.TotalSeconds, result); return(result); } catch (Exception exception) { watch.Stop(); ConsoleImpl.WriteTrace("[{0}] {1} took {2:0.##} second(s) and failed with {3}.", Name, actionName, watch.Elapsed.TotalSeconds, exception.Message); throw; } }
protected override void DownloadVersionCore(string latestVersion, string target, string hashfile) { var cached = Path.Combine(_paketCacheDir, latestVersion, "paket.exe"); if (!FileSystemProxy.FileExists(cached)) { ConsoleImpl.WriteInfo("Version {0} not found in cache.", latestVersion); DownloadAndPlaceInCache(latestVersion, target, cached, hashfile); return; } if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashfile, latestVersion, cached)) { ConsoleImpl.WriteWarning("Version {0} found in cache but it's hash isn't valid.", latestVersion); DownloadAndPlaceInCache(latestVersion, target, cached, hashfile); return; } ConsoleImpl.WriteInfo("Copying version {0} from cache.", latestVersion); ConsoleImpl.WriteTrace("{0} -> {1}", cached, target); FileSystemProxy.CopyFile(cached, target, true); }