Exemple #1
0
        static string LaunchPackageBootstrapper(
            PackageConfiguration packageConfiguration,
            string editorRepositoryPath,
            string editorPath,
            string targetPath,
            string packageId,
            Func <IPackageManager, Task> installPackage)
        {
            EnableVisualStyles();
            var installPath       = string.Empty;
            var executablePackage = default(IPackage);
            var packageManager    = CreatePackageManager(editorRepositoryPath);

            using (var monitor = new PackageConfigurationUpdater(packageConfiguration, packageManager, editorPath))
            {
                packageManager.PackageInstalling += (sender, e) =>
                {
                    var package = e.Package;
                    if (package.Id == packageId && e.Cancel)
                    {
                        executablePackage = package;
                    }
                };

                PackageHelper.RunPackageOperation(packageManager, () => installPackage(packageManager));
            }

            if (executablePackage != null)
            {
                if (string.IsNullOrEmpty(targetPath))
                {
                    var entryPoint = executablePackage.Id + NuGet.Constants.BonsaiExtension;
                    var message    = string.Format(Resources.InstallExecutablePackageWarning, executablePackage.Id);
                    var result     = MessageBox.Show(message, Resources.InstallExecutablePackageCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (result == DialogResult.Yes)
                    {
                        using (var dialog = new SaveFolderDialog())
                        {
                            dialog.FileName = executablePackage.Id;
                            if (dialog.ShowDialog() == DialogResult.OK)
                            {
                                targetPath = dialog.FileName;
                            }
                        }
                    }
                }

                if (!string.IsNullOrEmpty(targetPath))
                {
                    var targetFileSystem = new PhysicalFileSystem(targetPath);
                    installPath = PackageHelper.InstallExecutablePackage(executablePackage, targetFileSystem);
                }
            }
            return(installPath);
        }
Exemple #2
0
        internal static IPackage GetEditorPackage(
            PackageConfiguration packageConfiguration,
            string editorRepositoryPath,
            string editorPath,
            IPackageName editorPackageName,
            bool showDialog)
        {
            const string OldExtension  = ".old";
            var          backupExePath = editorPath + OldExtension;

            if (File.Exists(backupExePath))
            {
                try { File.Delete(backupExePath); }
                catch { } // best effort
            }

            var packageManager = CreatePackageManager(editorRepositoryPath);

            if (!showDialog)
            {
                packageManager.Logger = ConsoleLogger.Default;
                packageManager.RequiringLicenseAcceptance += (sender, e) => e.LicenseAccepted = true;
            }

            var missingPackages = GetMissingPackages(packageConfiguration.Packages, packageManager.LocalRepository).ToList();

            if (missingPackages.Count > 0)
            {
                if (showDialog)
                {
                    EnableVisualStyles();
                }
                using (var monitor = new PackageConfigurationUpdater(packageConfiguration, packageManager, editorPath, editorPackageName))
                {
                    Task RestoreMissingPackages()
                    {
                        var restoreTasks = missingPackages.Select(package => packageManager.StartRestorePackage(package.Id, ParseVersion(package.Version)));

                        return(Task.Factory.ContinueWhenAll(restoreTasks.ToArray(), operations =>
                        {
                            foreach (var task in operations)
                            {
                                if (task.IsFaulted || task.IsCanceled)
                                {
                                    continue;
                                }
                                var package = task.Result;
                                if (packageManager.LocalRepository.Exists(package.Id))
                                {
                                    packageManager.UpdatePackage(
                                        package,
                                        updateDependencies: false,
                                        allowPrereleaseVersions: true);
                                }
                                else
                                {
                                    packageManager.InstallPackage(
                                        package,
                                        ignoreDependencies: true,
                                        allowPrereleaseVersions: true,
                                        ignoreWalkInfo: true);
                                }
                            }

                            Task.WaitAll(operations);
                        }));
                    };

                    if (!showDialog)
                    {
                        RestoreMissingPackages().Wait();
                    }
                    else
                    {
                        PackageHelper.RunPackageOperation(packageManager, RestoreMissingPackages);
                    }
                }
            }

            var editorPackage = packageManager.LocalRepository.FindPackage(editorPackageName.Id);

            if (editorPackage == null || editorPackage.Version < editorPackageName.Version)
            {
                if (showDialog)
                {
                    EnableVisualStyles();
                }
                using (var monitor = new PackageConfigurationUpdater(packageConfiguration, packageManager, editorPath, editorPackageName))
                {
                    Task RestoreEditorPackage()
                    {
                        return(packageManager
                               .StartInstallPackage(editorPackageName.Id, editorPackageName.Version)
                               .ContinueWith(task => editorPackage = task.Result));
                    };

                    if (!showDialog)
                    {
                        RestoreEditorPackage().Wait();
                    }
                    else
                    {
                        PackageHelper.RunPackageOperation(
                            packageManager,
                            RestoreEditorPackage,
                            operationLabel: editorPackage != null ? "Updating..." : null);
                    }
                    if (editorPackage == null)
                    {
                        var assemblyName = Assembly.GetEntryAssembly().GetName();
                        var errorMessage = editorPackage == null ? Resources.InstallEditorPackageError : Resources.UpdateEditorPackageError;
                        if (!showDialog)
                        {
                            ConsoleLogger.Default.Log(MessageLevel.Error, errorMessage);
                        }
                        else
                        {
                            MessageBox.Show(errorMessage, assemblyName.Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        return(null);
                    }
                }
            }

            return(editorPackage);
        }