public static AnalysisConfig GenerateFile(ProcessedArgs args, TeamBuildSettings settings, IDictionary<string, string> serverProperties, ILogger logger) { if (args == null) { throw new ArgumentNullException("args"); } if (settings == null) { throw new ArgumentNullException("settings"); } if (serverProperties == null) { throw new ArgumentNullException("serverProperties"); } if (logger == null) { throw new ArgumentNullException("logger"); } AnalysisConfig config = new AnalysisConfig(); config.SonarProjectKey = args.ProjectKey; config.SonarProjectName = args.ProjectName; config.SonarProjectVersion = args.ProjectVersion; config.SonarQubeHostUrl = args.GetSetting(SonarProperties.HostUrl); config.SetBuildUri(settings.BuildUri); config.SetTfsUri(settings.TfsUri); config.SonarConfigDir = settings.SonarConfigDirectory; config.SonarOutputDir = settings.SonarOutputDirectory; config.SonarBinDir = settings.SonarBinDirectory; config.SonarRunnerWorkingDirectory = settings.SonarRunnerWorkingDirectory; // Add the server properties to the config config.ServerSettings = new AnalysisProperties(); foreach (var property in serverProperties) { if (!IsSecuredServerProperty(property.Key)) { AddSetting(config.ServerSettings, property.Key, property.Value); } } // Add command line arguments config.LocalSettings = new AnalysisProperties(); foreach (var property in args.LocalProperties.GetAllProperties()) { AddSetting(config.LocalSettings, property.Id, property.Value); } // Set the pointer to the properties file if (args.PropertiesFileName != null) { config.SetSettingsFilePath(args.PropertiesFileName); } config.Save(settings.AnalysisConfigFilePath); return config; }
private static IDownloader GetDownloader(ProcessedArgs args) { string username = args.GetSetting(SonarProperties.SonarUserName, null); string password = args.GetSetting(SonarProperties.SonarPassword, null); return new WebClientDownloader(username, password); }
public ISonarQubeServer Create(ProcessedArgs args) { if (args == null) { throw new ArgumentNullException("args"); } return new SonarWebService(GetDownloader(args), args.GetSetting(SonarProperties.HostUrl)); }
public ISonarQubeServer CreateSonarQubeServer(ProcessedArgs args, ILogger logger) { if (args == null) { throw new ArgumentNullException("args"); } if (logger == null) { throw new ArgumentNullException("logger"); } string username = args.GetSetting(SonarProperties.SonarUserName, null); string password = args.GetSetting(SonarProperties.SonarPassword, null); string hostUrl = args.GetSetting(SonarProperties.HostUrl, null); this.server = new SonarWebService(new WebClientDownloader(username, password, logger), hostUrl, logger); return server; }
private bool DoExecute(ProcessedArgs args) { Debug.Assert(args != null, "Not expecting the process arguments to be null"); this.logger.Verbosity = VerbosityCalculator.ComputeVerbosity(args.AggregateProperties, this.logger); InstallLoaderTargets(args); TeamBuildSettings teamBuildSettings = TeamBuildSettings.GetSettingsFromEnvironment(this.logger); // We're checking the args and environment variables so we can report all // config errors to the user at once if (teamBuildSettings == null) { this.logger.LogError(Resources.ERROR_CannotPerformProcessing); return false; } // Create the directories this.logger.LogDebug(Resources.MSG_CreatingFolders); if (!Utilities.TryEnsureEmptyDirectories(this.logger, teamBuildSettings.SonarConfigDirectory, teamBuildSettings.SonarOutputDirectory)) { return false; } ISonarQubeServer server = this.factory.CreateSonarQubeServer(args, this.logger); AnalysisProperties buildWrapperSettings = InstallBuildWrapper(server, teamBuildSettings.SonarBinDirectory, teamBuildSettings.SonarOutputDirectory); IDictionary<string, string> serverSettings; AnalyzerSettings analyzerSettings; if (!FetchArgumentsAndRulesets(server, args, teamBuildSettings, out serverSettings, out analyzerSettings)) { return false; } Debug.Assert(analyzerSettings != null, "Not expecting the analyzer settings to be null"); AnalysisConfigGenerator.GenerateFile(args, teamBuildSettings, serverSettings, analyzerSettings, buildWrapperSettings, this.logger); return true; }
private void InstallLoaderTargets(ProcessedArgs args) { if (args.InstallLoaderTargets) { ITargetsInstaller installer = this.factory.CreateTargetInstaller(); Debug.Assert(installer != null, "Factory should not return null"); installer.InstallLoaderTargets(this.logger); } else { this.logger.LogDebug(Resources.MSG_NotCopyingTargets); } }
private bool FetchArgumentsAndRulesets(ProcessedArgs args, TeamBuildSettings settings, out IDictionary<string, string> serverSettings, out AnalyzerSettings analyzerSettings) { string hostUrl = args.GetSetting(SonarProperties.HostUrl); serverSettings = null; analyzerSettings = null; ISonarQubeServer server = this.factory.CreateSonarQubeServer(args, this.logger); try { this.logger.LogInfo(Resources.MSG_FetchingAnalysisConfiguration); // Respect sonar.branch setting if set string projectBranch = null; args.TryGetSetting(SonarProperties.ProjectBranch, out projectBranch); // Fetch the SonarQube project properties serverSettings = server.GetProperties(args.ProjectKey, projectBranch); // Generate the FxCop rulesets this.logger.LogInfo(Resources.MSG_GeneratingRulesets); GenerateFxCopRuleset(server, args.ProjectKey, projectBranch, "csharp", "cs", "fxcop", Path.Combine(settings.SonarConfigDirectory, FxCopCSharpRuleset)); GenerateFxCopRuleset(server, args.ProjectKey, projectBranch, "vbnet", "vbnet", "fxcop-vbnet", Path.Combine(settings.SonarConfigDirectory, FxCopVBNetRuleset)); IAnalyzerProvider analyzerProvider = this.factory.CreateAnalyzerProvider(this.logger); Debug.Assert(analyzerProvider != null, "Factory should not return null"); analyzerSettings = analyzerProvider.SetupAnalyzers(server, settings, args.ProjectKey, projectBranch); } catch (WebException ex) { if (Utilities.HandleHostUrlWebException(ex, hostUrl, this.logger)) { return false; } throw; } finally { Utilities.SafeDispose(server); } return true; }
private bool DoExecute(ProcessedArgs args, ILogger logger) { if (args == null) { throw new ArgumentNullException("args"); } if (logger == null) { throw new ArgumentNullException("logger"); } logger.Verbosity = VerbosityCalculator.ComputeVerbosity(args.AggregateProperties, logger); InstallLoaderTargets(args, logger); TeamBuildSettings teamBuildSettings = TeamBuildSettings.GetSettingsFromEnvironment(logger); // We're checking the args and environment variables so we can report all // config errors to the user at once if (teamBuildSettings == null) { logger.LogError(Resources.ERROR_CannotPerformProcessing); return false; } // Create the directories logger.LogDebug(Resources.MSG_CreatingFolders); if (!Utilities.TryEnsureEmptyDirectories(logger, teamBuildSettings.SonarConfigDirectory, teamBuildSettings.SonarOutputDirectory)) { return false; } IDictionary<string, string> serverSettings; if (!FetchArgumentsAndRulesets(args, teamBuildSettings.SonarConfigDirectory, logger, out serverSettings)) { return false; } AnalysisConfigGenerator.GenerateFile(args, teamBuildSettings, serverSettings, logger); return true; }
private void InstallLoaderTargets(ProcessedArgs args, ILogger logger) { if (args.InstallLoaderTargets) { this.targetInstaller.InstallLoaderTargets(logger); } else { logger.LogDebug(Resources.MSG_NotCopyingTargets); } }
private bool FetchArgumentsAndRulesets(ProcessedArgs args, string configDir, ILogger logger, out IDictionary<string, string> serverSettings) { string hostUrl = args.GetSetting(SonarProperties.HostUrl); serverSettings = null; ISonarQubeServer server = this.serverFactory.Create(args); try { // Fetch the SonarQube project properties logger.LogInfo(Resources.MSG_FetchingAnalysisConfiguration); serverSettings = server.GetProperties(args.ProjectKey, logger); // Generate the FxCop rulesets logger.LogInfo(Resources.MSG_GeneratingRulesets); GenerateFxCopRuleset(server, args.ProjectKey, "csharp", "cs", "fxcop", Path.Combine(configDir, FxCopCSharpRuleset), logger); GenerateFxCopRuleset(server, args.ProjectKey, "vbnet", "vbnet", "fxcop-vbnet", Path.Combine(configDir, FxCopVBNetRuleset), logger); } catch (WebException ex) { if (Utilities.HandleHostUrlWebException(ex, hostUrl, logger)) { return false; } throw; } finally { Utilities.SafeDispose(server); } return true; }
/// <summary> /// Attempts to process the supplied command line arguments and /// reports any errors using the logger. /// Returns null unless all of the properties are valid. /// </summary> public static ProcessedArgs TryProcessArgs(string[] commandLineArgs, ILogger logger) { if (logger == null) { throw new ArgumentNullException("logger"); } ProcessedArgs processed = null; IEnumerable<ArgumentInstance> arguments; // This call will fail if there are duplicate, missing, or unrecognized arguments CommandLineParser parser = new CommandLineParser(Descriptors, false /* don't allow unrecognized */); bool parsedOk = parser.ParseArguments(commandLineArgs, logger, out arguments); // Handle the /install: command line only argument bool installLoaderTargets; parsedOk &= TryGetInstallTargetsEnabled(arguments, logger, out installLoaderTargets); // Handler for command line analysis properties IAnalysisPropertyProvider cmdLineProperties; parsedOk &= CmdLineArgPropertyProvider.TryCreateProvider(arguments, logger, out cmdLineProperties); // Handler for property file IAnalysisPropertyProvider globalFileProperties; string asmPath = Path.GetDirectoryName(typeof(PreProcessor.ArgumentProcessor).Assembly.Location); parsedOk &= FilePropertyProvider.TryCreateProvider(arguments, asmPath, logger, out globalFileProperties); if (parsedOk) { Debug.Assert(cmdLineProperties != null); Debug.Assert(globalFileProperties != null); processed = new ProcessedArgs( GetArgumentValue(KeywordIds.ProjectKey, arguments), GetArgumentValue(KeywordIds.ProjectName, arguments), GetArgumentValue(KeywordIds.ProjectVersion, arguments), installLoaderTargets, cmdLineProperties, globalFileProperties); if (!AreParsedArgumentsValid(processed, logger)) { processed = null; } } return processed; }
/// <summary> /// Performs any additional validation on the parsed arguments and logs errors /// if necessary. /// </summary> /// <returns>True if the arguments are valid, otherwise false</returns> private static bool AreParsedArgumentsValid(ProcessedArgs args, ILogger logger) { bool areValid = true; string hostUrl; if (!args.TryGetSetting(SonarProperties.HostUrl, out hostUrl) || string.IsNullOrWhiteSpace(hostUrl)) { logger.LogError(Resources.ERROR_Args_UrlRequired); areValid = false; } string projectKey = args.ProjectKey; if (!IsValidProjectKey(projectKey)) { logger.LogError(Resources.ERROR_InvalidProjectKeyArg); areValid = false; } return areValid; }
private bool FetchArgumentsAndRulesets(ISonarQubeServer server, ProcessedArgs args, TeamBuildSettings settings, out IDictionary <string, string> serverSettings, out List <AnalyzerSettings> analyzersSettings) { serverSettings = null; analyzersSettings = new List <AnalyzerSettings>(); try { this.logger.LogInfo(Resources.MSG_FetchingAnalysisConfiguration); // Respect sonar.branch setting if set string projectBranch = null; args.TryGetSetting(SonarProperties.ProjectBranch, out projectBranch); // Fetch the SonarQube project properties serverSettings = server.GetProperties(args.ProjectKey, projectBranch); // Fetch installed plugins IEnumerable <string> availableLanguages = server.GetAllLanguages(); foreach (PluginDefinition plugin in plugins) { if (!availableLanguages.Contains(plugin.Language)) { continue; } // Fetch project quality profile string qualityProfile; if (!server.TryGetQualityProfile(args.ProjectKey, projectBranch, plugin.Language, out qualityProfile)) { this.logger.LogDebug(Resources.RAP_NoQualityProfile, plugin.Language, args.ProjectKey); continue; } // Fetch rules IList <ActiveRule> activeRules = server.GetActiveRules(qualityProfile); if (!activeRules.Any()) { this.logger.LogDebug(Resources.RAP_NoActiveRules, plugin.Language); continue; } IList <string> inactiveRules = server.GetInactiveRules(qualityProfile, plugin.Language); this.logger.LogInfo(Resources.MSG_GeneratingRulesets); string fxCopPath = Path.Combine(settings.SonarConfigDirectory, string.Format(FxCopRulesetName, plugin.Language)); if (plugin.Language.Equals(VBNetLanguage)) { GenerateFxCopRuleset("fxcop-vbnet", activeRules, fxCopPath); } else { GenerateFxCopRuleset("fxcop", activeRules, fxCopPath); } // Generate Roslyn analyzers settings and rulesets IAnalyzerProvider analyzerProvider = this.factory.CreateRoslynAnalyzerProvider(this.logger); Debug.Assert(analyzerProvider != null, "Factory should not return null"); // Will be null if the processing of settings and active rules resulted in an empty ruleset AnalyzerSettings analyzer = analyzerProvider.SetupAnalyzer(settings, serverSettings, activeRules, inactiveRules, plugin.Language); if (analyzer != null) { analyzersSettings.Add(analyzer); } } } catch (WebException ex) { if (Utilities.HandleHostUrlWebException(ex, args.SonarQubeUrl, this.logger)) { return(false); } throw; } finally { Utilities.SafeDispose(server); } return(true); }
/// <summary> /// Combines the various configuration options into the AnalysisConfig file /// used by the build and post-processor. Saves the file and returns the config instance. /// </summary> /// <param name="localSettings">Processed local settings, including command line arguments supplied the user</param> /// <param name="buildSettings">Build environment settings</param> /// <param name="serverProperties">Analysis properties downloaded from the SonarQube server</param> /// <param name="analyzerSettings">Specifies the Roslyn analyzers to use. Can be empty</param> public static AnalysisConfig GenerateFile(ProcessedArgs localSettings, TeamBuildSettings buildSettings, IDictionary <string, string> serverProperties, List <AnalyzerSettings> analyzersSettings, ILogger logger) { if (localSettings == null) { throw new ArgumentNullException(nameof(localSettings)); } if (buildSettings == null) { throw new ArgumentNullException(nameof(buildSettings)); } if (serverProperties == null) { throw new ArgumentNullException(nameof(serverProperties)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } var config = new AnalysisConfig { SonarProjectKey = localSettings.ProjectKey, SonarProjectName = localSettings.ProjectName, SonarProjectVersion = localSettings.ProjectVersion, SonarQubeHostUrl = localSettings.SonarQubeUrl }; config.SetBuildUri(buildSettings.BuildUri); config.SetTfsUri(buildSettings.TfsUri); config.SonarConfigDir = buildSettings.SonarConfigDirectory; config.SonarOutputDir = buildSettings.SonarOutputDirectory; config.SonarBinDir = buildSettings.SonarBinDirectory; config.SonarScannerWorkingDirectory = buildSettings.SonarScannerWorkingDirectory; config.SourcesDirectory = buildSettings.SourcesDirectory; // Add the server properties to the config config.ServerSettings = new AnalysisProperties(); foreach (var property in serverProperties) { if (!Utilities.IsSecuredServerProperty(property.Key)) { AddSetting(config.ServerSettings, property.Key, property.Value); } } config.LocalSettings = new AnalysisProperties(); // From the local settings, we only write the ones coming from the cmd line foreach (var property in localSettings.CmdLineProperties.GetAllProperties()) { AddSetting(config.LocalSettings, property.Id, property.Value); } if (!string.IsNullOrEmpty(localSettings.Organization)) { AddSetting(config.LocalSettings, SonarProperties.Organization, localSettings.Organization); } // Set the pointer to the properties file if (localSettings.PropertiesFileName != null) { config.SetSettingsFilePath(localSettings.PropertiesFileName); } config.AnalyzersSettings = analyzersSettings ?? throw new ArgumentNullException(nameof(analyzersSettings)); config.Save(buildSettings.AnalysisConfigFilePath); return(config); }
public static AnalysisConfig GenerateFile(ProcessedArgs args, TeamBuildSettings settings, IDictionary <string, string> serverProperties, ILogger logger) { if (args == null) { throw new ArgumentNullException("args"); } if (settings == null) { throw new ArgumentNullException("settings"); } if (serverProperties == null) { throw new ArgumentNullException("serverProperties"); } if (logger == null) { throw new ArgumentNullException("logger"); } AnalysisConfig config = new AnalysisConfig(); config.SonarProjectKey = args.ProjectKey; config.SonarProjectName = args.ProjectName; config.SonarProjectVersion = args.ProjectVersion; config.SonarQubeHostUrl = args.GetSetting(SonarProperties.HostUrl); config.SetBuildUri(settings.BuildUri); config.SetTfsUri(settings.TfsUri); config.SonarConfigDir = settings.SonarConfigDirectory; config.SonarOutputDir = settings.SonarOutputDirectory; config.SonarBinDir = settings.SonarBinDirectory; config.SonarRunnerWorkingDirectory = settings.SonarRunnerWorkingDirectory; // Add the server properties to the config config.ServerSettings = new AnalysisProperties(); foreach (var property in serverProperties) { if (!IsSecuredServerProperty(property.Key)) { AddSetting(config.ServerSettings, property.Key, property.Value); } } // Add command line arguments config.LocalSettings = new AnalysisProperties(); foreach (var property in args.LocalProperties.GetAllProperties()) { AddSetting(config.LocalSettings, property.Id, property.Value); } // Set the pointer to the properties file if (args.PropertiesFileName != null) { config.SetSettingsFilePath(args.PropertiesFileName); } config.Save(settings.AnalysisConfigFilePath); return(config); }