繁体   English   中英

记录Powershell模块和脚本

[英]Documenting Powershell modules and scripts

随着Powershell 5引入OOP Classes支持 ,功能,脚本和模块的传统基于注释的 Powershell文档方法不再适用。 Get-Help没有为类,方法或属性提供任何帮助,看起来它会保持这种状态。 除此之外,Get-Help在尝试查找特定函数的信息时没有太多帮助,而实际上没有相关的模块或PowerShell脚本。

由于类对于更复杂的Powershell项目特别有用,因此对最新文档的需求比以往任何时候都更迫切。 DoxygenSandcastle帮助文件生成器这样的项目确实支持许多OO语言的帮助生成,但似乎无法处理Powershell代码。 快速浏览一下PoshBuild项目,发现它也是针对.NET语言项目的,并且需要集成到Visual Studio构建过程中,而纯Powershell代码没有。

还有PSDoc能够生成基于Get-Help输出的HTML或降价格式的模块文档,如果它支持类,这将是我想要的。

那么,如果有的话,我如何自动生成合理的文档

  1. .ps1脚本
  2. .psm1模块
  3. 我的Powershell代码中的类

使用基于注释的帮助文档语法?

@trebleCode仍然应该得到答案,我只是为感兴趣的人发布这个。

我刚开始尝试回答这个问题,但却分心了,从未完成过。 如果我没记错的话,我在Github上发现了一些讨论,他们说他们没有计划支持注释注释课程,这很令人难过,因为我喜欢Powershell评论。

我的想法是,通过调用内置帮助方法,您可以创建一个辅助函数,该函数将检测class关键字上方的这些非标准注释,并将它们转换为注释对象,而无需调用get-help 这些注释也可以存储在外部文件中。

下面我找到了将注释解析为对象并在代码中创建注释对象的代码。

# References: 
# https://learn-powershell.net/2015/08/07/invoking-private-static-methods-using-powershell/
# https://stackoverflow.com/questions/1259222/how-to-access-internal-class-using-reflection
# https://stackoverflow.com/questions/15652656/get-return-value-after-invoking-a-method-from-dll-using-reflection
# https://github.com/PowerShell/PowerShell/blob/a8627b83e5cea71c3576871eacad7f2b19826d53/src/System.Management.Automation/help/HelpCommentsParser.cs

$ExampleComment = @"
<#
.SYNOPSIS
    This was a triumph
#>
"@

$CommentLines = [Collections.Generic.List`1[String]]::new()
$InvokeArgs = @($ExampleComment, $CommentLines)

# GetMethod Filter
$BindingFlags = 'static','nonpublic','instance'

# GetMethod Filter: We need to specify overloaded methods by their parameters
$ParamTypes  = [Type]::GetTypeArray($InvokeArgs)
$ParamCount  = [System.Reflection.ParameterModifier]::new(2)

$HelpParser  = [psobject].Assembly.GetType('System.Management.Automation.HelpCommentsParser')
$CollectCommentText = $HelpParser.GetMethod('CollectCommentText', $BindingFlags, $null, $ParamTypes, $ParamCount)

# Extension methods aren't part of the class so null gets called first.
# TODO: Figure out return value
$CollectCommentText.Invoke($Null,$InvokeArgs)
$InvokeArgs

# Comment object but properties are read only.
$CommentHelp = [System.Management.Automation.Language.CommentHelpInfo]::new()
$CommentHelp.Synopsis
$CommentHelp.Description
$CommentHelp.Examples
$CommentHelp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM