From 6682149d299e2fc1664d7bd5b850bf3e48e3c7ee Mon Sep 17 00:00:00 2001 From: "Shaun Lawrie (via GitHub Actions)" Date: Sat, 16 Mar 2024 01:26:35 +1300 Subject: [PATCH] Update examples --- PwshSpectreConsole.Docs/package.json | 3 +- .../src/components/Asciinema.astro | 5 +-- .../src/powershell/Helpers.psm1 | 34 ++++++++++++------- .../src/powershell/UpdateDocs.ps1 | 18 ++++++---- .../public/progress/Add-SpectreJob.ps1 | 7 ++-- .../Invoke-SpectreCommandWithProgress.ps1 | 13 +++++-- .../Invoke-SpectreCommandWithStatus.ps1 | 5 ++- 7 files changed, 55 insertions(+), 30 deletions(-) diff --git a/PwshSpectreConsole.Docs/package.json b/PwshSpectreConsole.Docs/package.json index 6aabdea9..1aea7887 100644 --- a/PwshSpectreConsole.Docs/package.json +++ b/PwshSpectreConsole.Docs/package.json @@ -8,7 +8,8 @@ "build": "astro build", "preview": "astro preview", "astro": "astro", - "update-docs": "pwsh -NoProfile -File ./src/powershell/UpdateDocs.ps1" + "update-docs": "pwsh -NoProfile -File ./src/powershell/UpdateDocs.ps1", + "update-docs:no-commit": "pwsh -NoProfile -File ./src/powershell/UpdateDocs.ps1 -NoCommit" }, "dependencies": { "@astrojs/starlight": "^0.11.1", diff --git a/PwshSpectreConsole.Docs/src/components/Asciinema.astro b/PwshSpectreConsole.Docs/src/components/Asciinema.astro index 51f1bbb1..14d7db9d 100644 --- a/PwshSpectreConsole.Docs/src/components/Asciinema.astro +++ b/PwshSpectreConsole.Docs/src/components/Asciinema.astro @@ -51,10 +51,7 @@ const { src, settings } = Astro.props; players.set(elem, player); - // Wait a fe seconds before attaching the observer in case it's already on the screen - setTimeout(() => { - observer.observe(elem); - }, 2000); + observer.observe(elem); }); } diff --git a/PwshSpectreConsole.Docs/src/powershell/Helpers.psm1 b/PwshSpectreConsole.Docs/src/powershell/Helpers.psm1 index 63c16c83..2bc1b49a 100644 --- a/PwshSpectreConsole.Docs/src/powershell/Helpers.psm1 +++ b/PwshSpectreConsole.Docs/src/powershell/Helpers.psm1 @@ -93,7 +93,8 @@ function Get-Tag { function Update-HashFilesInGit { param ( [string] $StagingPath, - [string] $OutputPath + [string] $OutputPath, + [switch] $NoCommit ) Push-Location @@ -113,9 +114,13 @@ function Update-HashFilesInGit { # Update git hash files try { Set-Location $OutputPath - Write-Host "Committing hash files" - git add "*.sha256" *>$null - git commit -m "[skip ci] Update doc hashfiles" *>$null + if(!$NoCommit) { + Write-Host "Committing hash files" + git add "*.sha256" *>$null + git commit -m "[skip ci] Update doc hashfiles" *>$null + } else { + Write-Host "Skipping commit of hash files" + } } catch { Write-Host "No changes to commit" } @@ -143,7 +148,8 @@ function Update-HelpFiles { param ( [string] $StagingPath, [string] $OutputPath, - [string] $AsciiCastOutputPath + [string] $AsciiCastOutputPath, + [switch] $NoCommit ) Push-Location @@ -162,12 +168,16 @@ function Update-HelpFiles { try { Set-Location $OutputPath - Write-Host "Committing mdx files" - git add "*.mdx" *>$null - Set-Location $AsciiCastOutputPath - Write-Host "Committing cast files" - git add "*.cast" *>$null - git commit -m "[skip ci] Update docs" *>$null + if(!$NoCommit) { + Write-Host "Committing mdx files" + git add "*.mdx" *>$null + Set-Location $AsciiCastOutputPath + Write-Host "Committing cast files" + git add "*.cast" *>$null + git commit -m "[skip ci] Update docs" *>$null + } else { + Write-Host "Skipping commit of mdx and cast files" + } } catch { Write-Host "No changes to commit" } @@ -218,8 +228,6 @@ $script:AsciiCastTemplate = @' src={CAST_NAME} settings={{ loop: false, - preload: true, - poster: 'npt:0:0.5', terminalLineHeight: 1.1, theme: "spectre" }} diff --git a/PwshSpectreConsole.Docs/src/powershell/UpdateDocs.ps1 b/PwshSpectreConsole.Docs/src/powershell/UpdateDocs.ps1 index 735f1023..840aa8e1 100644 --- a/PwshSpectreConsole.Docs/src/powershell/UpdateDocs.ps1 +++ b/PwshSpectreConsole.Docs/src/powershell/UpdateDocs.ps1 @@ -3,7 +3,8 @@ param( [ValidateSet("dev", "prerelease", "main")] [string]$Branch = "dev", [switch]$NonInteractive, - [switch]$NoBuild + [switch]$NoBuild, + [switch]$NoCommit ) $ErrorActionPreference = "Stop" @@ -59,7 +60,7 @@ foreach ($doc in $docs) { # Update the hash files in git so the modified files can be detected # I regret doing it this way but cbf changing it now -Update-HashFilesInGit -StagingPath $stagingPath -OutputPath $outputPath +Update-HashFilesInGit -StagingPath $stagingPath -OutputPath $outputPath -NoCommit:$NoCommit # Format the files for astro $docs = Get-ChildItem $stagingPath -Filter "*.md" -Recurse | Where-Object { $_.Name -like "*-*" } @@ -153,16 +154,19 @@ foreach ($doc in $docs) { } Write-Host "Modified sample:" Write-Host -ForegroundColor DarkGray $code - try { + if($code -like "*Write-Error*") { + $ErrorActionPreference = "Continue" + $previousErrorActionPreference = $ErrorActionPreference + Invoke-Expression $code + $ErrorActionPreference = $previousErrorActionPreference + } else { Invoke-Expression $code - } catch { - Write-Warning "Error generating sample: $_" } $recording = Stop-SpectreRecording -Title "Example $([int]$example++)" $castName = ($doc.Name -replace '.md$', '' -replace '-', '').ToLower() + "Example$example" Set-Content -Path "$asciiCastOutputPath\$castName.cast" -Value $recording - $imports += "import $castName from '../../../../assets/examples/$castName.cast'`n" + $imports += "import $castName from '../../../../assets/examples/$castName.cast?url'`n" # Replace the code block with the ascii cast $castTemplate = Get-AsciiCastTemplate -Name $castName @@ -180,7 +184,7 @@ foreach ($doc in $docs) { } # Copy the files into the output directory in a way that doesn't crash the astro dev server -Update-HelpFiles -StagingPath $stagingPath -AsciiCastOutputPath $asciiCastOutputPath -OutputPath $outputPath +Update-HelpFiles -StagingPath $stagingPath -AsciiCastOutputPath $asciiCastOutputPath -OutputPath $outputPath -NoCommit:$NoCommit # Set some overrides to indicate it's the pre-release site if($Branch -eq "prerelease") { diff --git a/PwshSpectreConsole/public/progress/Add-SpectreJob.ps1 b/PwshSpectreConsole/public/progress/Add-SpectreJob.ps1 index e4fd471b..53db2d92 100644 --- a/PwshSpectreConsole/public/progress/Add-SpectreJob.ps1 +++ b/PwshSpectreConsole/public/progress/Add-SpectreJob.ps1 @@ -4,7 +4,8 @@ function Add-SpectreJob { Adds a Spectre job to a list of jobs. .DESCRIPTION - This function adds a Spectre job to the list of jobs you want to wait for with Wait-SpectreJobs. + This function adds a Spectre job to the list of jobs you want to wait for with Wait-SpectreJobs. + To retrieve the outcome of the job you need to use the standard PowerShell Receive-Job cmdlet. :::note This is only used inside `Invoke-SpectreCommandWithProgress` where the Spectre ProgressContext object is exposed. ::: @@ -20,7 +21,7 @@ function Add-SpectreJob { The PowerShell job to add to the context. .EXAMPLE - Invoke-SpectreCommandWithProgress -ScriptBlock { + $jobOutcomes = Invoke-SpectreCommandWithProgress -ScriptBlock { param ( $Context ) @@ -28,7 +29,9 @@ function Add-SpectreJob { $jobs += Add-SpectreJob -Context $Context -JobName "job 1" -Job (Start-Job { Start-Sleep -Seconds 2 }) $jobs += Add-SpectreJob -Context $Context -JobName "job 2" -Job (Start-Job { Start-Sleep -Seconds 4 }) Wait-SpectreJobs -Context $Context -Jobs $jobs + return $jobs.Job } + $jobOutcomes | Format-SpectreTable -Property Id, Name, PSJobTypeName, State, Command #> [Reflection.AssemblyMetadata("title", "Add-SpectreJob")] param ( diff --git a/PwshSpectreConsole/public/progress/Invoke-SpectreCommandWithProgress.ps1 b/PwshSpectreConsole/public/progress/Invoke-SpectreCommandWithProgress.ps1 index 4253e583..4ca5fd5f 100644 --- a/PwshSpectreConsole/public/progress/Invoke-SpectreCommandWithProgress.ps1 +++ b/PwshSpectreConsole/public/progress/Invoke-SpectreCommandWithProgress.ps1 @@ -57,27 +57,36 @@ function Invoke-SpectreCommandWithProgress { } .EXAMPLE - Invoke-SpectreCommandWithProgress -ScriptBlock { + $result = Invoke-SpectreCommandWithProgress -ScriptBlock { param ( $Context ) $task1 = $Context.AddTask("Completing a task with an unknown duration") $task1.IsIndeterminate = $true Start-Sleep -Seconds 5 $task1.Value = 100 + + return "Some result" } + Write-SpectreHost "Result: $result" .EXAMPLE - Invoke-SpectreCommandWithProgress -ScriptBlock { + $result = Invoke-SpectreCommandWithProgress -ScriptBlock { param ( $Context ) $job = Add-SpectreJob -Context $Context -JobName "Doing some work" -Job ( Start-Job { Start-Sleep -Seconds 10 + return 1234 } ) Wait-SpectreJobs -Context $Context -Jobs $job -EstimatedDurationSeconds 5 + + $result = Receive-Job -Job $job.Job + + return $result } + Write-SpectreHost "Result: $result" #> [Reflection.AssemblyMetadata("title", "Invoke-SpectreCommandWithProgress")] param ( diff --git a/PwshSpectreConsole/public/progress/Invoke-SpectreCommandWithStatus.ps1 b/PwshSpectreConsole/public/progress/Invoke-SpectreCommandWithStatus.ps1 index f8ac96e8..6e0746d7 100644 --- a/PwshSpectreConsole/public/progress/Invoke-SpectreCommandWithStatus.ps1 +++ b/PwshSpectreConsole/public/progress/Invoke-SpectreCommandWithStatus.ps1 @@ -22,7 +22,7 @@ function Invoke-SpectreCommandWithStatus { The color of the spinner. Valid values can be found with Get-SpectreDemoColors. .EXAMPLE - Invoke-SpectreCommandWithStatus -Spinner "Dots2" -Title "Showing a spinner..." -ScriptBlock { + $result = Invoke-SpectreCommandWithStatus -Spinner "Dots2" -Title "Showing a spinner..." -ScriptBlock { # Write updates to the host using Write-SpectreHost Start-Sleep -Seconds 1 Write-SpectreHost "`n[grey]LOG:[/] Doing some work " @@ -31,7 +31,10 @@ function Invoke-SpectreCommandWithStatus { Start-Sleep -Seconds 1 Write-SpectreHost "`n[grey]LOG:[/] Done " Start-Sleep -Seconds 1 + Write-SpectreHost " " + return "Some result" } + Write-SpectreHost "Result: $result" #> [Reflection.AssemblyMetadata("title", "Invoke-SpectreCommandWithStatus")] param (