Skip to content

Commit ec04dd0

Browse files
yi-Xu-0100niheavenrashil2000
authored
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 <niheaven@gmail.com> * Update libexec/scoop-hold.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-unhold.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * 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 <niheaven@gmail.com> * 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 <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * Update libexec/scoop-update.ps1 Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com> * 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 <niheaven@gmail.com> Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com>
1 parent 4a31bd3 commit ec04dd0

6 files changed

Lines changed: 55 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/v0.2.4...develop)
2+
3+
### Features
4+
5+
- **scoop-(un)hold:** Support `scoop (un)hold scoop` ([#5089](https://github.com/ScoopInstaller/Scoop/issues/5089))
6+
17
## [v0.2.4](https://github.com/ScoopInstaller/Scoop/compare/v0.2.3...v0.2.4) - 2022-08-08
28

39
### Features

bin/install.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Remove-Item "$dir\_tmp", $zipfile -Recurse -Force
7070

7171
ensure_robocopy_in_path
7272

73-
scoop config lastupdate ([System.DateTime]::Now.ToString('o'))
73+
set_config lastUpdate ([System.DateTime]::Now.ToString('o')) | Out-Null
7474
success 'Scoop was installed successfully!'
7575

7676
Write-Output "Type 'scoop help' for instructions."

lib/core.ps1

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -996,29 +996,16 @@ function show_app($app, $bucket, $version) {
996996
return $app
997997
}
998998

999-
function last_scoop_update() {
1000-
# PowerShell 6 returns an DateTime Object
1001-
$last_update = (get_config lastupdate)
1002-
1003-
if ($null -ne $last_update -and $last_update.GetType() -eq [System.String]) {
1004-
try {
1005-
$last_update = [System.DateTime]::Parse($last_update)
1006-
} catch {
1007-
$last_update = $null
1008-
}
1009-
}
1010-
return $last_update
1011-
}
1012-
1013999
function is_scoop_outdated() {
1014-
$last_update = $(last_scoop_update)
10151000
$now = [System.DateTime]::Now
1016-
if($null -eq $last_update) {
1017-
set_config lastupdate $now.ToString('o')
1018-
# enforce an update for the first time
1001+
try {
1002+
$expireHour = (New-TimeSpan (get_config lastUpdate) $now).TotalHours
1003+
return ($expireHour -ge 3)
1004+
} catch {
1005+
# If not System.DateTime
1006+
set_config lastUpdate ($now.ToString('o')) | Out-Null
10191007
return $true
10201008
}
1021-
return $last_update.AddHours(3) -lt $now.ToLocalTime()
10221009
}
10231010

10241011
function substitute($entity, [Hashtable] $params, [Bool]$regexEscape = $false) {

libexec/scoop-hold.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ if ($global -and !(is_admin)) {
3232
$apps | ForEach-Object {
3333
$app = $_
3434

35+
if ($app -eq 'scoop') {
36+
$update_until = [System.DateTime]::Now.AddDays(1)
37+
set_config 'update_until' $update_until.ToString('o') | Out-Null
38+
success "$app is now held and might not be updated until $($update_until.ToLocalTime())."
39+
return
40+
}
3541
if (!(installed $app $global)) {
3642
if ($global) {
3743
error "'$app' is not installed globally."

libexec/scoop-unhold.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ if ($global -and !(is_admin)) {
3232
$apps | ForEach-Object {
3333
$app = $_
3434

35+
if ($app -eq 'scoop') {
36+
set_config 'update_until' $null | Out-Null
37+
success "$app is no longer held and can be updated again."
38+
return
39+
}
3540
if (!(installed $app $global)) {
3641
if ($global) {
3742
error "'$app' is not installed globally."

libexec/scoop-update.ps1

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,28 @@ if(($PSVersionTable.PSVersion.Major) -lt 5) {
5454
Write-Output "Upgrade PowerShell: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows"
5555
break
5656
}
57+
$show_update_log = get_config 'show_update_log' $true
5758

58-
function update_scoop() {
59+
function update_scoop($show_update_log) {
5960
# check for git
60-
if(!(Test-CommandAvailable git)) { abort "Scoop uses Git to update itself. Run 'scoop install git' and try again." }
61+
if (!(Test-CommandAvailable git)) { abort "Scoop uses Git to update itself. Run 'scoop install git' and try again." }
62+
63+
try {
64+
$now = [System.DateTime]::Now.ToString('o')
65+
$update_until = [System.DateTime]::Parse((get_config update_until $now))
66+
if ((New-TimeSpan $update_until $now).TotalSeconds -lt 0) {
67+
warn "Skipping self-update until $($update_until.ToLocalTime())..."
68+
warn "If you want to update Scoop itself immediately, use 'scoop unhold scoop; scoop update'."
69+
return
70+
}
71+
} catch {
72+
warn "'update_until' has been set in the wrong format."
73+
warn "If you want to disable Scoop self-update for a moment, use 'scoop hold scoop'."
74+
}
75+
76+
set_config update_until $null | Out-Null
6177

6278
Write-Host "Updating Scoop..."
63-
$last_update = $(last_scoop_update)
64-
if ($null -eq $last_update) {$last_update = [System.DateTime]::Now}
65-
$last_update = $last_update.ToString('s')
66-
$show_update_log = get_config 'show_update_log' $true
6779
$currentdir = fullpath $(versiondir 'scoop' 'current')
6880
if (!(Test-Path "$currentdir\.git")) {
6981
$newdir = "$currentdir\..\new"
@@ -135,6 +147,11 @@ function update_scoop() {
135147
# }
136148

137149
shim "$currentdir\bin\scoop.ps1" $false
150+
}
151+
152+
function update_bucket($show_update_log) {
153+
# check for git
154+
if (!(Test-CommandAvailable git)) { abort "Scoop uses Git to update main bucket and others. Run 'scoop install git' and try again." }
138155

139156
foreach ($bucket in Get-LocalBucket) {
140157
Write-Host "Updating '$bucket' bucket..."
@@ -165,9 +182,6 @@ function update_scoop() {
165182
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"
166183
}
167184
}
168-
169-
set_config lastupdate ([System.DateTime]::Now.ToString('o')) | Out-Null
170-
success 'Scoop was updated successfully!'
171185
}
172186

173187
function update($app, $global, $quiet = $false, $independent, $suggested, $use_cache = $true, $check_hash = $true) {
@@ -304,7 +318,10 @@ if (-not ($apps -or $all)) {
304318
error 'scoop update: --no-cache is invalid when <app> is not specified.'
305319
exit 1
306320
}
307-
update_scoop
321+
update_scoop $show_update_log
322+
update_bucket $show_update_log
323+
set_config lastUpdate ([System.DateTime]::Now.ToString('o')) | Out-Null
324+
success 'Scoop was updated successfully!'
308325
} else {
309326
if ($global -and !(is_admin)) {
310327
'ERROR: You need admin rights to update global apps.'; exit 1
@@ -316,7 +333,10 @@ if (-not ($apps -or $all)) {
316333
$apps_param = $apps
317334

318335
if ($updateScoop) {
319-
update_scoop
336+
update_scoop $show_update_log
337+
update_bucket $show_update_log
338+
set_config lastUpdate ([System.DateTime]::Now.ToString('o')) | Out-Null
339+
success 'Scoop was updated successfully!'
320340
}
321341

322342
if ($apps_param -eq '*' -or $all) {

0 commit comments

Comments
 (0)