winutil/functions/private/Install-WinUtilWinget.ps1
Mr.k 3903eaaa24
Code Formatting of Repo - Add Preprocessing to Compilation Process - Introduction of Dev/Build Tools to WinUtil (Although very simple at the moment) (#2383)
* Replace Tabs with Spaces to follow the conventions

* Add Preprocessing in Compiler

* Compile from Anywhere you want - Running 'Compile.ps1' Works in any directory you call it from

* Code Formatting Changes

* Result of Preprocessing Step in 'Compile.ps1' Script - Remove Trailing Whitespace Characters

* Make Preprocessing more advanced

* Move Preprocessing to a separate script file

* Make Self Modification impossible for 'tools/Do-PreProcessing.ps1' Script - Make the workingdir same as sync.PSScriptRoot for consistency

* Revert commit b5dffd671f

* Patched a Bug of some Excluded Files not actually get excluded in 'Get-ChildItem' PS Cmdlet

* Update Replace Regex for Code Formatting in 'Do-PreProcessing' Script Tool

* Rename 'Do-PreProcessing' to 'Invoke-Preprocessing' - Update some Comments

* Make 'Invoke-Preprocessing' Modular - Update RegEx to handle more cases - Update Documentation - Add Validations & Useful feedback upon error

* Replace Tabs with Spaces to follow the conventions - 'applications.json' File

* Code Formatting Changes - 'Copy-Files' Private Function

* Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool

* Replace Tabs with Spaces to follow the conventions - Make 'ExcludedFiles' validation step check all filepaths before finally checking if any has failed

* Result of 'Invoke-Preprocessing' Script

* Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool
2024-08-06 15:35:17 -05:00

69 lines
3.4 KiB
PowerShell

function Install-WinUtilWinget {
<#
.SYNOPSIS
Installs Winget if it is not already installed.
.DESCRIPTION
This function will download the latest version of Winget and install it. If Winget is already installed, it will do nothing.
#>
$isWingetInstalled = Test-WinUtilPackageManager -winget
try {
if ($isWingetInstalled -eq "installed") {
Write-Host "`nWinget is already installed.`r" -ForegroundColor Green
return
} elseif ($isWingetInstalled -eq "outdated") {
Write-Host "`nWinget is Outdated. Continuing with install.`r" -ForegroundColor Yellow
} else {
Write-Host "`nWinget is not Installed. Continuing with install.`r" -ForegroundColor Red
}
# Gets the computer's information
if ($null -eq $sync.ComputerInfo) {
$ComputerInfo = Get-ComputerInfo -ErrorAction Stop
} else {
$ComputerInfo = $sync.ComputerInfo
}
if (($ComputerInfo.WindowsVersion) -lt "1809") {
# Checks if Windows Version is too old for Winget
Write-Host "Winget is not supported on this version of Windows (Pre-1809)" -ForegroundColor Red
return
}
# Install Winget via GitHub method.
# Used part of my own script with some modification: ruxunderscore/windows-initialization
Write-Host "Downloading Winget Prerequsites`n"
Get-WinUtilWingetPrerequisites
Write-Host "Downloading Winget and License File`r"
Get-WinUtilWingetLatest
Write-Host "Installing Winget w/ Prerequsites`r"
Add-AppxProvisionedPackage -Online -PackagePath $ENV:TEMP\Microsoft.DesktopAppInstaller.msixbundle -DependencyPackagePath $ENV:TEMP\Microsoft.VCLibs.x64.Desktop.appx, $ENV:TEMP\Microsoft.UI.Xaml.x64.appx -LicensePath $ENV:TEMP\License1.xml
Write-Host "Manually adding Winget Sources, from Winget CDN."
Add-AppxPackage -Path https://cdn.winget.microsoft.com/cache/source.msix #Seems some installs of Winget don't add the repo source, this should makes sure that it's installed every time.
Write-Host "Winget Installed" -ForegroundColor Green
Write-Host "Enabling NuGet and Module..."
Install-PackageProvider -Name NuGet -Force
Install-Module -Name Microsoft.WinGet.Client -Force
# Winget only needs a refresh of the environment variables to be used.
Write-Output "Refreshing Environment Variables...`n"
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
} catch {
Write-Host "Failure detected while installing via GitHub method. Continuing with Chocolatey method as fallback." -ForegroundColor Red
# In case install fails via GitHub method.
try {
# Install Choco if not already present
Install-WinUtilChoco
Start-Process -Verb runas -FilePath powershell.exe -ArgumentList "choco install winget-cli"
Write-Host "Winget Installed" -ForegroundColor Green
Write-Output "Refreshing Environment Variables...`n"
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
} catch {
throw [WingetFailedInstall]::new('Failed to install!')
}
}
}