Skip to content

Commit

Permalink
✨ Add Get-Karma function, rename Measure-Karma to Show-Karma (#238)
Browse files Browse the repository at this point in the history
* 🚚 Rename file before renaming function

* 💄 Rename Measure-Karma -> Show-Karma
Function name more appropriate for purpose.
Alias left behind for Measure-Karma for backwards-compatibility.

* ✨ Make Get-PSKoanFile a public function

* 📝 Update docs and doc links for Show-Karma

* 📝 Update Get-Blank docs

* 📝 Update Register-Advice docs

* 📝 Update Reset-PSKoan docs

* 📝 Update help for Set-PSKoanLocation

* 📝 Update help for Show-Advice

* 📝 add blank at EOF

* 📝 Update help uris

* ✨ Add Get-Karma command
Move internal logic from Show-Karma to Get-Karma where reasonable

* ✅ Update Show-Karma tests

* ✅ Update Show-Karma tests

* ✅ Add Get-Karma tests

* ♻️ Use Get-PSKoanFile for completions

* 🔧 Invoke-Koan needs to chill
This adds a short sleep between each status check for Invoke-Koan.
Constantly keeping tabs on the other thread is a waste of cpu.

* 🎨 Remove some tabs in Get-Koan

* 🔧 Recurse into Get-Karma, not Show-Karma

* ✅ Fix failing tests, trim tests
Removed tests only applicable to Show-Karma
Fixed mock & test conditions

* ✅ Fix mock in Show-Karma tests

* 📝 Quick update pass for documentation

* ✨ Change Get-Karma output
✅ Update tests

* 🔧 Fix CurrentTopic references

* 🎨 Update type display

* ✅ Update tests
  • Loading branch information
vexx32 committed Sep 8, 2019
1 parent b03bc1a commit 0cf03c8
Show file tree
Hide file tree
Showing 25 changed files with 745 additions and 417 deletions.
Binary file modified PSKoans/PSKoans.psd1
Binary file not shown.
20 changes: 17 additions & 3 deletions PSKoans/PSKoans.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class KoanAttribute : Attribute {
[uint32] $Position = [uint32]::MaxValue
[string] $Module = '_powershell'

Koan() {}
Koan() { }
}

class Koan : KoanAttribute {
Koan() : base() {}
Koan() : base() { }
}

# load classes
Expand Down Expand Up @@ -93,4 +93,18 @@ try {
}
}
}
catch {}
catch { }

$TypeData = @{
TypeName = 'PSKoans.Result'
DefaultDisplayPropertySet = @(
'Describe'
'It'
'Expectation'
'Meditation'
'KoansPassed'
'TotalKoans'
'CurrentTopic'
)
}
Update-TypeData @TypeData -Force
4 changes: 2 additions & 2 deletions PSKoans/Private/Get-Koan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
ExceptionType = 'System.IO.FileLoadException'
ExceptionMessage = 'Could not read the koan file. The file is blocked and may have been copied from an Internet location. Use the Unblock-File to remove the block on the file.'
ErrorId = 'PSKoans.KoanFileIsBlocked'
ErrorCategory = 'ReadError'
TargetObject = $_.FullName
ErrorCategory = 'ReadError'
TargetObject = $_.FullName
}
$PSCmdlet.ThrowTerminatingError( (New-PSKoanErrorRecord @ErrorDetails) )
}
Expand Down
2 changes: 1 addition & 1 deletion PSKoans/Private/Invoke-Koan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

$Status = $Thread.BeginInvoke()

do { } until ($Status.IsCompleted)
do { Start-Sleep -Milliseconds 1 } until ($Status.IsCompleted)

