Skip to content

Commit

Permalink
fix(scoop-checkup): Do not produce exception (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 committed Dec 1, 2021
1 parent 3bc7179 commit 3909b6c
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 130 deletions.
2 changes: 1 addition & 1 deletion bin/auto-pr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ foreach ($changedFile in $manifestsToUpdate) {
$status = _gitWrapper @splat -Command 'status' -Argument '--porcelain', '--untracked-files=no'
$status = $status | Where-Object { $_ -match "M\s{2}.*$($gci.Name)" }
if ($status -and $status.StartsWith('M ') -and $status.EndsWith($gci.Name)) {
$delim = if (Test-IsUnix) { '""' } else { '"' }
$delim = if ($SHOVEL_IS_UNIX) { '""' } else { '"' }
$commitA = '--message', "$delim${applicationName}: Update to version $version$delim"
if ($archived) {
$commitA += '--message', "${delim}Archive version $oldVersion$delim"
Expand Down
2 changes: 1 addition & 1 deletion bin/update-supporting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ foreach ($sup in $Sups) {
Start-Sleep -Seconds 2

Rename-Item $dir 'old' -ErrorAction 'SilentlyContinue'
Confirm-DirectoryExistence -Directory $dir | Out-Null
Confirm-DirectoryExistence -LiteralPath $dir | Out-Null
Start-Sleep -Seconds 2
try {
$fname = dl_urls $name $manifest.version $manifest '' (default_architecture) $dir $true $true
Expand Down
7 changes: 4 additions & 3 deletions lib/Diagnostic.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Use 'Write-UserMessage -Warning' to highlight the issue, and follow up with the
@('Helpers', 'New-IssuePrompt'),
@('buckets', 'Get-KnownBucket'),
@('decompress', 'Expand-7zipArchive'),
@('install', 'install_app'),
@('Git', 'Invoke-GitCmd')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Expand Down Expand Up @@ -52,7 +53,7 @@ function Test-DiagWindowsDefender {
[OutputType([bool])]
param([Switch] $Global)

if (Test-IsUnix) { return $true }
if ($SHOVEL_IS_UNIX) { return $true }

$defender = Get-Service -Name 'WinDefend' -ErrorAction 'SilentlyContinue'
if ((is_admin) -and ($defender -and $defender.Status) -and ($defender.Status -eq [System.ServiceProcess.ServiceControllerStatus]::Running)) {
Expand Down Expand Up @@ -128,7 +129,7 @@ function Test-DiagLongPathEnabled {
[OutputType([bool])]
param()

if (Test-IsUnix) { return $true }
if ($SHOVEL_IS_UNIX) { return $true }

# Verify supported windows version
if ([System.Environment]::OSVersion.Version.Major -lt 10 -or [System.Environment]::OSVersion.Version.Build -lt 1607) {
Expand Down Expand Up @@ -161,7 +162,7 @@ function Test-DiagEnvironmentVariable {

$result = $true

if (Test-IsUnix) {
if ($SHOVEL_IS_UNIX) {
# Unix "comspec"
if (!(Test-Path $env:SHELL -PathType 'Leaf')) {
Write-UserMessage -Message '''SHELL'' environment variable is not configured' -Warning
Expand Down
2 changes: 1 addition & 1 deletion lib/Git.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function Invoke-GitCmd {
$prox = get_config 'proxy' 'none'

if ($prox -and ($prox -ne 'none')) {
$keyword = if (Test-IsUnix) { 'export' } else { 'SET' }
$keyword = if ($SHOVEL_IS_UNIX) { 'export' } else { 'SET' }
$commandToRunWindows = $commandToRunNix = "$keyword HTTPS_PROXY=$prox&&$keyword HTTP_PROXY=$prox&&$commandToRun"
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/buckets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ function Add-Bucket {
throw "'$RepositoryUrl' is not valid git repository ($out)"
}

ensure $SCOOP_BUCKETS_DIRECTORY | Out-Null
$bucketDirectory = (ensure $bucketDirectory).Path
Confirm-DirectoryExistence -LiteralPath $SCOOP_BUCKETS_DIRECTORY | Out-Null
$bucketDirectory = (Confirm-DirectoryExistence -LiteralPath $bucketDirectory).Path
Write-UserMessage -Message 'Cloning bucket repository...' -Output:$false
Invoke-GitCmd -Command 'clone' -Argument '--quiet', """$RepositoryUrl""", """$bucketDirectory""" -Proxy

Expand Down
121 changes: 7 additions & 114 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function Get-UserAgent {
'AMD64' { $arch = 'Win64; x64;' }
}

if (Test-IsUnix) {
if ($SHOVEL_IS_UNIX) {
$system = Invoke-SystemComSpecCommand -Unix 'uname -s'
$arch = Invoke-SystemComSpecCommand -Unix 'uname -srv'
$arch = "$arch;"
Expand Down Expand Up @@ -114,7 +114,7 @@ function Invoke-SystemComSpecCommand {
)

process {
if (Test-IsUnix) {
if ($SHOVEL_IS_UNIX) {
$shell = $env:SHELL
$parameters = @('-c', $Unix)
} else {
Expand All @@ -141,7 +141,7 @@ function Test-IsArmArchitecture {
param()

process {
if (Test-IsUnix) {
if ($SHOVEL_IS_UNIX) {
return (Invoke-SystemComSpecCommand -Unix 'uname -m') -like 'aarch*'
} else {
return $env:PROCESSOR_IDENTIFIER -like 'ARMv*'
Expand All @@ -167,7 +167,7 @@ function get_config($name, $default) {

function set_config($name, $value) {
if ($null -eq $SCOOP_CONFIGURATION -or $SCOOP_CONFIGURATION.Count -eq 0) {
Split-Path -Path $SCOOP_CONFIGURATION_FILE | ensure | Out-Null
Split-Path -Path $SCOOP_CONFIGURATION_FILE | Confirm-DirectoryExistence | Out-Null
$SCOOP_CONFIGURATION = New-Object PSObject
$SCOOP_CONFIGURATION | Add-Member -MemberType 'NoteProperty' -Name $name -Value $value
} else {
Expand Down Expand Up @@ -336,43 +336,6 @@ function installed_apps($global) {
if (Test-Path $dir) { Get-ChildItem $dir -Exclude 'scoop' -Directory | Select-Object -ExpandProperty 'Name' }
}

function Get-AppFilePath {
<#
.SYNOPSIS
Get full path to the specific executable under specific application installed via scoop.
.PARAMETER App
Specifies the scoop's application name.
.PARAMETER File
Specifies the executable name.
#>
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory)]
[String] $App,
[Parameter(Mandatory)]
[String] $File
)

# Normal path to file
$path = versiondir $App (Select-CurrentVersion -AppName $App) $false | Join-Path -ChildPath $File
if (Test-Path -LiteralPath $path -PathType 'Leaf') { return $path }

# Global path to file
$path = versiondir $App (Select-CurrentVersion -AppName $App -Global) $true | Join-Path -ChildPath $File
if (Test-Path -LiteralPath $path -PathType 'Leaf') { return $path }

# Try path
$path = Get-Command -Name $File -ErrorAction 'SilentlyContinue'
if ($path -and (Test-Path -LiteralPath $path.Source -PathType 'Leaf')) {
Write-UserMessage -Message "Application '$App' is not installed via Scoop. Trying to use '$($path.Source)'" -Warning
return $path
}

# Not found
return $null
}

function Test-CommandAvailable {
<#
.SYNOPSIS
Expand All @@ -387,77 +350,6 @@ function Test-CommandAvailable {
process { return [bool] (Get-Command $Name -ErrorAction 'Ignore') }
}

function Get-HelperPath {
<#
.SYNOPSIS
Get full path to the often used application's executables.
.PARAMETER Helper
Specifies the name of helper application.
#>
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2', 'Zstd', 'Innoextract')]
[String] $Helper
)

process {
$helperPath = $null
switch ($Helper) {
'Aria2' { $helperPath = Get-AppFilePath 'aria2' 'aria2c.exe' }
'Innounp' { $helperPath = Get-AppFilePath 'innounp' 'innounp.exe' }
'Lessmsi' { $helperPath = Get-AppFilePath 'lessmsi' 'lessmsi.exe' }
'Zstd' { $HelperPath = Get-AppFilePath 'zstd' 'zstd.exe' }
'Innoextract' { $HelperPath = Get-AppFilePath 'innoextract' 'innoextract.exe' }
'7zip' {
$helperPath = Get-AppFilePath '7zip' '7z.exe'
if ([String]::IsNullOrEmpty($helperPath)) {
$helperPath = Get-AppFilePath '7zip-zstd' '7z.exe'
}
}
'Dark' {
$helperPath = Get-AppFilePath 'dark' 'dark.exe'
if ([String]::IsNullOrEmpty($helperPath)) {
$helperPath = Get-AppFilePath 'wixtoolset' 'dark.exe'
}
}
}

return $helperPath
}
}

function Test-HelperInstalled {
<#
.SYNOPSIS
Test if specified widely used application is installed.
.PARAMETER Helper
Specifies the name of application.
#>
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2', 'Zstd', 'Innoextract')]
[String] $Helper
)

process { return ![String]::IsNullOrWhiteSpace((Get-HelperPath -Helper $Helper)) }
}

function Test-Aria2Enabled {
<#
.SYNOPSIS
Test if aria2 application is installed and enabled.
#>
[CmdletBinding()]
[OutputType([bool])]
param()

process { return (Test-HelperInstalled -Helper 'Aria2') -and (get_config 'aria2-enabled' $true) }
}

# paths
function fname($path) { return Split-Path $path -Leaf }
function strip_ext($fname) { return $fname -replace '\.[^\.]*$' }
Expand Down Expand Up @@ -717,7 +609,7 @@ function warn_on_overwrite($shim_ps1, $path) {
function shim($path, $global, $name, $arg) {
if (!(Test-Path $path)) { throw [ScoopException] "Shim creation fail|-Cannot shim '$(fname $path)': could not find '$path'" } # TerminatingError thrown

$abs_shimdir = shimdir $global | ensure
$abs_shimdir = shimdir $global | Confirm-DirectoryExistence
if (!$name) { $name = strip_ext (fname $path) }

$shim = Join-Path $abs_shimdir $name.ToLower()
Expand Down Expand Up @@ -849,7 +741,7 @@ function remove_from_path($dir, $global) {
}

function ensure_scoop_in_path($global) {
$abs_shimdir = shimdir $global | ensure
$abs_shimdir = shimdir $global | Confirm-DirectoryExistence
# be aggressive (b-e-aggressive) and install scoop first in the path
ensure_in_path $abs_shimdir $global
}
Expand Down Expand Up @@ -1070,6 +962,7 @@ $SCOOP_CONFIGURATION = load_cfg $SCOOP_CONFIGURATION_FILE

# General variables
$SHOVEL_DEBUG_ENABLED = Test-ScoopDebugEnabled
$SHOVEL_IS_UNIX = Test-IsUnix

# TODO: Remove deprecated variables
$scoopdir = $SCOOP_ROOT_DIRECTORY
Expand Down
Loading

0 comments on commit 3909b6c

Please sign in to comment.