Skip to content

Commit b71bcd7

Browse files
author
Kapil Borle
committed
Add a skeleteon of a cmdlet to format script
1 parent 10ddacc commit b71bcd7

2 files changed

Lines changed: 145 additions & 1 deletion

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
using System;
14+
using System.Collections;
15+
using System.Globalization;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
20+
{
21+
[Cmdlet(VerbsLifecycle.Invoke, "Formatter")]
22+
public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
23+
{
24+
private const string defaultSettingsPreset = "CodeFormatting";
25+
private Settings defaultSettings;
26+
private Settings inputSettings;
27+
28+
29+
[ParameterAttribute(Mandatory = true)]
30+
[ValidateNotNull]
31+
public string ScriptDefinition { get; set; }
32+
33+
[Parameter(Mandatory = false)]
34+
[ValidateNotNull]
35+
public object Settings { get; set; }
36+
37+
protected override void BeginProcessing()
38+
{
39+
// todo move to a common initalize session method
40+
Helper.Instance = new Helper(SessionState.InvokeCommand, this);
41+
Helper.Instance.Initialize();
42+
object settingsFound;
43+
var settingsMode = PowerShell.ScriptAnalyzer.Settings.FindSettingsMode(
44+
Settings,
45+
null,
46+
out settingsFound);
47+
48+
switch (settingsMode)
49+
{
50+
case SettingsMode.Auto:
51+
this.WriteVerbose(
52+
String.Format(
53+
CultureInfo.CurrentCulture,
54+
Strings.SettingsNotProvided,
55+
""));
56+
this.WriteVerbose(
57+
String.Format(
58+
CultureInfo.CurrentCulture,
59+
Strings.SettingsAutoDiscovered,
60+
(string)settingsFound));
61+
break;
62+
63+
case SettingsMode.Preset:
64+
case SettingsMode.File:
65+
this.WriteVerbose(
66+
String.Format(
67+
CultureInfo.CurrentCulture,
68+
Strings.SettingsUsingFile,
69+
(string)settingsFound));
70+
break;
71+
72+
case SettingsMode.Hashtable:
73+
this.WriteVerbose(
74+
String.Format(
75+
CultureInfo.CurrentCulture,
76+
Strings.SettingsUsingHashtable));
77+
break;
78+
79+
default: // case SettingsMode.None
80+
this.WriteVerbose(
81+
String.Format(
82+
CultureInfo.CurrentCulture,
83+
Strings.SettingsCannotFindFile));
84+
break;
85+
}
86+
87+
try
88+
{
89+
defaultSettings = new Settings(defaultSettingsPreset);
90+
if (settingsMode != SettingsMode.None)
91+
{
92+
inputSettings = new Settings(settingsFound);
93+
ValidateInputSettings();
94+
}
95+
else
96+
{
97+
inputSettings = defaultSettings;
98+
}
99+
}
100+
catch
101+
{
102+
this.WriteWarning(String.Format(CultureInfo.CurrentCulture, Strings.SettingsNotParsable));
103+
return;
104+
}
105+
}
106+
107+
108+
protected override void ProcessRecord()
109+
{
110+
var ruleOrder = new string[]
111+
{
112+
"PSPlaceCloseBrace",
113+
"PSPlaceOpenBrace",
114+
"PSUseConsistentWhitespace",
115+
"PSUseConsistentIndentation",
116+
"PSAlignAssignmentStatement"
117+
};
118+
119+
foreach (var rule in ruleOrder)
120+
{
121+
if (!inputSettings.RuleArguments.ContainsKey(rule))
122+
{
123+
continue;
124+
}
125+
126+
this.WriteVerbose("Running " + rule);
127+
var currentSettingsHashtable = new Hashtable();
128+
currentSettingsHashtable.Add("IncludeRules", new string[] { rule });
129+
var ruleSettings = new Hashtable();
130+
ruleSettings.Add(rule, new Hashtable(inputSettings.RuleArguments[rule]));
131+
currentSettingsHashtable.Add("Rules", ruleSettings);
132+
var currentSettings = new Settings(currentSettingsHashtable);
133+
ScriptAnalyzer.Instance.UpdateSettings(currentSettings);
134+
ScriptAnalyzer.Instance.Initialize(this, null, null, null, null, false, false);
135+
}
136+
}
137+
138+
private void ValidateInputSettings()
139+
{
140+
// todo implement this
141+
return;
142+
}
143+
}
144+
}

Engine/PSScriptAnalyzer.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ FormatsToProcess = @('ScriptAnalyzer.format.ps1xml')
6565
FunctionsToExport = @()
6666

6767
# Cmdlets to export from this module
68-
CmdletsToExport = @('Get-ScriptAnalyzerRule','Invoke-ScriptAnalyzer')
68+
CmdletsToExport = @('Get-ScriptAnalyzerRule', 'Invoke-ScriptAnalyzer', 'Invoke-Formatter')
6969

7070
# Variables to export from this module
7171
VariablesToExport = @()

0 commit comments

Comments
 (0)