Skip to content

Commit 17655b6

Browse files
refactor(PSWriteOffice): 🔧 improve assembly loading and runtime path handling
* Enhanced the logic for determining the base directory for assemblies based on the development environment. * Updated the method for discovering native runtime libraries on Windows. * Refactored the assembly loading process to streamline the retrieval of DLLs based on the framework and edition. * Improved error handling during module importation.
1 parent b80c97d commit 17655b6

File tree

1 file changed

+97
-59
lines changed

1 file changed

+97
-59
lines changed

‎PSWriteOffice.psm1‎

Lines changed: 97 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,119 @@ $BinaryModules = @(
77
"PSWriteOffice.dll"
88
)
99

10-
# Get public and private function definition files.
11-
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue -Recurse -File)
12-
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue -Recurse -File)
13-
$Classes = @( Get-ChildItem -Path $PSScriptRoot\Classes\*.ps1 -ErrorAction SilentlyContinue -Recurse -File)
14-
$Enums = @( Get-ChildItem -Path $PSScriptRoot\Enums\*.ps1 -ErrorAction SilentlyContinue -Recurse -File)
15-
# Get all assemblies
16-
$AssemblyFolders = Get-ChildItem -Path $PSScriptRoot\Lib -Directory -ErrorAction SilentlyContinue -File
10+
# Ensure native runtime libraries are discoverable on Windows
11+
if ($IsWindows) {
12+
$arch = [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture
13+
$archFolder = switch ($arch) {
14+
'X64' {
15+
'win-x64'
16+
}
17+
'X86' {
18+
'win-x86'
19+
}
20+
'Arm64' {
21+
'win-arm64'
22+
}
23+
'Arm' {
24+
'win-arm'
25+
}
26+
default {
27+
'win-x64'
28+
}
29+
}
30+
31+
if ($Development) {
32+
$baseDir = if ($PSEdition -eq 'Core') {
33+
Join-Path $DevelopmentPath $DevelopmentFolderCore
34+
} else {
35+
Join-Path $DevelopmentPath $DevelopmentFolderDefault
36+
}
37+
} else {
38+
$baseDir = if ($PSEdition -eq 'Core') {
39+
Join-Path $PSScriptRoot "Lib/$Framework"
40+
} elseif ($FrameworkNet) {
41+
Join-Path $PSScriptRoot "Lib/$FrameworkNet"
42+
} else {
43+
$null
44+
}
45+
}
46+
47+
if ($baseDir) {
48+
$runtimePath = Join-Path $baseDir "runtimes/$archFolder/native"
49+
if (Test-Path $runtimePath) {
50+
Write-Warning -Message "Adding $runtimePath to PATH"
51+
$env:PATH = "$runtimePath;" + $env:PATH
52+
}
53+
}
54+
}
1755

1856
# Lets find which libraries we need to load
19-
if ($Development) {
57+
$Default = $false
58+
$Core = $false
59+
$Standard = $false
60+
foreach ($A in $AssemblyFolders.Name) {
61+
if ($A -eq 'Default') {
62+
$Default = $true
63+
} elseif ($A -eq 'Core') {
64+
$Core = $true
65+
} elseif ($A -eq 'Standard') {
66+
$Standard = $true
67+
}
68+
}
69+
if ($Standard -and $Core -and $Default) {
70+
$FrameworkNet = 'Default'
71+
$Framework = 'Standard'
72+
} elseif ($Standard -and $Core) {
73+
$Framework = 'Standard'
74+
$FrameworkNet = 'Standard'
75+
} elseif ($Core -and $Default) {
2076
$Framework = 'Core'
2177
$FrameworkNet = 'Default'
78+
} elseif ($Standard -and $Default) {
79+
$Framework = 'Standard'
80+
$FrameworkNet = 'Default'
81+
} elseif ($Standard) {
82+
$Framework = 'Standard'
83+
$FrameworkNet = 'Standard'
84+
} elseif ($Core) {
85+
$Framework = 'Core'
86+
$FrameworkNet = ''
87+
} elseif ($Default) {
88+
$Framework = ''
89+
$FrameworkNet = 'Default'
2290
} else {
23-
$Default = $false
24-
$Core = $false
25-
$Standard = $false
26-
foreach ($A in $AssemblyFolders.Name) {
27-
if ($A -eq 'Default') {
28-
$Default = $true
29-
} elseif ($A -eq 'Core') {
30-
$Core = $true
31-
} elseif ($A -eq 'Standard') {
32-
$Standard = $true
33-
}
34-
}
35-
if ($Standard -and $Core -and $Default) {
36-
$FrameworkNet = 'Default'
37-
$Framework = 'Standard'
38-
} elseif ($Standard -and $Core) {
39-
$Framework = 'Standard'
40-
$FrameworkNet = 'Standard'
41-
} elseif ($Core -and $Default) {
42-
$Framework = 'Core'
43-
$FrameworkNet = 'Default'
44-
} elseif ($Standard -and $Default) {
45-
$Framework = 'Standard'
46-
$FrameworkNet = 'Default'
47-
} elseif ($Standard) {
48-
$Framework = 'Standard'
49-
$FrameworkNet = 'Standard'
50-
} elseif ($Core) {
51-
$Framework = 'Core'
52-
$FrameworkNet = ''
53-
} elseif ($Default) {
54-
$Framework = ''
55-
$FrameworkNet = 'Default'
56-
}
91+
#Write-Error -Message 'No assemblies found'
5792
}
5893

94+
$Assembly = @(
95+
if ($Development) {
96+
if ($PSEdition -eq 'Core') {
97+
Get-ChildItem -Path $DevelopmentPath\$DevelopmentFolderCore -Filter '*.dll' -Recurse | Where-Object { $_.FullName -notmatch '[\\/]runtimes[\\/]' }
98+
} else {
99+
Get-ChildItem -Path $DevelopmentPath\$DevelopmentFolderDefault -Filter '*.dll' -Recurse | Where-Object { $_.FullName -notmatch '[\\/]runtimes[\\/]' }
100+
}
101+
} else {
102+
if ($Framework -and $PSEdition -eq 'Core') {
103+
Get-ChildItem -Path $PSScriptRoot\Lib\$Framework -Filter '*.dll' -Recurse | Where-Object { $_.FullName -notmatch '[\\/]runtimes[\\/]' }
104+
}
105+
if ($FrameworkNet -and $PSEdition -ne 'Core') {
106+
Get-ChildItem -Path $PSScriptRoot\Lib\$FrameworkNet -Filter '*.dll' -Recurse | Where-Object { $_.FullName -notmatch '[\\/]runtime(s[\\/]' }
107+
}
108+
}
109+
)
59110

60111
$BinaryDev = @(
61112
foreach ($BinaryModule in $BinaryModules) {
62113
if ($PSEdition -eq 'Core') {
63114
$Variable = Resolve-Path "$DevelopmentPath\$DevelopmentFolderCore\$BinaryModule"
64-
$DevelopmentAssemblyFolder = Resolve-Path "$DevelopmentPath\$DevelopmentFolderCore"
65115
} else {
66116
$Variable = Resolve-Path "$DevelopmentPath\$DevelopmentFolderDefault\$BinaryModule"
67-
$DevelopmentAssemblyFolder = Resolve-Path "$DevelopmentPath\$DevelopmentFolderDefault"
68117
}
69118
$Variable
70119
Write-Warning "Development mode: Using binaries from $Variable"
71120
}
72121
)
73122

74-
if ($Development) {
75-
$Assembly = Get-ChildItem -Path "$($DevelopmentAssemblyFolder.Path)\*.dll" -ErrorAction SilentlyContinue -File
76-
} else {
77-
$Assembly = @(
78-
if ($Framework -and $PSEdition -eq 'Core') {
79-
Get-ChildItem -Path $PSScriptRoot\Lib\$Framework\*.dll -ErrorAction SilentlyContinue #-Recurse
80-
}
81-
if ($FrameworkNet -and $PSEdition -ne 'Core') {
82-
Get-ChildItem -Path $PSScriptRoot\Lib\$FrameworkNet\*.dll -ErrorAction SilentlyContinue #-Recurse
83-
}
84-
)
85-
}
86-
87123
$FoundErrors = @(
88124
if ($Development) {
89125
foreach ($BinaryModule in $BinaryDev) {
@@ -111,8 +147,9 @@ $FoundErrors = @(
111147
}
112148
foreach ($Import in @($Assembly)) {
113149
try {
114-
# Write-Warning -Message $Import.FullName
150+
Write-Verbose -Message $Import.FullName
115151
Add-Type -Path $Import.Fullname -ErrorAction Stop
152+
# }
116153
} catch [System.Reflection.ReflectionTypeLoadException] {
117154
Write-Warning "Processing $($Import.Name) Exception: $($_.Exception.Message)"
118155
$LoaderExceptions = $($_.Exception.LoaderExceptions) | Sort-Object -Unique
@@ -145,7 +182,8 @@ $FoundErrors = @(
145182
if ($FoundErrors.Count -gt 0) {
146183
$ModuleName = (Get-ChildItem $PSScriptRoot\*.psd1).BaseName
147184
Write-Warning "Importing module $ModuleName failed. Fix errors before continuing."
148-
break
185+
throw "Importing module $ModuleName failed. Fix errors before continuing."
186+
#break
149187
}
150188

151189
Export-ModuleMember -Function '*' -Alias '*' -Cmdlet '*'

0 commit comments

Comments
 (0)