From 13458813fb86fa02da7cb0bf67356c8859bcd07f Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 22 Feb 2023 23:18:29 +0000 Subject: [PATCH 1/3] Added first attempt at validatePattern functionality for text, user-email and user-fullname --- Plaster/InvokePlaster.ps1 | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Plaster/InvokePlaster.ps1 b/Plaster/InvokePlaster.ps1 index 7b0644d..9d2e3e1 100644 --- a/Plaster/InvokePlaster.ps1 +++ b/Plaster/InvokePlaster.ps1 @@ -538,13 +538,23 @@ function Invoke-Plaster { } } - function PromptForInput($prompt, $default) { + function PromptForInput($prompt, $default, $pattern) { + if (!$pattern) { + $patternMatch = $true + } + do { $value = Read-Host -Prompt $prompt if (!$value -and $default) { $value = $default } - } while (!$value) + + if ($pattern) { + if ($value -match $pattern) { + $patternMatch = $true + } + } + } while (!$value -or !$patternMatch) $value } @@ -625,6 +635,8 @@ function Invoke-Plaster { $type = $Node.type $store = $Node.store + $pattern = $Node.pattern + $condition = $Node.condition $default = InterpolateAttributeValue $Node.default (GetErrorLocationParameterAttrVal $name default) @@ -675,6 +687,12 @@ function Invoke-Plaster { # Some default values might not come from the template e.g. some are harvested from .gitconfig if it exists. $defaultNotFromTemplate = $false + $splat = @{} + + if ($null -ne $pattern) { + $splat.Add('pattern', $pattern) + } + # Now prompt user for parameter value based on the parameter type. switch -regex ($type) { 'text' { @@ -689,7 +707,7 @@ function Invoke-Plaster { } } # Prompt the user for text input. - $value = PromptForInput $prompt $default + $value = PromptForInput $prompt $default @splat $valueToStore = $value } 'user-fullname' { @@ -710,7 +728,7 @@ function Invoke-Plaster { } # Prompt the user for text input. - $value = PromptForInput $prompt $default + $value = PromptForInput $prompt $default @splat $valueToStore = $value } 'user-email' { @@ -731,7 +749,7 @@ function Invoke-Plaster { } # Prompt the user for text input. - $value = PromptForInput $prompt $default + $value = PromptForInput $prompt $default @splat $valueToStore = $value } 'choice|multichoice' { From 8986d1766269311822332b15c1fa9fabd3ebe75d Mon Sep 17 00:00:00 2001 From: erikgraa Date: Thu, 23 Feb 2023 00:44:40 +0100 Subject: [PATCH 2/3] Added pattern to schema. Added examples. --- Plaster/Schema/PlasterManifest-v1.xsd | 5 + examples/plasterManifest-validatePattern.xml | 191 +++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 examples/plasterManifest-validatePattern.xml diff --git a/Plaster/Schema/PlasterManifest-v1.xsd b/Plaster/Schema/PlasterManifest-v1.xsd index fc0ca7a..9084743 100644 --- a/Plaster/Schema/PlasterManifest-v1.xsd +++ b/Plaster/Schema/PlasterManifest-v1.xsd @@ -212,6 +212,11 @@ The default value for the parameter which the user will be able to accept or change. + + + Used for parameter input validation. If the value matches the pattern, the value is accepted. Otherwise it is not. + + diff --git a/examples/plasterManifest-validatePattern.xml b/examples/plasterManifest-validatePattern.xml new file mode 100644 index 0000000..a579a61 --- /dev/null +++ b/examples/plasterManifest-validatePattern.xml @@ -0,0 +1,191 @@ + + + + + ac9f688f-503d-48c9-833a-f0d767d92f47 + NewPowerShellModule + + New PowerShell Module + + Plaster template for creating the files for a PowerShell module. + 0.2.0 + Module, ModuleManifest, Build + + + + + + + + + + + + + + + + + + + + + + + + + + + +Scaffold a PowerShell Module with the files required to run Pester tests, build with PSake and publish to the PSGallery. + + + + + + + + + + + + + + + + + + + (?s)^(.*) + // Author: $PLASTER_PARAM_FullName`r`n`$1 + + + + + + + + + + + + + + + + + + + `n`nYour new PowerShell module project $PLASTER_PARAM_ModuleName + with Git version control + has been created. + + +You can build your project by executing the 'build' task by pressing Ctrl+P, then type 'task build'. +You can publish your project to the PSGallery by pressing Ctrl+P, then type 'task publish'. + + + + +A Pester test has been created to validate the module's manifest file. Add additional test to the Tests directory. +You can run the Pester tests in your project by executing the 'test' task by pressing Ctrl+P, then type 'task test'. + + + + +You can generate help and additional documentation using platyPS by running the 'docs' task by pressing Ctrl+P, +then type 'task docs'. Add additional documentation written in platyPS markdown to the docs directory. You can +update the docs by running the 'docs' task again. + + + + From f9e4c12ab0543025f55879fc36c6276bee8c8092 Mon Sep 17 00:00:00 2001 From: erikgraa Date: Thu, 23 Feb 2023 00:53:29 +0100 Subject: [PATCH 3/3] Updates to input validation logic. Added debugging. --- Plaster/InvokePlaster.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Plaster/InvokePlaster.ps1 b/Plaster/InvokePlaster.ps1 index 9d2e3e1..ca38dbb 100644 --- a/Plaster/InvokePlaster.ps1 +++ b/Plaster/InvokePlaster.ps1 @@ -547,12 +547,15 @@ function Invoke-Plaster { $value = Read-Host -Prompt $prompt if (!$value -and $default) { $value = $default + $patternMatch = $true } - - if ($pattern) { + elseif ($value -and $pattern) { if ($value -match $pattern) { $patternMatch = $true } + else { + $PSCmdlet.WriteDebug("Value '$value' did not match the pattern '$pattern'") + } } } while (!$value -or !$patternMatch)