Tom*_*nik 5 powershell metadata
是否可以将一些元数据属性分配给PowerShell宏?我有一组宏,我想将它们封装到逻辑组.我想象的是:
[UnitTest]
function Do-Something()
{
...
}
Run Code Online (Sandbox Code Playgroud)
然后在运行时传递所有加载的宏并将其过滤掉,如:
$macrosInRuntime = Get-Item function:
$unitTestMacros = $macrosInRuntime |
? {$_.Attributes -contain "UnitTest"} # <-- ???
foreach ($macro in $unitTestMacros)
{
....
}
Run Code Online (Sandbox Code Playgroud)
我会很乐意帮助你
有趣的问题...... AFAIK没有这些功能属性.但我认为有一种半hacky方式使用基于注释的帮助属性(可能根本不是hacky,但我不太确定).
<#
.FUNCTIONALITY
TEST1
#>
function Do-Something1
{}
<#
.FUNCTIONALITY
TEST2
#>
function Do-Something2
{}
Get-ChildItem Function: | %{
$fun = $_.Name
try {
Get-Help $fun -Functionality TEST* | %{
switch($_.Functionality) {
'TEST1' { "$fun is for test 1" }
'TEST2' { "$fun is for test 2" }
}
}
}
catch {}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Do-Something1 is for test 1
Do-Something2 is for test 2
Run Code Online (Sandbox Code Playgroud)
也许这种方法在某些情况下可能有用.
另请参阅帮助中基于注释的帮助关键字部分:
man about_Comment_Based_Help
Run Code Online (Sandbox Code Playgroud)
更新 虽然上面的答案被接受,但我仍然不满意.这是另一种绝对不是hacky的方法.它也有一个优势,请参阅评论.此方法使用具有常规名称的额外别名.
# Functions to be used in tests, with any names
function Do-Something1 { "Something1..." }
function Do-Something2 { "Something2..." }
# Aliases that define tests, conventional names are UnitTest-*.
# Note: one advantage is that an alias can be defined anywhere,
# right where a function is defined or somewhere else. The latter
# is suitable in scenarios when we cannot modify the source files
# (or just do not want to).
Set-Alias UnitTest-Do-Something1 Do-Something1
Set-Alias UnitTest-Do-Something2 Do-Something2
# Get UnitTest-* aliases and extract function names for tests.
Get-Alias UnitTest-* | Select-Object -ExpandProperty Definition
# Or we can just invoke aliases themselves.
Get-Alias UnitTest-* | % { & $_}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
531 次 |
| 最近记录: |