コード例 #1
0
        private async Task ExecuteAsync(object sender, EventArgs e)
        {
            Telemetry.TrackUserTask("installdialogopened");

            ProjectItem item = await VsHelpers.GetSelectedItemAsync().ConfigureAwait(false);

            if (item != null)
            {
                string target = item.FileNames[1];

                Project project = await VsHelpers.GetProjectOfSelectedItemAsync().ConfigureAwait(false);

                if (project != null)
                {
                    string rootFolder = await project.GetRootFolderAsync().ConfigureAwait(false);

                    string        configFilePath = Path.Combine(rootFolder, Constants.ConfigFileName);
                    IDependencies dependencies   = Dependencies.FromConfigFile(configFilePath);

                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                    UI.InstallDialog dialog = new UI.InstallDialog(dependencies, _libraryCommandService, configFilePath, target, rootFolder);

                    var dte  = (DTE)Package.GetGlobalService(typeof(SDTE));
                    int hwnd = dte.MainWindow.HWnd;
                    WindowInteropHelper windowInteropHelper = new WindowInteropHelper(dialog);

                    // Set visual studio window's handle as the owner of the dialog.
                    // This will remove the dialog from alt-tab list and will not allow the user to switch the dialog box to the background
                    windowInteropHelper.Owner = new IntPtr(hwnd);

                    dialog.ShowDialog();
                }
            }
        }
コード例 #2
0
        private async Task ExecuteAsync(object sender, EventArgs e)
        {
            Telemetry.TrackUserTask("Execute-ManageLibrariesCommand");

            Project project = await VsHelpers.GetProjectOfSelectedItemAsync();

            if (project != null)
            {
                string rootFolder = await project.GetRootFolderAsync();

                string configFilePath = Path.Combine(rootFolder, Constants.ConfigFileName);

                if (File.Exists(configFilePath))
                {
                    await VsHelpers.OpenFileAsync(configFilePath);
                }
                else
                {
                    var      dependencies = _dependenciesFactory.FromConfigFile(configFilePath);
                    Manifest manifest     = await Manifest.FromFileAsync(configFilePath, dependencies, CancellationToken.None);

                    manifest.DefaultProvider = "cdnjs";
                    manifest.Version         = Manifest.SupportedVersions.Max().ToString();

                    await manifest.SaveAsync(configFilePath, CancellationToken.None);

                    await project.AddFileToProjectAsync(configFilePath);

                    Telemetry.TrackUserTask("Create-ConfigFile");
                }

                await VsHelpers.OpenFileAsync(configFilePath);
            }
        }
コード例 #3
0
        private async Task ExecuteAsync(object sender, EventArgs e)
        {
            Telemetry.TrackUserTask("Execute-InstallLibraryCommand");

            ProjectItem item = await VsHelpers.GetSelectedItemAsync().ConfigureAwait(false);

            Project project = await VsHelpers.GetProjectOfSelectedItemAsync().ConfigureAwait(false);

            if (project != null)
            {
                string target     = string.Empty;
                string rootFolder = await project.GetRootFolderAsync().ConfigureAwait(false);

                // Install command was invoked from a folder.
                // So the initial target location should be name of the folder from which
                // the command was invoked.
                if (item != null)
                {
                    target = item.FileNames[1];
                }
                else
                {
                    // Install command was invoked from project scope.
                    // If wwwroot exists, initial target location should be - wwwroot/lib.
                    // Else, target location should be - lib
                    if (Directory.Exists(Path.Combine(rootFolder, "wwwroot")))
                    {
                        target = Path.Combine(rootFolder, "wwwroot", "lib") + Path.DirectorySeparatorChar;
                    }
                    else
                    {
                        target = Path.Combine(rootFolder, "lib") + Path.DirectorySeparatorChar;
                    }
                }

                string        configFilePath = Path.Combine(rootFolder, Constants.ConfigFileName);
                IDependencies dependencies   = Dependencies.FromConfigFile(configFilePath);

                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                UI.InstallDialog dialog = new UI.InstallDialog(dependencies, _libraryCommandService, configFilePath, target, rootFolder);

                var dte  = (DTE)Package.GetGlobalService(typeof(SDTE));
                int hwnd = dte.MainWindow.HWnd;
                WindowInteropHelper windowInteropHelper = new WindowInteropHelper(dialog);

                // Set visual studio window's handle as the owner of the dialog.
                // This will remove the dialog from alt-tab list and will not allow the user to switch the dialog box to the background
                windowInteropHelper.Owner = new IntPtr(hwnd);

                dialog.ShowDialog();

                Telemetry.TrackUserTask("Open-InstallDialog");
            }
        }
