-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate-README.ps1
106 lines (83 loc) · 3.14 KB
/
Update-README.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<#
.SYNOPSIS
Updates the scripts list in README.md.
.DESCRIPTION
Updates the scripts list in README.md from scripts.csv.
.NOTES
Author: Jonathan Panning <jpann [at] impostr-labs.com>
Filename: Update-README.ps1
Created on: 12-06-2023
Version: 1.0
Last updated: 12-06-2023
.INPUTS
None. Does not accept any input.
.OUTPUTS
None. Does not output.
.EXAMPLE
PS> .\Update-README.ps1
#>
[CmdletBinding()]
[OutputType([System.Object[]])]
Param(
[parameter(Mandatory=$false)]
[switch]
$WhatIf
)
begin {
function List-Scripts([string]$Path) {
$files = Get-ChildItem -Recurse -Depth 1 -Path "$Path/*.ps1" -Attributes !Directory
$parentPath = Split-Path $Path -Parent
[int]$scriptNumber = 1
foreach ($file in $files) {
$help = Get-Help $file -Full
$scriptSubPath = $file.FullName.Replace($parentPath, "").SubString(1)
New-Object PSObject -Property @{
'No' = $scriptNumber++
'Script' = $file.Name
'Path' = $scriptSubPath.Replace("\", "/")
'Description' = $help.Synopsis
}
}
}
}
process {
cd $PSScriptRoot
$templateBlock =
@"
| Script | Description |
| ---------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| [Test.ps1](Scripts/Test/Test.ps1) | Test script. [Read more...](Scripts/Test/Test.md) |
%SCRIPT_LINE%
"@
$templateLine = "| [%FILENAME%](%FILE_SUBPATH%) | %FILE_DESC% [Read more...](%FILE_DOC_LINK%) |"
#$scripts = Import-Csv -Path "../Data/scripts.csv"
$scripts = List-Scripts -Path $pwd
$text = Get-Content -Path '../README.md' -Raw
$lineStart = "<!-- ScriptStart -->"
$lineEnd = "<!-- ScriptEnd -->"
$newScriptsBlock = $templateBlock
$scriptLines = @()
foreach ($script in $scripts) {
$scriptLine = $templateLine
# Examples:
# %FILENAME% = 'Test.ps1'
# %FILE_DOC_LINK% = 'Scripts/Test/Test.md'
# %FILE_SUBPATH% = 'Scripts/Test/Test.ps1'
# %FILE_DESC% = ' Test script.'
$scriptLine = $scriptLine.Replace("%FILENAME%", $script.Script)
$scriptLine = $scriptLine.Replace("%FILE_DOC_LINK%", $script.Path.Replace(".ps1", ".md"))
$scriptLine = $scriptLine.Replace("%FILE_SUBPATH%", $script.Path)
$scriptLine = $scriptLine.Replace("%FILE_DESC%", $script.Description)
$scriptLines += $scriptLine
}
$scriptLinesBlock = $scriptLines -Join "`r`n" | Out-String
$newScriptBlock = $templateBlock
$newScriptBlock = $newScriptsBlock.Replace("%SCRIPT_LINE%", $scriptLinesBlock)
$Pattern = "(?s)(?<=$($lineStart)\r?\n).*?(?=$($lineEnd))"
$text = ($text -Replace $Pattern, $newScriptBlock)
if ($WhatIf -eq $false) {
$text | Set-Content '../README.md'
} else {
$text | Write-Output
}
}