-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
DRY: Address New-Item calls in pre_install/post_install scripts #3166
Comments
Relate #2733
|
One step ahead of me 😁 |
Import-Module -Name .\lib\ManifestHelpers.psm1 -prefix Scoop -Verbose Example module function Expand-7zip {
<#
.SYNOPSIS
SOME DESCRIPTION
...
#>
param(
[String] $Archive,
[String] $Destination = '',
[Switch] $Recurse
)
process {
extract_7zip $Archive $Destination $Recurse
}
}
function Expand-Msi {
<#
.SYNOPSIS
SOME DESCRIPTION
...
#>
param(
[String] $Archive,
[String] $Destination = '',
[Switch] $UseLessMsi
)
process {
if ($UseLessMsi) {
extract_lessmsi $Archive $Destination
} else {
extract_msi $Archive $Destination
}
}
}
function New-Directory {
<#
.SYNOPSIS
SOME DESCRIPTION
...
#>
param(
[String] $Path,
[String] $Name,
[String] $Message = ''
)
process {
if (!(Test-Path (Join-Path -Path $Path -ChildPath $Name))) {
if ($Message) {
Write-Host $Message -f Yellow
}
New-Item -Path $Path -Name $name -ItemType Directory | Out-Null
}
}
}
function New-File {
<#
.SYNOPSIS
SOME DESCRIPTION
...
#>
param(
[String] $Path,
[String] $Name,
[String] $Encoding = 'utf-8',
[String] $Content = $null
)
process {
$file = Join-Path -Path $Path -ChildPath $Name
if (!(Test-Path $file)) {
if($Encoding -eq 'byte') {
if((Get-Command Set-Content).parameters.ContainsKey('AsByteStream')) {
# PowerShell Core (6.0+) '-Encoding byte' is replaced by '-AsByteStream'
Set-Content -Path $file -AsByteStream -Value $Content
}
else {
Set-Content -Path $file -Encoding byte -Value $Content
}
} else {
Set-Content -Path $file -Encoding $Encoding -Value $Content
}
}
}
}
function Write-File {
<#
.SYNOPSIS
SOME DESCRIPTION
...
#>
param(
[String] $Path,
[String] $Name,
[String] $Encoding = 'utf-8',
[String] $Content = $null
)
process {
$file = Join-Path -Path $Path -ChildPath $Name
if ((Test-Path $file)) {
if($Encoding -eq 'byte') {
if((Get-Command Set-Content).parameters.ContainsKey('AsByteStream')) {
# PowerShell Core (6.0+) '-Encoding byte' is replaced by '-AsByteStream'
Set-Content -Path $file -AsByteStream -Value $Content
}
else {
Set-Content -Path $file -Encoding byte -Value $Content
}
} else {
Set-Content -Path $file -Encoding $Encoding -Value $Content
}
}
}
} |
I like the idea of proper and powershell native code style, everything which make code more readable and reusable is awesome. But it should be adopted in whole scoop codebase or ditched. It's really mess when all code is dot sourced and just one module is real "module" with powershell standart codebase. If this start some real Scoop refactor to adopt powershell standart and best practise, then Go for it. Best would be to extract bucket folder, which will allow better maintanence of whole Scoop with less error domain. So best scenario:
|
Holy cow! Extracting core bucket always be the first work. |
Not always. For some hotfixes and small impact fixes it is not needed, but for future bigger refactoring / extending it's really crucial. |
Should we address the repeated calls of
New-Item
in manypre_install
/post_install
scripts to keep them DRY? 😄Most of them run
Test-Path
, display a message and create a directory or a file with no content.These scripts could be cleanup up and be more readable in JSON format by using some simple functions like:
The text was updated successfully, but these errors were encountered: