Пример #1
0
    private bool PerformInstallation(
        TargetAssembly targetAssembly,
        IPrerequisite[] missingPrerequisites)
    {
        using (ConsoleEx.WithForegroundColor(ConsoleColor.White))
            Console.Out.WriteLine($"{targetAssembly.Title}: installing prerequisites");

        var currentStep = 1;
        var totalSteps  = missingPrerequisites.Length * 2;

        // Download
        var installers = new List <IPrerequisiteInstaller>();

        foreach (var prerequisite in missingPrerequisites)
        {
            Console.Out.Write($"[{currentStep}/{totalSteps}] ");
            Console.Out.Write($"Downloading {prerequisite.DisplayName}... ");

            using (var progress = new ConsoleProgress(Console.Error))
            {
                var installer = prerequisite.DownloadInstaller(progress.Report);
                installers.Add(installer);
            }

            Console.Out.Write("Done");
            Console.Out.WriteLine();

            currentStep++;
        }

        // Install
        var isRebootRequired = false;

        foreach (var installer in installers)
        {
            Console.Out.Write($"[{currentStep}/{totalSteps}] ");
            Console.Out.Write($"Installing {installer.Prerequisite.DisplayName}... ");

            var installationResult = installer.Run();

            Console.Out.Write("Done");
            Console.Out.WriteLine();

            FileEx.TryDelete(installer.FilePath);

            if (installationResult == PrerequisiteInstallerResult.RebootRequired)
            {
                isRebootRequired = true;
            }

            currentStep++;
        }

        using (ConsoleEx.WithForegroundColor(ConsoleColor.White))
            Console.Out.WriteLine("Prerequisites installed successfully.");
        Console.Out.WriteLine();

        // Finalize
        if (isRebootRequired)
        {
            using (ConsoleEx.WithForegroundColor(ConsoleColor.DarkYellow))
            {
                Console.Out.WriteLine($"You need to restart Windows before you can run {targetAssembly.Title}.");
                Console.Out.WriteLine("Would you like to do it now? [y/n]");
            }

            var isRebootAccepted = Console.ReadKey(true).Key == ConsoleKey.Y;
            if (isRebootAccepted)
            {
                OperatingSystem.Reboot();
            }

            return(false);
        }

        return(true);
    }
Пример #2
0
    private void PerformInstall()
    {
        var currentStep = 1;
        var totalSteps  = _missingPrerequisites.Length * 2;

        // Download
        var installers = new List <IPrerequisiteInstaller>();

        foreach (var prerequisite in _missingPrerequisites)
        {
            UpdateStatus(@$ "[{currentStep}/{totalSteps}] Downloading {prerequisite.DisplayName}...");
            UpdateCurrentProgress(0);

            var installer = prerequisite.DownloadInstaller(p =>
            {
                UpdateCurrentProgress(p);
                UpdateTotalProgress((installers.Count + p) / (2.0 * _missingPrerequisites.Length));
            });

            installers.Add(installer);

            currentStep++;
        }

        // Install
        var isRebootRequired        = false;
        var installersFinishedCount = 0;

        foreach (var installer in installers)
        {
            UpdateStatus(@$ "[{currentStep}/{totalSteps}] Installing {installer.Prerequisite.DisplayName}...");
            UpdateCurrentProgress(-1);

            var installationResult = installer.Run();

            FileEx.TryDelete(installer.FilePath);

            if (installationResult == PrerequisiteInstallerResult.RebootRequired)
            {
                isRebootRequired = true;
            }

            UpdateTotalProgress(0.5 + ++installersFinishedCount / (2.0 * installers.Count));
            currentStep++;
        }

        // Finalize
        if (isRebootRequired)
        {
            var isRebootAccepted = MessageBox.Show(
                @$ "You need to restart Windows before you can run {_targetAssembly.Title}. " +
                @"Would you like to do it now?",
                @"Restart required",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning
                ) == DialogResult.Yes;

            if (isRebootAccepted)
            {
                OperatingSystem.Reboot();
            }

            Result = false;
        }
        else
        {
            Result = true;
        }

        Close();
    }