Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Mar 15, 2024
1 parent 91a184b commit 6682149
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 30 deletions.
3 changes: 2 additions & 1 deletion PwshSpectreConsole.Docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions PwshSpectreConsole.Docs/src/components/Asciinema.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
</script>
34 changes: 21 additions & 13 deletions PwshSpectreConsole.Docs/src/powershell/Helpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ function Get-Tag {
function Update-HashFilesInGit {
param (
[string] $StagingPath,
[string] $OutputPath
[string] $OutputPath,
[switch] $NoCommit
)

Push-Location
Expand All @@ -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"
}
Expand Down Expand Up @@ -143,7 +148,8 @@ function Update-HelpFiles {
param (
[string] $StagingPath,
[string] $OutputPath,
[string] $AsciiCastOutputPath
[string] $AsciiCastOutputPath,
[switch] $NoCommit
)

Push-Location
Expand All @@ -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"
}
Expand Down Expand Up @@ -218,8 +228,6 @@ $script:AsciiCastTemplate = @'
src={CAST_NAME}
settings={{
loop: false,
preload: true,
poster: 'npt:0:0.5',
terminalLineHeight: 1.1,
theme: "spectre"
}}
Expand Down
18 changes: 11 additions & 7 deletions PwshSpectreConsole.Docs/src/powershell/UpdateDocs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ param(
[ValidateSet("dev", "prerelease", "main")]
[string]$Branch = "dev",
[switch]$NonInteractive,
[switch]$NoBuild
[switch]$NoBuild,
[switch]$NoCommit
)

$ErrorActionPreference = "Stop"
Expand Down Expand Up @@ -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 "*-*" }
Expand Down Expand Up @@ -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
Expand All @@ -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") {
Expand Down
7 changes: 5 additions & 2 deletions PwshSpectreConsole/public/progress/Add-SpectreJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
:::
Expand All @@ -20,15 +21,17 @@ function Add-SpectreJob {
The PowerShell job to add to the context.
.EXAMPLE
Invoke-SpectreCommandWithProgress -ScriptBlock {
$jobOutcomes = Invoke-SpectreCommandWithProgress -ScriptBlock {
param (
$Context
)
$jobs = @()
$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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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 (
Expand Down

0 comments on commit 6682149

Please sign in to comment.