|
| 1 | +Set-StrictMode -Version Latest |
| 2 | +$ErrorActionPreference = "Stop" |
| 3 | +$PSDefaultParameterValues['*:ErrorAction']='Stop' |
| 4 | + |
| 5 | +# Always download the latest version |
| 6 | +# For more information about the cg.jar, please vist the following page: |
| 7 | +# https://github.com/excel-code-generator/code-generator |
| 8 | + |
| 9 | +$repo="excel-code-generator/code-generator" |
| 10 | +$repoPath="https://github.com/$repo" |
| 11 | +$rawPath="https://raw.githubusercontent.com/$repo" |
| 12 | + |
| 13 | +# latest version |
| 14 | +echo "Get the latest version" |
| 15 | +$releasesUri = "https://api.github.com/repos/$repo/releases/latest" |
| 16 | +$tag = (Invoke-WebRequest $releasesUri | ConvertFrom-Json)[0].tag_name |
| 17 | +echo "Latest version: $tag" |
| 18 | + |
| 19 | +# download |
| 20 | +echo "Downloading..." |
| 21 | +function download_file($name, $path) { |
| 22 | + echo "Download file $path " |
| 23 | + if ( Test-Path "$env:TEMP\$name" ) { |
| 24 | + del "$env:TEMP\$name" |
| 25 | + } |
| 26 | + |
| 27 | + Invoke-WebRequest $path -Out "$env:TEMP\$name" |
| 28 | +} |
| 29 | +download_file cg "$rawPath/$tag/LICENSE" |
| 30 | +download_file cg.bat "$rawPath/$tag/cg.bat" |
| 31 | +download_file cg.jar "$repoPath/releases/download/$tag/cg.jar" |
| 32 | +echo "Download success." |
| 33 | + |
| 34 | +# move file |
| 35 | +$cgPath = "$env:APPDATA\cg\bin" |
| 36 | +$existDir = Test-Path $cgPath |
| 37 | +if ($existDir -eq $False) { |
| 38 | + mkdir -p $cgPath |
| 39 | +} |
| 40 | + |
| 41 | +mv "$env:TEMP\cg" $cgPath |
| 42 | +mv "$env:TEMP\cg.bat" $cgPath |
| 43 | +mv "$env:TEMP\cg.jar" $cgPath |
| 44 | + |
| 45 | +# set environment |
| 46 | +echo "setting environment." |
| 47 | +$alreadyAdded = $False |
| 48 | +$path = [environment]::GetEnvironmentvariable("PATH", "User") |
| 49 | +$path.Split(";") | ForEach-Object -Process {if ($_ -eq $cgPath) { $alreadyAdded = $True }} |
| 50 | + |
| 51 | +if (! $alreadyAdded) { |
| 52 | + $path = $path + ";" + $cgPath |
| 53 | + $path = [environment]::SetEnvironmentvariable("PATH", $path, "User") |
| 54 | +} |
| 55 | + |
| 56 | +echo "Install success!" |
| 57 | +cg -v |
0 commit comments