1212
1313using System ;
1414using System . Collections ;
15+ using System . Collections . Generic ;
1516using System . Globalization ;
1617using System . Linq ;
1718using System . Management . Automation ;
19+ using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
1820
1921namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Commands
2022{
23+ using PSSASettings = Microsoft . Windows . PowerShell . ScriptAnalyzer . Settings ;
24+
2125 [ Cmdlet ( VerbsLifecycle . Invoke , "Formatter" ) ]
2226 public class InvokeFormatterCommand : PSCmdlet , IOutputWriter
2327 {
@@ -30,12 +34,37 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
3034 [ ValidateNotNull ]
3135 public string ScriptDefinition { get ; set ; }
3236
37+ // todo make this settings and maybe rename Settings class
3338 [ Parameter ( Mandatory = false ) ]
3439 [ ValidateNotNull ]
3540 public object Settings { get ; set ; }
3641
42+ #if DEBUG
43+ [ Parameter ( Mandatory = false ) ]
44+ public SwitchParameter AttachAndDebug
45+ {
46+ get { return attachAndDebug ; }
47+ set { attachAndDebug = value ; }
48+ }
49+ private bool attachAndDebug = false ;
50+ #endif
51+
3752 protected override void BeginProcessing ( )
3853 {
54+ #if DEBUG
55+ if ( attachAndDebug )
56+ {
57+ if ( System . Diagnostics . Debugger . IsAttached )
58+ {
59+ System . Diagnostics . Debugger . Break ( ) ;
60+ }
61+ else
62+ {
63+ System . Diagnostics . Debugger . Launch ( ) ;
64+ }
65+ }
66+ #endif
67+
3968 // todo move to a common initalize session method
4069 Helper . Instance = new Helper ( SessionState . InvokeCommand , this ) ;
4170 Helper . Instance . Initialize ( ) ;
@@ -86,10 +115,12 @@ protected override void BeginProcessing()
86115
87116 try
88117 {
89- defaultSettings = new Settings ( defaultSettingsPreset ) ;
118+ defaultSettings = new PSSASettings (
119+ defaultSettingsPreset ,
120+ PSSASettings . GetSettingPresetFilePath ) ;
90121 if ( settingsMode != SettingsMode . None )
91122 {
92- inputSettings = new Settings ( settingsFound ) ;
123+ inputSettings = new PSSASettings ( settingsFound ) ;
93124 ValidateInputSettings ( ) ;
94125 }
95126 else
@@ -116,6 +147,7 @@ protected override void ProcessRecord()
116147 "PSAlignAssignmentStatement"
117148 } ;
118149
150+ var text = new EditableText ( ScriptDefinition ) ;
119151 foreach ( var rule in ruleOrder )
120152 {
121153 if ( ! inputSettings . RuleArguments . ContainsKey ( rule ) )
@@ -131,8 +163,43 @@ protected override void ProcessRecord()
131163 currentSettingsHashtable . Add ( "Rules" , ruleSettings ) ;
132164 var currentSettings = new Settings ( currentSettingsHashtable ) ;
133165 ScriptAnalyzer . Instance . UpdateSettings ( currentSettings ) ;
134- ScriptAnalyzer . Instance . Initialize ( this , null , null , null , null , false , false ) ;
166+ ScriptAnalyzer . Instance . Initialize ( this , null , null , null , null , true , false ) ;
167+
168+ var corrections = new List < CorrectionExtent > ( ) ;
169+ var records = Enumerable . Empty < DiagnosticRecord > ( ) ;
170+
171+ // todo add a check for this while loop so that it doesn't go into a black hole
172+ do
173+ {
174+ var correctionApplied = new HashSet < int > ( ) ;
175+ foreach ( var correction in corrections )
176+ {
177+ // apply only one edit per line
178+ if ( correctionApplied . Contains ( correction . StartLineNumber ) )
179+ {
180+ continue ;
181+ }
182+
183+ correctionApplied . Add ( correction . StartLineNumber ) ;
184+ text . ApplyEdit ( correction ) ;
185+ }
186+
187+ records = ScriptAnalyzer . Instance . AnalyzeScriptDefinition ( text . ToString ( ) ) ;
188+ corrections = records . Select ( r => r . SuggestedCorrections . ElementAt ( 0 ) ) . ToList ( ) ;
189+
190+ // get unique correction instances
191+ // sort them by line numbers
192+ corrections . Sort ( ( x , y ) =>
193+ {
194+ return x . StartLineNumber < x . StartLineNumber ?
195+ 1 :
196+ ( x . StartLineNumber == x . StartLineNumber ? 0 : - 1 ) ;
197+ } ) ;
198+
199+ } while ( records . Any ( ) ) ;
135200 }
201+
202+ this . WriteObject ( text . ToString ( ) ) ;
136203 }
137204
138205 private void ValidateInputSettings ( )
0 commit comments