11$moduleParent = Split-Path - Parent (Split-Path - Parent $PSScriptRoot )
22$psutilsRoot = Join-Path $moduleParent ' psutils'
33$moduleManifest = Join-Path $psutilsRoot ' psutils.psd1'
4+ $modulesDir = Join-Path $psutilsRoot ' modules'
45
5- try {
6- Import-Module $moduleManifest - ErrorAction Stop
7- }
8- catch {
9- Write-Error " [profile/core/loadModule.ps1] Import-Module 失败: $moduleManifest :: $ ( $_.Exception.Message ) "
10- throw
6+ # ── 第 1 层:同步加载核心子模块(仅 profile 启动路径必需的 6 个) ──
7+ # 使用 Import-Module 逐个加载(.psm1 不支持 dot-source,必须走模块系统)
8+ # 加载顺序按依赖关系:os → cache(依赖 os) → test → env → proxy → wrapper
9+ $coreModules = @ (' os' , ' cache' , ' test' , ' env' , ' proxy' , ' wrapper' )
10+ foreach ($mod in $coreModules ) {
11+ $modPath = Join-Path $modulesDir " $mod .psm1"
12+ try {
13+ Import-Module $modPath - Global - ErrorAction Stop
14+ }
15+ catch {
16+ Write-Error " [profile/core/loadModule.ps1] Import-Module 失败: $modPath :: $ ( $_.Exception.Message ) "
17+ throw
18+ }
1119}
1220
13- # PSModulePath 去重(不追加额外路径,仅清理重复条目)
21+ # ── 第 2 层:PSModulePath 兜底(确保 PowerShell 自动发现 psutils.psd1) ──
22+ # 需要将 psutils 的父目录加入 PSModulePath(PowerShell 按 ModuleName/ModuleName.psd1 结构查找)
23+ $psutilsParent = Split-Path - Parent $psutilsRoot
1424$sep = [System.IO.Path ]::PathSeparator
15- $paths = ($env: PSModulePath -split [string ]$sep ) | Where-Object { $_ }
16-
1725$pathComparer = if ($IsWindows -or $env: OS -eq ' Windows_NT' ) {
1826 [System.StringComparer ]::OrdinalIgnoreCase
1927}
2028else {
2129 [System.StringComparer ]::Ordinal
2230}
2331
32+ # 将 psutils 父目录追加到 PSModulePath(仅在尚未存在时)
33+ $currentPaths = ($env: PSModulePath -split [string ]$sep ) | Where-Object { $_ }
34+ $alreadyInPath = $false
35+ foreach ($p in $currentPaths ) {
36+ if ($pathComparer.Equals ($p , $psutilsParent )) {
37+ $alreadyInPath = $true
38+ break
39+ }
40+ }
41+ if (-not $alreadyInPath ) {
42+ $env: PSModulePath = $env: PSModulePath + $sep + $psutilsParent
43+ }
44+
45+ # PSModulePath 去重(清理重复条目)
46+ $paths = ($env: PSModulePath -split [string ]$sep ) | Where-Object { $_ }
2447$seenPaths = [System.Collections.Generic.HashSet [string ]]::new($pathComparer )
2548$uniquePaths = [System.Collections.Generic.List [string ]]::new()
2649
@@ -32,3 +55,26 @@ foreach ($path in $paths) {
3255}
3356
3457$env: PSModulePath = ($uniquePaths.ToArray ()) -join $sep
58+
59+ # ── 第 3 层:OnIdle 事件延迟全量加载(空闲时静默加载完整 psutils 模块) ──
60+ # 将 wrapper.ps1 的加载也合并到此处
61+ $script :__PsutilsManifestPath = $moduleManifest
62+ $script :__WrapperScriptPath = Join-Path (Split-Path - Parent $PSScriptRoot ) ' wrapper.ps1'
63+ Register-EngineEvent - SourceIdentifier PowerShell.OnIdle - MaxTriggerCount 1 - Action {
64+ try {
65+ # 全量加载 psutils 模块(覆盖单独加载的子模块,补全其余子模块)
66+ Import-Module $script :__PsutilsManifestPath - Force - Global - ErrorAction Stop
67+ }
68+ catch {
69+ Write-Warning " [profile/loadModule.ps1] OnIdle psutils 全量加载失败: $ ( $_.Exception.Message ) "
70+ }
71+ try {
72+ # 延迟加载 wrapper.ps1(yaz, Add-CondaEnv 等函数)
73+ if (Test-Path $script :__WrapperScriptPath ) {
74+ . $script :__WrapperScriptPath
75+ }
76+ }
77+ catch {
78+ Write-Warning " [profile/loadModule.ps1] OnIdle wrapper.ps1 加载失败: $ ( $_.Exception.Message ) "
79+ }
80+ } | Out-Null
0 commit comments