Skip to content

Commit

Permalink
perf: Explore faster import guards (#269)
Browse files Browse the repository at this point in the history
- Closes #238
- Closes #261
  • Loading branch information
Ash258 authored Jan 28, 2022
1 parent 618d09b commit af4f206
Show file tree
Hide file tree
Showing 56 changed files with 406 additions and 616 deletions.
18 changes: 18 additions & 0 deletions .vscode/powershell.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
"$1",
],
},
"New command": {
"prefix": "newCommand",
"scope": "powershell",
"body": [
"'core'$1 | ForEach-Object {",
" . (Join-Path \\$PSScriptRoot \"..\\lib\\\\\\${_}.ps1\")",
"}",
],
},
"New module": {
"prefix": "newModule",
"scope": "powershell",
"body": [
"'core'$1 | ForEach-Object {",
" . (Join-Path \\$PSScriptRoot \"\\${_}.ps1\")",
"}",
],
},
"New Import": {
"prefix": "importAllLibs",
"scope": "powershell",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

- Increase command startup
- Prevent MSI installations in NanoServer images
- Installation will not fail if `commonstartmenu` or `startmenu` system folder is not defined
- Print not supported message only if operation will be executed
Expand Down
22 changes: 11 additions & 11 deletions lib/Alias.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('help', 'scoop_help'),
@('Helpers', 'New-IssuePrompt'),
@('commands', 'Invoke-ScoopCommand'),
@('install', 'msi_installed')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedAlias__ -eq $true) {
return
} else {
Write-Verbose 'Importing Alias'
}
$__importedAlias__ = $false

'core', 'Helpers', 'help', 'commands', 'install' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

$ALIAS_CMD_ALIAS = 'alias'
Expand Down Expand Up @@ -140,3 +138,5 @@ function Get-ScoopAlias {

return $aliases.GetEnumerator() | Sort-Object Name | Format-Table -Property 'Name', 'Summary', 'Command' -AutoSize -Wrap -HideTableHeaders:(!$Verbose)
}

$__importedAlias__ = $true
23 changes: 11 additions & 12 deletions lib/Applications.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('Helpers', 'New-IssuePrompt'),
@('json', 'ConvertToPrettyJson'),
@('manifest', 'Resolve-ManifestInformation'),
@('Dependencies', 'Resolve-DependsProperty'),
@('Versions', 'Clear-InstalledVersion')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedApplications__ -eq $true) {
return
} else {
Write-Verbose 'Importing Applications'
}
$__importedApplications__ = $false

'core', 'Helpers', 'json', 'manifest', 'Dependencies', 'Versions' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

#region Application instalaltion info file
Expand Down Expand Up @@ -322,3 +319,5 @@ function Test-ResolvedObjectIsInstalled {
return $false
}
}

$__importedApplications__ = $true
23 changes: 13 additions & 10 deletions lib/Cache.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('core', 'Test-ScoopDebugEnabled')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedCache__ -eq $true) {
return
} else {
Write-Verbose 'Importing Cache'
}
$__importedCache__ = $false

'core' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

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

return New-Object PSObject -Prop @{ 'app' = $app; 'version' = $version; 'url' = $url; 'size' = $size }
return New-Object PSObject -Property @{ 'app' = $app; 'version' = $version; 'url' = $url; 'size' = $size }
}
}

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

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

$_app = @{ 'Expression' = { "$($_.app) ($($_.version))" } }
Expand All @@ -56,3 +57,5 @@ function Show-CachedFileList {
Write-Output "Total: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalSize)"
}
}

$__importedCache__ = $true
19 changes: 11 additions & 8 deletions lib/Config.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('Helpers', 'New-IssuePrompt')
) | ForEach-Object {
if (!(Get-Command $_[1] -ErrorAction 'Ignore')) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedConfig__ -eq $true) {
return
} else {
Write-Verbose 'Importing Config'
}
$__importedConfig__ = $false

'core', 'Helpers' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

$SHOVEL_CONFIG_REMOVED = @(
Expand Down Expand Up @@ -55,3 +56,5 @@ function Convert-ConfigOption {
$OldConfigOptions, $NewConfigOptions | Out-Null
}
}

$__importedConfig__ = $true
20 changes: 10 additions & 10 deletions lib/Dependencies.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('Helpers', 'New-IssuePrompt'),
@('decompress', 'Expand-7zipArchive'),
@('install', 'msi_installed')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedDependencies__ -eq $true) {
} else {
Write-Verbose 'Importing Dependencies'
}
$__importedDependencies__ = $false

'core', 'Helpers', 'manifest', 'decompress', 'install' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

function Resolve-DependsProperty {
Expand Down Expand Up @@ -286,3 +284,5 @@ function Resolve-MultipleApplicationDependency {
}
}
}

$__importedDependencies__ = $true
23 changes: 10 additions & 13 deletions lib/Diagnostic.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ Diagnostic tests.
Return $true if the test passed, otherwise $false.
Use 'Write-UserMessage -Warning' to highlight the issue, and follow up with the recommended actions to rectify.
#>
if ($__importedDiagnostic__ -eq $true) {
return
} else {
Write-Verbose 'Importing Diagnostic'
}
$__importedDiagnostic__ = $false

