| | | 1 | | using System.Management.Automation; |
| | | 2 | | using System.Management.Automation.Language; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Utilities; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Utilities for validating PowerShell ScriptBlocks. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class PsScriptBlockValidation |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Checks if the provided PowerShell script text contains only a param() block |
| | | 13 | | /// and nothing else (no executable statements). |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="scriptText">The PowerShell script text to validate.</param> |
| | | 16 | | /// <param name="error">Output error message if validation fails.</param> |
| | | 17 | | /// <returns>True if the script contains only a param() block; otherwise, false.</returns> |
| | | 18 | | internal static bool HasParamAndNothingAfterParam_Ast(string scriptText, out string? error) |
| | | 19 | | { |
| | 10 | 20 | | error = null; |
| | | 21 | | |
| | 10 | 22 | | var ast = Parser.ParseInput(scriptText, out var tokens, out var errors); |
| | | 23 | | |
| | 10 | 24 | | if (errors.Length > 0) |
| | | 25 | | { |
| | 2 | 26 | | error = "Parse error: " + string.Join("; ", errors.Select(e => e.Message)); |
| | 1 | 27 | | return false; |
| | | 28 | | } |
| | | 29 | | |
| | 9 | 30 | | if (ast is not ScriptBlockAst sbAst) |
| | | 31 | | { |
| | 0 | 32 | | error = "Not a ScriptBlockAst."; |
| | 0 | 33 | | return false; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | // Find param block |
| | 9 | 37 | | var paramAst = sbAst.ParamBlock; |
| | 9 | 38 | | if (paramAst is null) |
| | | 39 | | { |
| | 2 | 40 | | error = "No param() block found."; |
| | 2 | 41 | | return false; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | // Any executable statements in Begin/Process/End blocks? |
| | 7 | 45 | | var hasAnyStatements = |
| | 7 | 46 | | (sbAst.BeginBlock?.Statements?.Count ?? 0) > 0 || |
| | 7 | 47 | | (sbAst.ProcessBlock?.Statements?.Count ?? 0) > 0 || |
| | 7 | 48 | | (sbAst.EndBlock?.Statements?.Count ?? 0) > 0; |
| | | 49 | | |
| | 7 | 50 | | if (hasAnyStatements) |
| | | 51 | | { |
| | 3 | 52 | | error = "Statements found after param()."; |
| | 3 | 53 | | return false; |
| | | 54 | | } |
| | | 55 | | |
| | 4 | 56 | | return true; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Checks if the param() block is the last element in the ScriptBlock AST. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="sbAst">The ScriptBlock AST to check.</param> |
| | | 63 | | /// <returns>True if the param() block is the last element; otherwise, false.</returns> |
| | | 64 | | internal static bool IsParamLast(ScriptBlockAst sbAst) |
| | | 65 | | { |
| | 11 | 66 | | if (sbAst.ParamBlock is null) |
| | | 67 | | { |
| | 4 | 68 | | return false; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // Collect all executable statements in all blocks |
| | 7 | 72 | | var allStatements = |
| | 7 | 73 | | (sbAst.BeginBlock?.Statements ?? Enumerable.Empty<StatementAst>()) |
| | 7 | 74 | | .Concat(sbAst.ProcessBlock?.Statements ?? Enumerable.Empty<StatementAst>()) |
| | 7 | 75 | | .Concat(sbAst.EndBlock?.Statements ?? Enumerable.Empty<StatementAst>()); |
| | | 76 | | |
| | | 77 | | // param() must be the last executable element, so no statements should exist |
| | 7 | 78 | | return !allStatements.Any(); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Checks if the provided PowerShell ScriptBlock has the param() block as the last element. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="scriptBlock">The PowerShell ScriptBlock to check.</param> |
| | | 85 | | /// <returns>True if the param() block is the last element; otherwise, false.</returns> |
| | | 86 | | internal static bool IsParamLast(ScriptBlock scriptBlock) |
| | | 87 | | { |
| | 7 | 88 | | var ast = scriptBlock?.Ast; |
| | | 89 | | |
| | 7 | 90 | | return ast is FunctionDefinitionAst funcDefAst |
| | 7 | 91 | | ? IsParamLast(funcDefAst.Body) |
| | 7 | 92 | | : ast is ScriptBlockAst sbAst && IsParamLast(sbAst); |
| | | 93 | | } |
| | | 94 | | } |