From f7d5a19c068f7a99bc62364f5869389d6772b6b1 Mon Sep 17 00:00:00 2001
From: Romain <96626929+Romanitho@users.noreply.github.com>
Date: Mon, 7 Mar 2022 02:35:40 +0100
Subject: [PATCH] Add WAU AutoUpdate support
---
Winget-AutoUpdate-Install.ps1 | 17 ++++-
Winget-AutoUpdate/config/about.xml | 7 ++
Winget-AutoUpdate/locale/de-DE.xml | 2 +-
Winget-AutoUpdate/locale/en-US.xml | 4 +-
Winget-AutoUpdate/locale/fr-FR.xml | 4 +-
Winget-AutoUpdate/winget-upgrade.ps1 | 105 ++++++++++++++++++++++++++-
6 files changed, 131 insertions(+), 8 deletions(-)
create mode 100644 Winget-AutoUpdate/config/about.xml
diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1
index 466f2fe..df92a1f 100644
--- a/Winget-AutoUpdate-Install.ps1
+++ b/Winget-AutoUpdate-Install.ps1
@@ -14,7 +14,10 @@ Install Winget-AutoUpdate and prerequisites silently
Specify Winget-AutoUpdate installation localtion. Default: C:\ProgramData\Winget-AutoUpdate\
.PARAMETER DoNotUpdate
-Do not run Winget-autoupdate after installation. By default, Winget-AutoUpdate is run just after installation.
+Do not run Winget-AutoUpdate after installation. By default, Winget-AutoUpdate is run just after installation.
+
+.PARAMETER DisableWAUAutoUpdate
+Disable Winget-AutoUpdate update checking. By default, WAU auto update if new version is available on Github.
.EXAMPLE
.\winget-install-and-update.ps1 -Silent -DoNotUpdate
@@ -24,7 +27,8 @@ Do not run Winget-autoupdate after installation. By default, Winget-AutoUpdate i
param(
[Parameter(Mandatory=$False)] [Alias('S')] [Switch] $Silent = $false,
[Parameter(Mandatory=$False)] [Alias('Path')] [String] $WingetUpdatePath = "$env:ProgramData\Winget-AutoUpdate",
- [Parameter(Mandatory=$False)] [Switch] $DoNotUpdate = $false
+ [Parameter(Mandatory=$False)] [Switch] $DoNotUpdate = $false,
+ [Parameter(Mandatory=$False)] [Switch] $DisableWAUAutoUpdate = $false
)
@@ -117,6 +121,15 @@ function Install-WingetAutoUpdate{
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings
Register-ScheduledTask -TaskName 'Winget-AutoUpdate-Notify' -InputObject $task -Force
+ # Install config file
+ [xml]$ConfigXML = @"
+
+
+ $(!($DisableWAUAutoUpdate))
+
+"@
+ $ConfigXML.Save("$WingetUpdatePath\config\config.xml")
+
Write-host "`nInstallation succeeded!" -ForegroundColor Green
Start-sleep 1
diff --git a/Winget-AutoUpdate/config/about.xml b/Winget-AutoUpdate/config/about.xml
new file mode 100644
index 0000000..31fdc77
--- /dev/null
+++ b/Winget-AutoUpdate/config/about.xml
@@ -0,0 +1,7 @@
+
+
+ Winget-AutoUpdate (WAU)
+ 1.4.0
+ Romanitho
+ https://github.com/Romanitho/Winget-AutoUpdate
+
\ No newline at end of file
diff --git a/Winget-AutoUpdate/locale/de-DE.xml b/Winget-AutoUpdate/locale/de-DE.xml
index b449098..b373116 100644
--- a/Winget-AutoUpdate/locale/de-DE.xml
+++ b/Winget-AutoUpdate/locale/de-DE.xml
@@ -2,7 +2,7 @@
diff --git a/Winget-AutoUpdate/locale/en-US.xml b/Winget-AutoUpdate/locale/en-US.xml
index 89a0cdf..a5afa83 100644
--- a/Winget-AutoUpdate/locale/en-US.xml
+++ b/Winget-AutoUpdate/locale/en-US.xml
@@ -2,13 +2,13 @@
diff --git a/Winget-AutoUpdate/locale/fr-FR.xml b/Winget-AutoUpdate/locale/fr-FR.xml
index 783c3f6..3362d5a 100644
--- a/Winget-AutoUpdate/locale/fr-FR.xml
+++ b/Winget-AutoUpdate/locale/fr-FR.xml
@@ -2,13 +2,13 @@
diff --git a/Winget-AutoUpdate/winget-upgrade.ps1 b/Winget-AutoUpdate/winget-upgrade.ps1
index 89ed233..a907ddf 100644
--- a/Winget-AutoUpdate/winget-upgrade.ps1
+++ b/Winget-AutoUpdate/winget-upgrade.ps1
@@ -57,7 +57,7 @@ function Write-Log ($LogMsg,$LogColor = "White") {
function Start-NotifTask ($Title,$Message,$MessageType,$Balise) {
- #Add XML variables
+#Add XML variables
[xml]$ToastTemplate = @"
@@ -228,6 +228,103 @@ function Get-ExcludedApps{
}
}
+function Start-WAUUpdateCheck{
+ #Get AutoUpdate status
+ [xml]$UpdateStatus = Get-Content "$WorkingDir\config\config.xml" -Encoding UTF8 -ErrorAction SilentlyContinue
+ $AutoUpdateStatus = $UpdateStatus.app.autoupdate
+
+ #Check if AutoUpdate is enabled
+ if ($AutoUpdateStatus -eq $false){
+ Write-Log "WAU Current version: $CurrentVersion. AutoUpdate is disabled." "Cyan"
+ return $false
+ }
+ #If enabled, check online available version
+ else{
+ #Get Github latest version
+ $WAUurl = 'https://api.github.com/repos/Romanitho/Winget-AutoUpdate/releases/latest'
+ $LatestVersion = (Invoke-WebRequest $WAUurl | ConvertFrom-Json)[0].tag_name
+ [version]$AvailableVersion = $LatestVersion.Replace("v","")
+
+ #Get current installed version
+ [xml]$About = Get-Content "$WorkingDir\config\about.xml" -Encoding UTF8 -ErrorAction SilentlyContinue
+ [version]$CurrentVersion = $About.app.version
+
+ #If newer version is avalable, return $True
+ if ($AvailableVersion -gt $CurrentVersion){
+ Write-Log "WAU Current version: $CurrentVersion. Version $AvailableVersion is available." "Yellow"
+ return $true
+ }
+ else{
+ Write-Log "WAU Current version: $CurrentVersion. Up to date." "Green"
+ return $false
+ }
+ }
+}
+
+function Update-WAU{
+ #Get WAU Github latest version
+ $WAUurl = 'https://api.github.com/repos/Romanitho/Winget-AutoUpdate/releases/latest'
+ $LatestVersion = (Invoke-WebRequest $WAUurl | ConvertFrom-Json)[0].tag_name
+
+ #Send available update notification
+ $Title = $NotifLocale.local.outputs.output[2].title -f "Winget-AutoUpdate"
+ $Message = $NotifLocale.local.outputs.output[2].message -f $CurrentVersion, $LatestVersion
+ $MessageType = "info"
+ $Balise = "Winget-AutoUpdate"
+ Start-NotifTask $Title $Message $MessageType $Balise
+
+ #Run WAU update
+ try{
+ #Force to create a zip file
+ $ZipFile = "$WorkingDir\WAU_update.zip"
+ New-Item $ZipFile -ItemType File -Force | Out-Null
+
+ #Download the zip
+ Write-Log "Starting downloading the GitHub Repository"
+ Invoke-RestMethod -Uri "$($WAUurl)/zipball/$($LatestVersion)" -OutFile $ZipFile
+ Write-Log 'Download finished'
+
+ #Extract Zip File
+ Write-Log "Starting unzipping the WAU GitHub Repository"
+ $location = "$WorkingDir\WAU_update"
+ Expand-Archive -Path $ZipFile -DestinationPath $location -Force
+ Get-ChildItem -Path $location -Recurse | Unblock-File
+ Write-Log "Unzip finished"
+ $TempPath = Resolve-Path "$location\Romanitho-Winget-AutoUpdate*\Winget-AutoUpdate\"
+ Copy-Item -Path "$TempPath\*" -Destination ".\" -Recurse -Force
+
+ #Remove update zip file
+ Write-Log "Cleaning temp files"
+ Remove-Item -Path $ZipFile -Force -ErrorAction SilentlyContinue
+ #Remove update folder
+ Remove-Item -Path $location -Recurse -Force -ErrorAction SilentlyContinue
+
+ #Set new version to conf.xml
+ [xml]$XMLconf = Get-content "$WorkingDir\config\about.xml" -Encoding UTF8 -ErrorAction SilentlyContinue
+ $XMLconf.app.version = $LatestVersion.Replace("v","")
+ $XMLconf.Save("$WorkingDir\config\about.xml")
+
+ #Send success Notif
+ $Title = $NotifLocale.local.outputs.output[3].title -f "Winget-AutoUpdate"
+ $Message = $NotifLocale.local.outputs.output[3].message -f $LatestVersion
+ $MessageType = "success"
+ $Balise = "Winget-AutoUpdate"
+ Start-NotifTask $Title $Message $MessageType $Balise
+
+ #Rerun with newer version
+ Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
+ exit
+ }
+ catch{
+ #Send Error Notif
+ $Title = $NotifLocale.local.outputs.output[4].title -f "Winget-AutoUpdate"
+ $Message = $NotifLocale.local.outputs.output[4].message
+ $MessageType = "error"
+ $Balise = "Winget-AutoUpdate"
+ Start-NotifTask $Title $Message $MessageType $Balise
+ Write-Log "WAU Update failed"
+ }
+}
<# MAIN #>
@@ -236,6 +333,12 @@ Init
#Check network connectivity
if (Test-Network){
+ #Check if WAU is up to date
+ $CheckWAUupdate = Start-WAUUpdateCheck
+ #If AutoUpdate is enabled and Update is avalaible, then run WAU update
+ if ($CheckWAUupdate){
+ Update-WAU
+ }
#Get exclude apps list
$toSkip = Get-ExcludedApps