簡體   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