예제 #1
0
        /// <summary>
        /// Attempt to find a properties file - either the one specified by the user, or the default properties file.
        /// Returns true if the path to a file could be resolved, othewise false.
        /// </summary>
        private static bool ResolveFilePath(string propertiesFilePath, string defaultPropertiesFileDirectory, ILogger logger, out AnalysisProperties properties)
        {
            properties = null;
            bool isValid = true;

            string resolvedPath = propertiesFilePath ?? TryGetDefaultPropertiesFilePath(defaultPropertiesFileDirectory, logger);

            if (resolvedPath != null)
            {
                if (File.Exists(resolvedPath))
                {
                    try
                    {
                        logger.LogDebug(Resources.MSG_Properties_LoadingPropertiesFromFile, resolvedPath);
                        properties = AnalysisProperties.Load(resolvedPath);
                    }
                    catch (InvalidOperationException)
                    {
                        logger.LogError(Resources.ERROR_Properties_InvalidPropertiesFile, resolvedPath);
                        isValid = false;
                    }
                }
                else
                {
                    logger.LogError(Resources.ERROR_Properties_GlobalPropertiesFileDoesNotExist, resolvedPath);
                    isValid = false;
                }
            }
            return(isValid);
        }
예제 #2
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);
        }
예제 #3
0
        public static FilePropertyProvider Load(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException("filePath");
            }
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(Resources.ERROR_SettingsFileNotFound, filePath);
            }

            AnalysisProperties   properties = AnalysisProperties.Load(filePath);
            FilePropertyProvider provider   = new FilePropertyProvider(properties, false);

            return(provider);
        }