Skip to content

Commit

Permalink
refactor: Application information file (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 committed Nov 15, 2020
2 parents 2bbe4c8 + 89c81f8 commit 5bef92b
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 21 deletions.
159 changes: 159 additions & 0 deletions lib/Applications.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
'core', 'json', 'Helpers', 'manifest', 'Versions' | ForEach-Object {
. (Join-Path $PSScriptRoot "$_.ps1")
}

#region Application instalaltion info file
function Get-InstalledApplicationInformation {
<#
.SYNOPSIS
Get stored information about installed application.
.PARAMETER AppName
Specifies the application name.
.PARAMETER Version
Specifies the version of the application.
Use 'CURRENT_' to lookup for the currently used version. (Respecting NO_JUNCTION and different version)
.PARAMETER Global
Specifies globally installed application.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String] $AppName,
[String] $Version = 'CURRENT_',
[Switch] $Global
)

process {
if ($Version -ceq 'CURRENT_') { $Version = Select-CurrentVersion -AppName $AppName -Global:$Global }
$applicationDirectory = versiondir $AppName $Version $Global
$installInfoPath = Join-Path $applicationDirectory 'scoop-install.json'

if (!(Test-Path $installInfoPath)) {
$old = Join-Path $applicationDirectory 'install.json'
# Migrate from old scoop's 'install.json'
if (Test-Path $old) {
Write-UserMessage -Message 'Migrating ''install.json'' to ''scoop-install.json''' -Info
Rename-Item $old 'scoop-install.json'
} else {
return $null
}
}

return parse_json $installInfoPath
}
}

function Get-InstalledApplicationInformationPropertyValue {
<#
.SYNOPSIS
Get specific property stored in application's information file.
.PARAMETER AppName
Specifies the application name.
.PARAMETER Version
Specifies the version of the application.
Use 'CURRENT_' to lookup for the currently used version. (Respecting NO_JUNCTION and different version)
.PARAMETER Global
Specifies globally installed application.
.PARAMETER Property
Specifies the property name to be evaluated.
.PARAMETER InputObject
Specifies the installation information object to be fed instead of loading it from file.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String] $AppName,
[String] $Version = 'CURRENT_',
[Switch] $Global,
[Parameter(Mandatory)]
[String] $Property,
[Object] $InputObject
)

process {
$info = if ($InputObject) { $InputObject } else { Get-InstalledApplicationInformation -AppName $AppName -Version $Version -Global:$Global }
$prop = $null

if ($info) {
$properties = @($info | Get-Member -MemberType 'NoteProperty' | Select-Object -ExpandProperty 'Name')
if ($properties -and $properties.Contains($Property)) { $prop = $info.$Property }
}

return $prop
}
}

function Set-InstalledApplicationInformationPropertyValue {
<#
.SYNOPSIS
Set specific property to be preserved in application's information file.
.PARAMETER AppName
Specifies the application name.
.PARAMETER Version
Specifies the version of application.
Use 'CURRENT_' to lookup for the currently used version. (Respecting NO_JUNCTION and different version)
.PARAMETER Global
Specifies globally installed application.
.PARAMETER Property
Specifies the property name to be saved.
.PARAMETER Value
Specifies the value to be saved.
.PARAMETER Force
Specifies to override the value saved in the file.
.PARAMETER InputObject
Specifies to feed installation information object instead of loading it all the time from file.
.PARAMETER PassThru
Specifies to return the new object from the function.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String] $AppName,
[String] $Version = 'CURRENT_',
[Switch] $Global,
[Parameter(Mandatory)]
[String[]] $Property,
[Parameter(Mandatory)]
[Object[]] $Value,
[Switch] $Force,
[Object] $InputObject,
[Switch] $PassThru
)

begin {
if ($Version -ceq 'CURRENT_') { $Version = Select-CurrentVersion -AppName $AppName -Global:$Global }
$info = if ($InputObject) { $InputObject } else { Get-InstalledApplicationInformation -AppName $AppName -Version $Version -Global:$Global }
if (!$info) { $info = @{ } }
if ($Property.Count -ne $Value.Count) {
throw [ScoopException] 'Property and value mismatch'
}
}

