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

feat(install): Initialize work on manifest helpers #3587

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4d92e89
Init work on helpers
Ash258 Aug 7, 2019
e453418
spaces
Ash258 Aug 7, 2019
cdfd211
Progress
Ash258 Aug 7, 2019
fc9694e
property
Ash258 Aug 7, 2019
ac7f797
Simplify
Ash258 Aug 7, 2019
36d60b6
Tweak
Ash258 Aug 7, 2019
e648a6a
fname
Ash258 Aug 7, 2019
6bbecbc
lt
Ash258 Aug 7, 2019
a0914ba
Add `Remove-AppDirItem` function
Ash258 Aug 15, 2019
cda4c3f
fix export
Ash258 Aug 15, 2019
daef3e4
Try to go with environment variables instead of globals
Ash258 Aug 18, 2019
f8111dd
Get variables from session
Ash258 Aug 18, 2019
12d2c45
Do not change install.ps1
Ash258 Aug 18, 2019
f5a48ef
Extract to function
Ash258 Aug 18, 2019
646f101
Revert f5a48ef8bafe674f5f6bc81ed49de2e5f52f10b8
Ash258 Aug 18, 2019
9690221
Set content only if exist
Ash258 Aug 18, 2019
1a30b7a
Add `New-JavaShortcutWrapper` function
Ash258 Aug 18, 2019
f278447
Simplify created bat
Ash258 Aug 18, 2019
6d5eb88
New-JavaShortcutWrapper: Support array of files
Ash258 Aug 21, 2019
1b84c10
%~dp0 already contain slash
Ash258 Sep 18, 2019
d6f2545
Code cleanup
Ash258 Oct 13, 2019
4f5aa4e
Merge branch 'develop' of github.com:lukesampson/scoop into helpers
Ash258 Oct 17, 2019
ea89517
Explicitly test for Leaf
Ash258 Oct 20, 2019
83363d4
Additional checks and code cleanup
Ash258 Nov 18, 2019
dc0bf68
singular
Ash258 Nov 18, 2019
df648ec
Cleanup
Ash258 Dec 31, 2019
ba3478c
slightly edited shovel implementation
Ash258 Dec 5, 2020
4610e67
Merge branch 'develop' of https://github.com/lukesampson/scoop into h…
Ash258 Jan 29, 2021
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
195 changes: 195 additions & 0 deletions lib/ManifestHelpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
. (Join-Path $PSScriptRoot 'core.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`, `$currentFilePersist`, `$currentFileDir` 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, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('Path', 'LiteralPath', 'Name', 'InputObject')]
[String[]] $File,
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Value')]
[Object[]] $Content,
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('ScriptBlock')]
[ScriptBlock] $Execution
)

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

if (!(Test-Path -LiteralPath $currentFilePersist -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 $currentFileDir -ItemType 'File' -Force | Out-Null
if ($cont) { Out-UTF8File -Path $currentFileDir -Value $cont }
}
}
}
}
}
#endregion Persistence

function Remove-AppDirItem {
<#
.SYNOPSIS
Removes the given item from application directory.
.PARAMETER Item
Specifies the item, which should be removed from $dir.
Wildcards are supported.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[SupportsWildcards()]
[String[]] $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 Edit-File {
<#
.SYNOPSIS
Finds and replaces text in given file.
.PARAMETER File
Specifies the file, which will be loaded.
File could be passed as full path (used for changing files outside $dir) or just relative path to $dir.
.PARAMETER Find
Specifies the string to be replaced.
.PARAMETER Replace
Specifies the string for replacing all occurrences.
Empty string is default => Found string will be removed.
.PARAMETER Regex
Specifies to use regular expression instead of simple match.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[System.IO.FileInfo] $File,
[Parameter(Mandatory)]
[String[]] $Find,
[String[]] $Replace,
[Switch] $Regex
)

begin {
# Use file from $dir
if (Join-Path $dir $File | Test-Path -PathType 'Leaf') { $File = Join-Path $dir $File }
}

process {
if (!(Test-Path $File -PathType 'Leaf')) {
error "File '$File' does not exist" -Err
return
}

$content = Get-Content $File

for ($i = 0; $i -lt $Find.Count; ++$i) {
$toFind = $Find[$i]
if (!$Replace -or ($null -eq $Replace[$i])) {
$toReplace = ''
} else {
$toReplace = $Replace[$i]
}

if ($Regex) {
$content = $content -replace $toFind, $toReplace
} else {
$content = $content.Replace($toFind, $toReplace)
}
}

Out-UTF8File -Path $File -Value $content
}
}

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.
Do not pass fullpath, just FILENAME!
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('Name', 'InputObject')]
[System.IO.FileInfo[]] $FileName
)

process {
foreach ($f in $FileName) {
Out-UTF8File -Path (Join-Path $dir "$f.bat") -Value "@start javaw.exe -jar `"%~dp0$f.jar`" %*"
}
}
}

#region Asserts
function Assert-Administrator {
<#
.SYNOPSIS
Test administrator privileges.
#>
if (!(is_admin)) { throw 'Administrator privileges are required' }
}

function Assert-ScoopConfigValue {
<#
.SYNOPSIS
Test specific value of scoop's configuration.
#>
param(
[Parameter(Mandatory)]
[String] $ConfigOption,
[Parameter(Mandatory)]
$ExpectedValue
# TODO: Add parameter to define operator (Where-Object)
)

process {
$actualValue = get_config $ConfigOption
if ($actualValue -ne $ExpectedValue) { throw "Configuration option '$ConfigOption' needs to be set to '$ExpectedValue'" }
}
}
#endregion Asserts
29 changes: 29 additions & 0 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,35 @@ function get_magic_bytes_pretty($file, $glue = ' ') {
return (get_magic_bytes $file | ForEach-Object { $_.ToString('x2') }) -join $glue
}

function Out-UTF8File {
<#
.SYNOPSIS
Write UTF8 (no-bom) file.
.DESCRIPTION
Use Set-Content -encoding utf8 on pwsh and WriteAllLines on powershell.
.PARAMETER Path
Specifies filename to be written.
.PARAMETER Content
Specifies content of to be written to file.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias('Path', 'LiteralPath')]
[System.IO.FileInfo] $File,
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('Value')]
$Content
)
process {
if ($PSVersionTable.PSVersion.Major -ge 6) {
Set-Content -LiteralPath $File -Value $Content -Encoding utf8
} else {
[System.IO.File]::WriteAllLines($File, ($Content -join "`r`n"))
}
}
}

##################
# Core Bootstrap #
##################
Expand Down
5 changes: 3 additions & 2 deletions lib/install.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
. "$psscriptroot/autoupdate.ps1"
. "$psscriptroot/buckets.ps1"
'autoupdate', 'buckets', 'ManifestHelpers' | ForEach-Object {
. "$PSScriptRoot\$_.ps1"
}

function nightly_version($date, $quiet = $false) {
$date_str = $date.tostring("yyyyMMdd")
Expand Down