From 3aea548b4baa96ebe044b1be39f3e9345f4b6013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8C=C3=A1bera?= Date: Sat, 17 Jul 2021 21:34:31 +0200 Subject: [PATCH 1/2] ref: Resolve circular dependencies --- lib/Helpers.ps1 | 60 ++++++++++++++++++++++++++++++++ lib/Update.ps1 | 31 +++++++++++++++++ lib/core.ps1 | 91 ------------------------------------------------- 3 files changed, 91 insertions(+), 91 deletions(-) diff --git a/lib/Helpers.ps1 b/lib/Helpers.ps1 index e9c7a91780..81207bd3d4 100644 --- a/lib/Helpers.ps1 +++ b/lib/Helpers.ps1 @@ -202,6 +202,66 @@ function Out-UTF8Content { process { Out-UTF8File -File $File -Content $Content } } +function Invoke-VariableSubstitution { + <# + .SYNOPSIS + Substitute (find and replace) provided parameters in provided entity. + .PARAMETER Entity + Specifies the entity to be substituted (searched in). + .PARAMETER Substitutes + Specifies the hashtable providing name and value pairs for "find and replace". + Hashtable keys should start with $ (dollar sign). Curly bracket variable syntax will be substituted automatically. + .PARAMETER EscapeRegularExpression + Specifies to escape regular expressions before replacing values. + #> + [CmdletBinding()] + param( + [AllowEmptyCollection()] + [AllowNull()] + $Entity, + [Parameter(Mandatory)] + [Alias('Parameters')] + [HashTable] $Substitutes, + [Switch] $EscapeRegularExpression + ) + + process { + $EscapeRegularExpression | Out-Null # PowerShell/PSScriptAnalyzer#1472 + $newEntity = $Entity + + if ($null -ne $newEntity) { + switch ($newEntity.GetType().Name) { + 'String' { + $Substitutes.GetEnumerator() | Sort-Object { $_.Name.Length } -Descending | ForEach-Object { + $value = if (($EscapeRegularExpression -eq $false) -or ($null -eq $_.Value)) { $_.Value } else { [Regex]::Escape($_.Value) } + $curly = '${' + $_.Name.TrimStart('$') + '}' + + $newEntity = $newEntity.Replace($curly, $value) + $newEntity = $newEntity.Replace($_.Name, $value) + } + } + 'Object[]' { + $newEntity = $newEntity | ForEach-Object { Invoke-VariableSubstitution -Entity $_ -Substitutes $Substitutes -EscapeRegularExpression:$regexEscape } + } + 'PSCustomObject' { + $newentity.PSObject.Properties | ForEach-Object { $_.Value = Invoke-VariableSubstitution -Entity $_ -Substitutes $Substitutes -EscapeRegularExpression:$regexEscape } + } + default { + # This is not needed, but to cover all possible use cases explicitly + $newEntity = $newEntity + } + } + } + + return $newEntity + } +} + +# TODO: Deprecate +function substitute($entity, [Hashtable] $params, [Bool]$regexEscape = $false) { + return Invoke-VariableSubstitution -Entity $entity -Substitutes $params -EscapeRegularExpression:$regexEscape +} + function Get-MagicByte { <# .SYNOPSIS diff --git a/lib/Update.ps1 b/lib/Update.ps1 index eb76c44378..043fcf4974 100644 --- a/lib/Update.ps1 +++ b/lib/Update.ps1 @@ -120,6 +120,37 @@ function Update-ScoopLocalBucket { } } +function last_scoop_update() { + # TODO: Config refactor + $lastUpdate = Invoke-ScoopCommand 'config' @('lastupdate') + + if ($null -ne $lastUpdate) { + try { + $lastUpdate = Get-Date ($lastUpdate.Substring(4)) + } catch { + Write-UserMessage -Message 'Config: Incorrect update date format' -Info + $lastUpdate = $null + } + } + + return $lastUpdate +} + +function is_scoop_outdated() { + $lastUp = last_scoop_update + $now = Get-Date + $res = $true + + if ($null -eq $lastUp) { + # TODO: Config refactor + Invoke-ScoopCommand 'config' @('lastupdate', ($now.ToString($UPDATE_DATE_FORMAT))) | Out-Null + } else { + $res = $lastUp.AddHours(3) -lt $now.ToLocalTime() + } + + return $res +} + function Update-Scoop { <# .SYNOPSIS diff --git a/lib/core.ps1 b/lib/core.ps1 index 2140799c2c..22d04100d9 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -928,97 +928,6 @@ function show_app($app, $bucket, $version) { return $app } -function last_scoop_update() { - # TODO: Config refactor - $lastUpdate = Invoke-ScoopCommand 'config' @('lastupdate') - - if ($null -ne $lastUpdate) { - try { - $lastUpdate = Get-Date ($lastUpdate.Substring(4)) - } catch { - Write-UserMessage -Message 'Config: Incorrect update date format' -Info - $lastUpdate = $null - } - } - - return $lastUpdate -} - -function is_scoop_outdated() { - $lastUp = last_scoop_update - $now = Get-Date - $res = $true - - if ($null -eq $lastUp) { - # TODO: Config refactor - Invoke-ScoopCommand 'config' @('lastupdate', ($now.ToString($UPDATE_DATE_FORMAT))) | Out-Null - } else { - $res = $lastUp.AddHours(3) -lt $now.ToLocalTime() - } - - return $res -} - -function Invoke-VariableSubstitution { - <# - .SYNOPSIS - Substitute (find and replace) provided parameters in provided entity. - .PARAMETER Entity - Specifies the entity to be substituted (searched in). - .PARAMETER Substitutes - Specifies the hashtable providing name and value pairs for "find and replace". - Hashtable keys should start with $ (dollar sign). Curly bracket variable syntax will be substituted automatically. - .PARAMETER EscapeRegularExpression - Specifies to escape regular expressions before replacing values. - #> - [CmdletBinding()] - param( - [AllowEmptyCollection()] - [AllowNull()] - $Entity, - [Parameter(Mandatory)] - [Alias('Parameters')] - [HashTable] $Substitutes, - [Switch] $EscapeRegularExpression - ) - - process { - $EscapeRegularExpression | Out-Null # PowerShell/PSScriptAnalyzer#1472 - $newEntity = $Entity - - if ($null -ne $newEntity) { - switch ($newEntity.GetType().Name) { - 'String' { - $Substitutes.GetEnumerator() | Sort-Object { $_.Name.Length } -Descending | ForEach-Object { - $value = if (($EscapeRegularExpression -eq $false) -or ($null -eq $_.Value)) { $_.Value } else { [Regex]::Escape($_.Value) } - $curly = '${' + $_.Name.TrimStart('$') + '}' - - $newEntity = $newEntity.Replace($curly, $value) - $newEntity = $newEntity.Replace($_.Name, $value) - } - } - 'Object[]' { - $newEntity = $newEntity | ForEach-Object { Invoke-VariableSubstitution -Entity $_ -Substitutes $Substitutes -EscapeRegularExpression:$regexEscape } - } - 'PSCustomObject' { - $newentity.PSObject.Properties | ForEach-Object { $_.Value = Invoke-VariableSubstitution -Entity $_ -Substitutes $Substitutes -EscapeRegularExpression:$regexEscape } - } - default { - # This is not needed, but to cover all possible use cases explicitly - $newEntity = $newEntity - } - } - } - - return $newEntity - } -} - -# TODO: Deprecate -function substitute($entity, [Hashtable] $params, [Bool]$regexEscape = $false) { - return Invoke-VariableSubstitution -Entity $entity -Substitutes $params -EscapeRegularExpression:$regexEscape -} - function format_hash([String] $hash) { # Convert base64 encoded hash values if ($hash -match '^(?:[A-Za-z\d+\/]{4})*(?:[A-Za-z\d+\/]{2}==|[A-Za-z\d+\/]{3}=|[A-Za-z\d+\/]{4})$') { From 26009a586df9ac5cf59bb7c452e17d8234996981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8C=C3=A1bera?= Date: Sat, 17 Jul 2021 21:47:30 +0200 Subject: [PATCH 2/2] required import --- lib/Update.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Update.ps1 b/lib/Update.ps1 index 043fcf4974..1384a2bb96 100644 --- a/lib/Update.ps1 +++ b/lib/Update.ps1 @@ -1,4 +1,4 @@ -'core', 'Git', 'Helpers', 'buckets', 'install', 'manifest' | ForEach-Object { +'core', 'Git', 'Helpers', 'buckets', 'install', 'manifest', 'commands' | ForEach-Object { . (Join-Path $PSScriptRoot "$_.ps1") }