Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Resolve circular dependencies #181

Merged
merged 2 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ref: Resolve circular dependencies
  • Loading branch information
Ash258 committed Jul 17, 2021
commit 3aea548b4baa96ebe044b1be39f3e9345f4b6013
60 changes: 60 additions & 0 deletions lib/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions lib/Update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
91 changes: 0 additions & 91 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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})$') {
Expand Down