@(
@('core', 'Test-ScoopDebugEnabled'),
@('Helpers', 'New-IssuePrompt'),
@('Config', 'Convert-ConfigOption'),
@('buckets', 'Get-KnownBucket'),
@('decompress', 'Expand-7zipArchive'),
@('install', 'msi_installed'),
@('Git', 'Invoke-GitCmd')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
'core', 'Helpers', 'Config', 'buckets', 'decompress', 'install', 'Git' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

function Test-DiagDrive {
Expand Down Expand Up @@ -481,3 +476,5 @@ function Test-ScoopConfigFile {

return $verdict
}

$__importedDiagnostic__ = $true
19 changes: 11 additions & 8 deletions lib/Git.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('core', 'Test-ScoopDebugEnabled')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedGit__ -eq $true) {
return
} else {
Write-Verbose 'Importing Git'
}
$__importedGit__ = $false

'core' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

function Invoke-GitCmd {
Expand Down Expand Up @@ -83,3 +84,5 @@ function Invoke-GitCmd {
Invoke-SystemComSpecCommand -Windows $commandToRunWindows -Unix $commandToRunNix
}
}

$__importedGit__ = $true
9 changes: 9 additions & 0 deletions lib/Helpers.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
if ($__importedHelpers__ -eq $true) {
return
} else {
Write-Verbose 'Importing Helpers'
}
$__importedHelpers__ = $false

function Write-UserMessage {
<#
.SYNOPSIS
Expand Down Expand Up @@ -428,3 +435,5 @@ class ScoopException: System.Exception {
}
}
#endregion Exceptions

$__importedHelpers__ = $true
22 changes: 12 additions & 10 deletions lib/Installation.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('Versions', 'Clear-InstalledVersion'),
@('install', 'msi_installed'), # TODO: Refactor and eliminate
@('manifest', 'Resolve-ManifestInformation')
) | ForEach-Object {
if (!(Get-Command $_[1] -ErrorAction 'Ignore')) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedInstallation__ -eq $true) {
return
} else {
Write-Verbose 'Importing Installation'
}
$__importedInstallation__ = $false

# TODO: Refactor and eliminate install import
'core', 'Versions', 'manifest', 'install' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

function Deny-MsiIntallationOnNanoServer {
Expand Down Expand Up @@ -192,3 +192,5 @@ function Set-ScoopInfoHelperFile {
$info | ConvertToPrettyJson | Out-UTF8File -Path (Join-Path $Directory 'scoop-install.json')
}
}

$__importedInstallation__ = $true
24 changes: 13 additions & 11 deletions lib/ManifestHelpers.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('Helpers', 'New-IssuePrompt'),
@('Versions', 'Clear-InstalledVersion')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedManifestHelpers__ -eq $true) {
return
} else {
Write-Verbose 'Importing ManifestHelpers'
}
$__importedManifestHelpers__ = $false

'core', 'Helpers', 'Versions' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

#region Persistence
Expand Down Expand Up @@ -136,12 +136,12 @@ function Edit-File {
}

process {
if (!(Test-Path $File -PathType 'Leaf')) {
if (!(Test-Path -LiteralPath $File -PathType 'Leaf')) {
Write-UserMessage -Message "File '$File' does not exist" -Err
return
}

$content = Get-Content $File
$content = Get-Content -LiteralPath $File

for ($i = 0; $i -lt $Find.Count; ++$i) {
$toFind = $Find[$i]
Expand Down Expand Up @@ -225,3 +225,5 @@ function Assert-ScoopConfigValue {
}
}
#endregion Asserts

$__importedManifestHelpers__ = $true
26 changes: 14 additions & 12 deletions lib/Search.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
@(
@('core', 'Test-ScoopDebugEnabled'),
@('Helpers', 'New-IssuePrompt'),
@('buckets', 'Get-KnownBucket'),
@('install', 'msi_installed'),
@('manifest', 'Resolve-ManifestInformation')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
if ($__importedSearch__ -eq $true) {
return
} else {
Write-Verbose 'Importing Search'
}
$__importedSearch__ = $false

'core', 'Helpers', 'buckets', 'install', 'manifest' | ForEach-Object {
. (Join-Path $PSScriptRoot "${_}.ps1")
}

$_breachedRateLimit = $false
Expand All @@ -31,8 +29,10 @@ function Test-GithubApiRateLimitBreached {
if (!$script:_breachedRateLimit) {
$h = @{}
if ($null -ne $script:_token) { $h = @{ 'Headers' = @{ 'Authorization' = "token $($script:_token)" } } }
$githubRateLimit = (Invoke-RestMethod -Uri 'https://api.github.com/rate_limit' @h ).resources.core

$githubRateLimit = (Invoke-RestMethod -Uri 'https://api.github.com/rate_limit' @h).resources.core
debug $githubRateLimit.remaining

if ($githubRateLimit.remaining -eq 0) {
$script:_breachedRateLimit = $true
$limitResetOn = [System.Timezone]::CurrentTimeZone.ToLocalTime(([System.Datetime]'1/1/1970').AddSeconds($githubRateLimit.reset)).ToString()
Expand Down Expand Up @@ -225,3 +225,5 @@ function Search-LocalBucket {

end { return $result }
}

$__importedSearch__ = $true
Loading

0 comments on commit af4f206

Please sign in to comment.