Пример #1
0
        public void DownloadVersion(string latestVersion, string target, bool silent)
        {
            var url = String.Format("https://github.com/fsprojects/Paket/releases/download/{0}/paket.exe", latestVersion);

            if (!silent)
            {
                Console.WriteLine("Starting download from {0}", url);
            }

            var request = PrepareWebRequest(url);

            using (var httpResponse = (HttpWebResponse)request.GetResponse())
            {
                using (var httpResponseStream = httpResponse.GetResponseStream())
                {
                    const int bufferSize = 4096;
                    byte[]    buffer     = new byte[bufferSize];
                    var       tmpFile    = BootstrapperHelper.GetTempFile("paket");

                    using (var fileStream = File.Create(tmpFile))
                    {
                        int bytesRead;
                        while ((bytesRead = httpResponseStream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fileStream.Write(buffer, 0, bytesRead);
                        }
                    }

                    File.Copy(tmpFile, target, true);
                    File.Delete(tmpFile);
                }
            }
        }
Пример #2
0
        public void SelfUpdate(string latestVersion, bool silent)
        {
            var    executingAssembly = Assembly.GetExecutingAssembly();
            string exePath           = executingAssembly.Location;
            var    localVersion      = BootstrapperHelper.GetLocalFileVersion(exePath);

            if (localVersion.StartsWith(latestVersion))
            {
                if (!silent)
                {
                    Console.WriteLine("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);

            if (!silent)
            {
                Console.WriteLine("Starting download of bootstrapper from {0}", url);
            }

            var request = PrepareWebRequest(url);

            string renamedPath     = Path.GetTempFileName();
            string tmpDownloadPath = Path.GetTempFileName();

            using (var httpResponse = (HttpWebResponse)request.GetResponse())
            {
                using (Stream httpResponseStream = httpResponse.GetResponseStream(), toStream = File.Create(tmpDownloadPath))
                {
                    httpResponseStream.CopyTo(toStream);
                }
            }
            try
            {
                BootstrapperHelper.FileMove(exePath, renamedPath);
                BootstrapperHelper.FileMove(tmpDownloadPath, exePath);
                if (!silent)
                {
                    Console.WriteLine("Self update of bootstrapper was successful.");
                }
            }
            catch (Exception)
            {
                if (!silent)
                {
                    Console.WriteLine("Self update failed. Resetting bootstrapper.");
                }
                BootstrapperHelper.FileMove(renamedPath, exePath);
                throw;
            }
        }
Пример #3
0
        private static void CancelKeyPressed(object sender, ConsoleCancelEventArgs e)
        {
            Console.WriteLine("Bootstrapper cancelled");
            var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var target = Path.Combine(folder, "paket.exe");

            var exitCode = 1;

            if (File.Exists(target))
            {
                var localVersion = BootstrapperHelper.GetLocalFileVersion(target);
                Console.WriteLine("Detected existing paket.exe ({0}). Cancelling normally.", localVersion);
                exitCode = 0;
            }
            Environment.Exit(exitCode);
        }
Пример #4
0
        public void SelfUpdate(string latestVersion, bool silent)
        {
            var    executingAssembly = Assembly.GetExecutingAssembly();
            string target            = executingAssembly.Location;
            var    localVersion      = BootstrapperHelper.GetLocalFileVersion(target);

            if (localVersion.StartsWith(latestVersion))
            {
                if (!silent)
                {
                    Console.WriteLine("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());

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

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

                if (!silent)
                {
                    Console.WriteLine("Starting download from {0}", sourcePath);
                }

                File.Copy(sourcePath, paketPackageFile);
            }
            else
            {
                if (!silent)
                {
                    Console.WriteLine("Starting download from {0}", paketDownloadUrl);
                }
                using (var client = new WebClient())
                {
                    PrepareWebClient(client, paketDownloadUrl);
                    client.DownloadFile(paketDownloadUrl, paketPackageFile);
                }
            }

            ZipFile.ExtractToDirectory(paketPackageFile, randomFullPath);

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

            try
            {
                BootstrapperHelper.FileMove(target, renamedPath);
                BootstrapperHelper.FileMove(paketSourceFile, target);
                if (!silent)
                {
                    Console.WriteLine("Self update of bootstrapper was successful.");
                }
            }
            catch (Exception)
            {
                if (!silent)
                {
                    Console.WriteLine("Self update failed. Resetting bootstrapper.");
                }
                BootstrapperHelper.FileMove(renamedPath, target);
                throw;
            }
            Directory.Delete(randomFullPath, true);
        }
Пример #5
0
        private static void StartPaketBootstrapping(IDownloadStrategy downloadStrategy, DownloadArguments dlArgs, bool silent)
        {
            Action <Exception> handleException = exception =>
            {
                if (!File.Exists(dlArgs.Target))
                {
                    Environment.ExitCode = 1;
                }
                BootstrapperHelper.WriteConsoleError(String.Format("{0} ({1})", exception.Message, downloadStrategy.Name));
            };

            try
            {
                var localVersion = BootstrapperHelper.GetLocalFileVersion(dlArgs.Target);

                var latestVersion = dlArgs.LatestVersion;
                if (latestVersion == String.Empty)
                {
                    latestVersion = downloadStrategy.GetLatestVersion(dlArgs.IgnorePrerelease);
                }

                if (dlArgs.DoSelfUpdate)
                {
                    if (!silent)
                    {
                        Console.WriteLine("Trying self update");
                    }
                    downloadStrategy.SelfUpdate(latestVersion, silent);
                }
                else
                {
                    var currentSemVer = String.IsNullOrEmpty(localVersion) ? new SemVer() : SemVer.Create(localVersion);
                    var latestSemVer  = SemVer.Create(latestVersion);
                    if (currentSemVer.CompareTo(latestSemVer) != 0)
                    {
                        downloadStrategy.DownloadVersion(latestVersion, dlArgs.Target, silent);
                        if (!silent)
                        {
                            Console.WriteLine("Done.");
                        }
                    }
                    else
                    {
                        if (!silent)
                        {
                            Console.WriteLine("Paket.exe {0} is up to date.", localVersion);
                        }
                    }
                }
            }
            catch (WebException exn)
            {
                var shouldHandleException = true;
                if (!File.Exists(dlArgs.Target))
                {
                    if (downloadStrategy.FallbackStrategy != null)
                    {
                        var fallbackStrategy = downloadStrategy.FallbackStrategy;
                        if (!silent)
                        {
                            Console.WriteLine("'{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, silent);
                        shouldHandleException = !File.Exists(dlArgs.Target);
                    }
                }
                if (shouldHandleException)
                {
                    handleException(exn);
                }
            }
            catch (Exception exn)
            {
                handleException(exn);
            }
        }
Пример #6
0
        private static void StartPaketBootstrapping(IDownloadStrategy downloadStrategy, DownloadArguments dlArgs, bool silent)
        {
            Action <Exception> handleException = exception =>
            {
                if (!File.Exists(dlArgs.Target))
                {
                    Environment.ExitCode = 1;
                }
                BootstrapperHelper.WriteConsoleError(String.Format("{0} ({1})", exception.Message, downloadStrategy.Name));
            };

            try
            {
                var localVersion = BootstrapperHelper.GetLocalFileVersion(dlArgs.Target);

                var latestVersion = dlArgs.LatestVersion;
                if (latestVersion == String.Empty)
                {
                    latestVersion = downloadStrategy.GetLatestVersion(dlArgs.IgnorePrerelease);
                }

                if (dlArgs.DoSelfUpdate)
                {
                    if (!silent)
                    {
                        Console.WriteLine("Trying self update");
                    }
                    downloadStrategy.SelfUpdate(latestVersion, silent);
                }
                else
                {
                    if (!localVersion.StartsWith(latestVersion))
                    {
                        downloadStrategy.DownloadVersion(latestVersion, dlArgs.Target, silent);
                        if (!silent)
                        {
                            Console.WriteLine("Done.");
                        }
                    }
                    else
                    {
                        if (!silent)
                        {
                            Console.WriteLine("Paket.exe {0} is up to date.", localVersion);
                        }
                    }
                }
            }
            catch (WebException exn)
            {
                var shouldHandleException = true;
                if (!File.Exists(dlArgs.Target))
                {
                    if (downloadStrategy.FallbackStrategy != null)
                    {
                        var fallbackStrategy = downloadStrategy.FallbackStrategy;
                        if (!silent)
                        {
                            Console.WriteLine("'{0}' download failed. Try fallback download from '{1}'.", downloadStrategy.Name, fallbackStrategy.Name);
                        }
                        StartPaketBootstrapping(fallbackStrategy, dlArgs, silent);
                        shouldHandleException = !File.Exists(dlArgs.Target);
                    }
                }
                if (shouldHandleException)
                {
                    handleException(exn);
                }
            }
            catch (Exception exn)
            {
                handleException(exn);
            }
        }