Skip to content

Commit

Permalink
Permit Specifying and Listing Topics for Measure-Karma (#101)
Browse files Browse the repository at this point in the history
* Update Measure-Karma to support listing topics
Update Measure-Karma to support specifying topics to check

* Update meditation prompts to support specified topics

* Rename validator class for clarity

* Rename list switch for consistency

* Replace reference to old function name in Measure-Karma CBH
  • Loading branch information
vexx32 committed Dec 14, 2018
1 parent 3802fdf commit 2f0fcea
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 11 deletions.
41 changes: 37 additions & 4 deletions PSKoans/Private/Write-MeditationPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@

[Parameter(Mandatory, ParameterSetName = 'Enlightened')]
[switch]
$Complete
$Complete,

[Parameter()]
[string[]]
$Topic
)

$Red = @{ForegroundColor = "Red"}
Expand Down Expand Up @@ -92,6 +96,13 @@ $Meditation
Your path thus far:
"@
WisdomTopic = @"
$($Koan -replace "`n","`n ")
Your path thus far in topics $($Topic -join ', '):
"@
OpenFolder = @"
Expand All @@ -103,6 +114,14 @@ You may run 'Measure-Karma -Meditate' to begin your meditation.
You cast your gaze back upon the path that you have walked:
"@
CompletedTopic = @"
Congratulations! You have taken the first steps towards enlightenment.
You have completed: $($Topic -join ', ')
You cast your gaze back upon the path that you have walked:
"@
BookSuggestion = @"
Expand All @@ -126,7 +145,13 @@ You may run 'Measure-Karma -Meditate' to begin your meditation.
Write-Host @Blue $Prompts['Meditate']
Write-Host @Red $Prompts['Subject']
Start-Sleep @SleepTime
Write-Host @Blue $Prompts['Wisdom']

if ($PSBoundParameters.ContainsKey('Topic')) {
Write-Host @Blue $Prompts['WisdomTopic']
}
else {
Write-Host @Blue $Prompts['Wisdom']
}

Write-Verbose 'Calculating progress...'
$ProgressAmount = "$KoansPassed/$TotalKoans"
Expand All @@ -143,7 +168,12 @@ You may run 'Measure-Karma -Meditate' to begin your meditation.
break
}
'Enlightened' {
Write-Host @Blue $Prompts['Completed']
if ($PSBoundParameters.ContainsKey('Topic')) {
Write-Host @Blue $Prompts['CompletedTopic']
}
else {
Write-Host @Blue $Prompts['Completed']
}

$ProgressAmount = "$KoansPassed/$TotalKoans"
[int]$ProgressWidth = $host.UI.RawUI.WindowSize.Width * 0.8 - ($ProgressAmount.Length + 4)
Expand All @@ -155,7 +185,10 @@ You may run 'Measure-Karma -Meditate' to begin your meditation.
$ProgressAmount
) | Write-Host @Blue

Write-Host @Blue $Prompts['BookSuggestion']
if (-not $PSBoundParameters.ContainsKey('Topic')) {
Write-Host @Blue $Prompts['BookSuggestion']
}

break
}
}
Expand Down
63 changes: 56 additions & 7 deletions PSKoans/Public/Measure-Karma.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
function Measure-Karma {
using namespace System.Management.Automation

class KoanTopics : IValidateSetValuesGenerator {
[string[]] GetValidValues() {
$Values = Get-ChildItem -Path $env:PSKoans_Folder -Recurse -Filter '*.Koans.ps1' |
Sort-Object -Property BaseName |
ForEach-Object {
$_.BaseName -replace '\.Koans$'
}

return $Values
}
}

function Measure-Karma {
<#
.SYNOPSIS
Reflect on your progress and check your answers.
.DESCRIPTION
Get-Enlightenment executes Pester against the koans to evaluate if you have made the necessary
Measure-Karma executes Pester against the koans to evaluate if you have made the necessary
corrections for success.
.PARAMETER Topic
Execute koans only from the selected Topic(s).
.PARAMETER ListTopics
Output a complete list of available koan topics.
.PARAMETER Contemplate
Opens your local koan folder.
.PARAMETER Reset
Expand All @@ -30,10 +48,21 @@
Module: PSKoans
#>
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = "Default")]
[Alias('Invoke-PSKoans', 'Test-Koans', 'Get-Enlightenment', 'Meditate','Clear-Path')]
[Alias('Invoke-PSKoans', 'Test-Koans', 'Get-Enlightenment', 'Meditate', 'Clear-Path')]
param(
[Parameter(ParameterSetName = 'Default')]
[Alias('Koan', 'File')]
[ValidateSet([KoanTopics])]
[string[]]
$Topic,

[Parameter(Mandatory, ParameterSetName = 'ListKoans')]
[Alias('ListKoans')]
[switch]
$ListTopics,

[Parameter(Mandatory, ParameterSetName = "OpenFolder")]
[Alias('Koans', 'Meditate')]
[Alias('Meditate')]
[switch]
$Contemplate,

Expand All @@ -42,11 +71,17 @@
$Reset
)
switch ($PSCmdlet.ParameterSetName) {
"Reset" {
'ListKoans' {
Get-ChildItem -Path $env:PSKoans_Folder -Recurse -File -Filter '*.Koans.ps1' |
ForEach-Object {
$_.BaseName -replace '\.Koans$'
}
}
'Reset' {
Write-Verbose "Reinitializing koan directory"
Initialize-KoanDirectory
}
"OpenFolder" {
'OpenFolder' {
Write-Verbose "Opening koans folder"
if (Get-Command -Name 'Code' -ErrorAction SilentlyContinue) {
Start-Process -FilePath 'code' -ArgumentList $env:PSKoans_Folder -NoNewWindow
Expand All @@ -62,6 +97,10 @@

Write-Verbose 'Sorting koans...'
$SortedKoanList = Get-ChildItem -Path $env:PSKoans_Folder -Recurse -Filter '*.Koans.ps1' |
Where-Object {
-not $PSBoundParameters.ContainsKey('Topic') -or
$_.BaseName -replace '\.Koans$' -in $Topic
} |
Get-Command { $_.FullName } |
Where-Object { $_.ScriptBlock.Attributes.Where{ $_.TypeID -match 'KoanAttribute' }.Count -gt 0 } |
Sort-Object { $_.ScriptBlock.Attributes.Where( {$_.TypeID -match 'KoanAttribute'}).Position }
Expand Down Expand Up @@ -96,7 +135,7 @@

Write-Verbose "Karma: $KoansPassed"
if ($PesterTests.FailedCount -gt 0) {
Write-Verbose "Your karma has been damaged."
Write-Verbose 'Your karma has been damaged.'
break
}
}
Expand All @@ -114,6 +153,11 @@
KoansPassed = $KoansPassed
TotalKoans = $TotalKoans
}

if ($PSBoundParameters.ContainsKey('Topic')) {
$Meditation.Add('Topic', $Topic)
}

Write-MeditationPrompt @Meditation
}
else {
Expand All @@ -122,6 +166,11 @@
KoansPassed = $KoansPassed
TotalKoans = $PesterTestCount
}

if ($PSBoundParameters.ContainsKey('Topic')) {
$Meditation.Add('Topic', $Topic)
}

Write-MeditationPrompt @Meditation
}
}
Expand Down

0 comments on commit 2f0fcea

Please sign in to comment.