Add initial winget support

This commit is contained in:
2Shirt 2023-06-25 02:21:26 -07:00
parent 55ce4d8ded
commit 9980dab27b
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 72 additions and 1 deletions

View file

@ -0,0 +1,37 @@
# WizardKit: Install winget (if needed)
#Requires -Version 3.0
if (Test-Path Env:\DEBUG) {
Set-PSDebug -Trace 1
}
$Host.UI.RawUI.WindowTitle = "WizardKit: Winget installer"
$Host.UI.RawUI.BackgroundColor = "black"
$Host.UI.RawUI.ForegroundColor = "white"
$ProgressPreference = "SilentlyContinue"
# STATIC VARIABLES
$EXIT_OK = 0
$EXIT_INSTALLED = 1
$EXIT_FAILED_TO_INSTALL = 2
# Main
$NeedsInstalled = $false
try {
$_ = $(winget --version)
}
catch {
$NeedsInstalled = $true
}
# Install
if (! $NeedsInstalled) {
exit $EXIT_INSTALLED
}
try {
Add-AppxPackage -ErrorAction Stop -RegisterByFamilyName -MainPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe
}
catch {
exit $EXIT_FAILED_TO_INSTALL
}
exit $EXIT_OK

View file

@ -26,7 +26,7 @@ from wk.cfg.windows_builds import (
OUTDATED_BUILD_NUMBERS,
WINDOWS_BUILDS,
)
from wk.exe import get_json_from_command, run_program
from wk.exe import get_json_from_command, run_program, wait_for_procs
from wk.kit.tools import find_kit_dir
from wk.std import (
GenericError,
@ -744,5 +744,39 @@ def stop_service(service_name) -> None:
raise GenericError(f'Failed to stop service {service_name}')
# Winget Functions
def winget_check(raise_exceptions: bool = False) -> None:
"""Check if winget is present, install if not."""
cmd = [
'powershell',
'-ExecutionPolicy', 'bypass',
'-File', find_kit_dir('Scripts').joinpath('install_winget.ps1'),
]
proc = run_program(cmd, check=False)
# Raise exception if requested
if raise_exceptions:
if proc.returncode == 1:
raise GenericWarning('Already installed')
if proc.returncode == 2:
raise GenericError('Failed to install')
def winget_upgrade() -> None:
"""Upgrade all supported programs with winget, returns subprocess.Popen."""
cmd = ['winget', 'upgrade', '--all']
# Adjust if running inside ConEmu
tmp_file = fr'{os.environ.get("TMP")}\run_winget.cmd'
if CONEMU:
with open(tmp_file, 'w', encoding='utf-8') as _f:
_f.write('@echo off\n')
_f.write(" ".join(cmd))
cmd = ('cmd', '/c', tmp_file, '-new_console:n', '-new_console:s33V')
run_program(cmd, check=False, pipe=False)
sleep(1)
wait_for_procs('winget.exe')
if __name__ == '__main__':
print("This file is not meant to be called directly.")