Skip to content

Commit af4f206

Browse files
authored
perf: Explore faster import guards (#269)
- Closes #238 - Closes #261
1 parent 618d09b commit af4f206

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+406
-616
lines changed

.vscode/powershell.code-snippets

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@
77
"$1",
88
],
99
},
10+
"New command": {
11+
"prefix": "newCommand",
12+
"scope": "powershell",
13+
"body": [
14+
"'core'$1 | ForEach-Object {",
15+
" . (Join-Path \\$PSScriptRoot \"..\\lib\\\\\\${_}.ps1\")",
16+
"}",
17+
],
18+
},
19+
"New module": {
20+
"prefix": "newModule",
21+
"scope": "powershell",
22+
"body": [
23+
"'core'$1 | ForEach-Object {",
24+
" . (Join-Path \\$PSScriptRoot \"\\${_}.ps1\")",
25+
"}",
26+
],
27+
},
1028
"New Import": {
1129
"prefix": "importAllLibs",
1230
"scope": "powershell",

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [0.6.5](https://github.com/Ash258/Scoop-Core/milestone/5)
44

5+
- Increase command startup
56
- Prevent MSI installations in NanoServer images
67
- Installation will not fail if `commonstartmenu` or `startmenu` system folder is not defined
78
- Print not supported message only if operation will be executed

lib/Alias.ps1

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('help', 'scoop_help'),
4-
@('Helpers', 'New-IssuePrompt'),
5-
@('commands', 'Invoke-ScoopCommand'),
6-
@('install', 'msi_installed')
7-
) | ForEach-Object {
8-
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
9-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
10-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
11-
}
1+
if ($__importedAlias__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing Alias'
5+
}
6+
$__importedAlias__ = $false
7+
8+
'core', 'Helpers', 'help', 'commands', 'install' | ForEach-Object {
9+
. (Join-Path $PSScriptRoot "${_}.ps1")
1210
}
1311

1412
$ALIAS_CMD_ALIAS = 'alias'
@@ -140,3 +138,5 @@ function Get-ScoopAlias {
140138

141139
return $aliases.GetEnumerator() | Sort-Object Name | Format-Table -Property 'Name', 'Summary', 'Command' -AutoSize -Wrap -HideTableHeaders:(!$Verbose)
142140
}
141+
142+
$__importedAlias__ = $true

lib/Applications.ps1

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('Helpers', 'New-IssuePrompt'),
4-
@('json', 'ConvertToPrettyJson'),
5-
@('manifest', 'Resolve-ManifestInformation'),
6-
@('Dependencies', 'Resolve-DependsProperty'),
7-
@('Versions', 'Clear-InstalledVersion')
8-
) | ForEach-Object {
9-
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
10-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
11-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
12-
}
1+
if ($__importedApplications__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing Applications'
5+
}
6+
$__importedApplications__ = $false
7+
8+
'core', 'Helpers', 'json', 'manifest', 'Dependencies', 'Versions' | ForEach-Object {
9+
. (Join-Path $PSScriptRoot "${_}.ps1")
1310
}
1411

1512
#region Application instalaltion info file
@@ -322,3 +319,5 @@ function Test-ResolvedObjectIsInstalled {
322319
return $false
323320
}
324321
}
322+
323+
$__importedApplications__ = $true

lib/Cache.ps1

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('core', 'Test-ScoopDebugEnabled')
4-
) | ForEach-Object {
5-
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
6-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
7-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
8-
}
1+
if ($__importedCache__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing Cache'
5+
}
6+
$__importedCache__ = $false
7+
8+
'core' | ForEach-Object {
9+
. (Join-Path $PSScriptRoot "${_}.ps1")
910
}
1011

1112
function Get-CachedFileInfo {
@@ -22,7 +23,7 @@ function Get-CachedFileInfo {
2223
$app, $version, $url = $File.Name -split '#'
2324
$size = filesize $File.Length
2425

25-
return New-Object PSObject -Prop @{ 'app' = $app; 'version' = $version; 'url' = $url; 'size' = $size }
26+
return New-Object PSObject -Property @{ 'app' = $app; 'version' = $version; 'url' = $url; 'size' = $size }
2627
}
2728
}
2829

