简体   繁体   English

记录Powershell模块和脚本

[英]Documenting Powershell modules and scripts

With Powershell 5 introducing OOP Classes support , the traditional comment-based Powershell documentation methods for functions, scripts and modules are no longer a good fit. 随着Powershell 5引入OOP Classes支持 ,功能,脚本和模块的传统基于注释的 Powershell文档方法不再适用。 Get-Help is not presenting any help for classes, methods or properties and it looks like it will be staying that way. Get-Help没有为类,方法或属性提供任何帮助,看起来它会保持这种状态。 Other than that, Get-Help is not much of help when trying to find information on a specific function without actually having the module or powershell script in question. 除此之外,Get-Help在尝试查找特定函数的信息时没有太多帮助,而实际上没有相关的模块或PowerShell脚本。

As classes are especially useful for more complex Powershell projects, the need for an up-to-date documentation is more pressing than ever. 由于类对于更复杂的Powershell项目特别有用,因此对最新文档的需求比以往任何时候都更迫切。 Projects like Doxygen and the Sandcastle Help File Builder do support help generation for a number of OO-languages, but do not seem to be able to handle Powershell code. DoxygenSandcastle帮助文件生成器这样的项目确实支持许多OO语言的帮助生成,但似乎无法处理Powershell代码。 A quick look at the PoshBuild project reveals that it is targeted at .NET language projects, too and needs to be integrated into the Visual Studio build process, which pure-Powershell code does not have. 快速浏览一下PoshBuild项目,发现它也是针对.NET语言项目的,并且需要集成到Visual Studio构建过程中,而纯Powershell代码没有。

There is also PSDoc capable of generating documentation for modules in HTML or markdown formats based on Get-Help output, which would have been pretty much what I want if it supported classes. 还有PSDoc能够生成基于Get-Help输出的HTML或降价格式的模块文档,如果它支持类,这将是我想要的。

So how do I auto-generate sensible documentation if I have 那么,如果有的话,我如何自动生成合理的文档

  1. .ps1 scripts .ps1脚本
  2. .psm1 modules .psm1模块
  3. classes in my Powershell code 我的Powershell代码中的类

using the comment-based help documentation syntax? 使用基于注释的帮助文档语法?

@trebleCode still deserves the answer, I'm just posting this for anyone interested. @trebleCode仍然应该得到答案,我只是为感兴趣的人发布这个。

I started trying to answer this question a while ago but got distracted and never finished. 我刚开始尝试回答这个问题,但却分心了,从未完成过。 If I recall correctly, there was some discussion I found on Github where they said they didn't plan on supporting comment annotated classes, which is sad because I like Powershell Comments. 如果我没记错的话,我在Github上发现了一些讨论,他们说他们没有计划支持注释注释课程,这很令人难过,因为我喜欢Powershell评论。

My thought here was that by calling the builtin help methods you could create a helper function that would detect these non-standard comments above the class keyword and convert them to comment objects without invoking get-help . 我的想法是,通过调用内置帮助方法,您可以创建一个辅助函数,该函数将检测class关键字上方的这些非标准注释,并将它们转换为注释对象,而无需调用get-help These comments could also be stored in external files. 这些注释也可以存储在外部文件中。

Below I found the code for parsing comments into objects and creating comment objects in code. 下面我找到了将注释解析为对象并在代码中创建注释对象的代码。

# 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