From 78f19f85677ec9a37e79038db163305dfea37bef Mon Sep 17 00:00:00 2001 From: Florian Hopfner Date: Sun, 11 Jun 2023 15:21:27 +0000 Subject: [PATCH 1/7] :construction_worker: add action to format comment based help --- .github/workflows/update-generated-text.yml | 41 ++++++++++++++++ build/buildtools.ps1 | 19 ++++++++ build/format-commentbasedhelp.ps1 | 54 +++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 .github/workflows/update-generated-text.yml create mode 100644 build/buildtools.ps1 create mode 100644 build/format-commentbasedhelp.ps1 diff --git a/.github/workflows/update-generated-text.yml b/.github/workflows/update-generated-text.yml new file mode 100644 index 00000000..36cf479f --- /dev/null +++ b/.github/workflows/update-generated-text.yml @@ -0,0 +1,41 @@ +# Run the scrips documented in https://github.com/d365collaborative/d365fo.tools/wiki/Building-tools +# Creates a pull request with the changes + +name: d365fo.tools-Generate-Text + +on: + workflow_dispatch: + +jobs: + + generateText: + name: Generate text + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + - name: Cache Powershell Modules + id: cache-powershell-modules + uses: actions/cache@v3 + with: + path: C:\Users\runneradmin\Documents\WindowsPowerShell\Modules + key: 20210527|${{ hashFiles('**/vsts-prerequisites.ps1, **/buildtools.ps1') }} + - name: Prerequisites + if: steps.cache-powershell-modules.outputs.cache-hit != 'true' + shell: powershell + run: build\vsts-prerequisites.ps1 + - name: BuildTools + if: steps.cache-powershell-modules.outputs.cache-hit != 'true' + shell: powershell + run: build\buildtools.ps1 + - name: Format comment based help + shell: powershell + run: build\formatCommentBasedHelp.ps1 + - name: Create a pull request for changes + uses: peter-evans/create-pull-request@v5 + with: + commit-message: | + 'Update comment based help and tests + + This pull request was automatically created by the d365fo.tools-Generate-Text action' + title: 'Update comment based help and tests' + body: 'This pull request was automatically created by the d365fo.tools-Generate-Text action' \ No newline at end of file diff --git a/build/buildtools.ps1 b/build/buildtools.ps1 new file mode 100644 index 00000000..5d607234 --- /dev/null +++ b/build/buildtools.ps1 @@ -0,0 +1,19 @@ +# Installs the modules required for the automatic text generation. +# See also https://github.com/d365collaborative/d365fo.tools/wiki/Building-tools + +Write-Host "Working on the machine named: $($env:computername)" +Write-Host "The user running is: $($env:UserName)" + +$modules = @("PSModuleDevelopment", "platyPS") + +foreach ($item in $modules) { + + $module = Get-InstalledModule -Name $item -ErrorAction SilentlyContinue + + if ($null -eq $module) { + Write-Host "Installing $item" -ForegroundColor Cyan + Install-Module -Name $item -Force -Confirm:$false -Scope CurrentUser -AllowClobber -SkipPublisherCheck + } + + Import-Module $item -Force +} \ No newline at end of file diff --git a/build/format-commentbasedhelp.ps1 b/build/format-commentbasedhelp.ps1 new file mode 100644 index 00000000..71273484 --- /dev/null +++ b/build/format-commentbasedhelp.ps1 @@ -0,0 +1,54 @@ +# Script to format the comments/documentation of the cmdlets used for the commend based help. +# based on https://gist.github.com/Splaxi/ff7485a24f6ed9937f3e8da76b5d4840 +# See also https://github.com/d365collaborative/d365fo.tools/wiki/Building-tools +$path = "$($env:SYSTEM_DEFAULTWORKINGDIRECTORY)\d365fo.tools" + +function Get-Header ($text) { + $start = $text.IndexOf('<#') + $temp = $start - 2 + if($temp -gt 0) { + $text.SubString(0, $start - 2) + } + else { + "" + } +} + +function Format-Help ($text) { + $start = $text.IndexOf('<#') + $end = $text.IndexOf('#>') + $help = $text.SubString($start + 2, $end - $start - 3) + + $skipfirst = $null # to avoid trailing spaces + foreach ($newline in $help.Split("`n")) { + if (-not $skipfirst) { $skipfirst = $true; continue } + $trimmed = $newline.Trim() + foreach ($line in $trimmed) { + if ($line.StartsWith(".")) { + " $line" + } + else { + " $line" + } + } + } +} + +function Get-Body ($text) { + $end = $text.IndexOf('#>') + $text.SubString($end, $text.Length - $end) +} + +$files = New-Object System.Collections.ArrayList +$filesPublic = Get-ChildItem -Path "$path\functions\*.ps1" +$files.AddRange($filesPublic) +$filesInternal = Get-ChildItem -Path "$path\internal\functions\*.ps1" +$files.AddRange($filesInternal) + +foreach ($file in $files) { + $text = ($file | Get-Content -Raw).Trim() + Set-Content -Path $file.FullName -Encoding UTF8 -Value (Get-Header $text).TrimEnd() + Add-Content -Path $file.FullName -Encoding UTF8 -Value "<#".Trim() + Add-Content -Path $file.FullName -Encoding UTF8 -Value (Format-Help $text) + Add-Content -Path $file.FullName -Encoding UTF8 -Value (Get-Body $text).TrimEnd() -NoNewline +} \ No newline at end of file From fbccf8dd76afdfab7fb58e4a687d21a07595dad5 Mon Sep 17 00:00:00 2001 From: Florian Hopfner Date: Sun, 11 Jun 2023 15:26:43 +0000 Subject: [PATCH 2/7] :construction_worker: fix PowerShell script file name --- .github/workflows/update-generated-text.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/update-generated-text.yml b/.github/workflows/update-generated-text.yml index 36cf479f..d07091a8 100644 --- a/.github/workflows/update-generated-text.yml +++ b/.github/workflows/update-generated-text.yml @@ -29,7 +29,7 @@ jobs: run: build\buildtools.ps1 - name: Format comment based help shell: powershell - run: build\formatCommentBasedHelp.ps1 + run: build\format-commentbasedhelp.ps1 - name: Create a pull request for changes uses: peter-evans/create-pull-request@v5 with: From 30d9d0561c9f6cb8f0c9027874dd92f0fac07469 Mon Sep 17 00:00:00 2001 From: Florian Hopfner Date: Sun, 11 Jun 2023 15:34:11 +0000 Subject: [PATCH 3/7] :construction_worker: change path for script execution --- build/format-commentbasedhelp.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/format-commentbasedhelp.ps1 b/build/format-commentbasedhelp.ps1 index 71273484..84c0879a 100644 --- a/build/format-commentbasedhelp.ps1 +++ b/build/format-commentbasedhelp.ps1 @@ -1,7 +1,7 @@ # Script to format the comments/documentation of the cmdlets used for the commend based help. # based on https://gist.github.com/Splaxi/ff7485a24f6ed9937f3e8da76b5d4840 # See also https://github.com/d365collaborative/d365fo.tools/wiki/Building-tools -$path = "$($env:SYSTEM_DEFAULTWORKINGDIRECTORY)\d365fo.tools" +$path = "$PSScriptRoot\..\d365fo.tools" function Get-Header ($text) { $start = $text.IndexOf('<#') From e3b46a0dd484d3e402146cfb745ff5f5ce429b09 Mon Sep 17 00:00:00 2001 From: Florian Hopfner Date: Sun, 11 Jun 2023 15:46:14 +0000 Subject: [PATCH 4/7] :construction_worker: add parameter unit test generation and documentation update --- .github/workflows/update-generated-text.yml | 6 ++++++ build/generate-parameterunittests.ps1 | 24 +++++++++++++++++++++ build/update-docs.ps1 | 11 ++++++++++ 3 files changed, 41 insertions(+) create mode 100644 build/generate-parameterunittests.ps1 create mode 100644 build/update-docs.ps1 diff --git a/.github/workflows/update-generated-text.yml b/.github/workflows/update-generated-text.yml index d07091a8..fd86d4bd 100644 --- a/.github/workflows/update-generated-text.yml +++ b/.github/workflows/update-generated-text.yml @@ -30,6 +30,12 @@ jobs: - name: Format comment based help shell: powershell run: build\format-commentbasedhelp.ps1 + - name: Generate parameter unit tests + shell: powershell + run: build\generate-parameterunittests.ps1 + - name: Update documentation + shell: powershell + run: build\update-docs.ps1 - name: Create a pull request for changes uses: peter-evans/create-pull-request@v5 with: diff --git a/build/generate-parameterunittests.ps1 b/build/generate-parameterunittests.ps1 new file mode 100644 index 00000000..d6d5a7e0 --- /dev/null +++ b/build/generate-parameterunittests.ps1 @@ -0,0 +1,24 @@ +# Script to generate the parameter unit tests. +# based on https://gist.github.com/Splaxi/2a24fc3c5193089ae7047ac5b8f104db +# See also https://github.com/d365collaborative/d365fo.tools/wiki/Building-tools +$path = "$PSScriptRoot\..\d365fo.tools" + +Import-Module $path -Force + +$excludeCommands = @() + +$commandsRaw = Get-Command -Module $moduleName + +if ($excludeCommands.Count -gt 0) { + $commands = $commandsRaw | Select-String -Pattern $excludeCommands -SimpleMatch -NotMatch + +} else { + $commands = $commandsRaw +} + +Remove-Item -Path "$path\tests\functions\*.Tests.ps1" +foreach ( $commandName in $commands) { + Invoke-PSMDTemplate CommandTest -OutPath "$path\tests\functions" -Name $commandName -Force +} + +Get-ChildItem -Path "$path\tests\functions" -Recurse -File | Set-PSMDEncoding \ No newline at end of file diff --git a/build/update-docs.ps1 b/build/update-docs.ps1 new file mode 100644 index 00000000..25e7cac0 --- /dev/null +++ b/build/update-docs.ps1 @@ -0,0 +1,11 @@ +# Script to generate the comment based markdown help files. +# based on https://gist.github.com/Splaxi/8934e13cb35918d13af6e3a21c208b0e +# See also https://github.com/d365collaborative/d365fo.tools/wiki/Building-tools +$path = "$PSScriptRoot\.." + +Import-Module "$path\d365fo.tools" -Force + +Remove-Item -Path "$path\docs\*.md" +$null = New-MarkdownHelp -Module $moduleName -OutputFolder "$path\docs" -Force + +Get-ChildItem -Path "$path\docs" -Recurse -File | Set-PSMDEncoding \ No newline at end of file From 92424937563c642856e2d3fac781c793fef6e3b9 Mon Sep 17 00:00:00 2001 From: Florian Hopfner Date: Sun, 11 Jun 2023 16:05:00 +0000 Subject: [PATCH 5/7] :construction_worker: fix module name --- build/generate-parameterunittests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/generate-parameterunittests.ps1 b/build/generate-parameterunittests.ps1 index d6d5a7e0..86771337 100644 --- a/build/generate-parameterunittests.ps1 +++ b/build/generate-parameterunittests.ps1 @@ -7,7 +7,7 @@ Import-Module $path -Force $excludeCommands = @() -$commandsRaw = Get-Command -Module $moduleName +$commandsRaw = Get-Command -Module d365fo.tools if ($excludeCommands.Count -gt 0) { $commands = $commandsRaw | Select-String -Pattern $excludeCommands -SimpleMatch -NotMatch From 256a5b3448ce54f56a4328090710417a306255da Mon Sep 17 00:00:00 2001 From: Florian Hopfner Date: Sun, 11 Jun 2023 16:05:31 +0000 Subject: [PATCH 6/7] :construction_worker: fix module name --- build/update-docs.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/update-docs.ps1 b/build/update-docs.ps1 index 25e7cac0..08e86f70 100644 --- a/build/update-docs.ps1 +++ b/build/update-docs.ps1 @@ -6,6 +6,6 @@ $path = "$PSScriptRoot\.." Import-Module "$path\d365fo.tools" -Force Remove-Item -Path "$path\docs\*.md" -$null = New-MarkdownHelp -Module $moduleName -OutputFolder "$path\docs" -Force +$null = New-MarkdownHelp -Module d365fo.tools -OutputFolder "$path\docs" -Force Get-ChildItem -Path "$path\docs" -Recurse -File | Set-PSMDEncoding \ No newline at end of file From 02198d2e31d05fe686a7355c08c4fc4286251c68 Mon Sep 17 00:00:00 2001 From: Florian Hopfner Date: Sun, 11 Jun 2023 16:21:52 +0000 Subject: [PATCH 7/7] :construction_worker: exclude aliases from parameter unit tests --- build/generate-parameterunittests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/generate-parameterunittests.ps1 b/build/generate-parameterunittests.ps1 index 86771337..6369f81e 100644 --- a/build/generate-parameterunittests.ps1 +++ b/build/generate-parameterunittests.ps1 @@ -7,7 +7,7 @@ Import-Module $path -Force $excludeCommands = @() -$commandsRaw = Get-Command -Module d365fo.tools +$commandsRaw = Get-Command -Module d365fo.tools -CommandType Function if ($excludeCommands.Count -gt 0) { $commands = $commandsRaw | Select-String -Pattern $excludeCommands -SimpleMatch -NotMatch