@@ -45,7 +46,7 @@ function Show-CachedFileList {
4546
$regex = $ApplicationFilter -join '|'
4647
if (!$ApplicationFilter) { $regex = '.*?' }
4748

48-
$files = Get-ChildItem -LiteralPath $SCOOP_CACHE_DIRECTORY -ErrorAction 'SilentlyContinue' -File | Where-Object -Property 'Name' -Match -Value "^($regex)#"
49+
$files = @(Get-ChildItem -LiteralPath $SCOOP_CACHE_DIRECTORY -ErrorAction 'SilentlyContinue' -File | Where-Object -Property 'Name' -Match -Value "^($regex)#")
4950
$totalSize = [double] ($files | Measure-Object -Property 'Length' -Sum).Sum
5051

5152
$_app = @{ 'Expression' = { "$($_.app) ($($_.version))" } }
@@ -56,3 +57,5 @@ function Show-CachedFileList {
5657
Write-Output "Total: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalSize)"
5758
}
5859
}
60+
61+
$__importedCache__ = $true

lib/Config.ps1

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('Helpers', 'New-IssuePrompt')
4-
) | ForEach-Object {
5-
if (!(Get-Command $_[1] -ErrorAction 'Ignore')) {
6-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
7-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
8-
}
1+
if ($__importedConfig__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing Config'
5+
}
6+
$__importedConfig__ = $false
7+
8+
'core', 'Helpers' | ForEach-Object {
9+
. (Join-Path $PSScriptRoot "${_}.ps1")
910
}
1011

1112
$SHOVEL_CONFIG_REMOVED = @(
@@ -55,3 +56,5 @@ function Convert-ConfigOption {
5556
$OldConfigOptions, $NewConfigOptions | Out-Null
5657
}
5758
}
59+
60+
$__importedConfig__ = $true

lib/Dependencies.ps1

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('Helpers', 'New-IssuePrompt'),
4-
@('decompress', 'Expand-7zipArchive'),
5-
@('install', 'msi_installed')
6-
) | ForEach-Object {
7-
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
8-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
9-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
10-
}
1+
if ($__importedDependencies__ -eq $true) {
2+
} else {
3+
Write-Verbose 'Importing Dependencies'
4+
}
5+
$__importedDependencies__ = $false
6+
7+
'core', 'Helpers', 'manifest', 'decompress', 'install' | ForEach-Object {
8+
. (Join-Path $PSScriptRoot "${_}.ps1")
119
}
1210

1311
function Resolve-DependsProperty {
@@ -286,3 +284,5 @@ function Resolve-MultipleApplicationDependency {
286284
}
287285
}
288286
}
287+
288+
$__importedDependencies__ = $true

lib/Diagnostic.ps1

+10-13
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@ Diagnostic tests.
33
Return $true if the test passed, otherwise $false.
44
Use 'Write-UserMessage -Warning' to highlight the issue, and follow up with the recommended actions to rectify.
55
#>
6+
if ($__importedDiagnostic__ -eq $true) {
7+
return
8+
} else {
9+
Write-Verbose 'Importing Diagnostic'
10+
}
11+
$__importedDiagnostic__ = $false
612

7-
@(
8-
@('core', 'Test-ScoopDebugEnabled'),
9-
@('Helpers', 'New-IssuePrompt'),
10-
@('Config', 'Convert-ConfigOption'),
11-
@('buckets', 'Get-KnownBucket'),
12-
@('decompress', 'Expand-7zipArchive'),
13-
@('install', 'msi_installed'),
14-
@('Git', 'Invoke-GitCmd')
15-
) | ForEach-Object {
16-
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
17-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
18-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
19-
}
13+
'core', 'Helpers', 'Config', 'buckets', 'decompress', 'install', 'Git' | ForEach-Object {
14+
. (Join-Path $PSScriptRoot "${_}.ps1")
2015
}
2116

2217
function Test-DiagDrive {
@@ -481,3 +476,5 @@ function Test-ScoopConfigFile {
481476

482477
return $verdict
483478
}
479+
480+
$__importedDiagnostic__ = $true

lib/Git.ps1

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('core', 'Test-ScoopDebugEnabled')
4-
) | ForEach-Object {
5-
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
6-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
7-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
8-
}
1+
if ($__importedGit__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing Git'
5+
}
6+
$__importedGit__ = $false
7+
8+
'core' | ForEach-Object {
9+
. (Join-Path $PSScriptRoot "${_}.ps1")
910
}
1011

1112
function Invoke-GitCmd {
@@ -83,3 +84,5 @@ function Invoke-GitCmd {
8384
Invoke-SystemComSpecCommand -Windows $commandToRunWindows -Unix $commandToRunNix
8485
}
8586
}
87+
88+
$__importedGit__ = $true

