Skip to content

Commit

Permalink
opt: Get rid of fullpath
Browse files Browse the repository at this point in the history
Use singular noun for `Get-MagicByte`

#13
  • Loading branch information
Ash258 committed May 17, 2020
1 parent 860dc91 commit 0da20a0
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 44 deletions.
6 changes: 3 additions & 3 deletions bin/checkhashes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ foreach ($current in $MANIFESTS) {
continue
}

$to_check = fullpath (cache_path $current.app $version $u)
$to_check = cache_path $current.app $version $u
$actual_hash = compute_hash $to_check $algorithm

# Append type of algorithm to both expected and actual if it's not sha256
Expand Down Expand Up @@ -146,10 +146,10 @@ foreach ($current in $MANIFESTS) {
Write-Host "$($current.app): " -NoNewline
Write-Host 'Mismatch found ' -ForegroundColor Red
$mismatched | ForEach-Object {
$file = fullpath (cache_path $current.app $version $current.urls[$_])
$file = cache_path $current.app $version $current.urls[$_]
Write-Host "`tURL:`t`t$($current.urls[$_])"
if (Test-Path $file) {
Write-Host "`tFirst bytes:`t$(Get-MagicBytes $file -Pretty)"
Write-Host "`tFirst bytes:`t$(Get-MagicByte -File $file -Pretty)"
}
Write-Host "`tExpected:`t$($current.hashes[$_])" -ForegroundColor Green
Write-Host "`tActual:`t`t$($actuals[$_])" -ForegroundColor Red
Expand Down
2 changes: 1 addition & 1 deletion lib/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function Out-UTF8File {
}
}

function Get-MagicBytes {
function Get-MagicByte {
<#
.SYNOPSIS
Get file's first 8 bytes.
Expand Down
9 changes: 3 additions & 6 deletions lib/Update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ function Update-ScoopCoreClone {

Write-UserMessage -Message "Cloning scoop installation from $Repo ($Branch)" -Info

# TODO: Get rid of fullpath
$newDir = fullpath (versiondir 'scoop' 'new')
$newDir = versiondir 'scoop' 'new'

git_clone -q --single-branch --branch $Branch $Repo "`"$newDir`""

Expand Down Expand Up @@ -145,8 +144,7 @@ function Update-Scoop {
# TODO: CONFIG refactor adoption
$configRepo = get_config 'SCOOP_REPO'
$configBranch = get_config 'SCOOP_BRANCH'
# TODO: Get rid of fullpath
$currentDir = fullpath (versiondir 'scoop' 'current')
$currentDir = versiondir 'scoop' 'current'

# Defaults
if (!$configRepo) {
Expand Down Expand Up @@ -277,8 +275,7 @@ function Update-App {

if (!$SkipHashCheck) {
$manifest_hash = hash_for_url $manifest $url $architecture
# TODO: Get rid of fullpath
$source = fullpath (cache_path $App $version $url)
$source = cache_path $App $version $url
$ok, $err = check_hash $source $manifest_hash (show_app $App $bucket)

if (!$ok) {
Expand Down
2 changes: 1 addition & 1 deletion lib/autoupdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function get_hash_for_app([String] $app, $config, [String] $version, [String] $u
write-host -f darkred "URL $url is not valid"
return $null
}
$file = fullpath (cache_path $app $version $url)
$file = cache_path $app $version $url
$hash = compute_hash $file 'sha256'
write-host -f DarkYellow 'Computed hash: ' -NoNewline
write-host -f Green $hash
Expand Down
48 changes: 31 additions & 17 deletions lib/core.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
. "$PSScriptRoot\Helpers.ps1"

function Get-AbsolutePath {
<#
.SYNOPSIS
Gets absolute path.
.PARAMETER Path
Specifies the path to evaluate.
.OUTPUTS
System.String
Absolute path, may or maynot existed.
#>
[CmdletBinding()]
[OutputType([String])]
param([Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [String] $Path)

process { return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) }
}

function Optimize-SecurityProtocol {
# .NET Framework 4.7+ has a default security protocol called 'SystemDefault',
# which allows the operating system to choose the best protocol to use.
Expand Down Expand Up @@ -369,10 +386,7 @@ function url_remote_filename($url) {
}

function ensure($dir) { if (!(test-path $dir)) { mkdir $dir > $null }; resolve-path $dir }
function fullpath($path) {
# should be ~ rooted
$executionContext.sessionState.path.getUnresolvedProviderPathFromPSPath($path)
}

function relpath($path) { "$($myinvocation.psscriptroot)\$path" } # relative to calling script
function friendly_path($path) {
$h = (Get-PsProvider 'FileSystem').home; if (!$h.endswith('\')) { $h += '\' }
Expand Down Expand Up @@ -625,7 +639,6 @@ function search_in_path($target) {

function ensure_in_path($dir, $global) {
$path = env 'PATH' $global
$dir = fullpath $dir
if ($path -notmatch [regex]::escape($dir)) {
write-output "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) path."

Expand Down Expand Up @@ -689,28 +702,24 @@ function strip_path($orig_path, $dir) {
}

function add_first_in_path($dir, $global) {
$dir = fullpath $dir

# future sessions
# Future sessions
$null, $currpath = strip_path (env 'path' $global) $dir
env 'path' $global "$dir;$currpath"

# this session
# This session
$null, $env:PATH = strip_path $env:PATH $dir
$env:PATH = "$dir;$env:PATH"
}

function remove_from_path($dir, $global) {
$dir = fullpath $dir

# future sessions
# Future sessions
$was_in_path, $newpath = strip_path (env 'path' $global) $dir
if ($was_in_path) {
Write-Output "Removing $(friendly_path $dir) from your path."
env 'path' $global $newpath
}

# current session
# Current session
$was_in_path, $newpath = strip_path $env:PATH $dir
if ($was_in_path) { $env:PATH = $newpath }
}
Expand Down Expand Up @@ -931,13 +940,18 @@ function run($exe, $arg, $msg, $continue_exit_codes) {
}

function get_magic_bytes($file) {
Show-DeprecatedWarning $MyInvocation 'Get-MagicBytes'
return Get-MagicBytes -File $file
Show-DeprecatedWarning $MyInvocation 'Get-MagicByte'
return Get-MagicByte -File $file
}

function get_magic_bytes_pretty($file, $glue = ' ') {
Show-DeprecatedWarning $MyInvocation 'Get-MagicBytes'
return Get-MagicBytes -File $file -Glue $glue -Pretty
Show-DeprecatedWarning $MyInvocation 'Get-MagicByte'
return Get-MagicByte -File $file -Glue $glue -Pretty
}

function fullpath($path) {
Show-DeprecatedWarning $MyInvocation 'Get-AbsolutePath'
return Get-AbsolutePath -Path $path
}
#endregion Deprecated

Expand Down
15 changes: 6 additions & 9 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function Find-Manifest($app, $bucket) {
}

function dl_with_cache($app, $version, $url, $to, $cookies = $null, $use_cache = $true) {
$cached = fullpath (cache_path $app $version $url)
$cached = cache_path $app $version $url

if (!(test-path $cached) -or !$use_cache) {
ensure $cachedir | Out-Null
Expand Down Expand Up @@ -179,7 +179,7 @@ function aria_exit_code($exitcode) {
function get_filename_from_metalink($file) {
$XML_MAGIC = '3C3F786D6C'
# Check if file starts with '<?xml'
if (!(Get-MagicBytes $file -Pretty -Glue '').StartsWith($XML_MAGIC)) { return $null }
if (!(Get-MagicByte -File $file -Pretty -Glue '').StartsWith($XML_MAGIC)) { return $null }

# Add System.Xml for reading metalink files
Add-Type -AssemblyName 'System.Xml'
Expand Down Expand Up @@ -256,7 +256,7 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
'filename' = url_filename $url
'target' = "$dir\$(url_filename $url)"
'cachename' = fname (cache_path $app $version $url)
'source' = fullpath (cache_path $app $version $url)
'source' = cache_path $app $version $url
}

if (!(test-path $data.$url.source)) {
Expand Down Expand Up @@ -627,8 +627,6 @@ function cookie_header($cookies) {
}

function is_in_dir($dir, $check) {
$check = "$(fullpath $check)"
$dir = "$(fullpath $dir)"
$check -match "^$([regex]::escape("$dir"))(\\|`$)"
}

Expand All @@ -654,7 +652,6 @@ function hash_for_url($manifest, $url, $arch) {

# returns (ok, err)
function check_hash($file, $hash, $app_name) {
$file = fullpath $file
if (!$hash) {
Write-UserMessage -Message "Warning: No hash in manifest. SHA256 for '$(fname $file)' is:`n $(compute_hash $file 'sha256')" -Warning
return $true, $null
Expand All @@ -676,7 +673,7 @@ function check_hash($file, $hash, $app_name) {
$msg += "App: $app_name`n"
$msg += "URL: $url`n"
if (Test-Path $file) {
$msg += "First bytes: $(Get-MagicBytes $file)`n"
$msg += "First bytes: $(Get-MagicByte -File $file)`n"
}
if ($expected -or $actual) {
$msg += "Expected: $expected`n"
Expand Down Expand Up @@ -1143,8 +1140,8 @@ function persist_data($manifest, $original_dir, $persist_dir) {

$source = $source.TrimEnd("/").TrimEnd("\\")

$source = fullpath "$dir\$source"
$target = fullpath "$persist_dir\$target"
$source = "$dir\$source"
$target = "$persist_dir\$target"

# if we have had persist data in the store, just create link and go
if (Test-Path $target) {
Expand Down
2 changes: 1 addition & 1 deletion lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
}

function manifest_path($app, $bucket) {
fullpath "$(Find-BucketDirectory $bucket)\$(sanitary_path $app).json"
return "$(Find-BucketDirectory $bucket)\$(sanitary_path $app).json"
}

function parse_json($path) {
Expand Down
2 changes: 1 addition & 1 deletion lib/psmodules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function ensure_in_psmodulepath($dir, $global) {
if (!$global -and $null -eq $path) {
$path = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
}
$dir = fullpath $dir

if ($path -notmatch [regex]::escape($dir)) {
write-output "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) PowerShell module path."

Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-reset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ foreach ($a in $apps) {
$manifest = installed_manifest $app $version $global
# if this is null we know the version they're resetting to
# is not installed
if ($manifest -eq $null) {
if ($null -eq $manifest) {
++$problems
Write-UserMessage -Message "'$app ($version)' isn't installed" -Err
continue
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-status.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
reset_aliases

# check if scoop needs updating
$currentdir = fullpath (versiondir 'scoop' 'current')
$currentdir = versiondir 'scoop' 'current'
$needs_update = $false

if (test-path "$currentdir\.git") {
Expand Down
3 changes: 1 addition & 2 deletions libexec/scoop-which.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ try {
}

$userShims = shimdir $false | Resolve-Path
# TODO: Get rid of fullpath
$globalShims = fullpath (shimdir $true) # don't resolve: may not exist
$globalShims = shimdir $true # don't resolve: may not exist

$FINAL_PATH = $null
$exitCode = 0
Expand Down
1 change: 0 additions & 1 deletion test/Scoop-Install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ Describe "is_in_dir" -Tag 'Scoop' {
is_in_dir "C:\test" "C:\foo" | Should -BeFalse
is_in_dir "C:\test" "C:\test\foo\baz.zip" | Should -betrue

is_in_dir "test" "$PSScriptRoot" | Should -betrue
is_in_dir "$PSScriptRoot\..\" "$PSScriptRoot" | Should -BeFalse
}
}
Expand Down

0 comments on commit 0da20a0

Please sign in to comment.