$Thread.EndInvoke($Status)
}
Expand Down
27 changes: 4 additions & 23 deletions PSKoans/Private/Show-MeditationPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,7 @@ function Show-MeditationPrompt {
$Complete,

[Parameter(Mandatory, ParameterSetName = 'Meditation')]
[ValidateScript(
{
$MissingKeys = switch ($_) {
{ $_.Keys -notcontains 'Name' } { 'Name' }
{ $_.Keys -notcontains 'Completed' } { 'Completed' }
{ $_.Keys -notcontains 'Total' } { 'Total' }
}

$ErrorString = if ($MissingKeys) {
"Hashtable bound to -CurrentTopic was missing required keys: {0}" -f ($MissingKeys -join ',')
}
elseif ($_.Keys.Count -gt 3) {
'Hashtable bound to -CurrentTopic should only have keys: Name, Completed, Total.'
}

if ($ErrorString) { throw $ErrorString }
else { $true }
}
)]
[hashtable]
[PSObject]
$CurrentTopic,

[Parameter()]
Expand Down Expand Up @@ -130,21 +111,21 @@ function Show-MeditationPrompt {
Write-Host @Blue $script:MeditationPrompts['Path']

Write-Verbose 'Calculating progress...'
$TopicProgressAmount = "{0}/{1}" -f $CurrentTopic['Completed'], $CurrentTopic['Total']
$TopicProgressAmount = "{0}/{1}" -f $CurrentTopic.Completed, $CurrentTopic.Total
$TotalProgressAmount = "$KoansPassed/$TotalKoans"

[int] $ProgressWidth = $ConsoleWidth * 0.65 - ($TotalProgressAmount.Width + 12)
[int] $TopicProgressWidth = $ProgressWidth / 2

#region TopicProgressBar
if ($RequestedTopic.Count -ne 1) {
[int] $PortionDone = ($CurrentTopic['Completed'] / $CurrentTopic['Total']) * $TopicProgressWidth
[int] $PortionDone = ($CurrentTopic.Completed / $CurrentTopic.Total) * $TopicProgressWidth

$ProgressBar = " [{3}]: [{0}{1}] {2}" -f @(
"$([char]0x25a0)" * $PortionDone
"$([char]0x2015)" * ($TopicProgressWidth - $PortionDone)
$TopicProgressAmount
$CurrentTopic['Name']
$CurrentTopic.Name
)
Write-Host $ProgressBar @Blue
}
Expand Down
111 changes: 28 additions & 83 deletions PSKoans/Public/Measure-Karma.ps1 → PSKoans/Public/Get-Karma.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
function Measure-Karma {
function Get-Karma {
[CmdletBinding(DefaultParameterSetName = 'Default',
HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Measure-Karma.md')]
HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Get-Karma.md')]
[OutputType([void])]
[Alias('Invoke-PSKoans', 'Test-Koans', 'Get-Enlightenment', 'Meditate', 'Clear-Path')]
[Alias()]
param(
[Parameter(ParameterSetName = 'Default')]
[Alias('Koan', 'File')]
[ArgumentCompleter(
{
param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)

$Values = Get-PSKoanLocation | Get-ChildItem -Recurse -Filter '*.Koans.ps1' |
Sort-Object -Property BaseName |
ForEach-Object {
$_.BaseName -replace '\.Koans$'
}

$Values = (Get-PSKoanFile).Topic
return @($Values) -like "$WordToComplete*"
}
)]
Expand All @@ -26,60 +21,13 @@
[Parameter(Mandatory, ParameterSetName = 'ListKoans')]
[Alias('ListKoans')]
[switch]
$ListTopics,

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

[Parameter()]
[Alias()]
[switch]
$ClearScreen,

[Parameter(ParameterSetName = 'Default')]
[Alias()]
[switch]
$Detailed
$ListTopics
)
switch ($PSCmdlet.ParameterSetName) {
'ListKoans' {
Get-PSKoanLocation |
Get-ChildItem -Recurse -File -Filter '*.Koans.ps1' |
ForEach-Object {
$_.BaseName -replace '\.Koans$'
}
}
'OpenFolder' {
Write-Verbose "Opening koans folder"
if ( $env:PSKoans_EditorPreference -eq 'code-insiders' -and (Get-Command -Name 'code-insiders' -ErrorAction SilentlyContinue) ) {
$VSCodeSplat = @{
FilePath = 'code-insiders'
ArgumentList = '"{0}"' -f (Get-PSKoanLocation)
NoNewWindow = $true
}
Start-Process @VSCodeSplat
}
elseif (Get-Command -Name 'code' -ErrorAction SilentlyContinue) {
$VSCodeSplat = @{
FilePath = 'code'
ArgumentList = '"{0}"' -f (Get-PSKoanLocation)
NoNewWindow = $true
}
Start-Process @VSCodeSplat
}
else {
Get-PSKoanLocation | Invoke-Item
}
Get-PSKoanFile
}
"Default" {
if ($ClearScreen) {
Clear-Host
}

Show-MeditationPrompt -Greeting

Write-Verbose 'Sorting koans...'
try {
if ($Topic) {
Expand Down Expand Up @@ -113,7 +61,7 @@
# Something's wrong; possibly a koan folder from older versions, or a folder exists but has no files
Write-Warning 'No koans found in your koan directory. Initiating full reset...'
Update-PSKoan -Confirm:$false
Measure-Karma @PSBoundParameters # Re-call ourselves with the same parameters
Get-Karma @PSBoundParameters # Re-call ourselves with the same parameters

return # Skip the rest of the function
}
Expand Down Expand Up @@ -141,40 +89,37 @@

$Meditation = if ($PesterTests.FailedCount -gt 0) {
$NextKoanFailed = $PesterTests.TestResult |
Where-Object Result -eq 'Failed' |
Select-Object -First 1

@{
DescribeName = $NextKoanFailed.Describe
Expectation = $NextKoanFailed.ErrorRecord
ItName = $NextKoanFailed.Name
Meditation = $NextKoanFailed.StackTrace
KoansPassed = $KoansPassed
TotalKoans = $TotalKoans
CurrentTopic = @{
Where-Object Result -eq 'Failed' |
Select-Object -First 1

[PSCustomObject]@{
PSTypeName = 'PSKoans.Result'
Describe = $NextKoanFailed.Describe
It = $NextKoanFailed.Name
Expectation = $NextKoanFailed.ErrorRecord
Meditation = $NextKoanFailed.StackTrace
KoansPassed = $KoansPassed
TotalKoans = $TotalKoans
CurrentTopic = [PSCustomObject]@{
Name = $KoanFile.Name -replace '\.Koans\.ps1$'
Completed = $PesterTests.PassedCount
Total = $PesterTests.TotalCount
}
Results = $PesterTests.TestResult
RequestedTopic = $Topic
}
}
else {
@{
Complete = $true
KoansPassed = $KoansPassed
TotalKoans = $TotalKoans
[PSCustomObject]@{
PSTypeName = 'PSKoans.CompleteResult'
KoansPassed = $KoansPassed
TotalKoans = $TotalKoans
RequestedTopic = $Topic
Complete = $true
}
}

if ($Detailed) {
$Meditation.Add('Results', $PesterTests.TestResult)
}

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

Show-MeditationPrompt @Meditation
$Meditation
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
function Get-PSKoanFile {
<#
.SYNOPSIS
Get the paths for module and user koan topics.
.DESCRIPTION
Returns objects describing the file paths for each topic.
.PARAMETER Topic
Specify one or more topic names or patterns to filter the list.
.EXAMPLE
Get-PSKoanFile
Returns all paths for all topics available in the module's Koan directory.
.EXAMPLE
Get-PSKoanFile -Topic AboutArrays
Returns path information for the AboutArrays topic.
#>

[CmdletBinding()]
[CmdletBinding(HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Get-PSKoanFile.md')]
[OutputType([PSObject[]])]
param(
[Parameter()]
Expand Down
9 changes: 2 additions & 7 deletions PSKoans/Public/Reset-PSKoan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ function Reset-PSKoan {
{
param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)

$Values = Get-PSKoanLocation | Get-ChildItem -Recurse -Filter '*.Koans.ps1' |
Sort-Object -Property BaseName |
ForEach-Object {
$_.BaseName -replace '\.Koans$'
}

$Values = (Get-PSKoanFile).Topic
return @($Values) -like "$WordToComplete*"
}
)]
Expand All @@ -35,7 +30,7 @@ function Reset-PSKoan {
$Context
)

$params = @{}
$params = @{ }
if ($Topic) {
$params.Topic = $Topic
}
Expand Down

0 comments on commit 0cf03c8

Please sign in to comment.