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

Refactor function names and fix installing 7zip locally if already globally available #3416

Merged
merged 10 commits into from
May 1, 2019
65 changes: 47 additions & 18 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,31 @@ function installed_apps($global) {
}

function file_path($app, $file) {
Show-DeprecatedWarning $MyInvocation 'Get-AppFilePath'
Get-AppFilePath -App $app -File $file
}

function Get-AppFilePath {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[String]
$App,
[Parameter(Mandatory = $true, Position = 1)]
[String]
$File
)

# normal path to file
$path = "$(versiondir $app 'current' $false)\$file"
if(Test-Path($path)) {
return $path
$Path = "$(versiondir $App 'current' $false)\$File"
if(Test-Path $Path) {
return $Path
}

# global path to file
$path = "$(versiondir $app 'current' $true)\$file"
if(Test-Path($path)) {
return $path
$Path = "$(versiondir $App 'current' $true)\$File"
if(Test-Path $Path) {
return $Path
}

# not found
Expand All @@ -175,24 +190,38 @@ Function Test-CommandAvailable {
Return [Boolean](Get-Command $Name -ErrorAction Ignore)
}

function 7zip_path() {
return (file_path '7zip' '7z.exe')
}
function Get-HelperPath {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2')]
[String]
$Helper
)

function 7zip_installed() {
return ![String]::IsNullOrWhiteSpace("$(7zip_path)")
switch ($Helper) {
'7zip' { Get-AppFilePath '7zip' '7z.exe' }
'Lessmsi' { Get-AppFilePath 'lessmsi' 'lessmsi.exe' }
'Innounp' { Get-AppFilePath 'innounp' 'innounp.exe' }
'Dark' { Get-AppFilePath 'dark' 'dark.exe' }
'Aria2' { Get-AppFilePath 'aria2' 'aria2c.exe' }
}
}

function aria2_path() {
return (file_path 'aria2' 'aria2c.exe')
}
function Test-HelperInstalled {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2')]
[String]
$Helper
)

function aria2_installed() {
return ![String]::IsNullOrWhiteSpace("$(aria2_path)")
return ![String]::IsNullOrWhiteSpace((Get-HelperPath -Helper $Helper))
}

function aria2_enabled() {
return (aria2_installed) -and (get_config 'aria2-enabled' $true)
function Test-Aria2Enabled {
return (Test-HelperInstalled -Helper Aria2) -and (get_config 'aria2-enabled' $true)
}

function app_status($app, $global) {
Expand Down
40 changes: 20 additions & 20 deletions lib/decompress.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Test-7ZipRequirement {
function Test-7zipRequirement {
[CmdletBinding(DefaultParameterSetName = "URL")]
[OutputType([Boolean])]
param(
Expand All @@ -13,14 +13,14 @@ function Test-7ZipRequirement {
if ((get_config 7ZIPEXTRACT_USE_EXTERNAL)) {
return $false
} else {
return ($URL | Where-Object { Test-7ZipRequirement -File $_ }).Count -gt 0
return ($URL | Where-Object { Test-7zipRequirement -File $_ }).Count -gt 0
}
} else {
return $File -match '\.((gz)|(tar)|(tgz)|(lzma)|(bz)|(bz2)|(7z)|(rar)|(iso)|(xz)|(lzh)|(nupkg))$'
}
}

function Test-LessMSIRequirement {
function Test-LessmsiRequirement {
[CmdletBinding()]
[OutputType([Boolean])]
param(
Expand All @@ -35,7 +35,7 @@ function Test-LessMSIRequirement {
}
}

function Expand-7ZipArchive {
function Expand-7zipArchive {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
Expand Down Expand Up @@ -63,10 +63,10 @@ function Expand-7ZipArchive {
try {
7z x "$Path" -o"$DestinationPath" (-split $Switches) -y | Out-File $LogLocation
} catch [System.Management.Automation.CommandNotFoundException] {
abort "Cannot find external 7Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!`nRun 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7Zip manually and try again."
abort "Cannot find external 7-Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!`nRun 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7-Zip manually and try again."
}
} else {
&(file_path 7zip 7z.exe) x "$Path" -o"$DestinationPath" (-split $Switches) -y | Out-File $LogLocation
& (Get-HelperPath -Helper 7zip) x "$Path" -o"$DestinationPath" (-split $Switches) -y | Out-File $LogLocation
}
if ($LASTEXITCODE -ne 0) {
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogLocation)"
Expand All @@ -76,12 +76,12 @@ function Expand-7ZipArchive {
}
if ((strip_ext $Path) -match '\.tar$' -or $Path -match '\.tgz$') {
# Check for tar
$ArchivedFile = &(file_path 7zip 7z.exe) l "$Path"
$ArchivedFile = & (Get-HelperPath -Helper 7zip) l "$Path"
if ($LASTEXITCODE -eq 0) {
$TarFile = $ArchivedFile[-3] -replace '.{53}(.*)', '$1' # get inner tar file name
Expand-7ZipArchive "$DestinationPath\$TarFile" $DestinationPath -Removal
Expand-7zipArchive "$DestinationPath\$TarFile" $DestinationPath -Removal
} else {
abort "Failed to list files in $Path.`nNot a 7Zip supported archive file."
abort "Failed to list files in $Path.`nNot a 7-Zip supported archive file."
}
}
if ($Removal) {
Expand All @@ -90,7 +90,7 @@ function Expand-7ZipArchive {
}
}

function Expand-MSIArchive {
function Expand-MsiArchive {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
Expand All @@ -104,7 +104,7 @@ function Expand-MSIArchive {
)
$LogLocation = "$(Split-Path $Path)\msi.log"
if ((get_config MSIEXTRACT_USE_LESSMSI)) {
&(file_path lessmsi lessmsi.exe) x "$Path" "$DestinationPath\" | Out-File $LogLocation
& (Get-HelperPath -Helper Lessmsi) x "$Path" "$DestinationPath\" | Out-File $LogLocation
if ($LASTEXITCODE -ne 0) {
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogLocation)"
}
Expand Down Expand Up @@ -143,7 +143,7 @@ function Expand-InnoArchive {
$Removal
)
$LogLocation = "$(Split-Path $Path)\innounp.log"
&(file_path innounp innounp.exe) -x -d"$DestinationPath" -c'{app}' "$Path" (-split $Switches) -y | Out-File $LogLocation
& (Get-HelperPath -Helper Innounp) -x -d"$DestinationPath" -c'{app}' "$Path" (-split $Switches) -y | Out-File $LogLocation
if ($LASTEXITCODE -ne 0) {
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogLocation)"
}
Expand Down Expand Up @@ -175,15 +175,15 @@ function Expand-ZipArchive {
[System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $DestinationPath)
} catch [System.IO.PathTooLongException] {
# try to fall back to 7zip if path is too long
if (7zip_installed) {
Expand-7ZipArchive $Path $DestinationPath -Removal
if (Test-HelperInstalled -Helper 7zip) {
Expand-7zipArchive $Path $DestinationPath -Removal
return
} else {
abort "Unzip failed: Windows can't handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch [System.IO.IOException] {
if (7zip_installed) {
Expand-7ZipArchive $Path $DestinationPath -Removal
if (Test-HelperInstalled -Helper 7zip) {
Expand-7zipArchive $Path $DestinationPath -Removal
return
} else {
abort "Unzip failed: Windows can't handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
Expand All @@ -202,13 +202,13 @@ function Expand-ZipArchive {
}

function extract_7zip($path, $to, $removal) {
Show-DeprecatedWarning $MyInvocation 'Expand-7ZipArchive'
Expand-7ZipArchive -Path $path -DestinationPath $to -Removal:$removal @args
Show-DeprecatedWarning $MyInvocation 'Expand-7zipArchive'
Expand-7zipArchive -Path $path -DestinationPath $to -Removal:$removal @args
}

function extract_msi($path, $to, $removal) {
Show-DeprecatedWarning $MyInvocation 'Expand-MSIArchive'
Expand-MSIArchive -Path $path -DestinationPath $to -Removal:$removal
Show-DeprecatedWarning $MyInvocation 'Expand-MsiArchive'
Expand-MsiArchive -Path $path -DestinationPath $to -Removal:$removal
}

function unpack_inno($path, $to, $removal) {
Expand Down
12 changes: 9 additions & 3 deletions lib/depends.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ function runtime_deps($manifest) {
function install_deps($manifest, $arch) {
$deps = @()

if (Test-7ZipRequirement -URL (url $manifest $arch)) { $deps += '7zip' }
if (Test-LessMSIRequirement -URL (url $manifest $arch)) { $deps += 'lessmsi' }
if ($manifest.innosetup) { $deps += 'innounp' }
if (!(Test-HelperInstalled -Helper 7zip) -and (Test-7zipRequirement -URL (url $manifest $arch))) {
$deps += '7zip'
}
if (!(Test-HelperInstalled -Helper Lessmsi) -and (Test-LessmsiRequirement -URL (url $manifest $arch))) {
$deps += 'lessmsi'
}
if (!(Test-HelperInstalled -Helper Innounp) -and $manifest.innosetup) {
$deps += 'innounp'
}

return $deps
}
10 changes: 5 additions & 5 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
Set-Content -Path $urlstxt $urlstxt_content

# build aria2 command
$aria2 = "& '$(aria2_path)' $($options -join ' ')"
$aria2 = "& '$(Get-HelperPath -Helper Aria2)' $($options -join ' ')"

# handle aria2 console output
Write-Host "Starting download with aria2 ..."
Expand Down Expand Up @@ -496,7 +496,7 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c
$extracted = 0;

# download first
if(aria2_enabled) {
if(Test-Aria2Enabled) {
dl_with_cache_aria2 $app $version $manifest $architecture $dir $cookies $use_cache $check_hash
} else {
foreach($url in $urls) {
Expand Down Expand Up @@ -545,10 +545,10 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c
if(msi $manifest $architecture) {
warn "MSI install is deprecated. If you maintain this manifest, please refer to the manifest reference docs."
} else {
$extract_fn = 'Expand-MSIArchive'
$extract_fn = 'Expand-MsiArchive'
}
} elseif(Test-7ZipRequirement -File $fname) { # 7zip
$extract_fn = 'Expand-7ZipArchive'
} elseif(Test-7zipRequirement -File $fname) { # 7zip
$extract_fn = 'Expand-7zipArchive'
}

if($extract_fn) {
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ $skip | Where-Object { $explicit_apps -contains $_ } | ForEach-Object {
}

$suggested = @{ };
if (aria2_enabled) {
if (Test-Aria2Enabled) {
warn "Scoop uses 'aria2c' for multi-connection downloads."
warn "Should it cause issues, run 'scoop config aria2-enabled false' to disable it."
}
Expand Down
4 changes: 2 additions & 2 deletions libexec/scoop-update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function update($app, $global, $quiet = $false, $independent, $suggested, $use_c
# Workaround for https://github.com/lukesampson/scoop/issues/2220 until install is refactored
# Remove and replace whole region after proper fix
Write-Host "Downloading new version"
if (aria2_enabled) {
if (Test-Aria2Enabled) {
dl_with_cache_aria2 $app $version $manifest $architecture $cachedir $manifest.cookie $true $check_hash
} else {
$urls = url $manifest $architecture
Expand Down Expand Up @@ -307,7 +307,7 @@ if (!$apps) {

$suggested = @{};
# # $outdated is a list of ($app, $global) tuples
if (aria2_enabled) {
if (Test-Aria2Enabled) {
warn "Scoop uses 'aria2c' for multi-connection downloads."
warn "Should it cause issues, run 'scoop config aria2-enabled false' to disable it."
}
Expand Down
Loading