Beispiel #1
0
        /// <summary>
        /// Factory method for producing AnalysisService instances. Handles loading of the PSScriptAnalyzer module
        /// and runspace pool instantiation before creating the service instance.
        /// </summary>
        /// <param name="settingsPath">Path to the PSSA settings file to be used for this service instance.</param>
        /// <param name="logger">EditorServices logger for logging information.</param>
        /// <returns>
        /// A new analysis service instance with a freshly imported PSScriptAnalyzer module and runspace pool.
        /// Returns null if problems occur. This method should never throw.
        /// </returns>
        public static AnalysisService Create(string settingsPath, ILogger logger)
        {
            try
            {
                RunspacePool analysisRunspacePool;
                PSModuleInfo pssaModuleInfo;
                try
                {
                    // Try and load a PSScriptAnalyzer module with the required version
                    // by looking on the script path. Deep down, this internally runs Get-Module -ListAvailable,
                    // so we'll use this to check whether such a module exists
                    analysisRunspacePool = CreatePssaRunspacePool(out pssaModuleInfo);
                }
                catch (Exception e)
                {
                    throw new AnalysisServiceLoadException("PSScriptAnalyzer runspace pool could not be created", e);
                }

                if (analysisRunspacePool == null)
                {
                    throw new AnalysisServiceLoadException("PSScriptAnalyzer runspace pool failed to be created");
                }

                // Having more than one runspace doesn't block code formatting if one
                // runspace is occupied for diagnostics
                analysisRunspacePool.SetMaxRunspaces(NumRunspaces);
                analysisRunspacePool.ThreadOptions = PSThreadOptions.ReuseThread;
                analysisRunspacePool.Open();

                var analysisService = new AnalysisService(
                    analysisRunspacePool,
                    settingsPath,
                    s_includedRules,
                    logger,
                    pssaModuleInfo);

                // Log what features are available in PSSA here
                analysisService.LogAvailablePssaFeatures();

                return(analysisService);
            }
            catch (AnalysisServiceLoadException e)
            {
                logger.WriteException("PSScriptAnalyzer cannot be imported, AnalysisService will be disabled", e);
                return(null);
            }
            catch (Exception e)
            {
                logger.WriteException("AnalysisService could not be started due to an unexpected exception", e);
                return(null);
            }
        }
Beispiel #2
0
 internal void InstantiateAnalysisService(string settingsPath = null)
 {
     // Create the analysis service. If this fails, the result will be null -- any exceptions are caught and logged
     this.AnalysisService = AnalysisService.Create(settingsPath, this.logger);
 }