public void AddFileToProject(Project project, string file, string itemType)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            if (string.IsNullOrWhiteSpace(file))
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (string.IsNullOrWhiteSpace(itemType))
            {
                throw new ArgumentNullException(nameof(itemType));
            }

            if (!this.IsFileInProject(project, file))
            {
                ProjectItem item             = project.ProjectItems.AddFromFile(file);
                Property    itemTypeProperty = VsShellUtils.FindProperty(item.Properties, Constants.ItemTypePropertyKey);
                if (itemTypeProperty != null)
                {
                    itemTypeProperty.Value = itemType;
                }
                else
                {
                    Debug.Fail("Failed to set the ItemType of the project item");
                }
            }
        }
Example #2
0
        public IEnumerable <RuleSetDeclaration> GetProjectRuleSetsDeclarations(Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            bool found = false;

            foreach (Property ruleSetProperty in VsShellUtils.EnumerateProjectProperties(project, Constants.CodeAnalysisRuleSetPropertyKey).Where(p => p != null))
            {
                found = true;

                string   ruleSetDirectoriesValue = VsShellUtils.FindProperty(ruleSetProperty.Collection, Constants.CodeAnalysisRuleSetDirectoriesPropertyKey)?.Value as string;
                string[] ruleSetDirectories      = ruleSetDirectoriesValue?.Split(new[] { RuleSetDirectoriesValueSpliter }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
                string   ruleSetValue            = ruleSetProperty.Value as string;
                string   activationContext       = TryGetPropertyConfiguration(ruleSetProperty)?.ConfigurationName ?? string.Empty;

                yield return(new RuleSetDeclaration(project, ruleSetProperty, ruleSetValue, activationContext, ruleSetDirectories));
            }

            if (!found)
            {
                VsShellUtils.WriteToSonarLintOutputPane(this.serviceProvider, Strings.CouldNotFindCodeAnalysisRuleSetPropertyOnProject, project.UniqueName);
            }
        }
Example #3
0
            public void QueueCheckIfUpdateIsRequired(Action <string> updateAction)
            {
                if (updateAction == null)
                {
                    throw new ArgumentNullException(nameof(updateAction));
                }

                Debug.Assert(this.BackgroundTask == null, "Not expecting this method to be called more than once");

                var projectSystem = this.host.GetService <IProjectSystemHelper>();

                projectSystem.AssertLocalServiceIsNotNull();

                IEnumerable <Language> languages = projectSystem.GetFilteredSolutionProjects()
                                                   .Select(Language.ForProject)
                                                   .Distinct();

                if (!languages.Any())
                {
                    return;
                }

                var solutionBinding = this.host.GetService <ISolutionBindingSerializer>();

                solutionBinding.AssertLocalServiceIsNotNull();

                var binding = solutionBinding.ReadSolutionBinding();

                if (binding == null)
                {
                    return;
                }

                if (binding.Profiles == null || binding.Profiles.Count == 0)
                {
                    // Old binding, force refresh immediately
                    VsShellUtils.WriteToSonarLintOutputPane(this.host, Strings.SonarLintProfileCheckNoProfiles);
                    updateAction(Strings.SonarLintInfoBarOldBindingFile);
                    return;
                }

                VsShellUtils.WriteToSonarLintOutputPane(this.host, Strings.SonarLintProfileCheck);

                CancellationToken token = this.TokenSource.Token;

                this.BackgroundTask = System.Threading.Tasks.Task.Run(() =>
                {
                    if (this.IsUpdateRequired(binding, languages, token))
                    {
                        this.host.UIDispatcher.BeginInvoke(new Action(() =>
                        {
                            // We mustn't update if was canceled
                            if (!token.IsCancellationRequested)
                            {
                                updateAction(null);
                            }
                        }));
                    }
                }, token);
            }
Example #4
0
            private bool HasProfileChanged(QualityProfile newProfileInfo, ApplicableQualityProfile oldProfileInfo)
            {
                if (!QualityProfile.KeyComparer.Equals(oldProfileInfo.ProfileKey, newProfileInfo.Key))
                {
                    VsShellUtils.WriteToSonarLintOutputPane(this.host, Strings.SonarLintProfileCheckDifferentProfile);
                    return(true); // The profile change to a different one
                }

                if (oldProfileInfo.ProfileTimestamp != newProfileInfo.QualityProfileTimestamp)
                {
                    VsShellUtils.WriteToSonarLintOutputPane(this.host, Strings.SonarLintProfileCheckProfileUpdated);
                    return(true); // The profile was updated
                }

                return(false);
            }
Example #5
0
        public IEnumerable <RuleSetDeclaration> GetProjectRuleSetsDeclarations(Project project)
        {
            /* This method walks through all of the available configurations (e.g. Debug, Release, Foo) and
             * attempts to fetch the values of a couple of properties from the project (CodeAnalysisRuleSet
             * and CodeAnalysisRuleSetDirectories). The collected data is put into a data object
             * and returned to the caller. The collected data includes the DTE Property object itself, which
             * is used later to update the ruleset value.
             *
             * TODO: consider refactoring. The code seems over-complicated: it finds the "ruleset"
             * property for all configurations, then backtracks to find the configuration, then looks
             * for the corresponding "ruleset directories" property.
             * Note: we are now fetching the "ruleset directories" property from the MSBuild project,
             * rather than through the DTE (the previous version of this code that used the DTE fails
             * for C# and VB projects that use the new project system).
             */

            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            bool found = false;

            var projectSystem = this.serviceProvider.GetService <IProjectSystemHelper>();

            var ruleSetProperties = VsShellUtils.GetProjectProperties(project, Constants.CodeAnalysisRuleSetPropertyKey);

            Debug.Assert(ruleSetProperties != null);
            Debug.Assert(ruleSetProperties.All(p => p != null), "Not expecting nulls in the list of properties");
            foreach (Property ruleSetProperty in ruleSetProperties)
            {
                found = true;

                string   activationContext       = TryGetPropertyConfiguration(ruleSetProperty)?.ConfigurationName ?? string.Empty;
                string   ruleSetDirectoriesValue = projectSystem.GetProjectProperty(project, Constants.CodeAnalysisRuleSetDirectoriesPropertyKey, activationContext);
                string[] ruleSetDirectories      = ruleSetDirectoriesValue?.Split(new[] { RuleSetDirectoriesValueSpliter }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
                string   ruleSetValue            = ruleSetProperty.Value as string;

                yield return(new RuleSetDeclaration(project, ruleSetProperty, ruleSetValue, activationContext, ruleSetDirectories));
            }

            if (!found)
            {
                logger.WriteLine(Strings.CouldNotFindCodeAnalysisRuleSetPropertyOnProject, project.UniqueName);
            }
        }
Example #6
0
            private bool IsUpdateRequired(BoundSonarQubeProject binding, IEnumerable <Language> projectLanguages,
                                          CancellationToken token)
            {
                Debug.Assert(binding != null);

                IDictionary <Language, SonarQubeQualityProfile> newProfiles = null;

                try
                {
                    newProfiles = TryGetLatestProfilesAsync(binding, projectLanguages, token).GetAwaiter().GetResult();
                }
                catch (Exception)
                {
                    VsShellUtils.WriteToSonarLintOutputPane(this.host, Strings.SonarLintProfileCheckFailed);
                    return(false); // Error, can't proceed
                }

                if (!newProfiles.Keys.All(binding.Profiles.ContainsKey))
                {
                    VsShellUtils.WriteToSonarLintOutputPane(this.host, Strings.SonarLintProfileCheckSolutionRequiresMoreProfiles);
                    return(true); // Missing profile, refresh
                }

                foreach (var keyValue in binding.Profiles)
                {
                    Language language = keyValue.Key;
                    ApplicableQualityProfile oldProfileInfo = keyValue.Value;

                    if (!newProfiles.ContainsKey(language))
                    {
                        // Not a relevant profile, we should just ignore it.
                        continue;
                    }

                    SonarQubeQualityProfile newProfileInfo = newProfiles[language];
                    if (this.HasProfileChanged(newProfileInfo, oldProfileInfo))
                    {
                        return(true);
                    }
                }

                VsShellUtils.WriteToSonarLintOutputPane(this.host, Strings.SonarLintProfileCheckQualityProfileIsUpToDate);
                return(false); // Up-to-date
            }
Example #7
0
 public void WriteLine(string messageFormat, params object[] args)
 {
     VsShellUtils.WriteToSonarLintOutputPane(this.serviceProvider, messageFormat, args);
 }
Example #8
0
 public void WriteLine(string message)
 {
     VsShellUtils.WriteToSonarLintOutputPane(this.serviceProvider, message);
 }
Example #9
0
 private void OutputMessage(string messageFormat, params object[] args)
 {
     VsShellUtils.WriteToSonarLintOutputPane(this.host, messageFormat, args);
 }
 private static void DEBUG_LogSqmCommandsToOutputWindow(string command)
 {
     VsShellUtils.WriteToSonarLintOutputPane(serviceProvider, "[DEBUG SonarLintSqmFacade] {0}", command);
 }
Example #11
0
 public void Write(string message)
 {
     VsShellUtils.WriteToSonarLintOutputPane(ServiceProvider.GlobalProvider, message);
 }