示例#1
0
        private void ReloadConfigurationFromDisk(SonarInterface sonarCommands)
        {
            Globals globals;
            globals = applicationObject.Solution.Globals;
            // get custom stuff

            string file = this.SolutionPath() + "\\" + VSSONARPROJECTFILE;

            try
            {
                globals[VSSONAR_HOST_KEY] = VSSonarUtils.ReadPropertyFromFile(VSSONAR_HOST_KEY, file);
                sonarCommands.HostUrl = (string)globals[VSSONAR_HOST_KEY];
                globals[VSSONAR_PROJECT_KEY] = VSSonarUtils.ReadPropertyFromFile(VSSONAR_PROJECT_KEY, file);
                sonarCommands.HostUrl = (string)globals[VSSONAR_PROJECT_KEY];
                globals[VSSONAR_LANGUAGE_KEY] = VSSonarUtils.ReadPropertyFromFile(VSSONAR_LANGUAGE_KEY, file);
                sonarCommands.Language = (string)globals[VSSONAR_LANGUAGE_KEY];
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Cannot read" + ex.StackTrace);
            }
        }
示例#2
0
        private bool ValidateConfiguration(SonarInterface sonarCommands)
        {
            // check if there is no security in sonar
            bool validConfig = true;
            Globals globals;
            globals = applicationObject.Solution.Globals;

            // check if there is some options in .vssonar that should be imported
            ReloadConfigurationFromDisk(sonarCommands);
            SetStatusMessage("");
            if (!globals.get_VariableExists(VSSONAR_VALID_CONFIG_KEY) || !globals[VSSONAR_VALID_CONFIG_KEY].Equals("YES"))
            {
                bool userCancel = false;
                validConfig = false;
                string password = "******";
                string user = "******";

                while (!validConfig && !userCancel)
                {
                    int returnCode = sonarCommands.AuthenticateUserAndValidateConfig(user, password);

                    switch (returnCode)
                    {
                        case SonarInterface.AUTH_CONFIG_OK:
                            globals[VSSONAR_PASSWORD_KEY] = password;
                            globals[VSSONAR_USER_KEY] = user;
                            validConfig = true;
                            SetStatusMessage(user + " Logged In Ok");
                            break;
                        case SonarInterface.AUTH_FAIL:
                            System.Windows.Forms.MessageBox.Show("Sonar is requesting authentication / Credentials Failed");
                            PasswordForm passForm = new PasswordForm();
                            passForm.ShowDialog();
                            if (passForm.Cancel == true)
                            {
                                userCancel = true;
                                SetStatusMessage("User Cancel Usage of Addin");
                            }
                            else
                            {
                                user = passForm.User;
                                password = passForm.Password;
                            }
                            break;
                        case SonarInterface.CONFIG_ERROR_NO_HOST_URL:
                            InputForm inFormHost = new InputForm("Insert Sonar Host URL [ex: http://localhost:9000]");
                            inFormHost.ShowDialog();
                            if (inFormHost.Cancel == true)
                            {
                                userCancel = true;
                                SetStatusMessage("User Cancel Usage of Addin");
                            }
                            else
                            {
                                globals[VSSONAR_HOST_KEY] = inFormHost.Answer;
                                sonarCommands.HostUrl = inFormHost.Answer;
                                VSSonarUtils.WriteDataToConfigurationFile(VSSONAR_HOST_KEY, inFormHost.Answer, this.SolutionPath() + "\\" + VSSONARPROJECTFILE);
                            }
                            break;
                        case SonarInterface.CONFIG_ERROR_INVALID_PROJECT_KEY:
                        case SonarInterface.CONFIG_ERROR_NO_PROJECT_KEY:
                            InputForm inFormProjectKey = new InputForm("Insert Project Key [ex: Tekla:VSSonarPlugin]");
                            inFormProjectKey.ShowDialog();
                            if (inFormProjectKey.Cancel == true)
                            {
                                userCancel = true;
                                SetStatusMessage("User Cancel Usage of Addin");
                            }
                            else
                            {
                                globals[VSSONAR_PROJECT_KEY] = inFormProjectKey.Answer;
                                sonarCommands.ProjectKey = inFormProjectKey.Answer;
                                VSSonarUtils.WriteDataToConfigurationFile(VSSONAR_PROJECT_KEY, inFormProjectKey.Answer, this.SolutionPath() + "\\" + VSSONARPROJECTFILE);
                            }
                            break;
                        case SonarInterface.CONFIG_ERROR_NO_LANGUAGE_KEY:
                            if (applicationObject.ActiveDocument.ProjectItem.ContainingProject.CodeModel.Language == CodeModelLanguageConstants.vsCMLanguageMC ||
                                applicationObject.ActiveDocument.ProjectItem.ContainingProject.CodeModel.Language == CodeModelLanguageConstants.vsCMLanguageVC)
                            {
                                globals[VSSONAR_LANGUAGE_KEY] = "c++";
                                sonarCommands.Language = "c++";
                            }

                            if (applicationObject.ActiveDocument.ProjectItem.ContainingProject.CodeModel.Language == CodeModelLanguageConstants.vsCMLanguageCSharp)
                            {
                                globals[VSSONAR_LANGUAGE_KEY] = "cs";
                                sonarCommands.Language = "cs";
                            }

                            VSSonarUtils.WriteDataToConfigurationFile(VSSONAR_LANGUAGE_KEY, sonarCommands.Language, this.SolutionPath() + "\\" + VSSONARPROJECTFILE);
                            break;

                        default:
                            break;
                    }
                }
                if (validConfig)
                {
                    globals[VSSONAR_VALID_CONFIG_KEY] = "YES";
                }
            }

            // update all relevant parameters
            if (globals.get_VariableExists(VSSONAR_PASSWORD_KEY))
            {
                sonarCommands.Password = (string)globals[VSSONAR_PASSWORD_KEY];
            }
            if (globals.get_VariableExists(VSSONAR_USER_KEY))
            {
                sonarCommands.Username = (string)globals[VSSONAR_USER_KEY];
            }
            if (globals.get_VariableExists(VSSONAR_LANGUAGE_KEY))
            {
                sonarCommands.Language = (string)globals[VSSONAR_LANGUAGE_KEY];
            }
            if (globals.get_VariableExists(VSSONAR_PROJECT_KEY))
            {
                sonarCommands.ProjectKey = (string)globals[VSSONAR_PROJECT_KEY];
            }
            if (globals.get_VariableExists(VSSONAR_HOST_KEY))
            {
                sonarCommands.HostUrl = (string)globals[VSSONAR_HOST_KEY];
            }

            return validConfig;
        }
