@@ -206,9 +206,9 @@ public SwitchParameter SaveDscDependency
206
206
}
207
207
private bool saveDscDependency ;
208
208
#endif // !PSV3
209
- #endregion Parameters
209
+ #endregion Parameters
210
210
211
- #region Overrides
211
+ #region Overrides
212
212
213
213
/// <summary>
214
214
/// Imports all known rules and loggers.
@@ -227,67 +227,74 @@ protected override void BeginProcessing()
227
227
Helper . Instance . SetPSVersionTable ( psVersionTable ) ;
228
228
}
229
229
230
- string [ ] rulePaths = Helper . ProcessCustomRulePaths ( customRulePath ,
231
- this . SessionState , recurseCustomRulePath ) ;
230
+ string [ ] rulePaths = Helper . ProcessCustomRulePaths (
231
+ customRulePath ,
232
+ this . SessionState ,
233
+ recurseCustomRulePath ) ;
234
+
232
235
if ( IsFileParameterSet ( ) )
233
236
{
234
237
ProcessPath ( ) ;
235
238
}
236
239
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 )
241
247
{
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 ;
249
260
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 ;
272
269
273
- if ( ! settingsFileAutoDiscovered )
274
- {
270
+ case SettingsMode . Hashtable :
275
271
this . WriteVerbose (
276
272
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 ;
284
283
}
285
284
286
- if ( settingFileHasErrors )
285
+ if ( settingsMode != SettingsMode . None )
287
286
{
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
+ }
291
298
}
292
299
293
300
ScriptAnalyzer . Instance . Initialize (
@@ -323,7 +330,8 @@ protected override void ProcessRecord()
323
330
ScriptAnalyzer . Instance . ModuleHandler = moduleHandler ;
324
331
this . WriteVerbose (
325
332
String . Format (
326
- "Temporary module location: {0}" ,
333
+ CultureInfo . CurrentCulture ,
334
+ Strings . ModuleDepHandlerTempLocation ,
327
335
moduleHandler . TempModulePath ) ) ;
328
336
ProcessInput ( ) ;
329
337
}
@@ -346,9 +354,10 @@ protected override void StopProcessing()
346
354
base . StopProcessing ( ) ;
347
355
}
348
356
349
- #endregion
357
+ #endregion
358
+
359
+ #region Private Methods
350
360
351
- #region Methods
352
361
private void ProcessInput ( )
353
362
{
354
363
IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
@@ -392,6 +401,7 @@ private bool IsFileParameterSet()
392
401
{
393
402
return String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) ;
394
403
}
395
- #endregion
404
+
405
+ #endregion // Private Methods
396
406
}
397
- }
407
+ }
0 commit comments