-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelect-Plugins.ps1
More file actions
89 lines (76 loc) · 3.12 KB
/
Copy pathSelect-Plugins.ps1
File metadata and controls
89 lines (76 loc) · 3.12 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
<#
.NOTES
Select-Plugins.ps1
Selectively enable / disable vCheck plugins from a cross-platform console menu.
vCheck 9 is PowerShell 7 only; the previous Windows Forms picker has been removed
in favour of a console menu that works on Windows, Linux and macOS.
Disabled plugins are renamed <pluginname>.ps1.disabled
Enabled plugins are renamed <pluginname>.ps1
Based on the original Select-Plugins by Phil Randal / Kevin Kirkpatrick.
#>
[cmdletbinding()]
param()
$Title = "Plugin Selection Plugin"
$Author = "Phil Randal"
$PluginVersion = 3.0
$Header = "Plugin Selection"
$Comments = "Plugin Selection"
$Display = "None"
# Start of Settings
# End of Settings
$PluginPath = (Split-Path ((Get-Variable MyInvocation).Value).MyCommand.Path)
If ($PluginPath -notmatch 'plugins$') {
$PluginPath = Join-Path $PluginPath "Plugins"
}
$plugins = Get-ChildItem -Path $PluginPath -Include *.ps1, *.ps1.disabled -Recurse |
Sort-Object { [int]($_.Name -replace '\D') } |
Select-Object FullName, Name,
@{Label = "Plugin"; expression = { $_.Name -replace '(.*)\.ps1(?:\.disabled|)$', '$1' } },
@{Label = "Enabled"; expression = { $_.Name -notmatch '.*\.disabled$' } }
if (-not $plugins) { Write-Warning "No plugins found under $PluginPath"; return }
# Track desired enabled-state per plugin
$state = [System.Collections.Generic.List[bool]]::new()
foreach ($p in $plugins) { $state.Add([bool]$p.Enabled) }
function Show-PluginMenu {
Write-Host ""
for ($i = 0; $i -lt $plugins.Count; $i++) {
$mark = if ($state[$i]) { "[x]" } else { "[ ]" }
Write-Host ("{0,4} {1} {2}" -f ($i + 1), $mark, $plugins[$i].Plugin)
}
Write-Host ""
Write-Host "Toggle by number (e.g. '3 5 10' or a range '3-8'); 'a'=all on, 'n'=all off, Enter=apply, 'q'=cancel"
}
while ($true) {
Show-PluginMenu
$in = (Read-Host "Selection").Trim()
if ($in -eq 'q') { Write-Warning "Cancelled - no changes made."; return }
if ($in -eq '') { break }
if ($in -eq 'a') { for ($i = 0; $i -lt $state.Count; $i++) { $state[$i] = $true }; continue }
if ($in -eq 'n') { for ($i = 0; $i -lt $state.Count; $i++) { $state[$i] = $false }; continue }
foreach ($tok in ($in -split '[,\s]+' | Where-Object { $_ })) {
if ($tok -match '^(\d+)-(\d+)$') {
$a = [int]$Matches[1]; $b = [int]$Matches[2]
for ($n = $a; $n -le $b; $n++) { if ($n -ge 1 -and $n -le $state.Count) { $state[$n - 1] = -not $state[$n - 1] } }
}
elseif ($tok -match '^\d+$') {
$n = [int]$tok; if ($n -ge 1 -and $n -le $state.Count) { $state[$n - 1] = -not $state[$n - 1] }
}
}
}
# Apply: rename files whose enabled-state changed
$changed = 0
for ($i = 0; $i -lt $plugins.Count; $i++) {
$oldname = $plugins[$i].Name
$newname = $plugins[$i].Plugin + $(if ($state[$i]) { '.ps1' } else { '.ps1.disabled' })
if ($newname -ne $oldname) {
$dir = $plugins[$i].FullName | Split-Path
if (Test-Path (Join-Path $dir $newname)) {
Write-Warning "Cannot rename `"$oldname`" to `"$newname`" - target already exists. Skipping."
}
else {
Rename-Item (Join-Path $dir $oldname) $newname
$changed++
}
}
}
Write-Host "$changed plugin(s) updated."