示例#1
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;
            }
        }
示例#2
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);
        }