Last active
December 3, 2022 06:11
-
-
Save imba-tjd/412371ef43b133eb8ead37e6fd428797 to your computer and use it in GitHub Desktop.
Update Node.js and Golang for Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 本来是下到临时文件夹中自动安装的,但exec.Command("xxx.msi")报"%1 is not a valid Win32 application.",用msiexec报错误1619,用cmd /c报0xc000013a。因此干脆就只做下载器了 | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"regexp" | |
"runtime" | |
) | |
var stable_ver_regex = regexp.MustCompile(`"toggleVisible" id="go(.+?)">`) | |
var unstable_ver_regex = regexp.MustCompile(`"toggle" id="go(.+?)">`) | |
func download(url string) []byte { | |
res, err := http.Get(url) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if res.StatusCode >= 400 { | |
log.Fatal("Response failed with status code:", res.StatusCode) | |
} | |
body, err := io.ReadAll(res.Body) | |
res.Body.Close() | |
return body | |
} | |
func main() { | |
if runtime.GOOS != "windows" { | |
log.Fatal("Only Windows is supported.") | |
} | |
unstable_flag := flag.Bool("unstable", false, "use unstable version.") | |
flag.Parse() | |
unstable_wanted := false | |
if unstable_flag != nil { | |
unstable_wanted = *unstable_flag | |
} | |
// get required version and link | |
fmt.Println("Getting version info...") | |
body := download("https://golang.google.cn/dl/") | |
bodystr := string(body) | |
var wanted_ver string | |
stable_ver := stable_ver_regex.FindStringSubmatch(bodystr)[1] | |
unstable_ver := unstable_ver_regex.FindStringSubmatch(bodystr)[1] | |
if !unstable_wanted { | |
wanted_ver = stable_ver | |
} else { | |
wanted_ver = unstable_ver | |
} | |
fmt.Println("Wanted:", wanted_ver) | |
filename := fmt.Sprintf("go%s.windows-amd64.msi", wanted_ver) | |
msiurl := "https://dl.google.com/go/" + filename | |
// download | |
msifile, err := os.Create(filename) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Downloading from", msiurl) | |
msibytes := download(msiurl) | |
_, err2 := msifile.Write(msibytes) | |
if err2 != nil { | |
log.Fatal(err2) | |
} | |
msifile.Close() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-GolangVer { | |
$ProgressPreference = 'SilentlyContinue' | |
$info = iwr 'https://golang.google.cn/dl/' | |
if (-not $?) { return } | |
$info -match '"toggleVisible" id="go(.+?)">' | Out-Null | |
$ver = $Matches[1] | |
$ProgressPreference = 'Continue' | |
return $ver | |
} | |
function Get-GolangVerCurrent { | |
try { | |
(go version) -match 'go version go(.+?) windows/amd64' | Out-Null | |
return $Matches[1] | |
} catch [Management.Automation.CommandNotFoundException] { | |
return '' | |
} | |
} | |
function Update-Golang { | |
if(-not [Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([Runtime.InteropServices.OSPlatform]::Windows)) | |
{ echo 'This script only supports Windows.'; return } | |
echo 'Retriving latest version info...' | |
$current_ver = Get-GolangVerCurrent | |
$latest_ver = Get-GolangVer | |
if ($latest_ver -eq $null) { return } | |
$filename = "go$latest_ver.windows-amd64.msi" | |
echo "Current version: $current_ver" | |
echo "Latest version: $latest_ver" | |
if ($current_ver -eq $latest_ver) { | |
echo 'You are using the latest version.' | |
return | |
} else { | |
$info = iwr ('https://dl.google.com/go/' + $filename) -Method HEAD | |
if (-not $?) { return } | |
echo ('Installer size: ' + ([int]::parse($info.Headers.'Content-Length') / 1MB).ToString("F1") + ' MB') | |
echo "Downloading..." | |
pushd $env:TEMP | |
iwr ('https://dl.google.com/go/' + $filename) -OutFile $filename | |
if (-not $?) { return } | |
echo "Installing..." | |
& "./$filename" '/passive' | |
popd | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $script:baseurl = 'https://nodejs.org/dist/latest/' | |
$script:baseurl = 'https://cdn.npmmirror.com/binaries/node/latest/' | |
function Get-NodeVer { | |
$ProgressPreference = 'SilentlyContinue' | |
$shasumdata = iwr ($script:baseurl + 'SHASUMS256.txt') | |
if (-not $?) { return } | |
$shasumdata -match 'node-(.+?)-x64.msi' | Out-Null | |
$ver = $Matches[1] | |
$ProgressPreference = 'Continue' | |
return $ver | |
} | |
function Get-NodeVerCurrent { | |
try { | |
return (node -v) | |
} catch [Management.Automation.CommandNotFoundException] { | |
return '' | |
} | |
} | |
function Update-Node { | |
if(-not [Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([Runtime.InteropServices.OSPlatform]::Windows)) | |
{ echo 'This script only supports Windows.'; return } | |
echo 'Retriving latest version info...' | |
$current_ver = Get-NodeVerCurrent | |
$latest_ver = Get-NodeVer | |
if ($latest_ver -eq $null) { return } | |
if ([Environment]::Is64BitOperatingSystem) { $sysbits = 'x64' } else { $sysbits = 'x86' } | |
$filename = "node-$latest_ver-$sysbits.msi" | |
echo "Current version: $current_ver" | |
echo "Latest version: $latest_ver" | |
if ($current_ver -eq $latest_ver) { | |
echo 'You are using the latest version.' | |
return | |
} else { | |
$info = iwr ($script:baseurl + $filename) -Method HEAD | |
if (-not $?) { return } | |
echo ('Installer size: ' + ([int]::parse($info.Headers.'Content-Length') / 1MB).ToString("F1") + ' MB') | |
echo "Downloading..." | |
pushd $env:TEMP | |
iwr ($script:baseurl + $filename) -OutFile $filename | |
if (-not $?) { return } | |
echo "Installing..." | |
& "./$filename" '/passive' | |
popd | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment