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);
                }
        }
Esempio n. 2
0
        private bool ValidateHash(string version, string paketFile)
        {
            var hashFile = Path.Combine(_paketCacheDir, version, "paket-sha256.txt");

            if (!FileSystemProxy.FileExists(hashFile))
            {
                ConsoleImpl.WriteInfo("No hash file of version {0} found in cache.", version);

                return(true);
            }

            var dict = FileSystemProxy.ReadAllLines(hashFile)
                       .Select(i => i.Split(' '))
                       .ToDictionary(i => i[1], i => i[0]);

            string expectedHash;

            if (!dict.TryGetValue("paket.exe", out expectedHash))
            {
                FileSystemProxy.DeleteFile(hashFile);

                throw new InvalidDataException("Paket hash file is corrupted");
            }

            using (var stream = FileSystemProxy.OpenRead(paketFile))
                using (var sha = new SHA256Managed())
                {
                    byte[] checksum = sha.ComputeHash(stream);
                    var    hash     = BitConverter.ToString(checksum).Replace("-", String.Empty);

                    return(string.Equals(expectedHash, hash, StringComparison.OrdinalIgnoreCase));
                }
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        protected override void SelfUpdateCore(string latestVersion)
        {
            var    executingAssembly = Assembly.GetExecutingAssembly();
            string exePath           = executingAssembly.Location;
            var    localVersion      = FileSystemProxy.GetLocalFileVersion(exePath);

            if (localVersion.StartsWith(latestVersion))
            {
                ConsoleImpl.WriteInfo("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.WriteInfo("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.WriteInfo("Self update of bootstrapper was successful.");
            }
            catch (Exception)
            {
                ConsoleImpl.WriteInfo("Self update failed. Resetting bootstrapper.");
                FileSystemProxy.MoveFile(renamedPath, exePath);
                throw;
            }
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        protected override PaketHashFile DownloadHashFileCore(string latestVersion)
        {
            var url = string.Format(Constants.PaketCheckSumDownloadUrlTemplate, latestVersion);

            ConsoleImpl.WriteInfo("Starting download from {0}", url);

            var content = WebRequestProxy.DownloadString(url);

            return(PaketHashFile.FromString(content));
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        protected override void DownloadHashFileCore(string latestVersion)
        {
            var cached = Path.Combine(_paketCacheDir, latestVersion, "paket-sha256.txt");

            if (!FileSystemProxy.FileExists(cached))
            {
                ConsoleImpl.WriteInfo("Hash file of version {0} not found in cache.", latestVersion);

                EffectiveStrategy.DownloadHashFile(latestVersion);
            }
        }
        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)));
        }
Esempio n. 12
0
        protected override void DownloadVersionCore(string latestVersion, string target)
        {
            var url = String.Format(Constants.PaketExeDownloadUrlTemplate, latestVersion);

            ConsoleImpl.WriteInfo("Starting download from {0}", url);

            var tmpFile = BootstrapperHelper.GetTempFile("paket");

            WebRequestProxy.DownloadFile(url, tmpFile);

            FileSystemProxy.CopyFile(tmpFile, target, true);
            FileSystemProxy.DeleteFile(tmpFile);
        }
Esempio n. 13
0
 private void TouchTarget(string target)
 {
     try
     {
         fileSystemProxy.Touch(target);
     }
     catch (FileNotFoundException)
     {
         ConsoleImpl.WriteInfo("Could not update the timestamps. File {0} not found!", _target);
     }
     catch (Exception)
     {
         ConsoleImpl.WriteInfo("Could not update the timestamps.");
     }
 }
Esempio n. 14
0
        private int Dotnet(string argString)
        {
            ConsoleImpl.WriteInfo("Running dotnet {0}", argString);
            var process = new System.Diagnostics.Process
            {
                StartInfo =
                {
                    FileName        = "dotnet",
                    Arguments       = argString,
                    CreateNoWindow  = true,
                    UseShellExecute = false
                }
            };

            process.Start();
            process.WaitForExit();
            return(process.ExitCode);
        }
        protected override string GetLatestVersionCore(bool ignorePrerelease)
        {
            try
            {
                return(EffectiveStrategy.GetLatestVersion(ignorePrerelease));
            }
            catch (WebException)
            {
                if (FallbackStrategy == null)
                {
                    var latestVersion = GetLatestVersionInCache(ignorePrerelease);

                    ConsoleImpl.WriteInfo("Unable to look up the latest version online, the cache contains version {0}.", latestVersion);

                    return(latestVersion);
                }
                throw;
            }
        }
