4
4
//
5
5
6
6
using System ;
7
+ using System . Collections ;
7
8
using System . Collections . Generic ;
8
9
using System . Collections . ObjectModel ;
9
10
using System . Management . Automation . Runspaces ;
10
11
using System . Reflection ;
12
+ using Microsoft . PowerShell . Commands ;
11
13
12
14
namespace Microsoft . PowerShell . EditorServices
13
15
{
@@ -23,6 +25,7 @@ namespace Microsoft.PowerShell.EditorServices
23
25
/// </summary>
24
26
public class RunspaceSynchronizer
25
27
{
28
+ private static readonly Version versionZero = new Version ( 0 , 0 ) ;
26
29
// Determines whether the HandleRunspaceStateChange event should attempt to sync the runspaces.
27
30
private static bool SourceActionEnabled = false ;
28
31
@@ -71,9 +74,9 @@ public class RunspaceSynchronizer
71
74
public static void InitializeRunspaces ( Runspace runspaceSource , Runspace runspaceTarget )
72
75
{
73
76
sourceRunspace = runspaceSource ;
74
- sourceEngineIntrinsics = ReflectionUtils . GetEngineIntrinsics ( sourceRunspace ) ;
77
+ sourceEngineIntrinsics = sourceRunspace . GetEngineIntrinsics ( ) ;
75
78
targetRunspace = runspaceTarget ;
76
- targetEngineIntrinsics = ReflectionUtils . GetEngineIntrinsics ( runspaceTarget ) ;
79
+ targetEngineIntrinsics = runspaceTarget . GetEngineIntrinsics ( ) ;
77
80
IsReadyForEvents = true ;
78
81
79
82
sourceEngineIntrinsics . Events . SubscribeEvent (
@@ -104,15 +107,15 @@ public static void Activate()
104
107
105
108
private static void HandleRunspaceStateChange ( object sender , PSEventArgs args )
106
109
{
107
- if ( ! SourceActionEnabled )
110
+ if ( ! SourceActionEnabled || sourceRunspace . Debugger . IsActive )
108
111
{
109
112
return ;
110
113
}
111
114
112
115
SourceActionEnabled = false ;
113
116
114
117
var newOrChangedModules = new List < PSModuleInfo > ( ) ;
115
- List < PSModuleInfo > modules = ReflectionUtils . GetModules ( sourceRunspace ) ;
118
+ List < PSModuleInfo > modules = sourceRunspace . GetModules ( ) ;
116
119
foreach ( PSModuleInfo module in modules )
117
120
{
118
121
if ( moduleCache . Add ( module ) )
@@ -150,8 +153,16 @@ private static void HandleRunspaceStateChange(object sender, PSEventArgs args)
150
153
{
151
154
if ( moduleInfo . Path != null )
152
155
{
156
+ string nameParameterValue = moduleInfo . Path ;
157
+ // If the version is greater than zero, the module info was probably imported by the psd1 or module base.
158
+ // If so, we can just import from the module base which is the root of the module folder.
159
+ if ( moduleInfo . Version > versionZero )
160
+ {
161
+ nameParameterValue = moduleInfo . ModuleBase ;
162
+ }
163
+
153
164
pwsh . AddCommand ( "Import-Module" )
154
- . AddParameter ( "Name" , moduleInfo . Path )
165
+ . AddParameter ( "Name" , nameParameterValue )
155
166
. AddParameter ( "Force" )
156
167
. AddStatement ( ) ;
157
168
}
@@ -172,40 +183,39 @@ private static void HandleRunspaceStateChange(object sender, PSEventArgs args)
172
183
}
173
184
174
185
#endregion
186
+ }
175
187
176
- // A collection of helper methods that use Reflection in some form.
177
- private class ReflectionUtils
178
- {
179
- private static BindingFlags bindingFlags = BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . Default ;
188
+ internal static class RunspaceExtensions
189
+ {
190
+ private static BindingFlags bindingFlags = BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . Default ;
180
191
181
- // Gets the modules loaded in a runspace.
182
- // This exists in runspace.ExecutionContext.Modules.GetModule(string[] patterns, bool all)
183
- internal static List < PSModuleInfo > GetModules ( Runspace runspace )
184
- {
185
- var executionContext = typeof ( Runspace )
186
- . GetProperty ( "ExecutionContext" , bindingFlags )
187
- . GetValue ( runspace ) ;
188
- var ModuleIntrinsics = executionContext . GetType ( )
189
- . GetProperty ( "Modules" , bindingFlags )
190
- . GetValue ( executionContext ) ;
191
- var modules = ModuleIntrinsics . GetType ( )
192
- . GetMethod ( "GetModules" , bindingFlags , null , new Type [ ] { typeof ( string [ ] ) , typeof ( bool ) } , null )
193
- . Invoke ( ModuleIntrinsics , new object [ ] { new string [ ] { "*" } , false } ) as List < PSModuleInfo > ;
194
- return modules ;
195
- }
192
+ // Gets the modules loaded in a runspace.
193
+ // This exists in runspace.ExecutionContext.Modules.GetModule(string[] patterns, bool all)
194
+ internal static List < PSModuleInfo > GetModules ( this Runspace runspace )
195
+ {
196
+ var executionContext = typeof ( Runspace )
197
+ . GetProperty ( "ExecutionContext" , bindingFlags )
198
+ . GetValue ( runspace ) ;
199
+ var ModuleIntrinsics = executionContext . GetType ( )
200
+ . GetProperty ( "Modules" , bindingFlags )
201
+ . GetValue ( executionContext ) ;
202
+ var modules = ModuleIntrinsics . GetType ( )
203
+ . GetMethod ( "GetModules" , bindingFlags , null , new Type [ ] { typeof ( string [ ] ) , typeof ( bool ) } , null )
204
+ . Invoke ( ModuleIntrinsics , new object [ ] { new string [ ] { "*" } , false } ) as List < PSModuleInfo > ;
205
+ return modules ;
206
+ }
196
207
197
- // Gets the engine intrinsics object on a Runspace.
198
- // This exists in runspace.ExecutionContext.EngineIntrinsics.
199
- internal static EngineIntrinsics GetEngineIntrinsics ( Runspace runspace )
200
- {
201
- var executionContext = typeof ( Runspace )
202
- . GetProperty ( "ExecutionContext" , bindingFlags )
203
- . GetValue ( runspace ) ;
204
- var engineIntrinsics = executionContext . GetType ( )
205
- . GetProperty ( "EngineIntrinsics" , bindingFlags )
206
- . GetValue ( executionContext ) as EngineIntrinsics ;
207
- return engineIntrinsics ;
208
- }
208
+ // Gets the engine intrinsics object on a Runspace.
209
+ // This exists in runspace.ExecutionContext.EngineIntrinsics.
210
+ internal static EngineIntrinsics GetEngineIntrinsics ( this Runspace runspace )
211
+ {
212
+ var executionContext = typeof ( Runspace )
213
+ . GetProperty ( "ExecutionContext" , bindingFlags )
214
+ . GetValue ( runspace ) ;
215
+ var engineIntrinsics = executionContext . GetType ( )
216
+ . GetProperty ( "EngineIntrinsics" , bindingFlags )
217
+ . GetValue ( executionContext ) as EngineIntrinsics ;
218
+ return engineIntrinsics ;
209
219
}
210
220
}
211
221
0 commit comments