コード例 #4
0
        private async Task BeforeQueryStatusAsync(object sender, EventArgs e)
        {
            OleMenuCommand button = (OleMenuCommand)sender;

            button.Visible = button.Enabled = false;

            // When command is invooked from a folder
            ProjectItem item = await VsHelpers.GetSelectedItemAsync().ConfigureAwait(false);

            // When command is invoked from project scope
            Project project = await VsHelpers.GetProjectOfSelectedItemAsync().ConfigureAwait(false);

            // We won't enable the command if it was not invoked from a project or a folder
            if (item?.ContainingProject == null && project == null)
            {
                return;
            }

            button.Visible = true;
            button.Enabled = KnownUIContexts.SolutionExistsAndNotBuildingAndNotDebuggingContext.IsActive && !_libraryCommandService.IsOperationInProgress;
        }
コード例 #5
0
        private async Task ExecuteAsync(object sender, EventArgs e)
        {
            ProjectItem projectItem = await VsHelpers.GetSelectedItemAsync();

            Project project = await VsHelpers.GetProjectOfSelectedItemAsync();

            try
            {
                var dependencies = _dependenciesFactory.FromConfigFile(projectItem.FileNames[1]);
                IEnumerable <string> packageIds = dependencies.Providers
                                                  .Where(p => p.NuGetPackageId != null)
                                                  .Select(p => p.NuGetPackageId)
                                                  .Distinct();

                if (!_isPackageInstalled)
                {
                    if (!UserWantsToInstall())
                    {
                        return;
                    }

                    await Task.Run(() =>
                    {
                        Logger.LogEvent(Resources.Text.Nuget_InstallingPackage, LogLevel.Status);

                        try
                        {
                            foreach (string packageId in packageIds)
                            {
                                IVsPackageInstaller2 installer = _componentModel.GetService <IVsPackageInstaller2>();
                                installer.InstallLatestPackage(null, project, packageId, true, false);
                            }

                            Telemetry.TrackUserTask("Install-NugetPackage");
                            Logger.LogEvent(Resources.Text.Nuget_PackageInstalled, LogLevel.Status);
                        }
                        catch (Exception ex)
                        {
                            Telemetry.TrackException(nameof(RestoreOnBuildCommand), ex);
                            Logger.LogEvent(Resources.Text.Nuget_PackageFailedToInstall, LogLevel.Status);
                        }
                    });
                }
                else
                {
                    await Task.Run(() =>
                    {
                        Logger.LogEvent(Resources.Text.Nuget_UninstallingPackage, LogLevel.Status);

                        try
                        {
                            foreach (string packageId in packageIds)
                            {
                                IVsPackageUninstaller uninstaller = _componentModel.GetService <IVsPackageUninstaller>();
                                uninstaller.UninstallPackage(project, packageId, false);
                            }

                            Telemetry.TrackUserTask("Uninstall-NugetPackage");
                            Logger.LogEvent(Resources.Text.Nuget_PackageUninstalled, LogLevel.Status);
                        }
                        catch (Exception ex)
                        {
                            Telemetry.TrackException(nameof(RestoreOnBuildCommand), ex);
                            Logger.LogEvent(Resources.Text.Nuget_PackageFailedToUninstall, LogLevel.Status);
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Telemetry.TrackException(nameof(RestoreOnBuildCommand), ex);
                Logger.LogEvent(Resources.Text.Nuget_PackageFailedToInstall, LogLevel.Status);
            }
        }
コード例 #6
0
        private async Task ExecuteAsync(object sender, EventArgs e)
        {
            Telemetry.TrackUserTask("Execute-InstallLibraryCommand");

            ProjectItem item = await VsHelpers.GetSelectedItemAsync().ConfigureAwait(false);

            Project project = await VsHelpers.GetProjectOfSelectedItemAsync().ConfigureAwait(false);

            if (project != null)
            {
                string rootFolder = await project.GetRootFolderAsync().ConfigureAwait(false);

                string        configFilePath = Path.Combine(rootFolder, Constants.ConfigFileName);
                IDependencies dependencies   = Dependencies.FromConfigFile(configFilePath);

                Manifest manifest = await GetManifestAsync(configFilePath, dependencies).ConfigureAwait(false);

                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                // If the manifest contains errors, we will not invoke the "Add Client-Side libraries" dialog
                // Instead we will display a message box indicating the syntax errors in manifest file.
                if (manifest == null)
                {
                    IVsUIShell shell = Package.GetGlobalService(typeof(SVsUIShell)) as IVsUIShell;
                    int        result;

                    shell.ShowMessageBox(dwCompRole: 0,
                                         rclsidComp: Guid.Empty,
                                         pszTitle: null,
                                         pszText: PredefinedErrors.ManifestMalformed().Message,
                                         pszHelpFile: null,
                                         dwHelpContextID: 0,
                                         msgbtn: OLEMSGBUTTON.OLEMSGBUTTON_OK,
                                         msgdefbtn: OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                                         msgicon: OLEMSGICON.OLEMSGICON_WARNING,
                                         fSysAlert: 0,
                                         pnResult: out result);

                    return;
                }

                string target = string.Empty;

                // Install command was invoked from a folder.
                // So the initial target location should be name of the folder from which
                // the command was invoked.
                if (item != null)
                {
                    target = item.FileNames[1];
                }
                else
                {
                    // Install command was invoked from project scope.
                    // If wwwroot exists, initial target location should be - wwwroot/lib.
                    // Else, target location should be - lib
                    if (Directory.Exists(Path.Combine(rootFolder, "wwwroot")))
                    {
                        target = Path.Combine(rootFolder, "wwwroot", "lib") + Path.DirectorySeparatorChar;
                    }
                    else
                    {
                        target = Path.Combine(rootFolder, "lib") + Path.DirectorySeparatorChar;
                    }
                }

                UI.InstallDialog dialog = new UI.InstallDialog(dependencies, _libraryCommandService, configFilePath, target, rootFolder, project);

                var dte  = (DTE)Package.GetGlobalService(typeof(SDTE));
                int hwnd = dte.MainWindow.HWnd;
                WindowInteropHelper windowInteropHelper = new WindowInteropHelper(dialog);

                // Set visual studio window's handle as the owner of the dialog.
                // This will remove the dialog from alt-tab list and will not allow the user to switch the dialog box to the background
                windowInteropHelper.Owner = new IntPtr(hwnd);

                dialog.ShowDialog();

                Telemetry.TrackUserTask("Open-InstallDialog");
            }
        }
コード例 #7
0
        private async Task ExecuteAsync(object sender, EventArgs e)
        {
            Telemetry.TrackUserTask("Execute-InstallLibraryCommand");

            ProjectItem item = await VsHelpers.GetSelectedItemAsync().ConfigureAwait(false);

            Project project = await VsHelpers.GetProjectOfSelectedItemAsync().ConfigureAwait(false);

            if (project != null)
            {
                string rootFolder = await project.GetRootFolderAsync().ConfigureAwait(false);

                string        configFilePath = Path.Combine(rootFolder, Constants.ConfigFileName);
                IDependencies dependencies   = _dependenciesFactory.FromConfigFile(configFilePath);

                Manifest manifest = await GetManifestAsync(configFilePath, dependencies).ConfigureAwait(false);

                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                // If the manifest contains errors, we will not invoke the "Add Client-Side libraries" dialog
                // Instead we will display a message box indicating the syntax errors in manifest file.
                if (manifest == null)
                {
                    IVsUIShell shell = Package.GetGlobalService(typeof(SVsUIShell)) as IVsUIShell;
                    int        result;

                    shell.ShowMessageBox(dwCompRole: 0,
                                         rclsidComp: Guid.Empty,
                                         pszTitle: null,
                                         pszText: PredefinedErrors.ManifestMalformed().Message,
                                         pszHelpFile: null,
                                         dwHelpContextID: 0,
                                         msgbtn: OLEMSGBUTTON.OLEMSGBUTTON_OK,
                                         msgdefbtn: OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                                         msgicon: OLEMSGICON.OLEMSGICON_WARNING,
                                         fSysAlert: 0,
                                         pnResult: out result);

                    return;
                }

                string target = string.Empty;

                // Install command was invoked from a folder.
                // So the initial target location should be name of the folder from which
                // the command was invoked.
                if (item != null)
                {
                    target = item.FileNames[1];
                }
                else
                {
                    // Install command was invoked from project scope.
                    // If wwwroot exists, initial target location should be - wwwroot/lib.
                    // Else, target location should be - lib
                    if (Directory.Exists(Path.Combine(rootFolder, "wwwroot")))
                    {
                        target = Path.Combine(rootFolder, "wwwroot", "lib") + Path.DirectorySeparatorChar;
                    }
                    else
                    {
                        target = Path.Combine(rootFolder, "lib") + Path.DirectorySeparatorChar;
                    }
                }

                string initialTargetLocation = CalculateSuggestedInstallPath(target, rootFolder);


                var selectedProviderBinding = new SelectedProviderBinding();
                var libraryIdViewModel      = new LibraryIdViewModel(new ProviderCatalogSearchService(() => selectedProviderBinding.SelectedProvider),
                                                                     string.Empty);

                var libraryNameBinding      = new LibraryNameBinding();
                var targetLocationViewModel = new TargetLocationViewModel(initialTargetLocation,
                                                                          libraryNameBinding,
                                                                          new LocationSearchService(dependencies.GetHostInteractions()));

                var dialogViewModel = new InstallDialogViewModel(
                    _libraryCommandService,
                    configFilePath,
                    dependencies,
                    libraryIdViewModel,
                    targetLocationViewModel,
                    selectedProviderBinding,
                    libraryNameBinding,
                    target,
                    project);

                var dialog = new UI.InstallDialog(dialogViewModel);
                dialog.ShowModal();

                Telemetry.TrackUserTask("Open-InstallDialog");
            }
        }