Ejemplo n.º 1
0
        /// <summary>
        /// Installes the available updates if there is an update available.
        /// </summary>
        /// <returns>Task.</returns>
        public async Task <SquirrelResult> InstallAvailableUpdatesAsync(SquirrelContext context)
        {
            Argument.IsNotNull(() => context);

            var result = new SquirrelResult
            {
                IsUpdateInstalledOrAvailable = false,
                CurrentVersion = GetCurrentApplicationVersion()
            };

            var channelUrl = GetChannelUrl(context);

            if (string.IsNullOrWhiteSpace(channelUrl))
            {
                return(result);
            }

            try
            {
                using (var mgr = new UpdateManager(channelUrl))
                {
                    Log.Info($"Checking for updates using url '{channelUrl}'");

                    var updateInfo = await mgr.CheckForUpdate();

                    if (updateInfo.ReleasesToApply.Count > 0)
                    {
                        Log.Info($"Found new version '{updateInfo.FutureReleaseEntry?.Version}' using url '{channelUrl}', installing update...");

                        result.IsUpdateInstalledOrAvailable = true;
                        result.NewVersion = updateInfo.FutureReleaseEntry?.Version?.ToString();

                        UpdateInstalling?.Invoke(this, new SquirrelEventArgs(result));

                        var releaseEntry = await mgr.UpdateApp();

                        if (releaseEntry != null)
                        {
                            Log.Info("Update installed successfully");

                            result.NewVersion = releaseEntry.Version?.ToString();
                        }
                        else
                        {
                            Log.Warning("Update finished, but no release entry was returned, falling back to previous update info");
                        }

                        IsUpdatedInstalled = true;

                        UpdateInstalled?.Invoke(this, new SquirrelEventArgs(result));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An error occurred while checking for or installing the latest updates");
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Installes the available updates if there is an update available.
        /// </summary>
        /// <returns>Task.</returns>
        public async Task <SquirrelResult> InstallAvailableUpdatesAsync(SquirrelContext context)
        {
            Argument.IsNotNull(() => context);

            var result = new SquirrelResult
            {
                IsUpdateInstalledOrAvailable = false,
                CurrentVersion = GetCurrentApplicationVersion()
            };

            var channelUrl = GetChannelUrl(context);

            if (string.IsNullOrWhiteSpace(channelUrl))
            {
                return(result);
            }

            try
            {
                // Do we actually have an update? Do a quick one here
                var checkResult = await CheckForUpdatesAsync(context);

                // Note that we don't want the process to stop updating, we only want to invoke
                if (checkResult.IsUpdateInstalledOrAvailable)
                {
                    Log.Info($"Found new version '{checkResult.NewVersion}' using url '{channelUrl}', installing update...");

                    result.NewVersion = checkResult.NewVersion;

                    UpdateInstalling?.Invoke(this, new SquirrelEventArgs(result));
                }
                else
                {
                    Log.Info($"Could not determine whether a new version was available for certain, going to run update anyway...");
                }

                // Executable wrapper
                var startInfo = CreateUpdateProcessStartInfo($"--update={channelUrl}");
                var process   = Process.Start(startInfo);

                var line = "0";

                while (!string.IsNullOrWhiteSpace(line))
                {
                    if (int.TryParse(line, out var progress))
                    {
                        RaiseProgressChanged(progress);
                    }

                    line = await process.StandardOutput.ReadLineAsync();
                }

                process.WaitForExit();

                // Only when we knew there was an update pending, we notify
                if (process.ExitCode == 0 && checkResult.IsUpdateInstalledOrAvailable)
                {
                    result.NewVersion = checkResult?.NewVersion ?? "unknown";
                    result.IsUpdateInstalledOrAvailable = true;

                    Log.Info("Update installed successfully");

                    IsUpdatedInstalled = true;

                    UpdateInstalled?.Invoke(this, new SquirrelEventArgs(result));

                    Log.Info("Update installed successfully");
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An error occurred while checking for or installing the latest updates");
            }

            return(result);
        }