需要 FileSystemWatcher 事件来重新加载当前工作环境中的模块

发布于 2024-12-23 06:08:46 字数 1412 浏览 4 评论 0原文

首先,我有一个类似的问题这里,但没有解决问题的方向。根据海报 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独夜无伴 2024-12-30 06:08:46

这就是我加载模块的方式:

Get-Module -listavailable| foreach{Import-Module $_.name}

This is how I load modules:

Get-Module -listavailable| foreach{Import-Module $_.name}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文