scoop-completion.psm1

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# This source code is licensed under the MIT License
# Project URL - https://github.com/Moeologist/scoop-completion
# Thanks to Posh-Git - https://github.com/dahlbyk/posh-git

# See scoop/lib/core.ps1
function script:load_cfg($file) {
    if (!(Test-Path $file)) {
        return $null
    }
    try {
        return (Get-Content $file -Raw | ConvertFrom-Json -ErrorAction Stop)
    }
    catch { }
}

$script:configHome = $env:XDG_CONFIG_HOME, "$env:USERPROFILE\.config" | Select-Object -First 1
$script:configFile = "$configHome\scoop\config.json"
$script:scoopConfig = load_cfg $script:configFile

function script:get_config($name, $default) {
    if ($null -eq $scoopConfig.$name -and $null -ne $default) {
        return $default
    }
    return $scoopConfig.$name
}

try {
    $Script:scoopdir = $env:SCOOP, (get_config 'rootPath'), "$env:USERPROFILE\scoop" |
    Where-Object { -not [String]::IsNullOrEmpty($_) } | Select-Object -First 1
}
catch { Write-Warning 'No scoop installed!' }

$script:aliasMap = get_config 'alias'

$script:ScoopCommands = @('alias', 'bucket', 'cache', 'checkup', 'cleanup', 'config', 'create', 'depends', 'export', 'help', 'hold', 'home',
    'info', 'install', 'list', 'prefix', 'reset', 'search', 'status', 'unhold', 'uninstall', 'update', 'virustotal', 'which')

$script:ScoopSubcommands = @{
    alias  = 'add list rm'
    bucket = 'add list known rm'
    cache  = 'rm show'
    config = 'rm'
}

$script:ScoopShortParams = @{
    install    = 'g i k s a'
    uninstall  = 'g p'
    cleanup    = 'g'
    virustotal = 'a s n'
    update     = 'f g i k s q'
}

$script:ScoopLongParams = @{
    install    = 'global independent no-cache skip arch'
    uninstall  = 'global purge'
    cleanup    = 'global'
    virustotal = 'arch scan no-depends'
    update     = 'force global independent no-cache skip quiet'
}

$script:ScoopParamValues = @{
    install    = @{
        a    = '32bit 64bit'
        arch = '32bit 64bit'
    }
    virustotal = @{
        a    = '32bit 64bit'
        arch = '32bit 64bit'
    }
}

$script:ScoopCommandsWithLongParams = $ScoopLongParams.Keys -join '|'
$script:ScoopCommandsWithShortParams = $ScoopShortParams.Keys -join '|'
$script:ScoopCommandsWithParamValues = $ScoopParamValues.Keys -join '|'

# 6> redirect Write-Host's output, (〒︿〒)
function script:ScoopAlias($filter) {
    if ($null -ne $script:aliasMap) {
        @($script:aliasMap.PSObject.Properties | Select-Object Name | ForEach-Object { $_.Name }
            Where-Object { $_ -like "$filter*" }
        )
    } else {
        @()
    }
}

function script:ScoopExpandCmdParams($commands, $command, $filter) {
    $commands.$command -split ' ' | Where-Object { $_ -like "$filter*" }
}

function script:ScoopExpandCmd($filter, $includeAliases) {
    $cmdList = @()
    $cmdList += $ScoopCommands
    if ($includeAliases) {
        $cmdList += ScoopAlias($filter)
    }
    $cmdList -like "$filter*" | Sort-Object
}

function script:ScoopLocalPackages($filter) {
    @(& Get-ChildItem -Path $script:scoopdir\apps -Name -Directory |
        Where-Object { $_ -ne "scoop" } |
        Where-Object { $_ -like "$filter*" }
    )
}

function script:ScoopRemotePackages($filter) {
    @(& Get-ChildItem -Path $script:scoopdir\buckets\ -Name | 
        ForEach-Object { Get-ChildItem -Path $script:scoopdir\buckets\$_\bucket -Name -Filter *.json } |
        ForEach-Object { if ( $_ -match '^([\w][\-\.\w]*)\.json$' ) { "$($Matches[1])" } } |
        Where-Object { $_ -like "$filter*" }
    )
}

function script:ScoopLocalCaches($filter) {
    @(& scoop cache show $filter |
        Out-String -Stream |
        ForEach-Object { if ( $_ -match '^\s*[\.1-9]+ [KMGB]+ ([\w][\-\.\w]*) .*$' ) { "$($Matches[1])" } } |
        Sort-Object -Unique |
        Where-Object { $_ -like "$filter*" }
    )
}

function script:ScoopLocalBuckets($filter) {
    @(& scoop bucket list | Where-Object { $_ -like "$filter*" })
}

function script:ScoopRemoteBuckets($filter) {
    @(& scoop bucket known | Where-Object { $_ -like "$filter*" })
}

