@@ -206,9 +206,9 @@ public SwitchParameter SaveDscDependency
206206 }
207207 private bool saveDscDependency ;
208208#endif // !PSV3
209- #endregion Parameters
209+ #endregion Parameters
210210
211- #region Overrides
211+ #region Overrides
212212
213213 /// <summary>
214214 /// Imports all known rules and loggers.
@@ -227,67 +227,74 @@ protected override void BeginProcessing()
227227 Helper . Instance . SetPSVersionTable ( psVersionTable ) ;
228228 }
229229
230- string [ ] rulePaths = Helper . ProcessCustomRulePaths ( customRulePath ,
231- this . SessionState , recurseCustomRulePath ) ;
230+ string [ ] rulePaths = Helper . ProcessCustomRulePaths (
231+ customRulePath ,
232+ this . SessionState ,
233+ recurseCustomRulePath ) ;
234+
232235 if ( IsFileParameterSet ( ) )
233236 {
234237 ProcessPath ( ) ;
235238 }
236239
237- var settingFileHasErrors = false ;
238- if ( settings == null
239- && processedPaths != null
240- && processedPaths . Count == 1 )
240+ object settingsFound ;
241+ var settingsMode = PowerShell . ScriptAnalyzer . Settings . FindSettingsMode (
242+ this . settings ,
243+ processedPaths == null || processedPaths . Count == 0 ? null : processedPaths [ 0 ] ,
244+ out settingsFound ) ;
245+
246+ switch ( settingsMode )
241247 {
242- // add a directory separator character because if there is no trailing separator character, it will return the parent
243- var directory = processedPaths [ 0 ] . TrimEnd ( System . IO . Path . DirectorySeparatorChar ) ;
244- if ( File . Exists ( directory ) )
245- {
246- // if given path is a file, get its directory
247- directory = System . IO . Path . GetDirectoryName ( directory ) ;
248- }
248+ case SettingsMode . Auto :
249+ this . WriteVerbose (
250+ String . Format (
251+ CultureInfo . CurrentCulture ,
252+ Strings . SettingsNotProvided ,
253+ path ) ) ;
254+ this . WriteVerbose (
255+ String . Format (
256+ CultureInfo . CurrentCulture ,
257+ Strings . SettingsAutoDiscovered ,
258+ ( string ) settingsFound ) ) ;
259+ break ;
249260
250- this . WriteVerbose (
251- String . Format (
252- "Settings not provided. Will look for settings file in the given path {0}." ,
253- path ) ) ;
254- var settingsFileAutoDiscovered = false ;
255- if ( Directory . Exists ( directory ) )
256- {
257- // if settings are not provided explicitly, look for it in the given path
258- // check if pssasettings.psd1 exists
259- var settingsFilename = "PSScriptAnalyzerSettings.psd1" ;
260- var settingsFilepath = System . IO . Path . Combine ( directory , settingsFilename ) ;
261- if ( File . Exists ( settingsFilepath ) )
262- {
263- settingsFileAutoDiscovered = true ;
264- this . WriteVerbose (
265- String . Format (
266- "Found {0} in {1}. Will use it to provide settings for this invocation." ,
267- settingsFilename ,
268- directory ) ) ;
269- settingFileHasErrors = ! ScriptAnalyzer . Instance . ParseProfile ( settingsFilepath , this . SessionState . Path , this ) ;
270- }
271- }
261+ case SettingsMode . Preset :
262+ case SettingsMode . File :
263+ this . WriteVerbose (
264+ String . Format (
265+ CultureInfo . CurrentCulture ,
266+ Strings . SettingsUsingFile ,
267+ ( string ) settingsFound ) ) ;
268+ break ;
272269
273- if ( ! settingsFileAutoDiscovered )
274- {
270+ case SettingsMode . Hashtable :
275271 this . WriteVerbose (
276272 String . Format (
277- "Cannot find a settings file in the given path {0}." ,
278- path ) ) ;
279- }
280- }
281- else
282- {
283- settingFileHasErrors = ! ScriptAnalyzer . Instance . ParseProfile ( this . settings , this . SessionState . Path , this ) ;
273+ CultureInfo . CurrentCulture ,
274+ Strings . SettingsUsingHashtable ) ) ;
275+ break ;
276+
277+ default : // case SettingsMode.None
278+ this . WriteVerbose (
279+ String . Format (
280+ CultureInfo . CurrentCulture ,
281+ Strings . SettingsCannotFindFile ) ) ;
282+ break ;
284283 }
285284
286- if ( settingFileHasErrors )
285+ if ( settingsMode != SettingsMode . None )
287286 {
288- this . WriteWarning ( "Cannot parse settings. Will abort the invocation." ) ;
289- stopProcessing = true ;
290- return ;
287+ try
288+ {
289+ var settingsObj = new Settings ( settingsFound ) ;
290+ ScriptAnalyzer . Instance . UpdateSettings ( settingsObj ) ;
291+ }
292+ catch
293+ {
294+ this . WriteWarning ( String . Format ( CultureInfo . CurrentCulture , Strings . SettingsNotParsable ) ) ;
295+ stopProcessing = true ;
296+ return ;
297+ }
291298 }
292299
293300 ScriptAnalyzer . Instance . Initialize (
@@ -323,7 +330,8 @@ protected override void ProcessRecord()
323330 ScriptAnalyzer . Instance . ModuleHandler = moduleHandler ;
324331 this . WriteVerbose (
325332 String . Format (
326- "Temporary module location: {0}" ,
333+ CultureInfo . CurrentCulture ,
334+ Strings . ModuleDepHandlerTempLocation ,
327335 moduleHandler . TempModulePath ) ) ;
328336 ProcessInput ( ) ;
329337 }
@@ -346,9 +354,10 @@ protected override void StopProcessing()
346354 base . StopProcessing ( ) ;
347355 }
348356
349- #endregion
357+ #endregion
358+
359+ #region Private Methods
350360
351- #region Methods
352361 private void ProcessInput ( )
353362 {
354363 IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
@@ -392,6 +401,7 @@ private bool IsFileParameterSet()
392401 {
393402 return String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) ;
394403 }
395- #endregion
404+
405+ #endregion // Private Methods
396406 }
397- }
407+ }
0 commit comments