-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathNewScriptAnalyzerSettingsFileCommand.cs
More file actions
537 lines (455 loc) · 20.7 KB
/
NewScriptAnalyzerSettingsFileCommand.cs
File metadata and controls
537 lines (455 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
{
/// <summary>
/// Creates a new PSScriptAnalyzer settings file.
/// The emitted file is always named PSScriptAnalyzerSettings.psd1 so that automatic
/// settings discovery works when the file is placed in a project directory.
/// </summary>
[Cmdlet(VerbsCommon.New, "ScriptAnalyzerSettingsFile", SupportsShouldProcess = true,
HelpUri = "https://github.com/PowerShell/PSScriptAnalyzer")]
public class NewScriptAnalyzerSettingsFileCommand : PSCmdlet, IOutputWriter
{
private const string SettingsFileName = "PSScriptAnalyzerSettings.psd1";
#region Parameters
/// <summary>
/// The directory where the settings file will be created.
/// Defaults to the current working directory.
/// </summary>
[Parameter(Mandatory = false, Position = 0)]
[ValidateNotNullOrEmpty]
public string Path { get; set; }
/// <summary>
/// The name of a built-in preset to use as the basis for the
/// generated settings file. When omitted, all rules and their default
/// configurable options are included. Valid values are resolved dynamically
/// from the shipped preset files and tab-completed via an argument completer
/// registered in PSScriptAnalyzer.psm1.
/// </summary>
[Parameter(Mandatory = false)]
[ValidateNotNullOrEmpty]
public string BaseOnPreset { get; set; }
/// <summary>
/// Overwrite an existing settings file at the target path.
/// </summary>
[Parameter(Mandatory = false)]
public SwitchParameter Force { get; set; }
#endregion Parameters
#region Overrides
/// <summary>
/// Initialise the analyser engine so that rule metadata is available.
/// </summary>
protected override void BeginProcessing()
{
Helper.Instance = new Helper(SessionState.InvokeCommand);
Helper.Instance.Initialize();
ScriptAnalyzer.Instance.Initialize(this, null, null, null, null, true);
}
/// <summary>
/// Generate and write the settings file.
/// </summary>
protected override void ProcessRecord()
{
// Validate -BaseOnPreset against the dynamically discovered presets.
if (!string.IsNullOrEmpty(BaseOnPreset))
{
var validPresets = Settings.GetSettingPresets().ToList();
if (!validPresets.Contains(BaseOnPreset, StringComparer.OrdinalIgnoreCase))
{
ThrowTerminatingError(
new ErrorRecord(
new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Strings.InvalidPresetName,
BaseOnPreset,
string.Join(", ", validPresets)
)
),
"InvalidPresetName",
ErrorCategory.InvalidArgument,
BaseOnPreset
)
);
}
}
string directory = string.IsNullOrEmpty(Path)
? SessionState.Path.CurrentFileSystemLocation.Path
: GetUnresolvedProviderPathFromPSPath(Path);
string targetPath = System.IO.Path.Combine(directory, SettingsFileName);
// Guard against overwriting an existing settings file unless -Force is specified.
if (File.Exists(targetPath) && !Force)
{
ThrowTerminatingError(
new ErrorRecord(
new IOException(
string.Format(
CultureInfo.CurrentCulture,
Strings.SettingsFileAlreadyExists,
targetPath
)
),
"SettingsFileAlreadyExists",
ErrorCategory.ResourceExists,
targetPath
)
);
}
string content;
if (!string.IsNullOrEmpty(BaseOnPreset))
{
content = GenerateFromPreset(BaseOnPreset);
}
else
{
content = GenerateFromAllRules();
}
if (ShouldProcess(targetPath, "Create settings file"))
{
// Ensure the target directory exists.
Directory.CreateDirectory(directory);
File.WriteAllText(targetPath, content, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
WriteObject(new FileInfo(targetPath));
}
}
#endregion Overrides
#region Settings generation
/// <summary>
/// Generates settings content from a built-in preset. The preset is parsed and
/// the output is normalised to include all top-level fields.
/// </summary>
private string GenerateFromPreset(string presetName)
{
string presetPath = Settings.GetSettingPresetFilePath(presetName);
if (presetPath == null || !File.Exists(presetPath))
{
ThrowTerminatingError(
new ErrorRecord(
new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Strings.PresetNotFound,
presetName
)
),
"PresetNotFound",
ErrorCategory.ObjectNotFound,
presetName
)
);
}
var parsed = new Settings(presetPath);
var ruleOptionMap = BuildRuleOptionMap();
var sb = new StringBuilder();
WriteHeader(sb, presetName);
sb.AppendLine("@{");
sb.AppendLine(" # Rules to run. When populated, only these rules are used.");
sb.AppendLine(" # Leave empty to run all rules.");
WriteStringArray(sb, "IncludeRules", parsed.IncludeRules);
sb.AppendLine();
sb.AppendLine(" # Rules to skip. Takes precedence over IncludeRules.");
WriteStringArray(sb, "ExcludeRules", parsed.ExcludeRules);
sb.AppendLine();
sb.AppendLine(" # Only report diagnostics at these severity levels.");
sb.AppendLine(" # Leave empty to report all severities.");
WriteSeverityArray(sb, parsed.Severities);
sb.AppendLine();
sb.AppendLine(" # Paths to modules or directories containing custom rules.");
sb.AppendLine(" # When specified, these rules are loaded in addition to (or instead");
sb.AppendLine(" # of) the built-in rules, depending on IncludeDefaultRules.");
sb.AppendLine(" # Note: Relative paths are resolved from the caller's working directory,");
sb.AppendLine(" # not the location of this settings file.");
WriteStringArray(sb, "CustomRulePath", parsed.CustomRulePath);
sb.AppendLine();
sb.AppendLine(" # When set to $true and CustomRulePath is specified, built-in rules");
sb.AppendLine(" # are loaded alongside custom rules. Has no effect without CustomRulePath.");
sb.AppendLine(string.Format(CultureInfo.InvariantCulture,
" IncludeDefaultRules = {0}", parsed.IncludeDefaultRules ? "$true" : "$false"));
sb.AppendLine();
sb.AppendLine(" # When set to $true, searches sub-folders under CustomRulePath for");
sb.AppendLine(" # additional rule modules. Has no effect without CustomRulePath.");
sb.AppendLine(string.Format(CultureInfo.InvariantCulture,
" RecurseCustomRulePath = {0}", parsed.RecurseCustomRulePath ? "$true" : "$false"));
sb.AppendLine();
sb.AppendLine(" # Per-rule configuration. Only configurable rules appear here.");
sb.AppendLine(" # Values from the preset are shown; other properties use defaults.");
if (parsed.RuleArguments != null && parsed.RuleArguments.Count > 0)
{
sb.AppendLine(" Rules = @{");
bool firstRule = true;
foreach (var ruleEntry in parsed.RuleArguments.OrderBy(kv => kv.Key, StringComparer.OrdinalIgnoreCase))
{
if (!firstRule)
{
sb.AppendLine();
}
firstRule = false;
string ruleName = ruleEntry.Key;
var presetArgs = ruleEntry.Value;
if (ruleOptionMap.TryGetValue(ruleName, out var optionInfos))
{
WriteRuleSettings(sb, ruleName, optionInfos, presetArgs);
}
else
{
WriteRuleSettingsRaw(sb, ruleName, presetArgs);
}
}
sb.AppendLine(" }");
}
else
{
sb.AppendLine(" Rules = @{}");
}
sb.AppendLine("}");
return sb.ToString();
}
/// <summary>
/// Generates settings content that includes every available rule with all
/// configurable properties set to their defaults.
/// </summary>
private string GenerateFromAllRules()
{
var ruleNames = new List<string>();
var ruleOptionMap = BuildRuleOptionMap(ruleNames);
var sb = new StringBuilder();
WriteHeader(sb, presetName: null);
sb.AppendLine("@{");
sb.AppendLine(" # Rules to run. When populated, only these rules are used.");
sb.AppendLine(" # Leave empty to run all rules.");
WriteStringArray(sb, "IncludeRules", ruleNames);
sb.AppendLine();
sb.AppendLine(" # Rules to skip. Takes precedence over IncludeRules.");
WriteStringArray(sb, "ExcludeRules", Enumerable.Empty<string>());
sb.AppendLine();
sb.AppendLine(" # Only report diagnostics at these severity levels.");
sb.AppendLine(" # Leave empty to report all severities.");
WriteSeverityArray(sb, Enumerable.Empty<string>());
sb.AppendLine();
sb.AppendLine(" # Paths to modules or directories containing custom rules.");
sb.AppendLine(" # When specified, these rules are loaded in addition to (or instead");
sb.AppendLine(" # of) the built-in rules, depending on IncludeDefaultRules.");
sb.AppendLine(" # Note: Relative paths are resolved from the caller's working directory,");
sb.AppendLine(" # not the location of this settings file.");
WriteStringArray(sb, "CustomRulePath", Enumerable.Empty<string>());
sb.AppendLine();
sb.AppendLine(" # When set to $true and CustomRulePath is specified, built-in rules");
sb.AppendLine(" # are loaded alongside custom rules. Has no effect without CustomRulePath.");
sb.AppendLine(" IncludeDefaultRules = $false");
sb.AppendLine();
sb.AppendLine(" # When set to $true, searches sub-folders under CustomRulePath for");
sb.AppendLine(" # additional rule modules. Has no effect without CustomRulePath.");
sb.AppendLine(" RecurseCustomRulePath = $false");
sb.AppendLine();
sb.AppendLine(" # Per-rule configuration. Only configurable rules appear here.");
sb.AppendLine(" Rules = @{");
bool firstRule = true;
foreach (var kvp in ruleOptionMap.OrderBy(kv => kv.Key, StringComparer.OrdinalIgnoreCase))
{
if (!firstRule)
{
sb.AppendLine();
}
firstRule = false;
WriteRuleSettings(sb, kvp.Key, kvp.Value, presetArgs: null);
}
sb.AppendLine(" }");
sb.AppendLine("}");
return sb.ToString();
}
/// <summary>
/// Builds a map of rule name to its configurable property metadata.
/// Optionally populates a list of all rule names encountered.
/// </summary>
private Dictionary<string, List<RuleOptionInfo>> BuildRuleOptionMap(List<string> allRuleNames = null)
{
var map = new Dictionary<string, List<RuleOptionInfo>>(StringComparer.OrdinalIgnoreCase);
string[] modNames = ScriptAnalyzer.Instance.GetValidModulePaths();
IEnumerable<IRule> rules = ScriptAnalyzer.Instance.GetRule(modNames, null)
?? Enumerable.Empty<IRule>();
foreach (IRule rule in rules)
{
string name = rule.GetName();
allRuleNames?.Add(name);
if (rule is ConfigurableRule)
{
var options = RuleOptionInfo.GetRuleOptions(rule);
if (options.Count > 0)
{
map[name] = options;
}
}
}
return map;
}
#endregion Settings generation
#region Formatting helpers
/// <summary>
/// Writes a comment header identifying the tool and version that generated
/// the file, along with the preset if one was specified.
/// </summary>
private static void WriteHeader(StringBuilder sb, string presetName)
{
Version version = typeof(ScriptAnalyzer).Assembly.GetName().Version;
string versionStr = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", version.Major, version.Minor, version.Build);
sb.AppendLine("#");
sb.AppendLine(string.Format(
CultureInfo.InvariantCulture,
"# PSScriptAnalyzer settings file ({0})",
versionStr));
if (!string.IsNullOrEmpty(presetName))
{
sb.AppendLine(string.Format(
CultureInfo.InvariantCulture,
"# Based on the '{0}' preset.",
presetName));
}
sb.AppendLine("#");
sb.AppendLine("# Generated by New-ScriptAnalyzerSettingsFile.");
sb.AppendLine("#");
sb.AppendLine();
}
/// <summary>
/// Writes a PowerShell string-array assignment such as IncludeRules = @( ... ).
/// </summary>
private static void WriteStringArray(StringBuilder sb, string key, IEnumerable<string> values)
{
var items = values?.ToList() ?? new List<string>();
if (items.Count == 0)
{
sb.AppendLine(string.Format(CultureInfo.InvariantCulture, " {0} = @()", key));
return;
}
sb.AppendLine(string.Format(CultureInfo.InvariantCulture, " {0} = @(", key));
foreach (string item in items)
{
sb.AppendLine(string.Format(CultureInfo.InvariantCulture, " '{0}'", item));
}
sb.AppendLine(" )");
}
/// <summary>
/// Writes the Severity array with an inline comment listing valid values.
/// </summary>
private static void WriteSeverityArray(StringBuilder sb, IEnumerable<string> values)
{
string validValues = string.Join(", ", Enum.GetNames(typeof(RuleSeverity)));
var items = values?.ToList() ?? new List<string>();
if (items.Count == 0)
{
sb.AppendLine(string.Format(CultureInfo.InvariantCulture, " Severity = @() # {0}", validValues));
return;
}
sb.AppendLine(string.Format(CultureInfo.InvariantCulture, " Severity = @( # {0}", validValues));
foreach (string item in items)
{
sb.AppendLine(string.Format(CultureInfo.InvariantCulture, " '{0}'", item));
}
sb.AppendLine(" )");
}
/// <summary>
/// Writes a rule settings block using option metadata, optionally merging
/// with values from a preset. Enable always appears first, followed by
/// the remaining properties sorted alphabetically.
/// </summary>
private static void WriteRuleSettings(
StringBuilder sb,
string ruleName,
List<RuleOptionInfo> optionInfos,
Dictionary<string, object> presetArgs)
{
sb.AppendLine(string.Format(CultureInfo.InvariantCulture, " {0} = @{{", ruleName));
foreach (RuleOptionInfo option in optionInfos)
{
object value = option.DefaultValue;
if (presetArgs != null
&& presetArgs.TryGetValue(option.Name, out object presetVal))
{
value = presetVal;
}
string formatted = FormatValue(value);
string comment = FormatPossibleValuesComment(option);
sb.AppendLine(string.Format(
CultureInfo.InvariantCulture,
" {0} = {1}{2}",
option.Name,
formatted,
comment));
}
sb.AppendLine(" }");
}
/// <summary>
/// Writes preset rule arguments verbatim when no option metadata is available.
/// </summary>
private static void WriteRuleSettingsRaw(
StringBuilder sb,
string ruleName,
Dictionary<string, object> args)
{
sb.AppendLine(string.Format(CultureInfo.InvariantCulture, " {0} = @{{", ruleName));
foreach (var kvp in args.OrderBy(k => k.Key, StringComparer.OrdinalIgnoreCase))
{
sb.AppendLine(string.Format(
CultureInfo.InvariantCulture,
" {0} = {1}",
kvp.Key,
FormatValue(kvp.Value)));
}
sb.AppendLine(" }");
}
/// <summary>
/// Formats a value as a PowerShell literal suitable for inclusion in a .psd1 file.
/// </summary>
private static string FormatValue(object value)
{
if (value is bool boolVal)
{
return boolVal ? "$true" : "$false";
}
if (value is int || value is long || value is double || value is float)
{
return Convert.ToString(value, CultureInfo.InvariantCulture);
}
if (value is string strVal)
{
return string.Format(CultureInfo.InvariantCulture, "'{0}'", strVal);
}
if (value is Array arr)
{
if (arr.Length == 0)
{
return "@()";
}
var elements = new List<string>();
foreach (object item in arr)
{
elements.Add(FormatValue(item));
}
return string.Format(CultureInfo.InvariantCulture, "@({0})", string.Join(", ", elements));
}
// Fallback - treat as string.
return string.Format(CultureInfo.InvariantCulture, "'{0}'", value);
}
/// <summary>
/// Returns an inline comment listing the valid values, or an empty string
/// when the option is unconstrained.
/// </summary>
private static string FormatPossibleValuesComment(RuleOptionInfo option)
{
if (option.PossibleValues == null || option.PossibleValues.Length == 0)
{
return string.Empty;
}
return " # " + string.Join(", ", option.PossibleValues.Select(v => v.ToString()));
}
#endregion Formatting helpers
}
}