|
| 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.Globalization; |
| 15 | +using System.Management.Automation; |
| 16 | + |
| 17 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands |
| 18 | +{ |
| 19 | + using PSSASettings = Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// A cmdlet to format a PowerShell script text. |
| 23 | + /// </summary> |
| 24 | + [Cmdlet(VerbsLifecycle.Invoke, "Formatter")] |
| 25 | + public class InvokeFormatterCommand : PSCmdlet, IOutputWriter |
| 26 | + { |
| 27 | + private const string defaultSettingsPreset = "CodeFormatting"; |
| 28 | + private Settings defaultSettings; |
| 29 | + private Settings inputSettings; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// The script text to be formated. |
| 33 | + /// |
| 34 | + /// *NOTE*: Unlike ScriptBlock parameter, the ScriptDefinition parameter require a string value. |
| 35 | + /// </summary> |
| 36 | + [ParameterAttribute(Mandatory = true)] |
| 37 | + [ValidateNotNull] |
| 38 | + public string ScriptDefinition { get; set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// A settings hashtable or a path to a PowerShell data file (.psd1) file that contains the settings. |
| 42 | + /// </summary> |
| 43 | + [Parameter(Mandatory = false)] |
| 44 | + [ValidateNotNull] |
| 45 | + public object Settings { get; set; } |
| 46 | + |
| 47 | +#if DEBUG |
| 48 | + [Parameter(Mandatory = false)] |
| 49 | + public Range Range { get; set; } |
| 50 | + |
| 51 | + [Parameter(Mandatory = false, ParameterSetName = "NoRange")] |
| 52 | + public int StartLineNumber { get; set; } = -1; |
| 53 | + [Parameter(Mandatory = false, ParameterSetName = "NoRange")] |
| 54 | + public int StartColumnNumber { get; set; } = -1; |
| 55 | + [Parameter(Mandatory = false, ParameterSetName = "NoRange")] |
| 56 | + public int EndLineNumber { get; set; } = -1; |
| 57 | + [Parameter(Mandatory = false, ParameterSetName = "NoRange")] |
| 58 | + public int EndColumnNumber { get; set; } = -1; |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Attaches to an instance of a .Net debugger |
| 62 | + /// </summary> |
| 63 | + [Parameter(Mandatory = false)] |
| 64 | + public SwitchParameter AttachAndDebug |
| 65 | + { |
| 66 | + get { return attachAndDebug; } |
| 67 | + set { attachAndDebug = value; } |
| 68 | + } |
| 69 | + |
| 70 | + private bool attachAndDebug = false; |
| 71 | +#endif |
| 72 | + |
| 73 | + protected override void BeginProcessing() |
| 74 | + { |
| 75 | +#if DEBUG |
| 76 | + if (attachAndDebug) |
| 77 | + { |
| 78 | + if (System.Diagnostics.Debugger.IsAttached) |
| 79 | + { |
| 80 | + System.Diagnostics.Debugger.Break(); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + System.Diagnostics.Debugger.Launch(); |
| 85 | + } |
| 86 | + } |
| 87 | +#endif |
| 88 | + |
| 89 | + try |
| 90 | + { |
| 91 | + inputSettings = PSSASettings.Create(Settings, null, this); |
| 92 | + if (inputSettings == null) |
| 93 | + { |
| 94 | + inputSettings = new PSSASettings( |
| 95 | + defaultSettingsPreset, |
| 96 | + PSSASettings.GetSettingPresetFilePath); |
| 97 | + } |
| 98 | + } |
| 99 | + catch |
| 100 | + { |
| 101 | + this.WriteWarning(String.Format(CultureInfo.CurrentCulture, Strings.SettingsNotParsable)); |
| 102 | + return; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected override void ProcessRecord() |
| 107 | + { |
| 108 | + // todo add tests to check range formatting |
| 109 | + string formattedScriptDefinition; |
| 110 | +#if DEBUG |
| 111 | + var range = Range; |
| 112 | + if (this.ParameterSetName.Equals("NoRange")) |
| 113 | + { |
| 114 | + range = new Range(StartLineNumber, StartColumnNumber, EndLineNumber, EndColumnNumber); |
| 115 | + } |
| 116 | + |
| 117 | + formattedScriptDefinition = Formatter.Format(ScriptDefinition, inputSettings, range, this); |
| 118 | +#endif // DEBUG |
| 119 | + |
| 120 | + formattedScriptDefinition = Formatter.Format(ScriptDefinition, inputSettings, null, this); |
| 121 | + this.WriteObject(formattedScriptDefinition); |
| 122 | + } |
| 123 | + |
| 124 | + private void ValidateInputSettings() |
| 125 | + { |
| 126 | + // todo implement this |
| 127 | + return; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments