PowerShell cleanup

* Show download progress again
  * Done by leaving $progressPreference set to system default
* If there are errors during download, then bail before extracting data
* Moving to a new style guide for PowerShell scripts
  * Using this: https://poshcode.gitbooks.io/powershell-practice-and-style/
  * Encoding set to UTF-8 BOM instead of UTF-8
This commit is contained in:
Alan Mason 2017-11-27 22:58:17 -08:00
parent e7dd6a1b54
commit 5d588f3563

View file

@ -1,182 +1,207 @@
# Wizard Kit: Download kit components # Wizard Kit: Download kit components
## Init ## ## Init ##
#Requires -Version 3.0 #Requires -Version 3.0
clear
if (Test-Path Env:\DEBUG) { if (Test-Path Env:\DEBUG) {
Set-PSDebug -Trace 1 Set-PSDebug -Trace 1
} }
$host.UI.RawUI.WindowTitle = "Wizard Kit: Build Tool" $Host.UI.RawUI.WindowTitle = "Wizard Kit: Build Tool"
$wd = $(Split-Path $MyInvocation.MyCommand.Path) $WD = $(Split-Path $MyInvocation.MyCommand.Path)
$bin = (Get-Item $wd).Parent.FullName $Bin = (Get-Item $WD).Parent.FullName
$root = (Get-Item $bin -Force).Parent.FullName $Root = (Get-Item $Bin -Force).Parent.FullName
$tmp = "{0}\tmp" -f $bin $Temp = "$Bin\tmp"
$errors = 0 $System32 = "{0}\System32" -f $Env:SystemRoot
pushd "$wd" Push-Location "$WD"
$host.UI.RawUI.BackgroundColor = "black" $Host.UI.RawUI.BackgroundColor = "black"
$host.UI.RawUI.ForegroundColor = "white" $Host.UI.RawUI.ForegroundColor = "white"
$progressPreference = 'silentlyContinue'
## Safety Check ##
if ($PSVersionTable.PSVersion.Major -eq 6 -and $PSVersionTable.OS -imatch "Windows 6.1") {
Write-Host -ForegroundColor "Red" "`nAborted."
Write-Host "`nThis script doesn't support PowerShell 6.0 on Windows 7."
Write-Host "Press Enter to exit... " -NoNewLine
Read-Host
exit
}
## Functions ## ## Functions ##
function download-file { function Abort {
param ([String]$path, [String]$name, [String]$url) Write-Host -ForegroundColor "Red" "`nAborted."
$outfile = "{0}\{1}" -f $path, $name WKPause "Press Enter to exit..."
exit
}
function DownloadFile ($Path, $Name, $Url) {
$OutFile = "{0}\{1}" -f $Path, $Name
Write-Host ("Downloading: {0}" -f $name) Write-Host ("Downloading: $Name")
New-Item -Type Directory $path 2>&1 | Out-Null New-Item -Type Directory $Path 2>&1 | Out-Null
try { try {
invoke-webrequest -uri $url -outfile $outfile Invoke-WebRequest -Uri $Url -OutFile $OutFile
} }
catch { catch {
Write-Host (" ERROR: Failed to download file." ) -foregroundcolor "Red" Write-Host (" ERROR: Failed to download file." ) -ForegroundColor "Red"
$errors += 1 $DownloadErrors += 1
} }
} }
function find-dynamic-url { function FindDynamicUrl ($SourcePage, $RegEx) {
param ([String]$source_page, [String]$regex)
$d_url = ""
# Get source page # Get source page
invoke-webrequest -uri $source_page -outfile "tmp_page" Invoke-Webrequest -Uri $SourcePage -OutFile "tmp_page"
# Search for real url # Search for real url
$d_url = Get-Content "tmp_page" | Where-Object {$_ -imatch $regex} $Url = Get-Content "tmp_page" | Where-Object {$_ -imatch $RegEx}
$d_url = $d_url -ireplace '.*(a |)href="([^"]+)".*', '$2' $Url = $Url -ireplace '.*(a |)href="([^"]+)".*', '$2'
$d_url = $d_url -ireplace ".*(a |)href='([^']+)'.*", '$2' $Url = $Url -ireplace ".*(a |)href='([^']+)'.*", '$2'
# Remove tmp_page # Remove tmp_page
Remove-Item "tmp_page" Remove-Item "tmp_page"
return $d_url $Url | Select-Object -First 1
} }
function wk_pause { function WKPause ($Message = "Press Enter to continue... ") {
param([string]$message = "Press Enter to continue... ") Write-Host $Message -NoNewLine
Write-Host $message Read-Host
$x = read-host
} }
## Safety Check ##
if ($PSVersionTable.PSVersion.Major -eq 6 -and $PSVersionTable.OS -imatch "Windows 6.1") {
Write-Host "`nThis script doesn't support PowerShell 6.0 on Windows 7."
Write-Host "Press Enter to exit... " -NoNewLine
Abort
}
## PowerShell equivalent of Python's "if __name__ == '__main__'"
# Code based on StackOverflow comments
# Question: https://stackoverflow.com/q/4693947
# Using answer: https://stackoverflow.com/a/5582692
# Asked by: https://stackoverflow.com/users/65164/mark-mascolino
# Answer by: https://stackoverflow.com/users/696808/bacon-bits
if ($MyInvocation.InvocationName -ne ".") {
Clear-Host
Write-Host "Wizard Kit: Build Tool`n`n`n`n`n"
## Download ## ## Download ##
$path = $tmp $DownloadErrors = 0
$Path = $Temp
# 7-Zip # 7-Zip
$url = "http://www.7-zip.org/a/7z1701.msi" DownloadFile -Path $Path -Name "7z-installer.msi" -Url "http://www.7-zip.org/a/7z1701.msi"
download-file $path "7z-installer.msi" $url DownloadFile -Path $Path -Name "7z-extra.7z" -Url "http://www.7-zip.org/a/7z1701-extra.7z"
$url = "http://www.7-zip.org/a/7z1701-extra.7z"
download-file $path "7z-extra.7z" $url
# ConEmu # ConEmu
$url = "https://github.com/Maximus5/ConEmu/releases/download/v17.11.09/ConEmuPack.171109.7z" $Url = "https://github.com/Maximus5/ConEmu/releases/download/v17.11.09/ConEmuPack.171109.7z"
download-file $path "ConEmuPack.7z" $url DownloadFile -Path $Path -Name "ConEmuPack.7z" -Url $Url
# Notepad++ # Notepad++
$url = "https://notepad-plus-plus.org/repository/7.x/7.5.1/npp.7.5.1.bin.minimalist.7z" $Url = "https://notepad-plus-plus.org/repository/7.x/7.5.1/npp.7.5.1.bin.minimalist.7z"
download-file $path "npp.7z" $url DownloadFile -Path $Path -Name "npp.7z" -Url $Url
# Python # Python
$url = "https://www.python.org/ftp/python/3.6.3/python-3.6.3-embed-win32.zip" $Url = "https://www.python.org/ftp/python/3.6.3/python-3.6.3-embed-win32.zip"
download-file $path "python32.zip" $url DownloadFile -Path $Path -Name "python32.zip" -Url $Url
$url = "https://www.python.org/ftp/python/3.6.3/python-3.6.3-embed-amd64.zip" $Url = "https://www.python.org/ftp/python/3.6.3/python-3.6.3-embed-amd64.zip"
download-file $path "python64.zip" $url DownloadFile -Path $Path -Name "python64.zip" -Url $Url
# Python: psutil # Python: psutil
$dl_page = "https://pypi.python.org/pypi/psutil" $DownloadPage = "https://pypi.python.org/pypi/psutil"
$regex = "href=.*-cp36-cp36m-win32.whl" $RegEx = "href=.*-cp36-cp36m-win32.whl"
$url = find-dynamic-url $dl_page $regex $Url = FindDynamicUrl $DownloadPage $RegEx
download-file $path "psutil32.whl" $url DownloadFile -Path $Path -Name "psutil32.whl" -Url $Url
$regex = "href=.*-cp36-cp36m-win_amd64.whl" $RegEx = "href=.*-cp36-cp36m-win_amd64.whl"
$url = find-dynamic-url $dl_page $regex $Url = FindDynamicUrl $DownloadPage $RegEx
download-file $path "psutil64.whl" $url DownloadFile -Path $Path -Name "psutil64.whl" -Url $Url
# Python: requests & dependancies # Python: requests & dependancies
$regex = "href=.*.py3-none-any.whl" $RegEx = "href=.*.py3-none-any.whl"
foreach ($mod in @("chardet", "certifi", "idna", "urllib3", "requests")) { foreach ($Module in @("chardet", "certifi", "idna", "urllib3", "requests")) {
$dl_page = "https://pypi.python.org/pypi/{0}" -f $mod $DownloadPage = "https://pypi.python.org/pypi/$Module"
$name = "{0}.whl" -f $mod $Name = "$Module.whl"
$url = find-dynamic-url $dl_page $regex $Url = FindDynamicUrl -SourcePage $DownloadPage -RegEx $RegEx
download-file $path $name $url DownloadFile -Path $Path -Name $Name -Url $Url
}
## Bail ##
# If errors were encountered during downloads
if ($DownloadErrors -gt 0) {
Abort
} }
## Extract ## ## Extract ##
# 7-Zip # 7-Zip
Write-Host "Extracting: 7-Zip" Write-Host "Extracting: 7-Zip"
try { try {
start "msiexec" -argumentlist @("/a", "$tmp\7z-installer.msi", "TARGETDIR=$tmp\7zi", "/qn") -wait $ArgumentList = @("/a", "$Temp\7z-installer.msi", "TARGETDIR=$Temp\7zi", "/qn")
$sz = "$tmp\7zi\Files\7-Zip\7z.exe" Start-Process -FilePath "$System32\msiexec.exe" -ArgumentList $ArgumentList -Wait
start $sz -argumentlist @("x", "$tmp\7z-extra.7z", "-o$bin\7-Zip", "-aoa", "-bso0", "-bse0", "-bsp0", "-x!x64\*.dll", "-x!Far", "-x!*.dll") -nonewwindow -wait $SevenZip = "$Temp\7zi\Files\7-Zip\7z.exe"
$ArgumentList = @(
"x", "$Temp\7z-extra.7z", "-o$Bin\7-Zip",
"-aoa", "-bso0", "-bse0", "-bsp0",
"-x!x64\*.dll", "-x!Far", "-x!*.dll")
Start-Process -FilePath $SevenZip -ArgumentList $ArgumentList -NoNewWindow -Wait
Start-Sleep 1 Start-Sleep 1
Move-Item "$bin\7-Zip\x64\7za.exe" "$bin\7-Zip\7za64.exe" Move-Item "$Bin\7-Zip\x64\7za.exe" "$Bin\7-Zip\7za64.exe"
Remove-Item "$bin\7-Zip\x64" -Recurse Remove-Item "$Bin\7-Zip\x64" -Recurse
Remove-Item "$tmp\7z*" -Recurse Remove-Item "$Temp\7z*" -Recurse
$sz = "$bin\7-Zip\7za.exe" $SevenZip = "$Bin\7-Zip\7za.exe"
} }
catch { catch {
Write-Host (" ERROR: Failed to extract files." ) -foregroundcolor "Red" Write-Host (" ERROR: Failed to extract files." ) -ForegroundColor "Red"
} }
# Notepad++ # Notepad++
Write-Host "Extracting: Notepad++" Write-Host "Extracting: Notepad++"
try { try {
start $sz -argumentlist @("x", "$tmp\npp.7z", "-o$bin\NotepadPlusPlus", "-aoa", "-bso0", "-bse0", "-bsp0") -nonewwindow -wait $ArgumentList = @(
Remove-Item "$tmp\npp.7z" "x", "$Temp\npp.7z", "-o$Bin\NotepadPlusPlus",
Move-Item "$bin\NotepadPlusPlus\notepad++.exe" "$bin\NotepadPlusPlus\notepadplusplus.exe" "-aoa", "-bso0", "-bse0", "-bsp0")
Start-Process -FilePath $SevenZip -ArgumentList $ArgumentList -NoNewWindow -Wait
Remove-Item "$Temp\npp.7z"
Move-Item "$Bin\NotepadPlusPlus\notepad++.exe" "$Bin\NotepadPlusPlus\notepadplusplus.exe"
} }
catch { catch {
Write-Host (" ERROR: Failed to extract files." ) -foregroundcolor "Red" Write-Host (" ERROR: Failed to extract files." ) -ForegroundColor "Red"
} }
# ConEmu # ConEmu
Write-Host "Extracting: ConEmu" Write-Host "Extracting: ConEmu"
try { try {
start $sz -argumentlist @("x", "$tmp\ConEmuPack.7z", "-o$bin\ConEmu", "-aoa", "-bso0", "-bse0", "-bsp0") -nonewwindow -wait $ArgumentList = @(
Remove-Item "$tmp\ConEmuPack.7z" "x", "$Temp\ConEmuPack.7z", "-o$Bin\ConEmu",
"-aoa", "-bso0", "-bse0", "-bsp0")
Start-Process -FilePath $SevenZip -ArgumentList $ArgumentList -NoNewWindow -Wait
Remove-Item "$Temp\ConEmuPack.7z"
} }
catch { catch {
Write-Host (" ERROR: Failed to extract files." ) -foregroundcolor "Red" Write-Host (" ERROR: Failed to extract files." ) -ForegroundColor "Red"
} }
# Python x32 # Python
Write-Host "Extracting: Python (x32)" foreach ($Arch in @("32", "64")) {
Write-Host "Extracting: Python (x$Arch)"
$Files = @(
"python$Arch.zip",
"certifi.whl",
"chardet.whl",
"idna.whl",
"psutil$Arch.whl",
"requests.whl",
"urllib3.whl"
)
try { try {
foreach ($file in @("python32.zip", "certifi.whl", "chardet.whl", "idna.whl", "psutil32.whl", "requests.whl", "urllib3.whl")) { foreach ($File in $Files) {
start $sz -argumentlist @("x", "$tmp\$file", "-o$bin\Python\x32", "-aoa", "-bso0", "-bse0", "-bsp0") -nonewwindow -wait $ArgumentList = @(
"x", "$Temp\$File", "-o$Bin\Python\x$Arch",
"-aoa", "-bso0", "-bse0", "-bsp0")
Start-Process -FilePath $SevenZip -ArgumentList $ArgumentList -NoNewWindow -Wait
} }
} }
catch { catch {
Write-Host (" ERROR: Failed to extract files." ) -foregroundcolor "Red" Write-Host (" ERROR: Failed to extract files." ) -ForegroundColor "Red"
} }
# Python x64
Write-Host "Extracting: Python (x64)"
try {
foreach ($file in @("python64.zip", "certifi.whl", "chardet.whl", "idna.whl", "psutil64.whl", "requests.whl", "urllib3.whl")) {
start $sz -argumentlist @("x", "$tmp\$file", "-o$bin\Python\x64", "-aoa", "-bso0", "-bse0", "-bsp0") -nonewwindow -wait
}
Remove-Item "$tmp\python*.zip"
Remove-Item "$tmp\*.whl"
}
catch {
Write-Host (" ERROR: Failed to extract files." ) -foregroundcolor "Red"
} }
Remove-Item "$Temp\python*.zip"
Remove-Item "$Temp\*.whl"
## Configure ## ## Configure ##
Write-Host "Configuring kit" Write-Host "Configuring kit"
wk_pause "Press Enter to open settings..." WKPause "Press Enter to open settings..."
start "$bin\NotepadPlusPlus\notepadplusplus.exe" -argumentlist @("$bin\Scripts\settings\main.py") -wait $Cmd = "$Bin\NotepadPlusPlus\notepadplusplus.exe"
Start-Process -FilePath $Cmd -ArgumentList @("$Bin\Scripts\settings\main.py") -Wait
Start-Sleep 1 Start-Sleep 1
## Done ## ## Done ##
popd Pop-Location
if ($errors -gt 0) { $ArgumentList = @("-run", "$Bin\Python\x32\python.exe", "$Bin\Scripts\update_kit.py", "-new_console:n")
wk_pause "Press Enter to exit..." Start-Process -FilePath "$Bin\ConEmu\ConEmu.exe" -ArgumentList $ArgumentList -verb RunAs
} else {
start "$bin\ConEmu\ConEmu.exe" -argumentlist @("-run", "$bin\Python\x32\python.exe", "$bin\Scripts\update_kit.py", "-new_console:n") -verb Runas
} }