process {
for ($i = 0; $i -lt $Property.Count; ++$i) {
$prop = $Property[$i]
$val = $Value[$i]
$properties = @($info | Get-Member -MemberType 'NoteProperty' | Select-Object -ExpandProperty 'Name')

if ($properties -and $properties.Contains($prop)) {
if ($Force) {
$info.$prop = $val
} else {
throw [ScoopException] "Property '$prop' is already set"
}
} else {
$info | Add-Member -MemberType 'NoteProperty' -Name $prop -Value $val
}
}
}

end {
$appDirectory = versiondir $AppName $Version $Global
# TODO: Trim nulls
# TODO: Out-InstalledApplicationInfoFile
$info | ConvertToPrettyJson | Out-UTF8File -Path (Join-Path $appDirectory 'scoop-install.json')

if ($PassThru) { return $info }
}
}
#endregion Application instalaltion info file
2 changes: 1 addition & 1 deletion lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ function ensure {
param([Parameter(Mandatory, ValueFromPipeline)] [Alias('Dir', 'Path', 'LiteralPath')] $Directory)

process {
if (!(Test-Path $Directory)) { New-Item $Directory -ItemType Directory -Force | Out-Null }
if (!(Test-Path $Directory)) { New-Item $Directory -ItemType Directory | Out-Null }

return Resolve-Path $Directory
}
Expand Down
1 change: 1 addition & 0 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function save_install_info($info, $dir) {
$info | ConvertToPrettyJson | Out-UTF8File -Path (Join-Path $dir 'scoop-install.json')
}

# TODO: Deprecate
function install_info($app, $version, $global) {
$d = versiondir $app $version $global
$path = Join-Path $d 'scoop-install.json'
Expand Down
20 changes: 10 additions & 10 deletions libexec/scoop-hold.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# -h, --help Show help for this command.
# -g, --global Hold globally installed app.

'getopt', 'help', 'Helpers', 'manifest' | ForEach-Object {
'core', 'getopt', 'Helpers', 'Applications' | ForEach-Object {
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
}

Expand Down Expand Up @@ -35,18 +35,18 @@ foreach ($app in $apps) {
continue
}

# TODO: Respect NO_JUNCTION
$dir = versiondir $app 'current' $global
$json = install_info $app 'current' $global
if ($json.hold -and ($json.hold -eq $true)) {
$splat = @{ 'AppName' = $app; 'Global' = $global }
$info = Get-InstalledApplicationInformation @splat
$splat.Add('Property', 'hold')
$splat.Add('InputObject', $info)
$current = Get-InstalledApplicationInformationPropertyValue @splat

if (($null -ne $current) -and ($current -eq $true)) {
Write-UserMessage -Message "'$app' is already held" -Warning
continue
}
$install = @{ }
# TODO: Add-member instead of duplicating of whole object
$json | Get-Member -MemberType Properties | ForEach-Object { $install.Add($_.Name, $json.($_.Name)) }
$install.hold = $true
save_install_info $install $dir

Set-InstalledApplicationInformationPropertyValue @splat -Value $true -Force
Write-UserMessage -Message "$app is now held and cannot be updated anymore." -Success
}

Expand Down
20 changes: 10 additions & 10 deletions libexec/scoop-unhold.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# -h, --help Show help for this command.
# -g, --global Unhold globally installed app.

'getopt', 'help', 'Helpers', 'manifest' | ForEach-Object {
'core', 'getopt', 'Helpers', 'Applications' | ForEach-Object {
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
}

Expand Down Expand Up @@ -36,18 +36,18 @@ foreach ($app in $apps) {
continue
}

# TODO: Respect NO_JUNCTION
$dir = versiondir $app 'current' $global
$json = install_info $app 'current' $global
if (!$json.hold -or ($json.hold -eq $false)) {
$splat = @{ 'AppName' = $app; 'Global' = $global }
$info = Get-InstalledApplicationInformation @splat
$splat.Add('Property', 'hold')
$splat.Add('InputObject', $info)
$current = Get-InstalledApplicationInformationPropertyValue @splat

if (($null -eq $current) -or ($current -eq $false)) {
Write-UserMessage -Message "'$app' is not held" -Warning
continue
}
# TODO: Remove member instead of duplicating object
$install = @{ }
$json | Get-Member -MemberType Properties | ForEach-Object { $install.Add($_.Name, $json.($_.Name)) }
$install.hold = $null
save_install_info $install $dir

Set-InstalledApplicationInformationPropertyValue @splat -Value $false -Force
Write-UserMessage -Message "$app is no longer held and can be updated again." -Success
}

Expand Down

0 comments on commit 5bef92b

Please sign in to comment.