Skip to content

Commit 051c336

Browse files
Merge pull request #2 from SeeminglyScience/add-powershell-core-support
Add support for PowerShell Core beta
2 parents 9dc2edd + acab246 commit 051c336

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

ImpliedReflection.build.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#requires -Module InvokeBuild, PSScriptAnalyzer, Pester, PlatyPS -Version 5.1
1+
#requires -Module @{ModuleName = 'Pester'; RequiredVersion = '3.4.3'}, InvokeBuild, PSScriptAnalyzer, PlatyPS -Version 5.1
22
[CmdletBinding()]
33
param()
44

@@ -58,7 +58,8 @@ task Test -If { $script:Discovery.HasTests -and $script:Settings.ShouldTest } {
5858
$pesterCC = "$PSScriptRoot\module\*\*.ps1", "$PSScriptRoot\module\*.psm1"
5959
Start-Job {
6060
Set-Location $using:projectRoot
61-
Invoke-Pester -PesterOption @{ IncludeVSCodeMarker = $true } -CodeCoverage $using:pesterCC
61+
62+
Invoke-Pester -CodeCoverage $using:pesterCC -PesterOption @{ IncludeVSCodeMarker = $true }
6263
} | Receive-Job -Wait -AutoRemoveJob
6364
}
6465

module/ImpliedReflection.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'ImpliedReflection.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.1.1'
15+
ModuleVersion = '0.1.2'
1616

1717
# ID used to uniquely identify this module
1818
GUID = '8834a5bf-9bf2-4f09-8415-3c1e561109f6'
@@ -80,7 +80,7 @@ PrivateData = @{
8080
# IconUri = ''
8181

8282
# ReleaseNotes of this module
83-
ReleaseNotes = '- Fixed the module not loading.'
83+
ReleaseNotes = '- Added support for PowerShell 6.0 beta 4.'
8484

8585
} # End of PSData hashtable
8686

module/ImpliedReflection.psm1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
Import-LocalizedData -BindingVariable Strings -FileName Strings -ErrorAction Ignore
22

