Skip to content

Commit

Permalink
perf: Lazy load themes to optimize module import time.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmims committed Apr 23, 2018
1 parent 9ae0497 commit d44b2b0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion PSConsoleTheme/PSConsoleTheme.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ $manifest = Test-ModuleManifest (Join-Path $PSScriptRoot 'PSConsoleTheme.psd1')
$Script:PSConsoleTheme = @{}
$PSConsoleTheme.ProfilePath = Join-Path $env:USERPROFILE '.psconsoletheme'
$PSConsoleTheme.Version = $manifest.Version
$PSConsoleTheme.Themes = Get-Theme
$PSConsoleTheme.Themes = @{}
$PSConsoleTheme.ThemesLoaded = $false

# Import user configuration
$PSConsoleTheme.User = Import-UserConfiguration
Expand Down
4 changes: 2 additions & 2 deletions PSConsoleTheme/Private/Get-Theme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function Get-Theme {
foreach ($path in $ThemePath) {
$configFiles = Get-ChildItem $path "*.json"

$processed = 0
foreach ($config in $configFiles) {
try
{
Expand All @@ -25,14 +24,15 @@ function Get-Theme {
Write-Warning ($theme_msgs.warning_ambiguous_theme -f $theme.name, $config.FullName)
break
}
$theme | Add-Member path $config.FullName
$themes.Add($theme.name, $theme)
$processed++
}
} catch {
Write-Warning $_
}
}
}
$Script:PSConsoleTheme.ThemesLoaded = $true

$themes
}
Expand Down
23 changes: 20 additions & 3 deletions PSConsoleTheme/Private/Import-UserConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,27 @@ function Import-UserConfiguration {

try {
$config = $configJson | Remove-JsonComments | ConvertFrom-Json
if($config | Test-User) {
if ($config.Theme -and $Script:PSConsoleTheme.Themes.Contains($config.Theme)) {
Set-TokenColorConfiguration $Script:PSConsoleTheme.Themes[$config.Theme]
if ($config | Test-User) {
if ($config.Path) {
try {
$theme = Import-ThemeConfiguration $config.Path -ErrorAction Stop
$theme | Add-Member path $config.Path
$Script:PSConsoleTheme.Themes.Add($theme.name, $theme)
} catch {
Write-Warning $_
}
} else {
$config | Add-Member Path $null
if ($config.Theme) {
if (!$Script:PSConsoleTheme.ThemesLoaded) {
$Script:PSConsoleTheme.Themes = Get-Theme
}
if ($Script:PSConsoleTheme.Themes.Contains($config.Theme)) {
Set-TokenColorConfiguration $Script:PSConsoleTheme.Themes[$config.Theme]
}
}
}

return $config
}
}
Expand Down
4 changes: 4 additions & 0 deletions PSConsoleTheme/Public/Get-ConsoleTheme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function Get-ConsoleTheme {
)

DynamicParam {
if (!$PSConsoleTheme.ThemesLoaded) {
$PSConsoleTheme.Themes = Get-Theme
}

if ($PSConsoleTheme.Themes.Count -gt 0) {
$parameterName = 'Name'

Expand Down
8 changes: 6 additions & 2 deletions PSConsoleTheme/Public/Set-ConsoleTheme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function Set-ConsoleTheme {
)

DynamicParam {
if (!$PSConsoleTheme.ThemesLoaded) {
$PSConsoleTheme.Themes = Get-Theme
}

if ($PSConsoleTheme.Themes.Count -gt 0) {
$parameterName = 'Name'

Expand Down Expand Up @@ -41,13 +45,13 @@ function Set-ConsoleTheme {
$Name = $PSBoundParameters['Name']

if ($Name) {
$Script:PSConsoleTheme.User.Theme = $Name
$theme = $PSConsoleTheme.Themes[$Name]
$Script:PSConsoleTheme.User.Theme = $Name
$Script:PSConsoleTheme.User.Path = $theme.path
try {
if (($theme | Test-Theme) -and ($theme.palette | Test-Palette)) {
Set-ColorPalette $theme
Set-TokenColorConfiguration $theme
Export-UserConfiguration
}
}
catch {
Expand Down

0 comments on commit d44b2b0

Please sign in to comment.