|
| 1 | +if($ENV:OS -ne 'Windows_NT'){ |
| 2 | + Write-Host 'Windows Plz!' -ForegroundColor Red |
| 3 | + return |
| 4 | +} |
| 5 | +if($PSVersionTable.PSVersion.Major -lt 3){ |
| 6 | + Write-Host "I need PowerShell major version >= 3, Current is $($PSVersionTable.PSVersion.Major)" -ForegroundColor Red |
| 7 | + return |
| 8 | +} |
| 9 | + |
| 10 | +$installLocation = $env:ggdir |
| 11 | +$arch = $env:ggarch |
| 12 | +#$branch = $env:ggbranch |
| 13 | +$ggApi = 'https://api.pzhacm.org/iivb/cu.json' |
| 14 | + |
| 15 | + |
| 16 | +if($PSVersionTable.PSVersion.Major -lt 5){ |
| 17 | + if (-not ([System.Management.Automation.PSTypeName]'Branch').Type){ |
| 18 | + Add-Type -TypeDefinition @" |
| 19 | + public enum Branch |
| 20 | + { |
| 21 | + Stable, |
| 22 | + Beta, |
| 23 | + Dev, |
| 24 | + Canary |
| 25 | + } |
| 26 | +"@ |
| 27 | + } |
| 28 | +} |
| 29 | +else{ |
| 30 | + Enum Branch{ |
| 31 | + Stable |
| 32 | + Beta |
| 33 | + Dev |
| 34 | + Canary |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#$arch check |
| 39 | +if ([string]::IsNullOrEmpty($arch)){ |
| 40 | + if($ENV:PROCESSOR_ARCHITECTURE -eq 'AMD64'){ |
| 41 | + $arch = 'x64' |
| 42 | + } |
| 43 | + else{ |
| 44 | + $arch = 'x86' |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#$installLocation check |
| 49 | +if ([string]::IsNullOrEmpty($installLocation)){ |
| 50 | + $installLocation = (Resolve-Path .\).Path |
| 51 | +} |
| 52 | + |
| 53 | +#$branch check |
| 54 | +switch ($env:ggbranch) |
| 55 | +{ |
| 56 | + 'canary' {$branch = [Branch]::Canary} |
| 57 | + 'dev' {$branch = [Branch]::Dev} |
| 58 | + 'beta' {$branch = [Branch]::Beta} |
| 59 | + default {$branch = [Branch]::Stable} |
| 60 | +} |
| 61 | +#if([Enum]::Getvalues([Branch]) -contains $branch) { |
| 62 | +# $Branch = [Enum]::Parse([Type]"Branch",$branch) |
| 63 | +#} |
| 64 | +Write-Host "Current branch is " -NoNewline -ForegroundColor DarkYellow |
| 65 | +Write-Host $branch -ForegroundColor Green |
| 66 | + |
| 67 | +if ($env:TEMP -eq $null) { |
| 68 | + $env:TEMP = Join-Path $installLocation 'temp' |
| 69 | +} |
| 70 | + |
| 71 | +function Check-InstallLocation { |
| 72 | + if((Test-Path $installLocation)){ |
| 73 | + if((Test-Path "$installLocation\chrome.exe")){ |
| 74 | + $onlineVersion = [System.Version]($JSON.$branch.$arch.version) |
| 75 | + $localVersion = (Get-Item "$installLocation\chrome.exe").VersionInfo.FileVersion |
| 76 | + if($onlineVersion -gt $localVersion){ |
| 77 | + Write-Host "Online version is " -NoNewline |
| 78 | + Write-Host $onlineVersion -NoNewline -ForegroundColor Green |
| 79 | + Write-Host ", Local version is " -NoNewline |
| 80 | + Write-Host $localVersion -NoNewline -ForegroundColor Yellow |
| 81 | + Write-Host ', let`s update!' |
| 82 | + } |
| 83 | + else{ |
| 84 | + Write-Host "You have the latest version($localVersion)/$branch/$arch" -ForegroundColor Green |
| 85 | + return $false |
| 86 | + } |
| 87 | + } else { |
| 88 | + if(-Not ((Get-ChildItem $installLocation | Measure-Object).Count -eq 0)){ |
| 89 | + Write-Host 'I need an empty folder!' -ForegroundColor Red |
| 90 | + return $false |
| 91 | + } |
| 92 | + } |
| 93 | + } else { |
| 94 | + Write-Host "Create directory $installLocation" -ForegroundColor Yellow |
| 95 | + New-Item -ItemType Directory -Force -Path $installLocation | Out-Null |
| 96 | + } |
| 97 | + return $true |
| 98 | +} |
| 99 | + |
| 100 | +function Download-String { |
| 101 | +param ( |
| 102 | + [string]$url |
| 103 | + ) |
| 104 | + $downloader = new-object System.Net.WebClient |
| 105 | + $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials |
| 106 | + if ($defaultCreds -ne $null) { |
| 107 | + $downloader.Credentials = $defaultCreds |
| 108 | + } |
| 109 | + $downloader.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy() |
| 110 | + return $downloader.DownloadString($url) |
| 111 | +} |
| 112 | + |
| 113 | +function Download-File { |
| 114 | +param ( |
| 115 | + [string]$url, |
| 116 | + [string]$targetFile |
| 117 | + ) |
| 118 | + #https://blogs.msdn.microsoft.com/jasonn/2008/06/13/downloading-files-from-the-internet-in-powershell-with-progress/ |
| 119 | + $uri = New-Object "System.Uri" "$url" |
| 120 | + $request = [System.Net.HttpWebRequest]::Create($uri) |
| 121 | + $request.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy() |
| 122 | + $request.Timeout = 60000 #60 second timeout |
| 123 | + $response = $request.GetResponse() |
| 124 | + $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) |
| 125 | + $responseStream = $response.GetResponseStream() |
| 126 | + $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create |
| 127 | + $buffer = new-object byte[] 256KB |
| 128 | + $count = $responseStream.Read($buffer,0,$buffer.length) |
| 129 | + $downloadedBytes = $count |
| 130 | + while ($count -gt 0) |
| 131 | + { |
| 132 | + $targetStream.Write($buffer, 0, $count) |
| 133 | + $count = $responseStream.Read($buffer,0,$buffer.length) |
| 134 | + $downloadedBytes = $downloadedBytes + $count |
| 135 | + Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100) |
| 136 | + } |
| 137 | + Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'" -Status "Ready" -Completed |
| 138 | + $targetStream.Flush() |
| 139 | + $targetStream.Close() |
| 140 | + $targetStream.Dispose() |
| 141 | + $responseStream.Dispose() |
| 142 | +} |
| 143 | + |
| 144 | +function Extract-File { |
| 145 | +param ( |
| 146 | + [string]$fileName, |
| 147 | + [string]$dest |
| 148 | + ) |
| 149 | + #WARNING: this function copy from chocolatey.org install.ps1 |
| 150 | + #Write-Host "Extract $fileName to $dest" -ForegroundColor Yellow |
| 151 | + Write-Host "Extracting $($fileName.split('\') | Select -Last 1)" -ForegroundColor Yellow |
| 152 | + $7zaExe = Join-Path $env:TEMP '7za.exe' |
| 153 | + if (-Not (Test-Path ($7zaExe))) { |
| 154 | + Write-Output "Downloading 7-Zip commandline tool prior to extraction." |
| 155 | + Download-File 'https://chocolatey.org/7za.exe' "$7zaExe" |
| 156 | + } |
| 157 | + $params = "x -o`"$dest`" -bd -y `"$fileName`"" |
| 158 | + $process = New-Object System.Diagnostics.Process |
| 159 | + $process.StartInfo = New-Object System.Diagnostics.ProcessStartInfo($7zaExe, $params) |
| 160 | + $process.StartInfo.RedirectStandardOutput = $true |
| 161 | + $process.StartInfo.UseShellExecute = $false |
| 162 | + $process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden |
| 163 | + $process.Start() | Out-Null |
| 164 | + $process.BeginOutputReadLine() |
| 165 | + $process.WaitForExit() |
| 166 | + $exitCode = $process.ExitCode |
| 167 | + $process.Dispose() |
| 168 | + |
| 169 | + $errorMessage = "Unable to unzip package using 7zip. Error:" |
| 170 | + switch ($exitCode) { |
| 171 | + 0 { break } |
| 172 | + 1 { throw "$errorMessage Some files could not be extracted" } |
| 173 | + 2 { throw "$errorMessage 7-Zip encountered a fatal error while extracting the files" } |
| 174 | + 7 { throw "$errorMessage 7-Zip command line error" } |
| 175 | + 8 { throw "$errorMessage 7-Zip out of memory" } |
| 176 | + 255 { throw "$errorMessage Extraction cancelled by the user" } |
| 177 | + default { throw "$errorMessage 7-Zip signalled an unknown error (code $exitCode)" } |
| 178 | + } |
| 179 | + |
| 180 | +} |
| 181 | + |
| 182 | +function Remove-IfExists { |
| 183 | +param ( |
| 184 | + [string]$file |
| 185 | + ) |
| 186 | + if(Test-Path $file){ |
| 187 | + Remove-Item $file |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +function Download-Chrome { |
| 192 | + $chrome7z = Join-Path $installLocation 'chrome.7z' |
| 193 | + $url = $JSON.$branch.$arch.cdn |
| 194 | + $downloadFileName = Join-Path $installLocation $($url.split('/') | Select -Last 1) |
| 195 | + Download-File $url $downloadFileName |
| 196 | + if(-Not (Test-Path $downloadFileName)){ |
| 197 | + Write-Host 'Chrome Download Fail!' -ForegroundColor Red |
| 198 | + return |
| 199 | + } |
| 200 | + $hash = (Get-FileHash $downloadFileName -Algorithm SHA256).Hash |
| 201 | + if($hash -ne $JSON.$branch.$arch.sha256){ |
| 202 | + Write-Host "SHA256 not match!" -ForegroundColor Red |
| 203 | + Remove-IfExists $downloadFileName |
| 204 | + return; |
| 205 | + } |
| 206 | + Extract-File $downloadFileName $installLocation |
| 207 | + Extract-File $chrome7z $installLocation |
| 208 | + Remove-IfExists $chrome7z |
| 209 | + Move-Item "$installLocation\Chrome-bin\*" -Destination $installLocation |
| 210 | + Remove-IfExists "$installLocation\Chrome-bin" |
| 211 | + Remove-IfExists $downloadFileName |
| 212 | + Write-Host 'Chrome Download Finished' -ForegroundColor Green |
| 213 | +} |
| 214 | + |
| 215 | +try{ |
| 216 | + $JSON = Download-String 'https://api.pzhacm.org/iivb/cu.json' | ConvertFrom-Json |
| 217 | +}catch{ |
| 218 | + Write-Host 'Get versions error!' -ForegroundColor Red |
| 219 | + return |
| 220 | +} |
| 221 | +if(Check-InstallLocation) { |
| 222 | + Download-Chrome |
| 223 | +} |
0 commit comments