Skip to content

Commit

Permalink
🐛 Update-PSKoan - Fix "Yes to All" for ShouldProcess Prompts (#292)
Browse files Browse the repository at this point in the history
* ♻️ Tidy ShouldProcess support
Currently, Update-PSKoan prompts for every file updated,
even if the user selects "A" for "Yes to All"
Fix is to have the ShouldProcess check only in the iterating function,
and allow the child function to use that preference instead of checking

* 🎨 Fix indentation issue
Silly PSSA  pipe indent format rules...

* 🎨 Adjust verb form to fit prompt format better
  • Loading branch information
vexx32 committed Oct 21, 2019
1 parent f6ad6b8 commit 29fd4d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
22 changes: 11 additions & 11 deletions PSKoans/Private/Update-PSKoanFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ function Update-PSKoanFile {
$PSBoundParameters.Remove('Confirm') > $null
$PSBoundParameters.Remove('WhatIf') > $null
Get-PSKoan @PSBoundParameters | ForEach-Object {
$moduleKoans = Get-KoanIt -Path $_.Path | ForEach-Object {
[PSCustomObject]@{
ID = $_.ID
Name = $_.Name
Ast = $_.Ast
}
} | Group-Object -Property ID -AsHashTable -AsString
$moduleKoans = Get-KoanIt -Path $_.Path |
ForEach-Object {
[PSCustomObject]@{
ID = $_.ID
Name = $_.Name
Ast = $_.Ast
}
} |
Group-Object -Property ID -AsHashTable -AsString

if (-not $moduleKoans) {
# Handles topics which do not have It blocks.
Expand All @@ -68,7 +70,7 @@ function Update-PSKoanFile {
'ID'
'Name'
'Ast'
@{ Name = 'SourceAst'; Expression = { $moduleKoans[$_.ID].Ast }}
@{ Name = 'SourceAst'; Expression = { $moduleKoans[$_.ID].Ast } }
) |
Sort-Object { $_.SourceAst.Extent.StartLineNumber } -Descending |
ForEach-Object {
Expand All @@ -82,9 +84,7 @@ function Update-PSKoanFile {
)
}

if ($PSCmdlet.ShouldProcess($path, 'Updating Koan File')) {
Set-Content -Path $path -Value $content.TrimEnd() -NoNewline
}
Set-Content -Path $path -Value $content.TrimEnd() -NoNewline
}
}
else {
Expand Down
19 changes: 9 additions & 10 deletions PSKoans/Public/Update-PSKoan.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using namespace System.Collections.Generic

function Update-PSKoan {
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName = 'TopicOnly',
ConfirmImpact = "High",
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'TopicOnly', ConfirmImpact = "High",
HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Update-PSKoan.md')]
[OutputType([void])]
param(
Expand Down Expand Up @@ -36,8 +33,8 @@ function Update-PSKoan {
}
switch ($pscmdlet.ParameterSetName) {
'IncludeModule' { $GetParams['IncludeModule'] = $IncludeModule }
'ModuleOnly' { $GetParams['Module'] = $Module }
{ $Topic } { $GetParams['Topic'] = $Topic }
'ModuleOnly' { $GetParams['Module'] = $Module }
{ $Topic } { $GetParams['Topic'] = $Topic }
}
$ModuleKoanList = Get-PSKoan @GetParams | Group-Object Topic -AsHashtable -AsString

Expand Down Expand Up @@ -77,14 +74,16 @@ function Update-PSKoan {
#>
{ $ModuleKoanList.ContainsKey($_) -and $UserKoanList.ContainsKey($_) } {
if ($UserKoanList[$_].Path -ne $DestinationPath) {
if ($PSCmdlet.ShouldProcess($_, 'Moving Topic')) {
if ($PSCmdlet.ShouldProcess($_, 'Move Topic')) {
Write-Verbose "Moving $_"

$UserKoanList[$_].Path | Move-Item -Destination $DestinationPath
}
}

Update-PSKoanFile -Topic $_
if ($PSCmdlet.ShouldProcess($_, 'Update Koan Topic')) {
Update-PSKoanFile -Topic $_
}

continue
}
Expand All @@ -95,7 +94,7 @@ function Update-PSKoan {
location.
#>
{ $ModuleKoanList.ContainsKey($_) } {
if ($PSCmdlet.ShouldProcess($_, 'Adding Topic')) {
if ($PSCmdlet.ShouldProcess($_, 'Add Topic')) {
Write-Verbose "Adding $_"

$ModuleKoanList[$_].Path | Copy-Item -Destination $DestinationPath -Force
Expand All @@ -110,7 +109,7 @@ function Update-PSKoan {
or renamed and delete the file from the users koan location.
#>
{ $UserKoanList.ContainsKey($_) } {
if ($PSCmdlet.ShouldProcess($_, 'Removing Topic')) {
if ($PSCmdlet.ShouldProcess($_, 'Remove Topic')) {
Write-Verbose "Removing $_"

$UserKoanList[$_].Path | Remove-Item
Expand Down

0 comments on commit 29fd4d1

Please sign in to comment.