lib/Helpers.ps1

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
if ($__importedHelpers__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing Helpers'
5+
}
6+
$__importedHelpers__ = $false
7+
18
function Write-UserMessage {
29
<#
310
.SYNOPSIS
@@ -428,3 +435,5 @@ class ScoopException: System.Exception {
428435
}
429436
}
430437
#endregion Exceptions
438+
439+
$__importedHelpers__ = $true

lib/Installation.ps1

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('Versions', 'Clear-InstalledVersion'),
4-
@('install', 'msi_installed'), # TODO: Refactor and eliminate
5-
@('manifest', 'Resolve-ManifestInformation')
6-
) | ForEach-Object {
7-
if (!(Get-Command $_[1] -ErrorAction 'Ignore')) {
8-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
9-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
10-
}
1+
if ($__importedInstallation__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing Installation'
5+
}
6+
$__importedInstallation__ = $false
7+
8+
# TODO: Refactor and eliminate install import
9+
'core', 'Versions', 'manifest', 'install' | ForEach-Object {
10+
. (Join-Path $PSScriptRoot "${_}.ps1")
1111
}
1212

1313
function Deny-MsiIntallationOnNanoServer {
@@ -192,3 +192,5 @@ function Set-ScoopInfoHelperFile {
192192
$info | ConvertToPrettyJson | Out-UTF8File -Path (Join-Path $Directory 'scoop-install.json')
193193
}
194194
}
195+
196+
$__importedInstallation__ = $true

lib/ManifestHelpers.ps1

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('Helpers', 'New-IssuePrompt'),
4-
@('Versions', 'Clear-InstalledVersion')
5-
) | ForEach-Object {
6-
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
7-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
8-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
9-
}
1+
if ($__importedManifestHelpers__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing ManifestHelpers'
5+
}
6+
$__importedManifestHelpers__ = $false
7+
8+
'core', 'Helpers', 'Versions' | ForEach-Object {
9+
. (Join-Path $PSScriptRoot "${_}.ps1")
1010
}
1111

1212
#region Persistence
@@ -136,12 +136,12 @@ function Edit-File {
136136
}
137137

138138
process {
139-
if (!(Test-Path $File -PathType 'Leaf')) {
139+
if (!(Test-Path -LiteralPath $File -PathType 'Leaf')) {
140140
Write-UserMessage -Message "File '$File' does not exist" -Err
141141
return
142142
}
143143

144-
$content = Get-Content $File
144+
$content = Get-Content -LiteralPath $File
145145

146146
for ($i = 0; $i -lt $Find.Count; ++$i) {
147147
$toFind = $Find[$i]
@@ -225,3 +225,5 @@ function Assert-ScoopConfigValue {
225225
}
226226
}
227227
#endregion Asserts
228+
229+
$__importedManifestHelpers__ = $true

lib/Search.ps1

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
@(
2-
@('core', 'Test-ScoopDebugEnabled'),
3-
@('Helpers', 'New-IssuePrompt'),
4-
@('buckets', 'Get-KnownBucket'),
5-
@('install', 'msi_installed'),
6-
@('manifest', 'Resolve-ManifestInformation')
7-
) | ForEach-Object {
8-
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
9-
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
10-
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
11-
}
1+
if ($__importedSearch__ -eq $true) {
2+
return
3+
} else {
4+
Write-Verbose 'Importing Search'
5+
}
6+
$__importedSearch__ = $false
7+
8+
'core', 'Helpers', 'buckets', 'install', 'manifest' | ForEach-Object {
9+
. (Join-Path $PSScriptRoot "${_}.ps1")
1210
}
1311

1412
$_breachedRateLimit = $false
@@ -31,8 +29,10 @@ function Test-GithubApiRateLimitBreached {
3129
if (!$script:_breachedRateLimit) {
3230
$h = @{}
3331
if ($null -ne $script:_token) { $h = @{ 'Headers' = @{ 'Authorization' = "token $($script:_token)" } } }
34-
$githubRateLimit = (Invoke-RestMethod -Uri 'https://api.github.com/rate_limit' @h ).resources.core
32+
33+
$githubRateLimit = (Invoke-RestMethod -Uri 'https://api.github.com/rate_limit' @h).resources.core
3534
debug $githubRateLimit.remaining
35+
3636
if ($githubRateLimit.remaining -eq 0) {
3737
$script:_breachedRateLimit = $true
3838
$limitResetOn = [System.Timezone]::CurrentTimeZone.ToLocalTime(([System.Datetime]'1/1/1970').AddSeconds($githubRateLimit.reset)).ToString()
@@ -225,3 +225,5 @@ function Search-LocalBucket {
225225

226226
end { return $result }
227227
}
228+
229+
$__importedSearch__ = $true

0 commit comments

Comments
 (0)