function script:ScoopExpandLongParams($cmd, $filter) {
    $ScoopLongParams[$cmd] -split ' ' |
    Where-Object { $_ -like "$filter*" } |
    Sort-Object |
    ForEach-Object { -join ("--", $_) }
}

function script:ScoopExpandShortParams($cmd, $filter) {
    $ScoopShortParams[$cmd] -split ' ' |
    Where-Object { $_ -like "$filter*" } |
    Sort-Object |
    ForEach-Object { -join ("-", $_) }
}

function script:ScoopExpandParamValues($cmd, $param, $filter) {
    $ScoopParamValues[$cmd][$param] -split ' ' |
    Where-Object { $_ -like "$filter*" } |
    Sort-Object
}

function script:ScoopTabExpansion($lastBlock) {

    switch -regex ($lastBlock) {
        # Handles Scoop <cmd> --<param> <value>
        "^(?<cmd>$ScoopCommandsWithParamValues).* --(?<param>.+) (?<value>\w*)$" {
            if ($ScoopParamValues[$matches['cmd']][$matches['param']]) {
                return ScoopExpandParamValues $matches['cmd'] $matches['param'] $matches['value']
            }
        }

        # Handles Scoop <cmd> -<shortparam> <value>
        "^(?<cmd>$ScoopCommandsWithParamValues).* -(?<param>.+) (?<value>\w*)$" {
            if ($ScoopParamValues[$matches['cmd']][$matches['param']]) {
                return ScoopExpandParamValues $matches['cmd'] $matches['param'] $matches['value']
            }
        }

        # Handles uninstall package names
        "^(uninstall|cleanup|virustotal|update|prefix|reset|hold|unhold)\s+(?:.+\s+)?(?<package>[\w][\-\.\w]*)?$" {
            return ScoopLocalPackages $matches['package']
        }

        # Handles install package names
        "^(install|info|home|depends)\s+(?:.+\s+)?(?<package>[\w][\-\.\w]*)?$" {
            return ScoopRemotePackages $matches['package']
        }

        # Handles cache (rm/show) cache names
        "^cache (rm|show)\s+(?:.+\s+)?(?<cache>[\w][\-\.\w]*)?$" {
            return ScoopLocalCaches $matches['cache']
        }

        # Handles bucket rm bucket names
        "^bucket rm\s+(?:.+\s+)?(?<bucket>[\w][\-\.\w]*)?$" {
            return ScoopLocalBuckets $matches['bucket']
        }

        # Handles bucket add bucket names
        "^bucket add\s+(?:.+\s+)?(?<bucket>[\w][\-\.\w]*)?$" {
            return ScoopRemoteBuckets $matches['bucket']
        }

        # Handles alias rm alias names
        "^alias rm\s+(?:.+\s+)?(?<alias>[\w][\-\.\w]*)?$" {
            return ScoopAlias $matches['alias']
        }

        # Handles Scoop help <cmd>
        "^help (?<cmd>\S*)$" {
            return ScoopExpandCmd $matches['cmd'] $false
        }

        # Handles Scoop <cmd> <subcmd>
        "^(?<cmd>$($ScoopSubcommands.Keys -join '|'))\s+(?<op>\S*)$" {
            return ScoopExpandCmdParams $ScoopSubcommands $matches['cmd'] $matches['op']
        }

        # Handles Scoop <cmd>
        "^(?<cmd>\S*)$" {
            return ScoopExpandCmd $matches['cmd'] $true
        }

        # Handles Scoop <cmd> --<param>
        "^(?<cmd>$ScoopCommandsWithLongParams).* --(?<param>\S*)$" {
            return ScoopExpandLongParams $matches['cmd'] $matches['param']
        }

        # Handles Scoop <cmd> -<shortparam>
        "^(?<cmd>$ScoopCommandsWithShortParams).* -(?<shortparam>\S*)$" {
            return ScoopExpandShortParams $matches['cmd'] $matches['shortparam']
        }
    }
}

function script:Get-AliasPattern($exe) {
    $aliases = @($exe, "$exe\.ps1", "$exe\.cmd") + @(Get-Alias | Where-Object { $_.Definition -eq $exe } | Select-Object -Exp Name)
    "($($aliases -join '|'))"
}

if (Test-Path Function:\TabExpansion) {
    Rename-Item Function:\TabExpansion TabExpansionBackup_Scoop
}

function TabExpansion($line, $lastWord) {
    $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()

    switch -regex ($lastBlock) {
        # Execute Scoop tab completion for all Scoop-related commands
        "^(sudo\s+)?$(Get-AliasPattern scoop)\s+(?<rest>.*)$" {
            $rest = $matches['rest']
            ScoopTabExpansion $rest
        }

        # Fall back on existing tab expansion
        default {
            if (Test-Path Function:\TabExpansionBackup_Scoop) {
                TabExpansionBackup_Scoop $line $lastWord
            }
        }
    }
}

$exportModuleMemberParams = @{
    Function = @(
        'TabExpansion'
    )
}

Export-ModuleMember @exportModuleMemberParams