-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup-completion.ps1
More file actions
159 lines (129 loc) · 4.67 KB
/
setup-completion.ps1
File metadata and controls
159 lines (129 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
param(
[AllowEmptyString()]
[string]$ContainerName,
[string]$ProfilePath = $PROFILE,
[switch]$Force
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$defaultContainerName = "sspworkflow"
if ([string]::IsNullOrWhiteSpace($ContainerName)) {
$ContainerName = $defaultContainerName
}
function Get-ContainerRuntime {
if (Get-Command podman -ErrorAction SilentlyContinue) {
return "podman"
}
if (Get-Command docker -ErrorAction SilentlyContinue) {
return "docker"
}
throw "Neither podman nor docker is available in PATH."
}
function Get-RecipeNames {
param(
[Parameter(Mandatory = $true)]
[string]$Runtime,
[Parameter(Mandatory = $true)]
[string]$Name
)
$helpLines = & $Runtime exec $Name /bin/bash -lc "cd /root/sspworkflow/work && runConversionChain help" 2>&1
if ($LASTEXITCODE -ne 0) {
$joined = $helpLines -join [Environment]::NewLine
throw "Could not read recipes from container '$Name'. Start the container first with sspworkflow-run-prod.ps1. Details:$([Environment]::NewLine)$joined"
}
$recipes = [System.Collections.Generic.List[string]]::new()
$inRecipeSection = $false
foreach ($line in $helpLines) {
if ($line -match '^\s*Available recipes:\s*$') {
$inRecipeSection = $true
continue
}
if (-not $inRecipeSection) {
continue
}
if ($line -match '^\s{4}([a-zA-Z0-9-]+)\s+#') {
$recipes.Add($matches[1])
continue
}
if ($line -match '^\S') {
break
}
}
return $recipes | Sort-Object -Unique
}
$runtime = Get-ContainerRuntime
$repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$wrapperPath = Join-Path $repoRoot "runConversionChain.ps1"
$completionPath = Join-Path $HOME ".sspworkflow-completion.ps1"
$staticCompletions = @(
"theme=",
"debug=true",
"validate=true",
"develop=true",
"pagedjs-polyfill=true",
"docx-file=",
"xml-file=",
"html-file="
)
$recipeNames = Get-RecipeNames -Runtime $runtime -Name $ContainerName
$allCompletions = @($staticCompletions + $recipeNames) | Sort-Object -Unique
$quotedCompletions = $allCompletions | ForEach-Object { "'$_'" }
$completionLiteral = ($quotedCompletions -join ", ")
$completionScript = @"
`$global:SspworkflowCompletions = @($completionLiteral)
function global:runConversionChain {
param(
[Parameter(Position = 0)]
[ArgumentCompleter({
param(`$commandName, `$parameterName, `$wordToComplete, `$commandAst, `$fakeBoundParameters)
foreach (`$entry in `$global:SspworkflowCompletions) {
if (`$entry -like "`$wordToComplete*") {
[System.Management.Automation.CompletionResult]::new(`$entry, `$entry, 'ParameterValue', `$entry)
}
}
})]
[string]`$FirstArgument,
[Parameter(Position = 1, ValueFromRemainingArguments = `$true)]
[string[]]`$RemainingArgs
)
if ([string]::IsNullOrWhiteSpace(`$FirstArgument)) {
& "$wrapperPath" @RemainingArgs
}
else {
& "$wrapperPath" `$FirstArgument @RemainingArgs
}
}
Set-Alias -Name rcc -Value runConversionChain -Scope Global
Register-ArgumentCompleter -CommandName 'runConversionChain.ps1' -ScriptBlock {
param(`$commandName, `$parameterName, `$wordToComplete, `$commandAst, `$fakeBoundParameters)
foreach (`$entry in `$global:SspworkflowCompletions) {
if (`$entry -like "`$wordToComplete*") {
[System.Management.Automation.CompletionResult]::new(`$entry, `$entry, 'ParameterValue', `$entry)
}
}
}
"@
if ((Test-Path $completionPath) -and -not $Force) {
Write-Host "Updating existing completion file at $completionPath"
}
Set-Content -Path $completionPath -Value $completionScript -Encoding UTF8
$profileDir = Split-Path -Parent $ProfilePath
if (-not (Test-Path $profileDir)) {
New-Item -ItemType Directory -Path $profileDir -Force | Out-Null
}
if (-not (Test-Path $ProfilePath)) {
New-Item -ItemType File -Path $ProfilePath -Force | Out-Null
}
$sourceLine = ". `"$completionPath`""
$profileContent = Get-Content -Path $ProfilePath -Raw
if (-not ($profileContent -match [regex]::Escape($sourceLine))) {
Add-Content -Path $ProfilePath -Value "`n$sourceLine"
Write-Host "Added completion import to profile: $ProfilePath"
}
else {
Write-Host "Profile already imports completion file: $ProfilePath"
}
. $completionPath
. $ProfilePath
Write-Host "PowerShell completion is configured for runConversionChain, runConversionChain.ps1 and rcc."
Write-Host "Current session reloaded profile: $ProfilePath"