Esempio n. 16
0
        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);
            }
        }
Esempio n. 17
0
        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);
        }
Esempio n. 18
0
        protected override void SelfUpdateCore(string latestVersion)
        {
            string target       = Assembly.GetExecutingAssembly().Location;
            var    localVersion = FileSystemProxy.GetLocalFileVersion(target);

            if (localVersion.StartsWith(latestVersion))
            {
                ConsoleImpl.WriteInfo("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());

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

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

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

                FileSystemProxy.CopyFile(sourcePath, paketPackageFile);
            }
            else
            {
                ConsoleImpl.WriteInfo("Starting download from {0}", paketDownloadUrl);

                WebRequestProxy.DownloadFile(paketDownloadUrl, paketPackageFile);
            }

            FileSystemProxy.ExtractToDirectory(paketPackageFile, randomFullPath);

            var paketSourceFile = Path.Combine(randomFullPath, "tools", "paket.bootstrapper.exe");
            var renamedPath     = BootstrapperHelper.GetTempFile("oldBootstrapper");

            try
            {
                FileSystemProxy.MoveFile(target, renamedPath);
                FileSystemProxy.MoveFile(paketSourceFile, target);
                ConsoleImpl.WriteInfo("Self update of bootstrapper was successful.");
            }
            catch (Exception)
            {
                ConsoleImpl.WriteInfo("Self update failed. Resetting bootstrapper.");
                FileSystemProxy.MoveFile(renamedPath, target);
                throw;
            }
            FileSystemProxy.DeleteDirectory(randomFullPath, true);
        }
Esempio n. 19
0
        protected override void DownloadVersionCore(string latestVersion, string target)
        {
            var apiHelper = new NugetApiHelper(PaketNugetPackageName, NugetSource);

            const string paketNupkgFile         = "paket.latest.nupkg";
            const string paketNupkgFileTemplate = "paket.{0}.nupkg";

            var paketDownloadUrl = apiHelper.GetLatestPackage();
            var paketFile        = paketNupkgFile;

            if (!String.IsNullOrWhiteSpace(latestVersion))
            {
                paketDownloadUrl = apiHelper.GetSpecificPackageVersion(latestVersion);
                paketFile        = String.Format(paketNupkgFileTemplate, latestVersion);
            }

            var randomFullPath = Path.Combine(Folder, Path.GetRandomFileName());

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

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

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

                FileSystemProxy.CopyFile(sourcePath, paketPackageFile);
            }
            else
            {
                ConsoleImpl.WriteInfo("Starting download from {0}", paketDownloadUrl);

                try
                {
                    WebRequestProxy.DownloadFile(paketDownloadUrl, paketPackageFile);
                }
                catch (WebException webException)
                {
                    if (webException.Status == WebExceptionStatus.ProtocolError && !string.IsNullOrEmpty(latestVersion))
                    {
                        var response = (HttpWebResponse)webException.Response;
                        if (response.StatusCode == HttpStatusCode.NotFound)
                        {
                            throw new WebException(String.Format("Version {0} wasn't found (404)", latestVersion),
                                                   webException);
                        }
                        if (response.StatusCode == HttpStatusCode.BadRequest)
                        {
                            // For cases like "The package version is not a valid semantic version"
                            throw new WebException(String.Format("Unable to get version '{0}': {1}", latestVersion, response.StatusDescription),
                                                   webException);
                        }
                    }
                    Console.WriteLine(webException.ToString());
                    throw;
                }
            }

            FileSystemProxy.ExtractToDirectory(paketPackageFile, randomFullPath);
            var paketSourceFile = Path.Combine(randomFullPath, "tools", "paket.exe");

            FileSystemProxy.CopyFile(paketSourceFile, target, true);
            FileSystemProxy.DeleteDirectory(randomFullPath, true);
        }