示例#3
0
        /// <summary>
        /// Excutes the selected command.
        /// </summary>
        /// <param name="CmdName"></param>
        /// <param name="ExecuteOption"></param>
        /// <param name="VarIn"></param>
        /// <param name="VarOut"></param>
        /// <param name="Handled"></param>
        public void Exec(string CmdName, vsCommandExecOption ExecuteOption, ref object VarIn,
            ref object VarOut, ref bool Handled)
        {
            string cmdArgs = "-solution_path " + SolutionPath();
            string filePath = applicationObject.SelectedItems.Item(1).ProjectItem.FileNames[1];

            // set executor
            ICommandExecution executor = new CommandExecution();
            SonarInterface sonarCommands = new SonarInterface(executor, SolutionPath(), Environment.GetEnvironmentVariable("USERPROFILE"));
            dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
            Handled = false;

            if (CmdName == addInInstance.ProgID + "." + MYCOMMANDRESET)
            {
                Globals globals;
                globals = applicationObject.Solution.Globals;
                globals[VSSONAR_VALID_CONFIG_KEY] = "NO";
                SetStatusMessage("VSSONAR ADDIN Command: Reset Configuration");
                Handled = true;
                return;
            }

            // pre set authentication
            if (!ValidateConfiguration(sonarCommands))
            {
                this.SetStatusMessage("No Valid Configuration / Or User Cancel");
                System.Windows.Forms.MessageBox.Show("Cannot Validate Configuration / Or User Cancel - Check ProjectKey");
                return;
            }

            if (theOutputPane != null)
            {
                theOutputPane.Clear();
                theOutputPane.Activate();
            }
            theOutputPane.OutputString("Start Analysis\r\n");

            if ((ExecuteOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
                && applicationObject.SelectedItems.MultiSelect == false)
            {
                if (CmdName == addInInstance.ProgID + "." + MYCOMMANDREPORTSERVERVIOLATIONS)
                {
                    SetStatusMessage("VSSONAR ADDIN Command: GetSonarViolations");
                    Handled = true;
                    PrintListToOutputPan(sonarCommands.GetSonarViolations(filePath));
                }
                if (CmdName == addInInstance.ProgID + "." + MYCOMMANDREPORTLOCALVIOLATIONS)
                {
                    SetStatusMessage("VSSONAR ADDIN Command: GetLocalViolations");
                    Handled = true;
                    PrintListToOutputPan(sonarCommands.GetLocalViolations(filePath));
                }
                if (CmdName == addInInstance.ProgID + "." + MYCOMMANDREPORTALLLOCALVIOLATIONS)
                {
                    SetStatusMessage("VSSONAR ADDIN Command: GetAllLocalViolations");
                    Handled = true;
                    PrintListToOutputPan(sonarCommands.GetAllLocalViolations(filePath));
                }
                if (CmdName == addInInstance.ProgID + "." + MYCOMMANDREPORTCOVERAGE)
                {
                    SetStatusMessage("VSSONAR ADDIN Command: GetCoverage");
                    Handled = true;
                    PrintListToOutputPan(sonarCommands.GetCoverage(filePath));
                }
                if (CmdName == addInInstance.ProgID + "." + MYCOMMANDREPORTSOURCEDIFF)
                {
                    SetStatusMessage("VSSONAR ADDIN Command: GetSourceDiff");
                    Handled = true;
                    PrintListToOutputPan(sonarCommands.GetSourceDiff(filePath));
                }
            }
            theOutputPane.OutputString("End Analysis");
        }