From 60a9ff33b08098f6eeb7b3c2915f8282bb4e7e01 Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:57:21 +0200 Subject: [PATCH] WiGui is back :) --- .../Winget-AutoUpdate/WAU-Installer-GUI.ps1 | 395 ++++++++++++++++++ 1 file changed, 395 insertions(+) create mode 100644 Sources/Winget-AutoUpdate/WAU-Installer-GUI.ps1 diff --git a/Sources/Winget-AutoUpdate/WAU-Installer-GUI.ps1 b/Sources/Winget-AutoUpdate/WAU-Installer-GUI.ps1 new file mode 100644 index 0000000..c485166 --- /dev/null +++ b/Sources/Winget-AutoUpdate/WAU-Installer-GUI.ps1 @@ -0,0 +1,395 @@ +# import Appx module if the powershell version is 7/core +if ( $psversionTable.PSEdition -eq "core" ) { + import-Module -name Appx -UseWIndowsPowershell -WarningAction:SilentlyContinue +} + +#Get the Working Dir +$Script:WorkingDir = $PSScriptRoot + + +<# FUNCTIONS #> +. "$WorkingDir\functions\Get-WingetCmd.ps1" + +#Function to start or update popup +Function Start-PopUp ($Message) { + + if (!$PopUpWindow) { + + #Create window + $inputXML = @" + + + + + +"@ + + [xml]$XAML = ($inputXML -replace "x:N", "N") + + #Read the form + $Reader = (New-Object System.Xml.XmlNodeReader $XAML) + $Script:PopUpWindow = [Windows.Markup.XamlReader]::Load($Reader) + $PopUpWindow.Icon = $IconBase64 + + #Store Form Objects In PowerShell + $XAML.SelectNodes("//*[@Name]") | ForEach-Object { + Set-Variable -Name "$($_.Name)" -Value $PopUpWindow.FindName($_.Name) -Scope Script + } + + $PopUpWindow.Show() + } + #Message to display + $PopUpLabel.Text = $Message + #Update PopUp + $PopUpWindow.Dispatcher.Invoke([action] {}, "Render") +} + +#Function to close popup +Function Close-PopUp { + $Script:PopUpWindow.Close() + $Script:PopUpWindow = $null +} + +function Get-WingetAppInfo ($SearchApp) { + class Software { + [string]$Name + [string]$Id + } + + #Search for winget apps + $AppResult = & $Winget search $SearchApp --accept-source-agreements --source winget | Out-String + + #Start Conversion of winget format to an array. Check if "-----" exists + if (!($AppResult -match "-----")) { + Start-PopUp "No application found!" + Start-Sleep 2 + Close-PopUp + return + } + + #Split winget output to lines + $lines = $AppResult.Split([Environment]::NewLine) | Where-Object { $_ } + + # Find the line that starts with "------" + $fl = 0 + while (-not $lines[$fl].StartsWith("-----")) { + $fl++ + } + + $fl = $fl - 1 + + #Get header titles [without remove separator] + $index = $lines[$fl] -split '(?<=\s)(?!\s)' + + # Line $fl has the header, we can find char where we find ID and Version [and manage non latin characters] + $idStart = $($index[0] -replace '[\u4e00-\u9fa5]', '**').Length + $versionStart = $idStart + $($index[1] -replace '[\u4e00-\u9fa5]', '**').Length + + # Now cycle in real package and split accordingly + $searchList = @() + For ($i = $fl + 2; $i -le $lines.Length; $i++) { + $line = $lines[$i] -replace "[\u2026]", " " #Fix "..." in long names + # If line contains an ID (Alphanumeric | Literal "." | Alphanumeric) + if ($line -match "\w\.\w") { + $software = [Software]::new() + #Manage non latin characters + $nameDeclination = $($line.Substring(0, $idStart) -replace '[\u4e00-\u9fa5]', '**').Length - $line.Substring(0, $idStart).Length + $software.Name = $line.Substring(0, $idStart - $nameDeclination).TrimEnd() + $software.Id = $line.Substring($idStart - $nameDeclination, $versionStart - $idStart).TrimEnd() + #add formatted soft to list + $searchList += $software + } + } + return $searchList +} + +function Get-WingetInstalledApps { + + #Json File where to export install apps + $jsonFile = "$env:TEMP\Installed_Apps.json" + + #Get list of installed Winget apps to json file + & $Winget export -o $jsonFile --accept-source-agreements | Out-Null + + #Convert from json file + $InstalledApps = get-content $jsonFile | ConvertFrom-Json + + #Return app list + return $InstalledApps.Sources.Packages.PackageIdentifier | Sort-Object | Get-Unique +} + +function Start-Installations ($AppsToInstall) { + + #Run Winget-Install script + Start-PopUp "Installing applications..." + $WAUInstallPath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Romanitho\Winget-AutoUpdate\" -Name InstallLocation + + #Try with admin rights. + try { + Start-Process "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File ""$WAUInstallPath\Winget-Install.ps1"" -AppIDs ""$AppsToInstall""" -Wait -Verb RunAs + } + catch { + Start-Process "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File ""$WAUInstallPath\Winget-Install.ps1"" -AppIDs ""$AppsToInstall""" -Wait + } + + #Installs finished + Start-PopUp "Done!" + Start-Sleep 2 + #Close Popup + Close-PopUp +} + +function Start-Uninstallations ($AppsToUninstall) { + #Run Winget-Install script + Start-PopUp "Uninstalling applications..." + $WAUInstallPath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Romanitho\Winget-AutoUpdate\" -Name InstallLocation + + #Run Winget-Install -Uninstall + Start-Process "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File ""$WAUInstallPath\Winget-Install.ps1"" -AppIDs ""$AppsToUninstall"" -Uninstall" -Wait -Verb RunAs + + Close-PopUp +} + +function Start-InstallGUI { + + ### FORM CREATION ### + + # GUI XAML file + $inputXML = @" + + + + + + + + +