-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathNewScriptAnalyzerSettingsFileCommand.cs
More file actions
289 lines (257 loc) · 10.4 KB
/
NewScriptAnalyzerSettingsFileCommand.cs
File metadata and controls
289 lines (257 loc) · 10.4 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
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Reflection;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
{
/// <summary>
/// Creates a new PSScriptAnalyzer settings file in the specified directory
/// optionally based on a preset, a blank template, or all rules with default arguments.
/// </summary>
[Cmdlet(VerbsCommon.New, "ScriptAnalyzerSettingsFile", SupportsShouldProcess = true)]
[OutputType(typeof(string))]
public sealed class NewScriptAnalyzerSettingsFileCommand : PSCmdlet, IOutputWriter
{
private const string BaseOption_All = "All";
private const string BaseOption_Blank = "Blank";
/// <summary>
/// Target directory (or file path) where the settings file will be created. Defaults to
/// current location.
/// </summary>
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty]
public string Path { get; set; }
/// <summary>
/// Settings file format/extension (e.g. json, psd1). Defaults to first supported format.
/// </summary>
[Parameter]
[ArgumentCompleter(typeof(FileFormatCompleter))]
[ValidateNotNullOrEmpty]
public string FileFormat { get; set; }
/// <summary>
/// Base content: 'Blank', 'All', or a preset name returned by Get-SettingPresets.
/// 'Blank' -> minimal empty settings.
/// 'All' -> include all rules and their configurable arguments with current defaults.
/// preset -> copy preset contents.
/// </summary>
[Parameter]
[ArgumentCompleter(typeof(SettingsBaseCompleter))]
[ValidateNotNullOrEmpty]
public string Base { get; set; } = BaseOption_Blank;
/// <summary>
/// Overwrite existing file if present.
/// </summary>
[Parameter]
public SwitchParameter Force { get; set; }
protected override void BeginProcessing()
{
Helper.Instance = new Helper(SessionState.InvokeCommand);
Helper.Instance.Initialize();
string[] rulePaths = Helper.ProcessCustomRulePaths(null, SessionState, false);
ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths);
}
protected override void ProcessRecord()
{
// Default Path
if (string.IsNullOrWhiteSpace(Path))
{
Path = SessionState.Path.CurrentFileSystemLocation.ProviderPath;
}
// If user passed an existing file path, switch to its directory.
if (File.Exists(Path))
{
Path = System.IO.Path.GetDirectoryName(Path);
}
// Require the directory to already exist (do not create it).
if (!Directory.Exists(Path))
{
ThrowTerminatingError(new ErrorRecord(
new DirectoryNotFoundException($"Directory '{Path}' does not exist."),
"DIRECTORY_NOT_FOUND",
ErrorCategory.ObjectNotFound,
Path));
return;
}
// Ensure FileSystem provider for target Path.
ProviderInfo providerInfo;
try
{
SessionState.Path.GetResolvedProviderPathFromPSPath(Path, out providerInfo);
}
catch (Exception ex)
{
ThrowTerminatingError(new ErrorRecord(
new InvalidOperationException($"Cannot resolve path '{Path}': {ex.Message}", ex),
"PATH_RESOLVE_FAILED",
ErrorCategory.InvalidArgument,
Path));
return;
}
if (!string.Equals(providerInfo.Name, "FileSystem", StringComparison.OrdinalIgnoreCase))
{
ThrowTerminatingError(new ErrorRecord(
new InvalidOperationException("Target path must be in the FileSystem provider."),
"INVALID_PROVIDER",
ErrorCategory.InvalidArgument,
Path));
}
// Default format to first supported.
if (string.IsNullOrWhiteSpace(FileFormat))
{
FileFormat = Settings.GetSettingsFormats().First();
}
// Validate requested format.
if (!Settings.GetSettingsFormats().Any(f => string.Equals(f, FileFormat, StringComparison.OrdinalIgnoreCase)))
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException($"Unsupported settings format '{FileFormat}'."),
"UNSUPPORTED_FORMAT",
ErrorCategory.InvalidArgument,
FileFormat));
}
var targetFile = System.IO.Path.Combine(Path, $"{Settings.DefaultSettingsFileName}.{FileFormat}");
if (File.Exists(targetFile) && !Force)
{
WriteWarning($"Settings file already exists: {targetFile}. Use -Force to overwrite.");
return;
}
SettingsData data;
try
{
data = BuildSettingsData();
}
catch (Exception ex)
{
ThrowTerminatingError(new ErrorRecord(
ex,
"BUILD_SETTINGS_FAILED",
ErrorCategory.InvalidData,
Base));
return;
}
string content;
try
{
content = Settings.Serialize(data, FileFormat);
}
catch (Exception ex)
{
ThrowTerminatingError(new ErrorRecord(
ex,
"SERIALIZE_FAILED",
ErrorCategory.InvalidData,
FileFormat));
return;
}
if (ShouldProcess(targetFile, "Create settings file"))
{
try
{
File.WriteAllText(targetFile, content);
WriteVerbose($"Created settings file: {targetFile}");
}
catch (Exception ex)
{
ThrowTerminatingError(new ErrorRecord(
ex,
"CREATE_FILE_FAILED",
ErrorCategory.InvalidData,
targetFile));
return;
}
WriteObject(targetFile);
}
}
private SettingsData BuildSettingsData()
{
if (string.Equals(Base, BaseOption_Blank, StringComparison.OrdinalIgnoreCase))
{
return new SettingsData(); // empty snapshot
}
if (string.Equals(Base, BaseOption_All, StringComparison.OrdinalIgnoreCase))
{
return BuildAllSettingsData();
}
// Preset
var presetPath = Settings.TryResolvePreset(Base);
if (presetPath == null)
{
throw new FileNotFoundException($"Preset '{Base}' not found.");
}
return Settings.Create(presetPath);
}
private SettingsData BuildAllSettingsData()
{
var ruleNames = new List<string>();
var ruleArgs = new Dictionary<string, Dictionary<string, object>>(StringComparer.OrdinalIgnoreCase);
var modNames = ScriptAnalyzer.Instance.GetValidModulePaths();
var rules = ScriptAnalyzer.Instance.GetRule(modNames, null) ?? Enumerable.Empty<IRule>();
foreach (var rule in rules)
{
var name = rule.GetName();
ruleNames.Add(name);
if (rule is ConfigurableRule configurable)
{
var props = rule.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
var argDict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
foreach (var p in props)
{
if (p.GetCustomAttribute<ConfigurableRulePropertyAttribute>(inherit: true) == null)
{
continue;
}
argDict[p.Name] = p.GetValue(rule);
}
if (argDict.Count > 0)
{
ruleArgs[name] = argDict;
}
}
}
return new SettingsData
{
IncludeRules = ruleNames,
RuleArguments = ruleArgs,
};
}
#region Completers
private sealed class FileFormatCompleter : IArgumentCompleter
{
public IEnumerable<CompletionResult> CompleteArgument(string commandName,
string parameterName, string wordToComplete, CommandAst commandAst,
IDictionary fakeBoundParameters)
{
foreach (var fmt in Settings.GetSettingsFormats())
{
if (fmt.StartsWith(wordToComplete ?? string.Empty, StringComparison.OrdinalIgnoreCase))
{
yield return new CompletionResult(fmt, fmt, CompletionResultType.ParameterValue, $"Settings format '{fmt}'");
}
}
}
}
private sealed class SettingsBaseCompleter : IArgumentCompleter
{
public IEnumerable<CompletionResult> CompleteArgument(string commandName,
string parameterName, string wordToComplete, CommandAst commandAst,
IDictionary fakeBoundParameters)
{
var bases = new List<string> { BaseOption_Blank, BaseOption_All };
bases.AddRange(Settings.GetSettingPresets());
foreach (var b in bases)
{
if (b.StartsWith(wordToComplete ?? string.Empty, StringComparison.OrdinalIgnoreCase))
{
yield return new CompletionResult(b, b, CompletionResultType.ParameterValue, $"Base template '{b}'");
}
}
}
}
#endregion
}
}