-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-RubyVersion.ps1
More file actions
154 lines (118 loc) · 4.79 KB
/
Get-RubyVersion.ps1
File metadata and controls
154 lines (118 loc) · 4.79 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
<#
Shows Ruby version(s) currently in use, installed, or remote.
#>
function Get-RubyVersion {
[CmdletBinding(DefaultParameterSetName = 'Default')]
[OutputType([RubyVersionDescriptor[]])]
param(
[Parameter(Mandatory, ParameterSetName = 'Current')]
[switch] $Current,
[Parameter(Mandatory, ParameterSetName = 'Configured')]
[switch] $Configured,
[Parameter(Mandatory, ParameterSetName = 'Installed')]
[switch] $Installed,
[Parameter(Mandatory, ParameterSetName = 'All')]
[Parameter(Mandatory, ParameterSetName = 'RemoteAll')]
[switch] $All,
[Parameter(Mandatory, ParameterSetName = 'Remote')]
[Parameter(Mandatory, ParameterSetName = 'RemoteAll')]
[switch] $Remote
)
$callerErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = [Management.Automation.ActionPreference]::Stop
try {
$enabled = switch -Exact ($PSCmdlet.ParameterSetName) {
'Default' { $true }
'Current' { $Current }
'Configured' { $Configured }
'Installed' { $Installed }
'All' { $All }
'Remote' { $Remote }
'RemoteAll' { $Remote }
}
if (-not $enabled) {
# Weird flex, but ok
Write-Error "All switches are disabled."
}
if ($PSCmdlet.ParameterSetName -eq 'Default') {
$Current = $true
}
$shell = Get-ShellRubyVersion -ErrorAction Ignore
if ($shell -and $Current) {
return $shell
}
$local = Get-LocalRubyVersion -ErrorAction Ignore
if ($local -and $Current) {
return $local
}
$global = Get-GlobalRubyVersion -ErrorAction Ignore
if ($global -and $Current) {
return $global
}
$system = Get-SystemRubyVersion -ErrorAction Ignore
if ($system -and $Current) {
return $system
}
$configuredVersions = $shell, $local, $global, $system `
| Where-Object { $_ } `
| Group-Object Version `
| ForEach-Object {
$desc = $_.Group[0]
foreach ($item in $_.Group) {
$desc.Configuration = $desc.Configuration -bor $item.Configuration
}
$desc.Source = $_.Group.Source
$desc
}
if ($Configured) {
if (!$configuredVersions) {
Write-Error 'No ruby installation found' `
-RecommendedAction 'Ensure that Ruby installations are present and that RbEnv is configured to find them.'
}
return $configuredVersions
}
$configuredVersions = $configuredVersions | ForEach-Object {$h = @{}} { $h[$_.Version] = $_ } {$h}
$versions = [Collections.Generic.List[RubyVersionDescriptor]]::new()
$versionsPath = Get-VersionsPath
foreach ($installPath in $versionsPath.EnumerateDirectories()) {
$version = [RubyVersion]::Parse($installPath.Name)
$descriptor = $configuredVersions[$version]
if (!$descriptor) {
$descriptor = [RubyVersionDescriptor]::new($version, $null, $installPath, $null)
}
$versions.Add($descriptor)
}
# NB: Can't check $All directly, because it's also used with $Remote.
if ($Installed -or $PSCmdlet.ParameterSetName -eq 'All') {
if($versions.Count -eq 0) {
Write-Error 'No ruby installation found' `
-RecommendedAction 'Ensure that Ruby installations are present and that RbEnv is configured to find them.'
}
return $versions | Sort-Object Version -Descending
}
## $Remote ####################
$installedVersions = $versions | ForEach-Object {$h = @{}} { $h[$_.Version] = $_ } {$h}
$remoteVersions = Get-RemoteRubyVersion -All:$All -ErrorAction Ignore
$versions = [Collections.Generic.List[RubyVersionDescriptor]]::new()
foreach ($remoteVersion in $remoteVersions.Version) {
$descriptor = $installedVersions[$remoteVersion]
if ($descriptor) {
$descriptor.Configuration = $descriptor.Configuration -bor [RubyConfiguration]::Remote
}
else {
$descriptor = [RubyVersionDescriptor]::new(
$remoteVersion,
[RubyConfiguration]::Remote,
$null,
$null
)
}
$versions.Add($descriptor)
}
return $versions | Sort-Object Version -Descending
}
catch {
$global:Error.RemoveAt(0)
Write-Error $_ -ErrorAction $callerErrorActionPreference
}
}