Example #1
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private async void Execute(object sender, EventArgs e)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            string                message           = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
            SettingsManager       settingsManager   = new ShellSettingsManager((IServiceProvider)ServiceProvider);
            WritableSettingsStore userSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
            String                serverUrl         = userSettingsStore.GetString("ScantistSCA", "ServerURL", "https://api.scantist.io/");
            String                token             = userSettingsStore.GetString("ScantistSCA", "Token", "");
            String                projectName       = userSettingsStore.GetString("ScantistSCA", "ProjectName", "");

            ProjectDetailWindow projectDetailWindow = new ProjectDetailWindow();

            projectDetailWindow.ServerUrl   = serverUrl;
            projectDetailWindow.Token       = token;
            projectDetailWindow.ProjectName = projectName;
            projectDetailWindow.ShowModal();

            String ServerUrl   = projectDetailWindow.ServerUrl;
            String Token       = projectDetailWindow.Token;
            String ProjectName = projectDetailWindow.ProjectName;
            String FilePath    = projectDetailWindow.txtFileName.Text;

            if (projectDetailWindow.DialogResult.GetValueOrDefault(false))
            {
                var dialogFactory = await ServiceProvider.GetServiceAsync(typeof(SVsThreadedWaitDialogFactory)).ConfigureAwait(false) as IVsThreadedWaitDialogFactory;

                IVsThreadedWaitDialog2 dialog = null;
                if (dialogFactory != null)
                {
                    dialogFactory.CreateInstance(out dialog);
                }

                if (dialog != null && dialog.StartWaitDialog(
                        "Scantist SCA", "Retrieving the project dependency",
                        "We are getting there soon...", null,
                        "ScantistSCA is running...",
                        0, false,
                        true) == VSConstants.S_OK)
                {
                    if (!userSettingsStore.CollectionExists("ScantistSCA"))
                    {
                        userSettingsStore.CreateCollection("ScantistSCA");
                    }

                    userSettingsStore.SetString("ScantistSCA", "ServerURL", ServerUrl);
                    userSettingsStore.SetString("ScantistSCA", "Token", Token);
                    userSettingsStore.SetString("ScantistSCA", "ProjectName", ProjectName);

                    CommandParameters commandParameters = new CommandParameters();
                    commandParameters.parseCommandLine(new string[11] {
                        "-t", Token, "-serverUrl",
                        ServerUrl, "-scanType", "source_code", "-f", Path.GetDirectoryName(FilePath),
                        "-project_name", ProjectName, "--debug"
                    });
                    int scanID = new Application().run(commandParameters);

                    if (scanID == 0)
                    {
                        VsShellUtilities.ShowMessageBox(
                            this.package,
                            "Cannot trigger SCA scan. Please check log file for detail.",
                            "Error",
                            OLEMSGICON.OLEMSGICON_INFO,
                            OLEMSGBUTTON.OLEMSGBUTTON_OK,
                            OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    }
                    else
                    {
                        VsShellUtilities.ShowMessageBox(
                            this.package,
                            "Scan " + scanID + " is successfully created.",
                            "Completed",
                            OLEMSGICON.OLEMSGICON_INFO,
                            OLEMSGBUTTON.OLEMSGBUTTON_OK,
                            OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);

                        userSettingsStore.SetString("ScantistSCA", "ScanID", scanID.ToString());

                        await this.package.JoinableTaskFactory.RunAsync(async delegate
                        {
                            ToolWindowPane window = await this.package.ShowToolWindowAsync(typeof(ComponentResult), 0, true, this.package.DisposalToken);
                            if ((null == window) || (null == window.Frame))
                            {
                                throw new NotSupportedException("Cannot create tool window");
                            }
                        });
                    }
                }
                int usercancel;
                dialog.EndWaitDialog(out usercancel);
            }
        }