Esempio n. 1
0
        /// <summary>
        /// Returns a provider containing the analysis settings coming from all providers (analysis config file, environment, settings file).
        /// Optionally includes settings downloaded from the SonarQube server.
        /// </summary>
        /// <remarks>This could include settings imported from a settings file</remarks>
        public static IAnalysisPropertyProvider GetAnalysisSettings(this AnalysisConfig config, bool includeServerSettings)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            List <IAnalysisPropertyProvider> providers = new List <IAnalysisPropertyProvider>();

            // Note: the order in which the providers are added determines the precedence
            // Add local settings
            if (config.LocalSettings != null)
            {
                providers.Add(new ListPropertiesProvider(config.LocalSettings));
            }

            // Add file settings
            string settingsFilePath = config.GetSettingsFilePath();

            if (settingsFilePath != null)
            {
                ListPropertiesProvider fileProvider = new ListPropertiesProvider(AnalysisProperties.Load(settingsFilePath));
                providers.Add(fileProvider);
            }

            // Add scanner environment settings
            IAnalysisPropertyProvider envProvider;

            if (EnvScannerPropertiesProvider.TryCreateProvider(null, out envProvider))
            {
                providers.Add(envProvider);
            }

            // Add server settings
            if (includeServerSettings && config.ServerSettings != null)
            {
                providers.Add(new ListPropertiesProvider(config.ServerSettings));
            }

            IAnalysisPropertyProvider provider;

            switch (providers.Count)
            {
            case 0:
                provider = EmptyPropertyProvider.Instance;
                break;

            case 1:
                provider = providers[0];
                break;

            default:
                provider = new AggregatePropertiesProvider(providers.ToArray());
                break;
            }

            return(provider);
        }
        /// <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="args">Processed 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</param>
        /// <param name="programmaticProperties">Any additional programmatically set analysis properties. Any user-specified values will take priority</param>
        public static AnalysisConfig GenerateFile(ProcessedArgs args,
            TeamBuildSettings buildSettings,
            IDictionary<string, string> serverProperties,
            AnalyzerSettings analyzerSettings,
            AnalysisProperties programmaticProperties,
            ILogger logger)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            if (buildSettings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (serverProperties == null)
            {
                throw new ArgumentNullException("serverProperties");
            }
            if (analyzerSettings == null)
            {
                throw new ArgumentNullException("analyzerSettings");
            }
            if (programmaticProperties == null)
            {
                throw new ArgumentNullException("programmaticProperties");
            }
            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(buildSettings.BuildUri);
            config.SetTfsUri(buildSettings.TfsUri);

            config.SonarConfigDir = buildSettings.SonarConfigDirectory;
            config.SonarOutputDir = buildSettings.SonarOutputDirectory;
            config.SonarBinDir = buildSettings.SonarBinDirectory;
            config.SonarRunnerWorkingDirectory = buildSettings.SonarRunnerWorkingDirectory;
            config.SourcesDirectory = buildSettings.SourcesDirectory;

            // 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 and programmatic properties.
            // Command line arguments take precedence
            AggregatePropertiesProvider aggProvider = new AggregatePropertiesProvider(args.LocalProperties, new ListPropertiesProvider(programmaticProperties));

            config.LocalSettings = new AnalysisProperties();
            foreach (var property in aggProvider.GetAllProperties())
            {
                AddSetting(config.LocalSettings, property.Id, property.Value);
            }

            // Set the pointer to the properties file
            if (args.PropertiesFileName != null)
            {
                config.SetSettingsFilePath(args.PropertiesFileName);
            }

            config.AnalyzerSettings = analyzerSettings;

            config.Save(buildSettings.AnalysisConfigFilePath);

            return config;
        }
        /// <summary>
        /// Attempts to process the supplied command line arguments and
        /// reports any errors using the logger.
        /// Returns false if any parsing errors were encountered.
        /// </summary>
        public static bool TryProcessArgs(string[] commandLineArgs, ILogger logger, out IBootstrapperSettings settings)
        {
            if (commandLineArgs == null)
            {
                throw new ArgumentNullException("commandLineArgs");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            settings = null;

            IEnumerable<ArgumentInstance> arguments;

            // This call will fail if there are duplicate or missing arguments
            CommandLineParser parser = new CommandLineParser(Descriptors, true /* allow unrecognized arguments*/);
            bool parsedOk = parser.ParseArguments(commandLineArgs, logger, out arguments);

            // 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(Bootstrapper.ArgumentProcessor).Assembly.Location);
            parsedOk &= FilePropertyProvider.TryCreateProvider(arguments, asmPath, logger, out globalFileProperties);

            AnalysisPhase phase;
            parsedOk &= TryGetPhase(commandLineArgs.Length, arguments, logger, out phase);

            Debug.Assert(!parsedOk || cmdLineProperties != null);
            Debug.Assert(!parsedOk || globalFileProperties != null);

            if (parsedOk)
            {
                Debug.Assert(cmdLineProperties != null);
                Debug.Assert(globalFileProperties != null);
                IAnalysisPropertyProvider properties = new AggregatePropertiesProvider(cmdLineProperties, globalFileProperties);

                IList<string> baseChildArgs = RemoveBootstrapperArgs(commandLineArgs);

                if (phase == AnalysisPhase.PreProcessing)
                {
                    settings = CreatePreProcessorSettings(baseChildArgs, properties, globalFileProperties, logger);
                }
                else
                {
                    settings = CreatePostProcessorSettings(baseChildArgs, properties, logger);
                }
            }

            return settings != null;
        }
        /// <summary>
        /// Returns a provider containing all of the analysis settings from the config.
        /// Optionally includes settings downloaded from the SonarQube server.
        /// </summary>
        /// <remarks>This could include settings imported from a settings file</remarks>
        public static IAnalysisPropertyProvider GetAnalysisSettings(this AnalysisConfig config, bool includeServerSettings)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            List<IAnalysisPropertyProvider> providers = new List<IAnalysisPropertyProvider>();

            // Note: the order in which the providers are added determines the precedence
            // Add local settings
            if (config.LocalSettings != null)
            {
                providers.Add(new ListPropertiesProvider(config.LocalSettings));
            }
            
            // Add file settings
            string settingsFilePath = config.GetSettingsFilePath();
            if (settingsFilePath != null)
            {
                ListPropertiesProvider fileProvider = new ListPropertiesProvider(AnalysisProperties.Load(settingsFilePath));
                providers.Add(fileProvider);
            }

            // Add server settings
            if (includeServerSettings && config.ServerSettings != null)
            {
                providers.Add(new ListPropertiesProvider(config.ServerSettings));
            }

            IAnalysisPropertyProvider provider = null;
            switch(providers.Count)
            {
                case 0:
                    provider = EmptyPropertyProvider.Instance;
                    break;
                case 1:
                    provider = providers[0];
                    break;
                default:
                    provider = new AggregatePropertiesProvider(providers.ToArray());
                    break;
            }

            return provider;
        }