|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Description: Generic Update Script for PortableApps |
| 3 | +# Author: Urs Roesch <[email protected]> |
| 4 | +# ----------------------------------------------------------------------------- |
| 5 | + |
| 6 | +# ----------------------------------------------------------------------------- |
| 7 | +# Globals |
| 8 | +# ----------------------------------------------------------------------------- |
| 9 | +$AppRoot = "$PSScriptRoot\..\.." |
| 10 | +$AppInfoDir = "$AppRoot\App\AppInfo" |
| 11 | +$AppInfoIni = "$AppInfoDir\appinfo.ini" |
| 12 | +$UpdateIni = "$AppInfoDir\update.ini" |
| 13 | +$Debug = $True |
| 14 | + |
| 15 | +# ----------------------------------------------------------------------------- |
| 16 | +# Functions |
| 17 | +# ----------------------------------------------------------------------------- |
| 18 | +Function Debug () { |
| 19 | + param( [string] $Message ) |
| 20 | + If (-Not($Debug)) { return } |
| 21 | + Write-Host $Message |
| 22 | +} |
| 23 | + |
| 24 | +# ----------------------------------------------------------------------------- |
| 25 | +Function Parse-Ini { |
| 26 | + param ( |
| 27 | + $IniFile |
| 28 | + ) |
| 29 | + |
| 30 | + $IniContent = Get-Content $IniFile |
| 31 | + |
| 32 | + $resulttable=@() |
| 33 | + foreach ($line in $IniContent) { |
| 34 | + Debug "Processing $line" |
| 35 | + if ($line[0] -eq ";") { |
| 36 | + Debug "Skip comment line" |
| 37 | + } |
| 38 | + |
| 39 | + elseif ($line[0] -eq "[") { |
| 40 | + $Section = $line.replace("[","").replace("]","") |
| 41 | + Debug "Found new section: $Section" |
| 42 | + } |
| 43 | + elseif ($line -like "*=*") { |
| 44 | + Debug "Found Keyline" |
| 45 | + $resulttable += @{ |
| 46 | + Section = $Section |
| 47 | + Key = $line.split("=")[0].Trim() |
| 48 | + Value = $line.split("=")[1].Trim() |
| 49 | + } |
| 50 | + } |
| 51 | + else { |
| 52 | + Debug "Skip line" |
| 53 | + } |
| 54 | + } |
| 55 | + return $resulttable |
| 56 | +} |
| 57 | + |
| 58 | +# ----------------------------------------------------------------------------- |
| 59 | +Function Fetch-Section() { |
| 60 | + param( [string] $Key ) |
| 61 | + $Section = @{} |
| 62 | + Foreach ($Item in $Config) { |
| 63 | + If ($Item["Section"] -eq $Key) { |
| 64 | + $Section += @{ $Item["Key"] = $Item["Value"] } |
| 65 | + } |
| 66 | + } |
| 67 | + return $Section |
| 68 | +} |
| 69 | + |
| 70 | +# ----------------------------------------------------------------------------- |
| 71 | +Function Url-Basename { |
| 72 | + param( |
| 73 | + [string] $URL |
| 74 | + ) |
| 75 | + $Elements = $URL.split('/') |
| 76 | + $Basename = $Elements[$($Elements.Length-1)] |
| 77 | + return $Basename |
| 78 | +} |
| 79 | + |
| 80 | +# ----------------------------------------------------------------------------- |
| 81 | +Function Check-Sum { |
| 82 | + param( |
| 83 | + [string] $Checksum, |
| 84 | + [string] $File |
| 85 | + ) |
| 86 | + ($Algorithm, $Sum) = $Checksum.Split(':') |
| 87 | + $Result = (Get-FileHash -Path $File -Algorithm $Algorithm).Hash |
| 88 | + Debug "Checksum of INI ($Sum) and downloaded file ($Result)" |
| 89 | + return ($Sum -eq $Result) |
| 90 | +} |
| 91 | + |
| 92 | +# ----------------------------------------------------------------------------- |
| 93 | +Function Download-ZIP { |
| 94 | + param( |
| 95 | + [string] $URL, |
| 96 | + [string] $Checksum |
| 97 | + ) |
| 98 | + $PathZip = "$PSScriptRoot\$(Url-Basename -URL $URL)" |
| 99 | + If (!(Test-Path $PathZip)) { |
| 100 | + Debug "Downloading file from '$URL'" |
| 101 | + Invoke-WebRequest -Uri $URL -OutFile $PathZip |
| 102 | + } |
| 103 | + If (!(Check-Sum -Checksum $Checksum -File $PathZip)) { |
| 104 | + Debug "Checksum of File $PathZip does not match with '$Checksum'" |
| 105 | + Exit 1 |
| 106 | + } |
| 107 | + Debug "Downloaded ZIP file '$PathZip'" |
| 108 | + return $PathZip |
| 109 | +} |
| 110 | + |
| 111 | +# ----------------------------------------------------------------------------- |
| 112 | +Function Update-Zip { |
| 113 | + param( |
| 114 | + [string] $URL, |
| 115 | + [string] $TargetDir, |
| 116 | + [string] $ExtractDir, |
| 117 | + [string] $Checksum |
| 118 | + ) |
| 119 | + $ZipFile = $(Download-ZIP -URL $URL -Checksum $Checksum) |
| 120 | + $TargetPath = "$AppRoot\App\$TargetDir" |
| 121 | + Expand-Archive -LiteralPath $ZipFile -DestinationPath $PSScriptRoot -Force |
| 122 | + If (Test-Path $TargetPath) { |
| 123 | + Write-Output "Removing $TargetPath" |
| 124 | + Remove-Item -Path $TargetPath -Force -Recurse |
| 125 | + } |
| 126 | + Debug "Move $ExtractDir to $TargetPath" |
| 127 | + Move-Item -Path $PSScriptRoot\$ExtractDir -Destination $TargetPath -Force |
| 128 | + Debug "Cleanup $ZipFile" |
| 129 | + Remove-Item $ZipFile |
| 130 | +} |
| 131 | + |
| 132 | +# ----------------------------------------------------------------------------- |
| 133 | +Function Update-Appinfo-Item() { |
| 134 | + param( |
| 135 | + [string] $IniFile, |
| 136 | + [string] $Match, |
| 137 | + [string] $Replace |
| 138 | + ) |
| 139 | + If (Test-Path $IniFile) { |
| 140 | + $Content = (Get-Content $IniFile) |
| 141 | + $Content -replace $Match, $Replace | Out-File -FilePath $IniFile |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +# ----------------------------------------------------------------------------- |
| 146 | +Function Update-Appinfo() { |
| 147 | + $Version = (Fetch-Section "Version") |
| 148 | + Update-Appinfo-Item ` |
| 149 | + -IniFile $AppInfoIni ` |
| 150 | + -Match '^PackageVersion\s*=.*' ` |
| 151 | + -Replace "PackageVersion=$($Version['Package'])" |
| 152 | + Update-Appinfo-Item ` |
| 153 | + -IniFile $AppInfoIni ` |
| 154 | + -Match '^DisplayVersion\s*=.*' ` |
| 155 | + -Replace "DisplayVersion=$($Version['Display'])" |
| 156 | +} |
| 157 | + |
| 158 | +# ----------------------------------------------------------------------------- |
| 159 | +Function Update-Application() { |
| 160 | + $Archive = (Fetch-Section 'Archive') |
| 161 | + $Position = 1 |
| 162 | + While ($True) { |
| 163 | + If (-Not ($Archive.ContainsKey("URL$Position"))) { |
| 164 | + Break |
| 165 | + } |
| 166 | + Update-ZIP ` |
| 167 | + -URL $Archive["URL$Position"] ` |
| 168 | + -TargetDir $Archive["TargetDir$Position"] ` |
| 169 | + -ExtractDir $Archive["ExtractDir$Position"] ` |
| 170 | + -Checksum $Archive["Checksum$Position"] |
| 171 | + $Position += 1 |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +# ----------------------------------------------------------------------------- |
| 176 | +Function Windows-Path() { |
| 177 | + param( [string] $Path ) |
| 178 | + $Path = $Path -replace ".*drive_(.)", '$1:' |
| 179 | + $Path = $Path.Replace("/", "\") |
| 180 | + return $Path |
| 181 | +} |
| 182 | + |
| 183 | +# ----------------------------------------------------------------------------- |
| 184 | +Function Create-Launcher() { |
| 185 | + Set-Location $AppRoot |
| 186 | + $AppPath = (Get-Location) |
| 187 | + $Launcher = "..\PortableApps.comLauncher\PortableApps.comLauncherGenerator.exe" |
| 188 | + If ($AppPath[0] -eq '/') { |
| 189 | + Debug "Running Launcher: wine $Launcher $(Windows-Path AppPath)" |
| 190 | + Invoke-Expression "wine $Launcher /s $(Windows-Path $AppPath)" |
| 191 | + } |
| 192 | + Else { |
| 193 | + Debug "Running Launcher: wine $Launcher AppPath" |
| 194 | + Invoke-Expression "$Launcher $AppPath" |
| 195 | + } |
| 196 | + Write-FileSystemCache $AppPath.Drive.Name |
| 197 | +} |
| 198 | + |
| 199 | +# ----------------------------------------------------------------------------- |
| 200 | +Function Create-Installer() { |
| 201 | + Set-Location $AppRoot |
| 202 | + $AppPath = (Get-Location) |
| 203 | + $Installer = "..\PortableApps.comInstaller\PortableApps.comInstaller.exe" |
| 204 | + If ($AppPath[0] -eq '/') { |
| 205 | + Debug "Running Installer: wine $Installer $(Windows-Path AppPath)" |
| 206 | + Invoke-Expression "wine $Installer $(Windows-Path $AppPath)" |
| 207 | + } |
| 208 | + Else { |
| 209 | + # Windows seems to need a bit of break before |
| 210 | + # writing the file completely to disk |
| 211 | + Debug "Sleeping ..." |
| 212 | + Sleep 5 |
| 213 | + Debug "Running Installer: $Installer $AppPath" |
| 214 | + Invoke-Expression "$Installer $AppPath" |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +# ----------------------------------------------------------------------------- |
| 219 | +# Main |
| 220 | +# ----------------------------------------------------------------------------- |
| 221 | +$Config = (Parse-Ini $UpdateIni) |
| 222 | +Update-Application |
| 223 | +Update-Appinfo |
| 224 | +Create-Launcher |
| 225 | +Create-Installer |
0 commit comments