3+
# In PowerShell 6 a lot of field names are changed to fit .NET standards. This is why you should
4+
# avoid using reflection :)
5+
$instancePrefix = ''
6+
if ($PSVersionTable.PSVersion.Major -ge 6) {
7+
$instancePrefix = '_'
8+
}
9+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '',
10+
Justification='Module scope variable used in other files.')]
11+
$FIELD_REFERENCE = @{
12+
adapter = "${instancePrefix}adapter"
13+
indexes = "${instancePrefix}indexes"
14+
# These haven't been changed yet, but probably will soon. Keeping it here so it's easy to fix.
15+
dotNetStaticAdapter = 'dotNetStaticAdapter'
16+
readOnly = 'readOnly'
17+
writeOnly = 'writeOnly'
18+
}
19+
320
# Include all function files.
421
Get-ChildItem $PSScriptRoot\Public\*.ps1 | ForEach-Object {
522
. $PSItem.FullName

module/Public/Add-PrivateMember.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ function Add-PrivateMember {
3636
function NewPropertyCacheEntry([MemberInfo] $property) {
3737
$cacheEntry = [ref].Assembly.
3838
GetType('System.Management.Automation.DotNetAdapter+PropertyCacheEntry').
39-
GetConstructor(60, $null, @($property.GetType()), 1).
39+
GetConstructor($FLAG_MAP.Instance, $null, @($property.GetType()), 1).
4040
Invoke($property)
4141

4242
# The cache entry uses these fields to determine if the properties for the
4343
# Getter/Setter delegates should create the delegate. They are set to false during
4444
# construction if the method isn't public.
45-
$cacheEntry.GetType().GetField('writeOnly', 60).SetValue($cacheEntry, $false)
45+
$cacheEntry.GetType().GetField($script:FIELD_REFERENCE.writeOnly, 60).SetValue($cacheEntry, $false)
4646
$isWritable = $property.SetMethod -or
4747
('Field' -eq $property.MemberType -and
4848
-not $property.IsInitOnly)
4949

5050
if ($isWritable) {
51-
$cacheEntry.GetType().GetField('readOnly', 60).SetValue($cacheEntry, $false)
51+
$cacheEntry.GetType().GetField($script:FIELD_REFERENCE.readOnly, 60).SetValue($cacheEntry, $false)
5252
}
5353
return $cacheEntry
5454
}
@@ -128,7 +128,7 @@ function Add-PrivateMember {
128128
Type = $target.psobject.BaseObject
129129
Flags = $FLAG_MAP.Static
130130
Adapter = [psobject].
131-
GetField('dotNetStaticAdapter', $FLAG_MAP.Static).
131+
GetField($script:FIELD_REFERENCE.dotNetStaticAdapter, $FLAG_MAP.Static).
132132
GetValue($null)
133133
}
134134
}
@@ -137,7 +137,7 @@ function Add-PrivateMember {
137137
Type = $target.GetType()
138138
Flags = $FLAG_MAP.Instance
139139
Adapter = [PSMethod].
140-
GetField('adapter', $FLAG_MAP.Instance).
140+
GetField($script:FIELD_REFERENCE.adapter, $FLAG_MAP.Instance).
141141
GetValue($target.psobject.Methods.Item('GetType'))
142142
}
143143
}
@@ -173,7 +173,7 @@ function Add-PrivateMember {
173173

174174
$alreadyExists = $table.
175175
GetType().
176-
GetField('indexes', $FLAG_MAP.Instance).
176+
GetField($script:FIELD_REFERENCE.indexes, $FLAG_MAP.Instance).
177177
GetValue($table).
178178
ContainsKey($memberName)
179179

module/Public/Disable-ImpliedReflection.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ function Disable-ImpliedReflection {
1616
[ErrorCategory]::InvalidOperation,
1717
$null))
1818
}
19+
$fieldPrefix = ''
20+
if ($PSVersionTable.PSVersion.Major -ge 6) {
21+
$fieldPrefix = 's_'
22+
}
1923
foreach ($visibilityScope in 'static', 'instance') {
2024
foreach ($memberType in 'Property', 'Method') {
2125
[ref].Assembly.
2226
GetType('System.Management.Automation.DotNetAdapter').
23-
GetField("${visibilityScope}${memberType}CacheTable", [BindingFlags]'Static, NonPublic').
27+
GetField("${fieldPrefix}${visibilityScope}${memberType}CacheTable", [BindingFlags]'Static, NonPublic').
2428
GetValue($null).
2529
Clear()
2630
}

test/Add-PrivateMember.Tests.ps1

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ Describe 'Add-PrivateMember operation' {
3737
$ExecutionContext._context._formatDBManager | Should Be $null
3838
}
3939
It 'can chain with return property parameter' {
40+
# More examples of why not to use reflection.
41+
$moduleTable = '_moduleTable'
42+
if ($PSVersionTable.PSVersion.Major -ge 6) {
43+
$moduleTable = 'ModuleTable'
44+
}
4045
$ExecutionContext |
4146
Add-PrivateMember _context |
4247
Add-PrivateMember Modules |
43-
Add-PrivateMember _moduleTable |
48+
Add-PrivateMember $moduleTable |
4449
Should BeOfType 'System.Collections.Generic.Dictionary[string,psmoduleinfo]'
4550
}
4651
It 'returns the object with PassThru' {
@@ -57,13 +62,22 @@ Describe 'Add-PrivateMember operation' {
5762
[scriptblock]::EmptyScriptBlock | Should BeOfType scriptblock
5863
}
5964
It 'can access fields' {
60-
[scriptblock]::_cachedScripts.GetType().Name | Should Be 'ConcurrentDictionary`2'
65+
if ($PSVersionTable.PSVersion.Major -ge 6) {
66+
[scriptblock]::s_cachedScripts.GetType().Name | Should Be 'ConcurrentDictionary`2'
67+
} else {
68+
[scriptblock]::_cachedScripts.GetType().Name | Should Be 'ConcurrentDictionary`2'
69+
}
6170
}
6271
It 'can access methods' {
6372
[scriptblock]::TokenizeWordElements('one two three') | Should Be 'one','two','three'
6473
}
6574
It 'added methods are the correct type' {
66-
[scriptblock]::BindArgumentsForScripblockInvoke |
75+
# Always wondered if this misspelling was for some reason on purpose.
76+
$methodName = 'BindArgumentsForScripBlockInvoke'
77+
if ($PSVersionTable.PSVersion.Major -ge 6) {
78+
$methodName = 'BindArgumentsForScriptBlockInvoke'
79+
}
80+
[scriptblock]::$methodName |
6781
Should BeOfType 'System.Management.Automation.PSMethod'
6882
}
6983
}

0 commit comments

Comments
 (0)