From 8870b2c875d244d425038bb9392080d5e31e2586 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Sat, 23 Mar 2024 10:55:20 +0100 Subject: [PATCH] Optimize Package Control installer scripts (#240) This commit ... 1. applies some code style changes to package control installer powershell scripts 2. adds some logging messages about progress 3. simplifies task killing (`pkill subl`, dropping `pkill plugin_host`) because the former also kills all sub-processes, which includes plugin_host. 4. Tweaks install_helper plugin to avoid concurrent tries to install dependencies. Package Control 4 automatically installs all missing libraries at startup. The install_helper, just needs to wait for completion before terminating ST again. This speeds setting ST & PC up on MacOS runners, significantly. --- scripts/install_package_control.ps1 | 51 +++++++------- scripts/install_package_control.sh | 21 +++--- scripts/install_package_control_helper.py | 81 ++++++++++++----------- 3 files changed, 78 insertions(+), 75 deletions(-) diff --git a/scripts/install_package_control.ps1 b/scripts/install_package_control.ps1 index c056e497..d3ee41df 100644 --- a/scripts/install_package_control.ps1 +++ b/scripts/install_package_control.ps1 @@ -13,16 +13,17 @@ $STPU = "C:\st\Data\Packages\User" New-Item -itemtype directory $STPU -force >$null $PC_PATH = "$STIP\Package Control.sublime-package" -if (-not (test-path $PC_PATH)) { +if (-not (Test-Path $PC_PATH)) { + Write-Verbose "Downloading Package Control.sublime-package" $PC_URL = "https://github.com/wbond/package_control/releases/latest/download/Package.Control.sublime-package" (New-Object System.Net.WebClient).DownloadFile($PC_URL, $PC_PATH) } $PC_SETTINGS = "C:\st\Data\Packages\User\Package Control.sublime-settings" -if (-not (test-path $PC_SETTINGS)) { - write-verbose "creating Package Control.sublime-settings" - "{`"auto_upgrade`": false, `"ignore_vcs_packages`": true, `"remove_orphaned`": false, `"submit_usage`": false }" | out-file -filepath $PC_SETTINGS -encoding utf8 +if (-not (Test-Path $PC_SETTINGS)) { + Write-Verbose "Creating Package Control.sublime-settings" + "{`"auto_upgrade`": false, `"ignore_vcs_packages`": true, `"remove_orphaned`": false, `"submit_usage`": false }" | Out-File -filepath $PC_SETTINGS -encoding utf8 } $PCH_PATH = "$STP\0_install_package_control_helper" @@ -32,35 +33,33 @@ $BASE = Split-Path -parent $PSCommandPath Copy-Item "$BASE\install_package_control_helper.py" "$PCH_PATH\install_package_control_helper.py" Copy-Item "$BASE\.python-version" "$PCH_PATH\.python-version" -for ($i=1; $i -le 3; $i++) { - if (test-path "$PCH_PATH\success") { - remove-item "$PCH_PATH\success" -Force - } +Write-Verbose "Starting Sublime Text." + +for ($i=1; ($i -le 3) -and -not (Test-Path "$PCH_PATH\failed") -and -not (Test-Path "$PCH_PATH\success"); $i++) { & "C:\st\sublime_text.exe" - $startTime = get-date - while ((-not (test-path "$PCH_PATH\success")) -and (((get-date) - $startTime).totalseconds -le 60)){ - write-host -nonewline "." - start-sleep -seconds 5 + $startTime = Get-Date + while ((-not (Test-Path "$PCH_PATH\failed")) -and (-not (Test-Path "$PCH_PATH\success")) -and (((Get-Date) - $startTime).totalseconds -le 60)) { + Write-Host -nonewline "." + Start-Sleep -seconds 5 } - stop-process -force -processname sublime_text -ea silentlycontinue - start-sleep -seconds 2 - if (test-path "$PCH_PATH\success") { - break - } + Stop-Process -force -processname sublime_text -ea silentlycontinue + Start-Sleep -seconds 4 } +Write-Host -if (-not (test-path "$PCH_PATH\success")) { - if (test-path "$PCH_PATH\log") { - get-content -Path "$PCH_PATH\log" - } - remove-item "$PCH_PATH" -Recurse -Force +Write-Verbose "Terminated Sublime Text." + +if (Test-Path "$PCH_PATH\log") { + Get-Content -Path "$PCH_PATH\log" +} + +if (-not (Test-Path "$PCH_PATH\success")) { + Remove-Item "$PCH_PATH" -Recurse -Force throw "Timeout: Fail to install Package Control." } -start-sleep -seconds 5 -remove-item "$PCH_PATH" -Recurse -Force -write-host +Remove-Item "$PCH_PATH" -Recurse -Force -write-verbose "Package Control installed." +Write-Verbose "Package Control installed." diff --git a/scripts/install_package_control.sh b/scripts/install_package_control.sh index c2432ecd..b4414827 100644 --- a/scripts/install_package_control.sh +++ b/scripts/install_package_control.sh @@ -43,12 +43,13 @@ fi PC_PATH="$STIP/Package Control.sublime-package" if [ ! -f "$PC_PATH" ]; then + echo Downloading Package Control.sublime-package PC_URL="https://github.com/wbond/package_control/releases/latest/download/Package.Control.sublime-package" curl -s -L "$PC_URL" -o "$PC_PATH" fi if [ ! -f "$STP/User/Package Control.sublime-settings" ]; then - echo creating Package Control.sublime-settings + echo Creating Package Control.sublime-settings [ ! -d "$STP/User" ] && mkdir -p "$STP/User" # make sure Pakcage Control does not complain echo '{"auto_upgrade": false, "ignore_vcs_packages": true, "remove_orphaned": false, "submit_usage": false }' > "$STP/User/Package Control.sublime-settings" @@ -63,23 +64,23 @@ if [ ! -d "$PCH_PATH" ]; then cp "$BASE/.python-version" "$PCH_PATH/.python-version" fi - # launch sublime text in background echo Starting Sublime Text -for i in {1..3}; do - rm -f "$PCH_PATH/success" +for i in {1..3}; do subl & ENDTIME=$(( $(date +%s) + 60 )) - while [ ! -f "$PCH_PATH/success" ] && [ $(date +%s) -lt $ENDTIME ] ; do + while [ ! -f "$PCH_PATH/failed" ] && [ ! -f "$PCH_PATH/success" ] && [ $(date +%s) -lt $ENDTIME ] ; do printf "." sleep 5 done - pkill "[Ss]ubl" || true - pkill 'plugin_host' || true - sleep 4 + sleep 2 + pkill subl || true + sleep 2 + + [ -f "$PCH_PATH/failed" ] && break [ -f "$PCH_PATH/success" ] && break done @@ -91,15 +92,11 @@ if [ -f "$PCH_PATH/log" ]; then fi if [ ! -f "$PCH_PATH/success" ]; then - if [ -f "$PCH_PATH/log" ]; then - cat "$PCH_PATH/log" - fi echo "Timeout: Fail to install Package Control." rm -rf "$PCH_PATH" exit 1 fi rm -rf "$PCH_PATH" -echo "" echo "Package Control installed." diff --git a/scripts/install_package_control_helper.py b/scripts/install_package_control_helper.py index bb681b3a..c19d0179 100644 --- a/scripts/install_package_control_helper.py +++ b/scripts/install_package_control_helper.py @@ -5,9 +5,9 @@ def plugin_loaded(): - logfile = os.path.join( - sublime.packages_path(), "0_install_package_control_helper", "log" - ) + package_path = os.path.join(sublime.packages_path(), "0_install_package_control_helper") + + logfile = os.path.join(package_path, "log") log = open(logfile, "a", encoding="utf-8") @@ -18,64 +18,71 @@ def kill_subl(restart=False): log.close() if sublime.platform() == "osx": - cmd = "sleep 1; pkill [Ss]ubl; pkill plugin_host; sleep 1; " + cmd = "pkill subl" if restart: - cmd = ( - cmd + "osascript -e 'tell application \"Sublime Text\" to activate'" - ) + cmd += "; sleep 1; subl'" elif sublime.platform() == "linux": - cmd = "sleep 1; pkill [Ss]ubl; pkill plugin_host; sleep 1; " + cmd = "pkill subl" if restart: - cmd = cmd + "subl" + cmd += "; sleep 1; subl" elif sublime.platform() == "windows": - cmd = "sleep 1 & taskkill /F /im sublime_text.exe & sleep 1 " + cmd = "taskkill /F /im sublime_text.exe" if restart: - cmd = cmd + '& "{}"'.format(sublime.executable_path()) + cmd += ' & sleep 1 & "{}"'.format(sublime.executable_path()) else: return subprocess.Popen(cmd, shell=True) def touch(file_name): - f = os.path.join( - sublime.packages_path(), "0_install_package_control_helper", file_name - ) - open(f, "a").close() + open(os.path.join(package_path, file_name), "a").close() - def satisfy_libraries(): + def check_package_control(): + """ + Wait for Package Control to be loaded. + """ if "Package Control" in sys.modules: - package_control = sys.modules["Package Control"].package_control + sublime.set_timeout(check_libraries, 2000) else: - sublime.set_timeout(satisfy_libraries, 5000) - return + sublime.set_timeout(check_package_control, 2000) - manager = package_control.package_manager.PackageManager() + num_retries = 0 - # query and install missing libraries - required_libraries = manager.find_required_libraries() - missing_libraries = manager.find_missing_libraries( - required_libraries=required_libraries - ) - if missing_libraries: - manager.install_libraries(required_libraries, fail_early=False) + def check_libraries(): + """ + Wait for Package Control to finish bootstrapping. - # re-query missing libraries - missing_libraries = manager.find_missing_libraries( - required_libraries=required_libraries - ) + PC4 automatically installs missing libraries at startup + just need to wait for it being completed. + """ + package_control = sys.modules["Package Control"].package_control + manager = package_control.package_manager.PackageManager() + missing_libraries = manager.find_missing_libraries() if missing_libraries: - log.write("missing dependencies:" + "\n") + nonlocal num_retries + + if num_retries < 3: + num_retries += 1 + sublime.set_timeout(check_libraries, 2000) + return + + log.write("missing libraries:" + "\n") log.write(" ".join(sorted(missing_libraries)) + "\n") + touch("failed") else: touch("success") - kill_subl() + kill_subl(restart=False) - # restart sublime when `sublime.error_message` is run def error_message(message): + """Restart sublime when `sublime.error_message()` is run.""" + log.write(message + "\n") + kill_subl(restart=True) + + def info_message(message): + """Print output from `sublime.message_dialog()` to logfile.""" log.write(message + "\n") - kill_subl(True) sublime.error_message = error_message - sublime.message_dialog = error_message - sublime.set_timeout(satisfy_libraries, 5000) + sublime.message_dialog = info_message + sublime.set_timeout(check_package_control, 2000)