需要 FileSystemWatcher 事件来重新加载当前工作环境中的模块
首先,我有一个类似的问题这里,但没有解决问题的方向。根据海报 Andy Arismendi 的想法,我想出了一个可能的解决方案,这是一个新问题:
我是运行以下脚本,将以下代码块绑定到 FileChanged 事件。当 .psm1 模块文件更改时,代码块会检查文件是否已加载并尝试重新加载。如果没有,则导入该模块。但是,我认为该事件与绑定该事件的初始脚本正在运行的当前运行环境无关。我在代码下面列出了两个问题:
# create a FileSystemWatcher on the currect directory
$filter = '*.psm1'
$folder = $PWD
$watcher = New-object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false; EnableRaisingEvents = $true; NotifyFilter = [IO.NotifyFilters]'LastWrite'}
Register-ObjectEvent $watcher Changed -SourceIdentifier FileChanged -Action {
$folder = $PWD
$name = $Event.SourceEventArgs.Name
$filename = $name.Remove($name.IndexOf('.'), 5)
Write-Host $PWD
$loadedModule = Get-Module | ? { $_.Name -eq $filename }
write-host $filename
if ($loadedModule) {
write-host "Reloading Module $folder\$($filename)"
Reload-Module $filename
} else {
write-host "Importing Module $folder\$($filename)"
Import-Module .\$filename
}
}
问题: 1) 当 .psm1 文件更改时,事件运行两次(一次保存文件)
2) 即使它运行重新加载/导入命令,它也必须在不同的环境中运行,因为它们从未加载。但所有输出都会转到脚本绑定事件的当前运行环境。
就像我提到的,我确信代码块与当前会话不相关。
First off, I have a similar question here, but didn't have a direction on solving the problem. With an idea from a poster Andy Arismendi I come up with a possible solution and this is a new problem:
I am running the below script which binds the below code block to the FileChanged event. When a .psm1 module file changes the code block checks if the file is loaded and attempts to reload. If not, it imports the module. However, I am of the opinion that somehow the event is not associated with the current running environment where the intial script that bound the event is running. I have listed the two problems below the code:
# create a FileSystemWatcher on the currect directory
$filter = '*.psm1'
$folder = $PWD
$watcher = New-object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false; EnableRaisingEvents = $true; NotifyFilter = [IO.NotifyFilters]'LastWrite'}
Register-ObjectEvent $watcher Changed -SourceIdentifier FileChanged -Action {
$folder = $PWD
$name = $Event.SourceEventArgs.Name
$filename = $name.Remove($name.IndexOf('.'), 5)
Write-Host $PWD
$loadedModule = Get-Module | ? { $_.Name -eq $filename }
write-host $filename
if ($loadedModule) {
write-host "Reloading Module $folder\$($filename)"
Reload-Module $filename
} else {
write-host "Importing Module $folder\$($filename)"
Import-Module .\$filename
}
}
Problems:
1) When the .psm1 file changes the event runs twice (for one save of the file)
2) Even though it runs the reload/import commands, it must be operating in a different environment as they are never loaded. But all output does go to the current running environment where the script bound the event.
Like I mentioned, I am convinced that somehow the code block is not associated with the current session.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我加载模块的方式:
This is how I load modules: