Skip to content

Commit

Permalink
feat: Add universal manifest helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 committed Aug 21, 2020
1 parent 15b52ec commit 816fa1d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
105 changes: 105 additions & 0 deletions lib/ManifestHelpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'Helpers' | ForEach-Object {
. (Join-Path $PSScriptRoot "$_.ps1")
}

#region Persistence
function Test-Persistence {
<#
.SYNOPSIS
Persistence check helper for files.
.DESCRIPTION
This will save some lines to not always write `if (!(Test-Path "$persist_dir\$file")) { New-item "$dir\$file" | Out-Null }` inside manifests.
variables `$currentFile`, `$filePersistPath`, `$fileDirPath` are exposed and could be used inside `Execution` block.
.PARAMETER File
Specifies the file to be checked.
Do not prefix with $dir. All files are already checked against $dir and $persist_dir.
.PARAMETER Content
Specifies the content/value of the created file. Value should be array of strings or string.
.PARAMETER Execution
Specifies custom scriptblock to run when file is not persisted.
https://github.com/lukesampson/scoop-extras/blob/a84b257fd9636d02295b48c3fd32826487ca9bd3/bucket/ditto.json#L25-L33
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, Position, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[String[]] $File,
[Parameter(ValueFromPipelineByPropertyName)]
[Object[]] $Content,
[Parameter(ValueFromPipelineByPropertyName)]
[ScriptBlock] $Execution
)

process {
for ($ind = 0; $ind -lt $File.Count; ++$ind) {
$currentFile = $File[$ind]
$filePersistPath = Join-Path $persist_dir $currentFile
$fileDirPath = Join-Path $dir $currentFile

if (!(Test-Path -LiteralPath $filePersistPath -PathType Leaf)) {
if ($Execution) {
& $Execution
} else {
# Handle edge case when there is only one file and multiple contents caused by
# If `Test-Persistence alfa.txt @('new', 'beta')` is used,
# Powershell will bind Content as simple array with 2 values instead of Array with nested array with 2 values.
if (($File.Count -eq 1) -and ($Content.Count -gt 1)) {
$cont = $Content
} elseif ($ind -lt $Content.Count) {
$cont = $Content[$ind]
} else {
$cont = $null
}

# File needs to be precreated in case of nested directories
New-Item -Path $fileDirPath -ItemType File -Force | Out-Null
if ($cont) { Out-UTF8File -Path $fileDirPath -Value $cont }
}
}
}
}
}


#endregion Persistence

function Remove-AppDirItem {
<#
.SYNOPSIS
Removes the given item from application directory.
Wildcards are supported.
.PARAMETER Item
Specifies the item for removing from $dir.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[SupportsWildcards()]
[System.IO.FileInfo[]] $Item
)

process {
# GCI is not suitable as it do not support nested folder with include
foreach ($it in $Item) {
Join-Path $dir $it | Remove-Item -ErrorAction SilentlyContinue -Force -Recurse
}
}
}


function New-JavaShortcutWrapper {
<#
.SYNOPSIS
Creates new shim-like batch file wrapper to spawn jar files within start menu (using shortcut).
.PARAMETER FileName
Specifies the jar executable filename without .jar extension.
#>
[CmdletBinding()]
param([Parameter(Mandatory, ValueFromPipeline)] [System.IO.FileInfo[]] $FileName)

process {
foreach ($f in $FileName) {
(Join-Path $dir "$f.bat") | Out-UTF8Content -Value "@start javaw.exe -jar `"%~dp0$f.jar`" %*"
}
}
}
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 | Out-Null }
if (!(Test-Path $Directory)) { New-Item $Directory -ItemType Directory | Out-Null }

return Resolve-Path $Directory
}
Expand Down
2 changes: 1 addition & 1 deletion lib/install.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'Helpers', 'autoupdate', 'buckets', 'decompress' | ForEach-Object {
'Helpers', 'autoupdate', 'buckets', 'decompress', 'ManifestHelpers' | ForEach-Object {
. (Join-Path $PSScriptRoot "$_.ps1")
}

Expand Down

0 comments on commit 816fa1d

Please sign in to comment.