Skip to content

Commit

Permalink
perf(scoop): Load libs only once (#4839)
Browse files Browse the repository at this point in the history
  • Loading branch information
niheaven authored Apr 21, 2022
1 parent 7ee74a0 commit 6296822
Show file tree
Hide file tree
Showing 42 changed files with 145 additions and 186 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
- **scoop-download:** Add failure check ([#4822](https://github.com/ScoopInstaller/Scoop/pull/4822))
- **install:** Fix issue with installation inside containers ([#4837](https://github.com/ScoopInstaller/Scoop/pull/4837))

### Performance Improvements

- **scoop:** Load libs only once ([#4839](https://github.com/ScoopInstaller/Scoop/issues/4839))

### Code Refactoring

- **bucket:** Move 'Find-Manifest' and 'list_buckets' to 'buckets' ([#4814](https://github.com/ScoopInstaller/Scoop/issues/4814))
Expand Down
6 changes: 3 additions & 3 deletions bin/checkver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ param(
)

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\autoupdate.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\autoupdate.ps1"
. "$PSScriptRoot\..\lib\json.ps1"
. "$PSScriptRoot\..\lib\versions.ps1"
. "$PSScriptRoot\..\lib\install.ps1" # needed for hash generation
Expand Down Expand Up @@ -105,7 +105,7 @@ Get-Event | ForEach-Object {
$Queue | ForEach-Object {
$name, $json = $_

$substitutions = Get-VersionSubstitution $json.version
$substitutions = Get-VersionSubstitution $json.version # 'autoupdate.ps1'

$wc = New-Object Net.Webclient
if ($json.checkver.useragent) {
Expand Down Expand Up @@ -337,7 +337,7 @@ while ($in_progress -gt 0) {
Write-Host 'Forcing autoupdate!' -ForegroundColor DarkMagenta
}
try {
Invoke-AutoUpdate $App $Dir $json $ver $matchesHashtable
Invoke-AutoUpdate $App $Dir $json $ver $matchesHashtable # 'autoupdate.ps1'
} catch {
if ($ThrowError) {
throw $_
Expand Down
56 changes: 40 additions & 16 deletions bin/scoop.ps1
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
#Requires -Version 5
param($cmd)
param($SubCommand)

Set-StrictMode -off
Set-StrictMode -Off

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\commands.ps1"
. "$PSScriptRoot\..\lib\help.ps1"

# for aliases where there's a local function, re-alias so the function takes precedence
$aliases = Get-Alias | Where-Object { $_.Options -notmatch 'ReadOnly|AllScope' } | ForEach-Object { $_.Name }
Get-ChildItem Function: | Where-Object -Property Name -In -Value $aliases | ForEach-Object {
Set-Alias -Name $_.Name -Value Local:$($_.Name) -Scope Script
}

$commands = commands
if ('--version' -contains $cmd -or (!$cmd -and '-v' -contains $args)) {
Write-Host "Current Scoop version:"
Invoke-Expression "git -C '$(versiondir 'scoop' 'current')' --no-pager log --oneline HEAD -n 1"
Write-Host ""
switch ($SubCommand) {
({ $SubCommand -in @($null, '--help', '/?') }) {
if (!$SubCommand -and $Args -eq '-v') {
$SubCommand = '--version'
} else {
exec 'help'
}
}
({ $SubCommand -eq '--version' }) {
Write-Host 'Current Scoop version:'
if ((Test-CommandAvailable git) -and (Test-Path "$PSScriptRoot\..\.git") -and (get_config SCOOP_BRANCH 'master') -ne 'master') {
Invoke-Expression "git -C '$PSScriptRoot\..' --no-pager log --oneline HEAD -n 1"
} else {
$version = Select-String -Pattern '^## \[(v[\d.]+)\].*?([\d-]+)$' -Path "$PSScriptRoot\..\CHANGELOG.md"
Write-Host $version.Matches.Groups[1].Value -ForegroundColor Cyan -NoNewline
Write-Host " - Released at $($version.Matches.Groups[2].Value)"
}
Write-Host ''

Get-LocalBucket | ForEach-Object {
$bucketLoc = Find-BucketDirectory $_ -Root
if(Test-Path (Join-Path $bucketLoc '.git')) {
Write-Host "'$_' bucket:"
Invoke-Expression "git -C '$bucketLoc' --no-pager log --oneline HEAD -n 1"
Write-Host ""
Get-LocalBucket | ForEach-Object {
$bucketLoc = Find-BucketDirectory $_ -Root
if ((Test-Path (Join-Path $bucketLoc '.git')) -and (Test-CommandAvailable git)) {
Write-Host "'$_' bucket:"
Invoke-Expression "git -C '$bucketLoc' --no-pager log --oneline HEAD -n 1"
Write-Host ''
}
}
}
({ $SubCommand -in (commands) }) {
if ($Args -in @('-h', '--help', '/?')) {
exec 'help' @($SubCommand)
} else {
exec $SubCommand $Args
}
}
default {
"scoop: '$SubCommand' isn't a scoop command. See 'scoop help'."
exit 1
}
}
elseif (@($null, '--help', '/?') -contains $cmd -or $args[0] -contains '-h') { exec 'help' $args }
elseif ($commands -contains $cmd) { exec $cmd $args }
else { "scoop: '$cmd' isn't a scoop command. See 'scoop help'."; exit 1 }
8 changes: 1 addition & 7 deletions lib/autoupdate.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<#
TODO
- clean up
#>
. "$PSScriptRoot\core.ps1"
. "$PSScriptRoot\json.ps1"

# Must included with 'json.ps1'
function find_hash_in_rdf([String] $url, [String] $basename) {
$data = $null
try {
Expand Down
2 changes: 0 additions & 2 deletions lib/buckets.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
. "$PSScriptRoot\core.ps1"

$bucketsdir = "$scoopdir\buckets"

function Find-BucketDirectory {
Expand Down
4 changes: 2 additions & 2 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ function show_app($app, $bucket, $version) {

function last_scoop_update() {
# PowerShell 6 returns an DateTime Object
$last_update = (scoop config lastupdate)
$last_update = (get_config lastupdate)

if ($null -ne $last_update -and $last_update.GetType() -eq [System.String]) {
try {
Expand All @@ -934,7 +934,7 @@ function is_scoop_outdated() {
$last_update = $(last_scoop_update)
$now = [System.DateTime]::Now
if($null -eq $last_update) {
scoop config lastupdate $now.ToString('o')
set_config lastupdate $now.ToString('o')
# enforce an update for the first time
return $true
}
Expand Down
3 changes: 0 additions & 3 deletions lib/diagnostic.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ Diagnostic tests.
Return $true if the test passed, otherwise $false.
Use 'warn' to highlight the issue, and follow up with the recommended actions to rectify.
#>
. "$PSScriptRoot\buckets.ps1"

function check_windows_defender($global) {
$defender = Get-Service -Name WinDefend -ErrorAction SilentlyContinue
if (Test-CommandAvailable Get-MpPreference) {
Expand Down Expand Up @@ -55,4 +53,3 @@ function check_long_paths {

return $true
}

4 changes: 0 additions & 4 deletions lib/install.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
. "$PSScriptRoot\core.ps1"
. "$PSScriptRoot\autoupdate.ps1"
. "$PSScriptRoot\buckets.ps1"

function nightly_version($date, $quiet = $false) {
$date_str = $date.tostring("yyyyMMdd")
if (!$quiet) {
Expand Down
5 changes: 1 addition & 4 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
. "$PSScriptRoot\core.ps1"
. "$PSScriptRoot\autoupdate.ps1"

function manifest_path($app, $bucket) {
fullpath "$(Find-BucketDirectory $bucket)\$(sanitary_path $app).json"
}
Expand Down Expand Up @@ -88,7 +85,7 @@ function supports_architecture($manifest, $architecture) {
return -not [String]::IsNullOrEmpty((arch_specific 'url' $manifest $architecture))
}

function generate_user_manifest($app, $bucket, $version) {
function generate_user_manifest($app, $bucket, $version) { # 'autoupdate.ps1' 'buckets.ps1' 'manifest.ps1'
$null, $manifest, $bucket, $null = Find-Manifest $app $bucket
if ("$($manifest.version)" -eq "$version") {
return manifest_path $app $bucket
Expand Down
3 changes: 1 addition & 2 deletions lib/versions.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# versions
function Get-LatestVersion {
<#
.SYNOPSIS
Expand Down Expand Up @@ -29,7 +28,7 @@ function Get-LatestVersion {
}
}

function Select-CurrentVersion {
function Select-CurrentVersion { # 'manifest.ps1'
<#
.SYNOPSIS
Select current version of installed app, from 'current\manifest.json' or modified time of version directory
Expand Down
4 changes: 1 addition & 3 deletions libexec/scoop-alias.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ param(
[Switch]$verbose = $false
)

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\install.ps1" # shim related

$script:config_alias = 'alias'

Expand Down
4 changes: 0 additions & 4 deletions libexec/scoop-bucket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
# scoop bucket known
param($cmd, $name, $repo)

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\help.ps1"

$usage_add = 'usage: scoop bucket add <name> [<repo>]'
$usage_rm = 'usage: scoop bucket rm <name>'

Expand Down
2 changes: 0 additions & 2 deletions libexec/scoop-cache.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# scoop cache rm *
param($cmd)

. "$PSScriptRoot\..\lib\help.ps1"

function cacheinfo($file) {
$app, $version, $url = $file.Name -split '#'
New-Object PSObject -Property @{ Name = $app; Version = $version; Length = $file.Length; URL = $url }
Expand Down
5 changes: 2 additions & 3 deletions libexec/scoop-cat.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

param($app)

. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\json.ps1" # 'ConvertToPrettyJson'
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Find-Manifest' (indirectly)

if (!$app) { error '<app> missing'; my_usage; exit 1 }

Expand Down
1 change: 0 additions & 1 deletion libexec/scoop-checkup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Help: Performs a series of diagnostic tests to try to identify things that may
# cause problems with Scoop.

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\diagnostic.ps1"

$issues = 0
Expand Down
9 changes: 3 additions & 6 deletions libexec/scoop-cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
# -g, --global Cleanup a globally installed app
# -k, --cache Remove outdated download cache

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\versions.ps1"
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\versions.ps1" # 'Select-CurrentVersion'
. "$PSScriptRoot\..\lib\install.ps1" # persist related

$opt, $apps, $err = getopt $args 'gk' 'global', 'cache'
if ($err) { "scoop cleanup: $err"; exit 1 }
Expand Down
3 changes: 0 additions & 3 deletions libexec/scoop-config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@

param($name, $value)

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\help.ps1"

if (!$name) {
$scoopConfig
} elseif ($name -like '--help') {
Expand Down
8 changes: 2 additions & 6 deletions libexec/scoop-depends.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# Usage: scoop depends <app>
# Summary: List dependencies for an app

. "$PSScriptRoot\..\lib\depends.ps1"
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\decompress.ps1"
. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\depends.ps1" # 'Get-Dependency'
. "$PSScriptRoot\..\lib\manifest.ps1" # 'default_architecture'

$opt, $apps, $err = getopt $args 'a:' 'arch='
$app = $apps[0]
Expand Down
7 changes: 4 additions & 3 deletions libexec/scoop-download.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
# -u, --no-update-scoop Don't update Scoop before downloading if it's outdated
# -a, --arch <32bit|64bit> Use the specified architecture, if the app supports it

. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\json.ps1" # 'autoupdate.ps1' (indirectly)
. "$PSScriptRoot\..\lib\autoupdate.ps1" # 'generate_user_manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'default_architecture' 'generate_user_manifest' 'Find-Manifest' (indirectly)
. "$PSScriptRoot\..\lib\install.ps1"

$opt, $apps, $err = getopt $args 'fhua:' 'force', 'no-hash-check', 'no-update-scoop', 'arch='
if ($err) { error "scoop download: $err"; exit 1 }
Expand Down
6 changes: 2 additions & 4 deletions libexec/scoop-export.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
# Summary: Exports (an importable) list of installed apps
# Help: Lists all installed apps.

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\versions.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\versions.ps1" # 'Select-CurrentVersion'
. "$PSScriptRoot\..\lib\manifest.ps1" # 'default_architecture' 'Select-CurrentVersion' (indirectly)

$def_arch = default_architecture

Expand Down
4 changes: 0 additions & 4 deletions libexec/scoop-help.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
# Summary: Show help for a command
param($cmd)

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\commands.ps1"
. "$PSScriptRoot\..\lib\help.ps1"

function print_help($cmd) {
$file = Get-Content (command_path $cmd) -raw

Expand Down
5 changes: 2 additions & 3 deletions libexec/scoop-hold.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Usage: scoop hold <apps>
# Summary: Hold an app to disable updates

. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\versions.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'install_info' 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\versions.ps1" # 'Select-CurrentVersion'

$apps = $args

Expand Down
5 changes: 1 addition & 4 deletions libexec/scoop-home.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
# Summary: Opens the app homepage
param($app)

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Find-Manifest' (indirectly)

if ($app) {
$null, $manifest, $bucket, $null = Find-Manifest $app
Expand Down
6 changes: 2 additions & 4 deletions libexec/scoop-info.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
# Options:
# -v, --verbose Show full paths and URLs

. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\versions.ps1"
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Find-Manifest' (indirectly)
. "$PSScriptRoot\..\lib\versions.ps1" # 'Get-InstalledVersion'

$opt, $app, $err = getopt $args 'v' 'verbose'
if ($err) { error "scoop info: $err"; exit 1 }
Expand Down
11 changes: 5 additions & 6 deletions libexec/scoop-install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
# -s, --skip Skip hash validation (use with caution!)
# -a, --arch <32bit|64bit> Use the specified architecture, if the app supports it

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\decompress.ps1"
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\json.ps1" # 'autoupdate.ps1' (indirectly)
. "$PSScriptRoot\..\lib\autoupdate.ps1" # 'generate_user_manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'default_architecture' 'generate_user_manifest' 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\decompress.ps1"
. "$PSScriptRoot\..\lib\shortcuts.ps1"
. "$PSScriptRoot\..\lib\psmodules.ps1"
. "$PSScriptRoot\..\lib\versions.ps1"
. "$PSScriptRoot\..\lib\help.ps1"
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\depends.ps1"

$opt, $apps, $err = getopt $args 'gikusa:' 'global', 'independent', 'no-cache', 'no-update-scoop', 'skip', 'arch='
Expand Down
Loading

0 comments on commit 6296822

Please sign in to comment.