Trex.psm1
|
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingEmptyCatchBlock', '', Justification = 'WaitOne throws AbandonedMutexException when it acquires a mutex whose last owner exited without releasing it; the lock is then held and the import proceeds.')] param() $pwrsImportLock = New-Object System.Threading.Mutex($false, ('Local\pwrs-import-' + $PID)) try { $null = $pwrsImportLock.WaitOne() } catch [System.Threading.AbandonedMutexException] { } try { $root = $PSScriptRoot $tfm = if ($PSVersionTable.PSEdition -eq 'Core') { 'net10.0' } else { 'netstandard2.0' } if (-not ('Pwrs.Bootstrap.Loader' -as [type])) { $stage = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'pwrs-load', $PID) $null = [System.IO.Directory]::CreateDirectory($stage) $boot = [System.IO.Path]::Combine($stage, 'Pwrs.Bootstrap.dll') [System.IO.File]::Copy([System.IO.Path]::Combine($root, $tfm, 'Pwrs.Bootstrap.dll'), $boot, $true) $null = [System.Reflection.Assembly]::LoadFrom($boot) } try { $shell = [Pwrs.Bootstrap.Loader]::Load($root, 'Trex', $tfm) } catch { if ($tfm -eq 'net10.0' -and [Environment]::Version.Major -lt 8) { throw ('Trex' + ' needs PowerShell 7.4 or later: its PowerShell 7 half is built against .NET 8, and this is PowerShell ' + $PSVersionTable.PSVersion + ' on .NET ' + [Environment]::Version + '.') } throw } $shell.GetType('Pwrs.Modules.Trex.PwrsModuleRoot', $true).GetField('Value').SetValue($null, $root) foreach ($m in Get-Module -All) { if ($m.ModuleType -eq 'Binary' -and ($m.Path -eq $shell.Location -or $m.Name -like '*Trex.Shell*')) { Remove-Module -ModuleInfo $m -Force -ErrorAction SilentlyContinue } } try { Import-Module -Assembly $shell -Force $reload = $shell.GetTypes() | Where-Object { $_.Name -eq 'PwrsModule' } | Select-Object -First 1 if ($null -ne $reload) { $null = $reload::ReloadNative() } } catch { $inner = $_.Exception while ($null -ne $inner.InnerException) { $inner = $inner.InnerException } throw $inner } Export-ModuleMember -Cmdlet 'New-TrexPattern', 'Select-TrexMatch', 'Test-TrexMatch', 'Get-TrexFile', 'Get-TrexFileType', 'Get-TrexLine', 'New-TrexStreamScanner', 'Edit-TrexText', 'Protect-TrexText', 'Group-TrexMatch', 'Get-TrexToken', 'Get-TrexRecord', 'Get-TrexRecordShape', 'ConvertTo-TrexPattern', 'ConvertFrom-TrexText', 'ConvertTo-TrexLiteral', 'Find-TrexRecord', 'New-TrexIndex', 'Get-TrexIndex', 'Get-TrexRule', 'Invoke-TrexRule', 'ConvertTo-TrexSarif', 'Measure-TrexMagnitude', 'Measure-TrexStress', 'Measure-TrexFlow', 'Measure-TrexObservation', 'Measure-TrexEcho', 'Measure-TrexOrbit', 'Measure-TrexShape', 'Measure-TrexRelation', 'Measure-TrexSpectral', 'Measure-TrexSeam', 'New-TrexGrammar', 'Invoke-TrexGrammar', 'New-TrexBpe', 'Import-TrexBpe', 'Export-TrexBpe', 'ConvertTo-TrexBpe', 'New-TrexPrefilter', 'Test-TrexPrefilter', 'Register-TrexAtom', 'Import-TrexAtom', 'Get-TrexAtom', 'Unregister-TrexAtom', 'Test-TrexAtom', 'New-TrexLibrary', 'Get-TrexClock', 'Set-TrexClock', 'Get-TrexInfo' -Alias 'New-TxPattern', 'Select-TxMatch', 'Test-TxMatch', 'Get-TxFile', 'Get-TxFileType', 'Get-TxLine', 'New-TxStreamScanner', 'Edit-TxText', 'Protect-TxText', 'Group-TxMatch', 'Get-TxToken', 'Get-TxRecord', 'Get-TxRecordShape', 'ConvertTo-TxPattern', 'ConvertFrom-TxText', 'ConvertTo-TxLiteral', 'Find-TxRecord', 'New-TxIndex', 'Get-TxIndex', 'Get-TxRule', 'Invoke-TxRule', 'ConvertTo-TxSarif', 'Measure-TxMagnitude', 'Measure-TxStress', 'Measure-TxFlow', 'Measure-TxObservation', 'Measure-TxEcho', 'Measure-TxOrbit', 'Measure-TxShape', 'Measure-TxRelation', 'Measure-TxSpectral', 'Measure-TxSeam', 'New-TxGrammar', 'Invoke-TxGrammar', 'New-TxBpe', 'Import-TxBpe', 'Export-TxBpe', 'ConvertTo-TxBpe', 'New-TxPrefilter', 'Test-TxPrefilter', 'Register-TxAtom', 'Import-TxAtom', 'Get-TxAtom', 'Unregister-TxAtom', 'Test-TxAtom', 'New-TxLibrary', 'Get-TxClock', 'Set-TxClock', 'Get-TxInfo' } finally { $pwrsImportLock.ReleaseMutex() $pwrsImportLock.Dispose() } |