From ec04dd07bcdeab20dd3e9fa391612e4650161f0f Mon Sep 17 00:00:00 2001 From: yi_Xu Date: Thu, 11 Aug 2022 17:20:40 +0800 Subject: [PATCH] feat(scoop-(un)hold): Support `scoop (un)hold scoop` (#5089) * feat(scoop-config): Add new configuration of `SCOOP_HOLD` Allow to disable Scoop itself updates. This configuration have the same function with 'scoop (un)hold scoop'. * perf(scoop-update): Separate `update_bucket` from `update_scoop` * perf(scoop-(un)hold): remove big overarching if-statement * perf(scoop-config): use `SCOOP_HOLD_DAYS` instead of `SCOOP_HOLD` * perf(scoop-config): Update forward Compatible code * Update libexec/scoop-config.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-hold.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-unhold.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * perf(scoop-update): Update last_scoop_update * docs(changelog): update changelog to add feature * fix(changelog): fix changelog typo * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-config.ps1 Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> * Update libexec/scoop-update.ps1 Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> * refactor: Use `lastUpdate` instead of `lastupdate` Consistent with scoop-export. * fix(install): make config lastUpdate silent * refactor(scoop-update): Remove `SCOOP_HOLD` * fix: update changelog remove none used code. * Update lib/core.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung * perf(scoop-update): Handle the judgment in try-catch * fix(scoop-update): Remove 'update_until' when update scoop itself * Update lib/core.ps1 Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> * docs(CHANGELOG): Update changelog Co-authored-by: Hsiao-nan Cheung Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> --- CHANGELOG.md | 6 ++++++ bin/install.ps1 | 2 +- lib/core.ps1 | 25 ++++++------------------ libexec/scoop-hold.ps1 | 6 ++++++ libexec/scoop-unhold.ps1 | 5 +++++ libexec/scoop-update.ps1 | 42 +++++++++++++++++++++++++++++----------- 6 files changed, 55 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1e0bb8791..b39837ac8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/v0.2.4...develop) + +### Features + +- **scoop-(un)hold:** Support `scoop (un)hold scoop` ([#5089](https://github.com/ScoopInstaller/Scoop/issues/5089)) + ## [v0.2.4](https://github.com/ScoopInstaller/Scoop/compare/v0.2.3...v0.2.4) - 2022-08-08 ### Features diff --git a/bin/install.ps1 b/bin/install.ps1 index 68dcf6671c..46cc0c21cc 100644 --- a/bin/install.ps1 +++ b/bin/install.ps1 @@ -70,7 +70,7 @@ Remove-Item "$dir\_tmp", $zipfile -Recurse -Force ensure_robocopy_in_path -scoop config lastupdate ([System.DateTime]::Now.ToString('o')) +set_config lastUpdate ([System.DateTime]::Now.ToString('o')) | Out-Null success 'Scoop was installed successfully!' Write-Output "Type 'scoop help' for instructions." diff --git a/lib/core.ps1 b/lib/core.ps1 index 94f555587d..d354bc77cf 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -996,29 +996,16 @@ function show_app($app, $bucket, $version) { return $app } -function last_scoop_update() { - # PowerShell 6 returns an DateTime Object - $last_update = (get_config lastupdate) - - if ($null -ne $last_update -and $last_update.GetType() -eq [System.String]) { - try { - $last_update = [System.DateTime]::Parse($last_update) - } catch { - $last_update = $null - } - } - return $last_update -} - function is_scoop_outdated() { - $last_update = $(last_scoop_update) $now = [System.DateTime]::Now - if($null -eq $last_update) { - set_config lastupdate $now.ToString('o') - # enforce an update for the first time + try { + $expireHour = (New-TimeSpan (get_config lastUpdate) $now).TotalHours + return ($expireHour -ge 3) + } catch { + # If not System.DateTime + set_config lastUpdate ($now.ToString('o')) | Out-Null return $true } - return $last_update.AddHours(3) -lt $now.ToLocalTime() } function substitute($entity, [Hashtable] $params, [Bool]$regexEscape = $false) { diff --git a/libexec/scoop-hold.ps1 b/libexec/scoop-hold.ps1 index ec4f8ce070..648317c839 100644 --- a/libexec/scoop-hold.ps1 +++ b/libexec/scoop-hold.ps1 @@ -32,6 +32,12 @@ if ($global -and !(is_admin)) { $apps | ForEach-Object { $app = $_ + if ($app -eq 'scoop') { + $update_until = [System.DateTime]::Now.AddDays(1) + set_config 'update_until' $update_until.ToString('o') | Out-Null + success "$app is now held and might not be updated until $($update_until.ToLocalTime())." + return + } if (!(installed $app $global)) { if ($global) { error "'$app' is not installed globally." diff --git a/libexec/scoop-unhold.ps1 b/libexec/scoop-unhold.ps1 index 2aa93d2df8..e4009e0109 100644 --- a/libexec/scoop-unhold.ps1 +++ b/libexec/scoop-unhold.ps1 @@ -32,6 +32,11 @@ if ($global -and !(is_admin)) { $apps | ForEach-Object { $app = $_ + if ($app -eq 'scoop') { + set_config 'update_until' $null | Out-Null + success "$app is no longer held and can be updated again." + return + } if (!(installed $app $global)) { if ($global) { error "'$app' is not installed globally." diff --git a/libexec/scoop-update.ps1 b/libexec/scoop-update.ps1 index ca502dd7ce..f418fe9b7d 100644 --- a/libexec/scoop-update.ps1 +++ b/libexec/scoop-update.ps1 @@ -54,16 +54,28 @@ if(($PSVersionTable.PSVersion.Major) -lt 5) { Write-Output "Upgrade PowerShell: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows" break } +$show_update_log = get_config 'show_update_log' $true -function update_scoop() { +function update_scoop($show_update_log) { # check for git - if(!(Test-CommandAvailable git)) { abort "Scoop uses Git to update itself. Run 'scoop install git' and try again." } + if (!(Test-CommandAvailable git)) { abort "Scoop uses Git to update itself. Run 'scoop install git' and try again." } + + try { + $now = [System.DateTime]::Now.ToString('o') + $update_until = [System.DateTime]::Parse((get_config update_until $now)) + if ((New-TimeSpan $update_until $now).TotalSeconds -lt 0) { + warn "Skipping self-update until $($update_until.ToLocalTime())..." + warn "If you want to update Scoop itself immediately, use 'scoop unhold scoop; scoop update'." + return + } + } catch { + warn "'update_until' has been set in the wrong format." + warn "If you want to disable Scoop self-update for a moment, use 'scoop hold scoop'." + } + + set_config update_until $null | Out-Null Write-Host "Updating Scoop..." - $last_update = $(last_scoop_update) - if ($null -eq $last_update) {$last_update = [System.DateTime]::Now} - $last_update = $last_update.ToString('s') - $show_update_log = get_config 'show_update_log' $true $currentdir = fullpath $(versiondir 'scoop' 'current') if (!(Test-Path "$currentdir\.git")) { $newdir = "$currentdir\..\new" @@ -135,6 +147,11 @@ function update_scoop() { # } shim "$currentdir\bin\scoop.ps1" $false +} + +function update_bucket($show_update_log) { + # check for git + if (!(Test-CommandAvailable git)) { abort "Scoop uses Git to update main bucket and others. Run 'scoop install git' and try again." } foreach ($bucket in Get-LocalBucket) { Write-Host "Updating '$bucket' bucket..." @@ -165,9 +182,6 @@ function update_scoop() { git -C "$bucketLoc" --no-pager log --no-decorate --grep='^(chore)' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' "$previousCommit..HEAD" } } - - set_config lastupdate ([System.DateTime]::Now.ToString('o')) | Out-Null - success 'Scoop was updated successfully!' } function update($app, $global, $quiet = $false, $independent, $suggested, $use_cache = $true, $check_hash = $true) { @@ -304,7 +318,10 @@ if (-not ($apps -or $all)) { error 'scoop update: --no-cache is invalid when is not specified.' exit 1 } - update_scoop + update_scoop $show_update_log + update_bucket $show_update_log + set_config lastUpdate ([System.DateTime]::Now.ToString('o')) | Out-Null + success 'Scoop was updated successfully!' } else { if ($global -and !(is_admin)) { 'ERROR: You need admin rights to update global apps.'; exit 1 @@ -316,7 +333,10 @@ if (-not ($apps -or $all)) { $apps_param = $apps if ($updateScoop) { - update_scoop + update_scoop $show_update_log + update_bucket $show_update_log + set_config lastUpdate ([System.DateTime]::Now.ToString('o')) | Out-Null + success 'Scoop was updated successfully!' } if ($apps_param -eq '*' -or $all) {