Skip to content

Commit

Permalink
final table edit
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Dec 7, 2023
1 parent 1810751 commit 734fa06
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 66 deletions.
11 changes: 4 additions & 7 deletions Pwsh/Modules/TestWhetherVariablesAreExported.short.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ function ModVar.GetVar {
param(
[ArgumentCompletions('Script', 'Global', 0, 1, 2)]
[string]$Scope )
Get-Variable mod* | ft -AutoSize | write-host
Get-Variable mod* | ft -AutoSize | out-string | write-host
}
function Render.Array {
param( [string]$Name )
process {
$_ | Join-String -f "`n {0}" -op "$Name = [" -os "`n]"
}
}
function ModVar.ListImport { process {
function ModVar.ListImport {
#
process {
$Mod = $_
,@($mod.ExportedVariables.Keys) | Render.Array 'Export Variablesname'
,@($mod.ExportedCommands.Keys) | Render.Array 'Export Commands'
Expand All @@ -31,8 +33,3 @@ function ModVar.ListImport { process {
Module.OnLoad.BeforeExport
Export-ModuleMember -Function @('ModVar.*') -Variable @('ModVar_*')
Module.OnLoad.AfterExport

# 'ModVar_Exists', 'ModVar_Null', 'ModVar_OnLoad_AfterExport', 'ModVar_OnLoad_BeforeExport', 'ModVar_DidStuff'
# | Join-String -sep "`n" -SingleQuote -op 'To Look For: [ ' -os "`n ]"

# [1] Import normally, check whether Before and After vars exported
133 changes: 74 additions & 59 deletions Pwsh/Modules/TestWhetherVariablesExport-Summary.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@

This is a Summary of the Results of this test: [TestWhetherVariablesAreExported.psm1](./TestWhetherVariablesAreExported.psm1)

## About:

This answers:

[1] Does the location of `Export-ModuleMember` matter when exporting variables ? Yes.
[2] Do variables created after the import, get exported, assuming they match the filters? They do not.

These results are from this test: [TestWhetherVariablesAreExported.short.psm1](./TestWhetherVariablesAreExported.short.psm1)

- [About:](#about)
- [What exports on Import?](#what-exports-on-import)
- [What exports on Import?](#what-exports-on-import-1)
- [Phase 1: On load, before `Export-ModuleMember`](#phase-1-on-load-before-export-modulemember)
- [Phase 2: On load, after `Export-ModuleMember`](#phase-2-on-load-after-export-modulemember)
- [Phase 3: After load, user invokes `DoStuff()`](#phase-3-after-load-user-invokes-dostuff)

![screenshot of test](./img/MyInvocationInfo-AsScript.1.png)

```ps1
$script:ModVar_Exists = @{ Name = 'alice' }
$script:ModVar_Null = $null
function Module.OnLoad {
$script:ModVar_OnLoad = @{ name = 'bob' }
function Module.OnLoad.AfterExport {
$script:ModVar_OnLoad_AfterExport = @{ name = 'kelly' }
}
function Module.OnLoad.BeforeExport {
$script:ModVar_OnLoad_BeforeExport = @{ name = 'bob' }
}
function DoStuff {
function ModVar.DoStuff {
$script:ModVar_DidStuff = @{ name = 'herbert' }
}
Module.OnLoad
Module.OnLoad.BeforeExport
Export-ModuleMember -Function @('ModVar.*') -Variable @('ModVar_*')
Module.OnLoad.AfterExport
```


## What exports on Import?

What exactly is exported if you were to [1] import, and then [2] call `DoStuff` to add a new variable ?
I am testing three phases

1. On load, before `Export-ModuleMember`
2. On load, after `Export-ModuleMember`
3. After load, user invokes `DoStuff()` themselves

```ps1
# [1] State from the initial import
Import-Module ./TestWhetherVariablesAreExported.psm1 -Force -PassThru
Expand All @@ -28,72 +55,60 @@ Get-Variable
ModVar.DoStuff
Get-Variable
```
```ps1
```


## What exports on Import?

function ModVar.GetVar {
param( [string]$Scope )
# Get-Variable 'ModVar*' -Scope Script
$splat = @{
Name = 'ModVar*'
}
# hr -fg 'orange'
# h1 '=> modvar'
if($Scope) { $Splat.Scope = $Scope }
$query = Get-Variable @splat
$query | %{
$_.Name | Msg
$_.Value | ConvertTo-Json -depth 4 | Join-String -sep "`n" | Msg
}
I am testing three phases

(Get-Variable 'ModVar*').count | Join-string -op 'Found N modVars: ' | Msg
}
1. On load, before `Export-ModuleMember`
2. On load, after `Export-ModuleMember`
3. After load, user invokes `DoStuff()` themselves

function Module.OnLoad {
' ==> Module.Init' | Msg
$ref = $script:ModVar_AlwaysExists
$ref.Name = 'Alice'
$ref.StartTime = [Datetime]::Now
}
function ModVar.DoStuff {
'=> enter ' | Msg
ModVar.GetVar -Scope script
ModVar.GetVar -Scope global
```ps1
# [1] State from the initial import
Import-Module ./TestWhetherVariablesAreExported.short.psm1 -Force -PassThru
Get-Variable
$ref = $script:ModVar_AlwaysExists
$ref.StuffTime = [Datetime]::now
# [2] Test if new variables are exported?
ModVar.DoStuff
Get-Variable
```

$script:ModVar_DidStuff = @{ id = 100 }

'=> exit ' | Msg
ModVar.GetVar -Scope script
ModVar.GetVar -Scope global
}
### Phase 1: On load, before `Export-ModuleMember`

| Name | Exists |
| ------------------- | ------ |
| Exists | True |
| Null | True |
| OnLoad_BeforeExport | True |
| OnLoad_AfterExport | False |
| DidStuff | False |

'=> before Init' | Msg
ModVar.GetVar -Scope Script
ModVar.GetVar -Scope global

Module.OnLoad
### Phase 2: On load, after `Export-ModuleMember`

'=> after Init' | Msg
ModVar.GetVar -Scope Script
ModVar.GetVar -Scope global
| Name | Exists |
| ------------------- | ------ |
| Exists | True |
| Null | True |
| OnLoad_BeforeExport | True |
| OnLoad_AfterExport | False |
| DidStuff | False |


### Phase 3: After load, user invokes `DoStuff()`

| Name | Exists |
| ------------------- | ------ |
| Exists | True |
| Null | True |
| OnLoad_BeforeExport | True |
| OnLoad_AfterExport | False |
| DidStuff | False |

Export-ModuleMember -Function @('ModVar.*') -Variable @('ModVar*') -Alias @('ModVar.*')
@'
Try:
> Get-Command ModVar.* | Ft -auto
> Get-Variable 'ModVar*'
'@
Gcm 'ModVar.*' | ft -auto | out-string

(Get-Variable 'ModVar*').count | Join-string -op 'Found N modVars: '
```
All three are the same.
That's not what I expected.
I thought `OnLoad_AfterExport` would have to be exported, and `DidStuff` would not.

0 comments on commit 734fa06

Please sign in to comment.