-
Notifications
You must be signed in to change notification settings - Fork 4
/
psperl.ps1
147 lines (140 loc) · 6.28 KB
/
psperl.ps1
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
# command line arguments
param (
[switch]$available = $false,
[switch]$init = $false,
[switch]$setup = $false,
[switch]$list = $false,
[switch]$version = $false,
[switch]$v = $false,
[string]$install = '',
[string]$switch = '',
[string]$use = '',
[int]$major = 0
)
# Make sure we only attempt to work for PowerShell v5 and greater
# this allows the use of classes.
if ($PSVersionTable.PSVersion.Major -lt 5) {
throw "PowerShell v5.0+ is required for psperl. https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell?view=powershell-6";
}
$psperl_path = (Split-Path -parent $MyInvocation.MyCommand.Definition);
$env:PSPERL_ROOT = $psperl_path;
$env:PSPERL_VERSION = '0.1.2';
# Import our classes
. "$($psperl_path)\src\PSPerl.ps1";
$psperl = [PSPerl]::new($psperl_path, $Profile);
$global:DebugPreference = 'Continue'
if ($setup) { $psperl.Setup(); }
elseif ($init) { $psperl.Init(); }
elseif ($available) {
[int]$ps_bits = $psperl.ArchPS();
[int]$os_bits = $psperl.ArchOS();
Write-Host("We're using a $($ps_bits)-bit PowerShell on a $($os_bits)-bit Windows OS.");
Write-Host("On 64-bit Windows:");
Write-Host(" 32-bit PowerShell: You can use 32-bit, 32-bit USE_64_BIT_INT Perls without problem.");
Write-Host(" You _can_ use 64-bit Perls, but may run into trouble. RISK");
Write-Host(" 64-bit PowerShell: You can use 32-bit, 32-bit USE_64_BIT_INT, or 64-bit Perls.");
Write-Host("On 32-bit Windows:");
Write-Host(" You can only use 32-bit or 32-bit with USE_64_BIT_INT Perls");
Write-Host("");
[array]$data = $psperl.AvailablePerls();
if ($major -gt 0) {
Write-Host("Perls available where major version is: $($major)");
Write-Host("");
if ($os_bits -eq 64) {
[String]$message = '64-bit Perls';
if ($ps_bits -ne 64) { $message = "$($message) (RISKY on a 32-bit PS)"}
Write-Host("$($message):");
ForEach($row in ($data | Where-Object {($_.x64) -And ($_.major -eq $major)})) {
Write-Host(" - $($row.install_name)");
}
}
Write-Host("");
Write-Host("32-bit with USE_64_BIT_INT Perls:")
ForEach($row in ($data | Where-Object {(-Not $_.x64) -And ($_.USE_64_BIT_INT) -And ($_.major -eq $major)})) {
Write-Host(" - $($row.install_name)");
}
Write-Host("");
Write-Host("32-bit Perls:")
ForEach($row in ($data | Where-Object {(-Not $_.x64) -And (-Not $_.USE_64_BIT_INT) -And ($_.major -eq $major)})) {
Write-Host(" - $($row.install_name)");
}
}
else {
Write-Host("The current release for each major Perl:");
Write-Host("");
if ($os_bits -eq 64) {
[String]$message = '64-bit Perls';
if ($ps_bits -ne 64) { $message = "$($message) (RISKY on a 32-bit PS)"}
Write-Host("$($message):");
ForEach($row in ($data | Where-Object {($_.x64)} | Group-Object -Property major | Sort-Object -Descending -Property Name)) {
Write-Host(" - $($row | ForEach-Object {$_.Group[0].install_name})");
}
}
Write-Host("");
Write-Host("32-bit with USE_64_BIT_INT Perls:")
ForEach($row in ($data | Where-Object {(-Not $_.x64) -And ($_.USE_64_BIT_INT)} | Group-Object -Property major | Sort-Object -Descending -Property Name)) {
Write-Host(" - $($row | ForEach-Object {$_.Group[0].install_name})");
}
Write-Host("");
Write-Host("32-bit Perls:")
ForEach($row in ($data | Where-Object {(-Not $_.x64) -And (-Not $_.USE_64_BIT_INT)} | Group-Object -Property major | Sort-Object -Descending -Property Name)) {
Write-Host(" - $($row | ForEach-Object {$_.Group[0].install_name})");
}
}
Write-Host("");
}
elseif ($install) {
Write-Host("You'd like to install version $($install). Let's see what we can do.");
[array]$data = $psperl.AvailablePerls();
# grep through our available perls for the one specified by name.
# this "grep" (Where-Object) is insane. If only one thing is found, it doesn't
# return a list with one item. It just returns the item. If more than one are
# found, then you get a list of items. -sigh
# List form: $found.GetType() = 'System.Object[]'
# Single Object form: $found.GetType() = 'System.Management.Automation.PSCustomObject'
$found = $data | Where-Object {$_.install_name -eq $install};
# if nothing is found, a null thingy is returned. This can be tested with a simple truthiness test
if (-Not $found) {
throw "No installable version of Perl found by the name $($install). Try `"psperl -available`" to get a list of available perl installations.";
}
# check to see if we got just one.
if ($found -is 'System.Object') {
$psperl.Install($found);
}
else {
throw "Unexpected results `"$($found.GetType())`" searching for `"$($install)`". Try `"psperl -available`" to get a list of available perl installations.";
}
}
elseif ($switch) { $psperl.Use($switch, $true); }
elseif ($use) { $psperl.Use($use, $false); }
elseif ($list) {
Write-Host("Perls installed on your system: ");
Write-Host("");
ForEach ($dir in (Get-ChildItem -Path "$($env:PSPERL_ROOT)\_perls" -Directory)) {
if ($dir.Name -eq $env:PSPERL_CURRENT) {
Write-Host -NoNewline " * ";
}
else {
Write-Host -NoNewline " ";
}
Write-Host $dir.Name;
}
}
elseif ($version -or $v) {
Write-Host("This is PSPerl v$($env:PSPERL_VERSION)");
Write-Host("");
Write-Host("Directories of note:");
Write-Host("$($env:PSPERL_ROOT)\_config");
Write-Host("$($env:PSPERL_ROOT)\_locals");
Write-Host("$($env:PSPERL_ROOT)\_perls");
Write-Host("$($env:PSPERL_ROOT)\_zips");
Write-Host("");
Write-Host("https://github.com/genio/psperl");
Write-Host("");
}
else {
Get-Content -Path "$($env:PSPERL_ROOT)\helpfile.txt" | Write-Host
}
# To turn on Debugging, $global:DebugPreference = 'Continue'
# To turn off Debugging, $global:DebugPreference = 'SilentlyContinue'
exit;