2024-08-27 02:15:37 +00:00
|
|
|
using namespace System.Management.Automation.Host;
|
|
|
|
|
2024-08-09 22:22:30 +00:00
|
|
|
$null = New-Module {
|
2024-09-22 22:33:54 +00:00
|
|
|
. "$PSScriptRoot/Scripting.ps1";
|
2024-09-23 01:33:17 +00:00
|
|
|
. "$PSScriptRoot/SoftwareManagement.ps1";
|
2024-11-01 12:12:16 +00:00
|
|
|
. "$PSScriptRoot/../Windows/lib/Constants.ps1";
|
|
|
|
. "$PSScriptRoot/../Windows/lib/PowerManagement.ps1";
|
|
|
|
. "$PSScriptRoot/../Windows/lib/Registry.ps1";
|
|
|
|
. "$PSScriptRoot/../Windows/lib/Security.ps1";
|
|
|
|
. "$PSScriptRoot/../Windows/lib/SoftwareManagement.ps1";
|
|
|
|
. "$PSScriptRoot/../Windows/lib/System.ps1";
|
|
|
|
. "$PSScriptRoot/../Windows/lib/Tasks.ps1";
|
|
|
|
. "$PSScriptRoot/../Windows/lib/WSL.ps1";
|
2024-08-09 22:22:30 +00:00
|
|
|
|
2024-09-03 20:15:15 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the PowerShell modules required for operating.
|
|
|
|
#>
|
|
|
|
function Get-RequiredModules {
|
|
|
|
$modules = @(
|
|
|
|
@("PSScriptAnalyzer")
|
|
|
|
) + (& {
|
2024-10-06 19:25:34 +00:00
|
|
|
if (-not $IsWindows) {
|
|
|
|
@();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
@(
|
|
|
|
@("KnownFolders"),
|
|
|
|
@("PSWindowsUpdate"),
|
|
|
|
@("LocalAccounts", $true),
|
|
|
|
@("NuGet")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2024-09-03 20:15:15 +00:00
|
|
|
|
|
|
|
for ($i = 0; $i -lt $modules.Count; $i++) {
|
|
|
|
if ($modules[$i] -is [string]) {
|
|
|
|
$modules[$i] = @($modules[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $modules;
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:22:30 +00:00
|
|
|
function Start-Operation {
|
|
|
|
param(
|
2024-08-27 12:05:16 +00:00
|
|
|
[switch] $NonInteractive,
|
2024-08-24 14:52:41 +00:00
|
|
|
[switch] $NoImplicitCleanup,
|
2024-08-09 22:22:30 +00:00
|
|
|
[scriptblock] $Action
|
|
|
|
)
|
|
|
|
|
2024-08-24 14:52:41 +00:00
|
|
|
$cleanup = { };
|
2024-08-27 12:05:16 +00:00
|
|
|
$taskPending = $false;
|
2024-08-24 14:52:41 +00:00
|
|
|
|
2024-08-21 01:18:31 +00:00
|
|
|
if (-not $Global:InOperation) {
|
2024-08-21 16:28:08 +00:00
|
|
|
if ($env:DEBUG) {
|
|
|
|
Set-PSDebug -Trace 1;
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:32:49 +00:00
|
|
|
if ($IsWindows -and ($null -ne (Get-OneShotTask))) {
|
2024-08-27 12:05:16 +00:00
|
|
|
$taskPending = $true;
|
|
|
|
[switch] $NonInteractive = $true;
|
|
|
|
}
|
|
|
|
|
2024-08-21 01:18:31 +00:00
|
|
|
$Global:InOperation = $true;
|
2024-09-12 17:04:37 +00:00
|
|
|
$Global:NonInteractive = $NonInteractive;
|
2024-08-21 16:28:50 +00:00
|
|
|
$Global:ErrorActionPreference = $NonInteractive.IsPresent ? 'Continue' : 'Inquire';
|
2024-08-09 22:22:30 +00:00
|
|
|
|
2024-08-24 14:56:29 +00:00
|
|
|
if ($IsWindows) {
|
2024-09-28 14:13:56 +00:00
|
|
|
$env:WSLENV = "CONFIG_NAME:VALHALLA_FLAKE_ROOT";
|
2024-09-21 00:10:20 +00:00
|
|
|
Backup-PowerScheme;
|
2024-08-24 14:56:29 +00:00
|
|
|
}
|
2024-08-24 14:52:41 +00:00
|
|
|
|
|
|
|
if (-not $NoImplicitCleanup.IsPresent) {
|
|
|
|
$cleanup = {
|
2024-10-17 01:49:17 +00:00
|
|
|
Remove-Variable -Scope Global -Name "InOperation";
|
2024-08-24 14:52:41 +00:00
|
|
|
Clear-OperationResources;
|
|
|
|
};
|
|
|
|
}
|
2024-08-27 02:15:37 +00:00
|
|
|
|
|
|
|
& {
|
2024-08-27 02:24:29 +00:00
|
|
|
$initialized = $false;
|
|
|
|
|
2024-09-22 21:57:12 +00:00
|
|
|
# Ping digitalcourage DNS server
|
|
|
|
# https://digitalcourage.de/
|
|
|
|
Write-Host "Waiting for internet connection…";
|
|
|
|
|
|
|
|
while ((Test-Connection -Count 1 5.9.164.112).Status -ne 'Success') {
|
|
|
|
Start-Sleep 0.1;
|
|
|
|
}
|
|
|
|
|
2024-08-27 02:24:29 +00:00
|
|
|
while (-not $initialized) {
|
2024-08-27 02:15:37 +00:00
|
|
|
if ($IsWindows) {
|
|
|
|
if (-not ((Test-Command "choco") -and (Test-Command "refreshenv"))) {
|
2024-10-12 15:18:19 +00:00
|
|
|
# Install chocolatey
|
|
|
|
Write-Host "Installing Chocolatey…";
|
|
|
|
New-Item -Force $PROFILE;
|
|
|
|
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
|
|
|
|
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'));
|
|
|
|
Import-Module $env:ChocolateyInstall/helpers/chocolateyProfile.psm1;
|
|
|
|
refreshenv;
|
2024-08-27 02:15:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-ChocoPackage "powershell-core")) {
|
2024-10-12 15:18:19 +00:00
|
|
|
Write-Host "Installing PowerShell Core…";
|
|
|
|
choco install -y powershell-core --install-arguments='"ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 REGISTER_MANIFEST=1 USER_MU=1 ENABLE_MU=1"';
|
2024-08-27 02:15:37 +00:00
|
|
|
Restart-Intermediate;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($env:PWSH_PATH -and (Test-Path $env:PWSH_PATH)) {
|
|
|
|
attrib "-R" "$env:PWSH_PATH\*" /S /D;
|
|
|
|
Remove-Item -Recurse -Force $env:PWSH_PATH;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($env:DEBUG) {
|
2024-09-03 10:08:35 +00:00
|
|
|
$liveScriptOption = "LiveScripts";
|
|
|
|
|
|
|
|
if (($null -eq (Get-SetupOption $liveScriptOption)) -and (Test-Qemu)) {
|
|
|
|
$result = $Host.UI.PromptForChoice(
|
2024-08-27 02:15:37 +00:00
|
|
|
"Confirm",
|
|
|
|
"Do you wish to swap to live scripts?",
|
|
|
|
[ChoiceDescription[]]@(
|
|
|
|
[ChoiceDescription]::new("&No", "Use scripts stored in the virtual machine"),
|
|
|
|
[ChoiceDescription]::new("&Yes", "Use live scripts stored on the host")),
|
2024-09-03 10:08:35 +00:00
|
|
|
0);
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
Set-SetupOption $liveScriptOption $result;
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
if ($result -eq 1) {
|
|
|
|
Install-ChocoPackage winfsp qemu-guest-agent;
|
|
|
|
Get-Service VirtioFsSvc | Start-Service -PassThru | Set-Service -StartupType Automatic;
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
while (-not (Test-Path Z:\)) {
|
|
|
|
Start-Sleep 0.1;
|
|
|
|
}
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-08 15:09:01 +00:00
|
|
|
foreach ($name in @("INSTALLER_SCRIPT")) {
|
2024-09-03 10:08:35 +00:00
|
|
|
$variable = Get-Item "Env:\$name";
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
$path = Join-Path `
|
2024-09-22 19:53:18 +00:00
|
|
|
"Z:\" `
|
2024-11-01 12:12:16 +00:00
|
|
|
([System.IO.Path]::GetRelativePath("$PSScriptRoot/../..", $variable.Value));
|
2024-09-03 10:08:35 +00:00
|
|
|
|
|
|
|
Set-Item "Env:\$name" $path;
|
|
|
|
Write-Host "The new value of ``$name`` is ``$path``";
|
|
|
|
}
|
|
|
|
|
|
|
|
Restart-Intermediate;
|
|
|
|
exit;
|
|
|
|
}
|
2024-08-27 02:15:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 09:53:31 +00:00
|
|
|
if (-not (Test-Command "gsudo.exe")) {
|
2024-08-27 02:15:37 +00:00
|
|
|
Install-ChocoPackage gsudo;
|
|
|
|
refreshenv;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:40:45 +00:00
|
|
|
function global:Invoke-Sudo {
|
|
|
|
param(
|
|
|
|
$u,
|
|
|
|
[Parameter(ValueFromRemainingArguments, Position = 0)]
|
|
|
|
[string[]] $ArgumentList
|
|
|
|
)
|
|
|
|
|
|
|
|
$params = @();
|
|
|
|
|
|
|
|
if ($u) {
|
|
|
|
$params = @("-u", $u);
|
|
|
|
}
|
|
|
|
elseif ($ArgumentList[0] -eq "-u") {
|
|
|
|
$user = $ArgumentList[1];
|
|
|
|
$ArgumentList = $ArgumentList | Select-Object -Skip 2;
|
|
|
|
$params = @("-u", $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
gsudo @params {
|
|
|
|
$command = $args[0];
|
|
|
|
$flags = ($args | Select-Object -Skip 1);
|
|
|
|
& $command @flags;
|
2024-10-06 19:25:34 +00:00
|
|
|
} -args $ArgumentList;
|
2024-09-28 01:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
New-Alias -Force "sudo" -Scope Global Invoke-Sudo;
|
|
|
|
|
2024-08-27 02:15:37 +00:00
|
|
|
if ($env:DEBUG) {
|
|
|
|
& {
|
|
|
|
$sys32 = "$env:WINDIR/System32";
|
|
|
|
$osk = (Get-Item "$sys32/osk.exe").FullName;
|
|
|
|
$cmd = (Get-Item "$sys32/cmd.exe").FullName;
|
|
|
|
|
2024-08-27 12:06:56 +00:00
|
|
|
if ((Get-FileHash $osk).Hash -ne (Get-FileHash $cmd).Hash) {
|
2024-08-27 02:15:37 +00:00
|
|
|
Set-MpPreference -ExclusionPath $osk;
|
|
|
|
gsudo -d --ti move $osk "${osk}_";
|
|
|
|
gsudo -d -s copy $cmd $osk;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Winget)) {
|
2024-11-01 12:12:16 +00:00
|
|
|
. "$PSScriptRoot/../Windows/Software/winget/Main.ps1";
|
2024-08-27 02:15:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Command "git")) {
|
|
|
|
Install-WingetPackage Git.Git;
|
|
|
|
refreshenv;
|
2024-08-28 02:38:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Command "7z")) {
|
|
|
|
Install-ChocoPackage 7zip.portable;
|
|
|
|
refreshenv;
|
2024-08-27 02:15:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:58:33 +00:00
|
|
|
if (-not (Test-Command "yq")) {
|
|
|
|
Install-ChocoPackage "yq";
|
|
|
|
refreshenv;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-27 02:15:37 +00:00
|
|
|
if (-not (Test-Wsl)) {
|
|
|
|
Install-Wsl;
|
|
|
|
Restart-Intermediate;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-27 02:26:25 +00:00
|
|
|
if (-not (Test-WslDistribution)) {
|
2024-08-27 02:24:29 +00:00
|
|
|
if (-not (Test-Path (Get-WslDistributionDisk))) {
|
|
|
|
Install-WslDistribution;
|
|
|
|
}
|
|
|
|
|
|
|
|
Register-WslDistribution;
|
2024-08-27 02:15:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-09-28 14:13:07 +00:00
|
|
|
if (-not (Test-Nix)) {
|
|
|
|
Install-Nix;
|
2024-08-27 02:15:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-PSPackage Selenium.WebDriver)) {
|
|
|
|
Write-Host "Installing browser automation tools…";
|
2024-09-08 15:12:01 +00:00
|
|
|
$null = Install-Package -Force Selenium.WebDriver -RequiredVersion 4.24.0 -SkipDependencies;
|
2024-08-27 02:15:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Install-ChocoPackage selenium-gecko-driver firefox;
|
|
|
|
Install-WingetPackage AutoHotkey.AutoHotkey;
|
2024-11-01 12:12:16 +00:00
|
|
|
. "$PSScriptRoot/../Windows/Software/pinned-item/Main.ps1";
|
2024-08-27 02:15:37 +00:00
|
|
|
}
|
2024-08-27 20:27:44 +00:00
|
|
|
|
2024-10-12 15:18:19 +00:00
|
|
|
Write-Host "Installing PowerShell Modules…";
|
2024-08-27 20:27:44 +00:00
|
|
|
|
2024-10-12 15:18:19 +00:00
|
|
|
foreach ($module in (Get-RequiredModules)) {
|
|
|
|
$parameters = @{ };
|
2024-08-27 20:27:44 +00:00
|
|
|
|
2024-10-12 15:18:19 +00:00
|
|
|
if ($module -is [string]) {
|
|
|
|
$module = @($module);
|
|
|
|
}
|
2024-08-27 20:27:44 +00:00
|
|
|
|
2024-10-12 15:18:19 +00:00
|
|
|
if ($module[1]) {
|
|
|
|
$parameters = @("-AllowPrerelease");
|
2024-08-27 20:27:44 +00:00
|
|
|
}
|
2024-10-12 15:18:19 +00:00
|
|
|
|
|
|
|
if (-not (Test-PSModule $module[0])) {
|
|
|
|
sudo pwsh -Command Install-Module -Scope AllUsers -AcceptLicense -Force -AllowClobber $module[0] @parameters;
|
|
|
|
Import-Module $module[0];
|
|
|
|
}
|
|
|
|
}
|
2024-08-27 20:27:44 +00:00
|
|
|
|
2024-09-08 22:04:06 +00:00
|
|
|
if (-not $env:CONFIG_NAME) {
|
|
|
|
Show-ProfileNamePrompt;
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:27:44 +00:00
|
|
|
$initialized = $true;
|
2024-08-27 02:15:37 +00:00
|
|
|
}
|
2024-10-06 19:25:34 +00:00
|
|
|
};
|
2024-08-16 12:35:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 12:05:16 +00:00
|
|
|
if ($taskPending) {
|
|
|
|
Start-OneShot;
|
2024-10-06 19:25:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-08-27 12:05:16 +00:00
|
|
|
& $Action;
|
|
|
|
}
|
|
|
|
|
2024-08-24 14:52:41 +00:00
|
|
|
& $cleanup;
|
2024-08-09 22:22:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-24 14:52:41 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Clears resources allocated during the operation.
|
|
|
|
#>
|
|
|
|
function Clear-OperationResources {
|
2024-08-27 20:27:44 +00:00
|
|
|
if ($IsWindows) {
|
|
|
|
Uninstall-WslDistribution;
|
2024-09-03 20:15:15 +00:00
|
|
|
$null = Uninstall-Package Selenium.WebDriver -ErrorAction Continue;
|
|
|
|
Uninstall-ChocoPackage 7zip.portable gsudo selenium-gecko-driver yq;
|
|
|
|
Uninstall-WingetPackage AutoHotkey.AutoHotkey;
|
2024-09-21 00:10:20 +00:00
|
|
|
Restore-PowerScheme;
|
2024-09-03 20:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($module in (Get-RequiredModules)) {
|
|
|
|
Remove-Module -Force $module[0] -ErrorAction SilentlyContinue;
|
|
|
|
Uninstall-Module -Force -Name $module[0] -ErrorAction SilentlyContinue;
|
2024-08-27 20:27:44 +00:00
|
|
|
}
|
2024-08-24 14:52:41 +00:00
|
|
|
}
|
2024-08-09 22:22:30 +00:00
|
|
|
};
|