|
| 1 | +function main { |
| 2 | + Write-Output 'Basics' |
| 3 | + Set-Location $env:GITHUB_WORKSPACE |
| 4 | + |
| 5 | + & choco.exe install pandoc --no-progress |
| 6 | + refreshenv |
| 7 | + |
| 8 | + if ($env:RELEASETAG) { |
| 9 | + $ReleaseTag = $env:RELEASETAG |
| 10 | + } else { |
| 11 | + $ReleaseTag = ($env:GITHUB_REF -replace 'refs/tags/', '') |
| 12 | + "RELEASETAG=$ReleaseTag" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append |
| 13 | + } |
| 14 | + |
| 15 | + if ($env:RELEASEFILE) { |
| 16 | + $ReleaseFile = $env:RELEASEFILE |
| 17 | + } else { |
| 18 | + $ReleaseFile = ($env:GITHUB_REPOSITORY -split '/')[1] + '_' + $ReleaseTag + '.zip' |
| 19 | + "RELEASEFILE=$ReleaseFile" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append |
| 20 | + } |
| 21 | + |
| 22 | + if ($env:RELEASENAME) { |
| 23 | + $ReleaseName = $env:RELEASENAME |
| 24 | + } else { |
| 25 | + $ReleaseName = "Release $ReleaseTag" |
| 26 | + "RELEASENAME=$ReleaseName" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append |
| 27 | + } |
| 28 | + |
| 29 | + $BuildDir = $('./build/' + ($env:GITHUB_REPOSITORY -split '/')[1]) + '_' + $ReleaseTag |
| 30 | + New-Item $BuildDir -ItemType 'directory' | Out-Null |
| 31 | + |
| 32 | + Write-Output "BuildDir: $BuildDir" |
| 33 | + Write-Output "ReleaseFile: $ReleaseFile" |
| 34 | + Write-Output "ReleaseName: $ReleaseName" |
| 35 | + Write-Output "ReleaseTag: $ReleaseTag" |
| 36 | + |
| 37 | + |
| 38 | + Write-Output 'Copy basic files' |
| 39 | + Set-Location $env:GITHUB_WORKSPACE |
| 40 | + |
| 41 | + Copy-Item '.\src\*' $BuildDir -Recurse |
| 42 | + Copy-Item '.\LICENSE.txt' "$BuildDir\LICENSE.txt" -Force |
| 43 | + |
| 44 | + |
| 45 | + Write-Output 'Convert markdown files to HTML and copy them' |
| 46 | + Set-Location $env:GITHUB_WORKSPACE |
| 47 | + |
| 48 | + @( |
| 49 | + ('.\docs\CHANGELOG.md', "$BuildDir\docs\CHANGELOG.html"), |
| 50 | + ('.\docs\CODE_OF_CONDUCT.md', "$BuildDir\docs\CODE_OF_CONDUCT.html"), |
| 51 | + ('.\docs\CONTRIBUTING.md', "$BuildDir\docs\CONTRIBUTING.html"), |
| 52 | + ('.\docs\README.md', "$BuildDir\docs\README.html") |
| 53 | + ) | ForEach-Object { |
| 54 | + # update version number |
| 55 | + $tempFileContent = Get-Content $($_[0]) -Raw |
| 56 | + $tempFileContent = $tempFileContent -replace 'XXXVersionStringXXX', ($ReleaseTag.replace('-', '--').replace('_', '__').replace(' ', '_')) |
| 57 | + $tempFileContent = $tempFileContent -replace '<!--XXXRemoveWhenBuildingXXX', '' |
| 58 | + $tempFileContent = $tempFileContent -replace 'XXXRemoveWhenBuildingXXX-->', '' |
| 59 | + $tempFileContent | Set-Content $($_[0]) |
| 60 | + # convert to HTML |
| 61 | + & pandoc.exe $($_[0]) --resource-path=".;docs" -f gfm -t html --self-contained -H .\build\pandoc_header.html --css .\build\pandoc_css_empty.css --metadata pagetitle="$(([System.IO.FileInfo]"$($_[0])").basename) - $(($env:GITHUB_REPOSITORY -split '/')[1])" -o $($_[1]) |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + Write-Output 'Update version number in script' |
| 66 | + Set-Location $env:GITHUB_WORKSPACE |
| 67 | + |
| 68 | + Set-Location $BuildDir |
| 69 | + |
| 70 | + ((Get-Content Export-RecipientPermissions.ps1 -Raw) -replace 'XXXVersionStringXXX', $ReleaseTag) | Set-Content Export-RecipientPermissions.ps1 |
| 71 | + |
| 72 | + |
| 73 | + Write-Output 'Create file hashes and place them in file hashes.txt' |
| 74 | + Set-Location $env:GITHUB_WORKSPACE |
| 75 | + |
| 76 | + Set-Location $BuildDir |
| 77 | + |
| 78 | + Remove-Item 'hashes.txt' -Force |
| 79 | + |
| 80 | + $Hashes = ForEach ($File in (Get-ChildItem -File -Recurse)) { |
| 81 | + Get-FileHash -LiteralPath $File.FullName -Algorithm SHA256 | Select-Object @{N = 'PathRelative'; E = { Resolve-Path -LiteralPath $file.FullName -Relative } }, Algorithm, Hash |
| 82 | + } |
| 83 | + |
| 84 | + $Hashes | Export-Csv hashes.txt |
| 85 | + |
| 86 | + |
| 87 | + Write-Output 'Create release file' |
| 88 | + Set-Location $env:GITHUB_WORKSPACE |
| 89 | + |
| 90 | + Compress-Archive $BuildDir $ReleaseFile |
| 91 | + |
| 92 | + |
| 93 | + Write-Output 'Output additional information for release' |
| 94 | + Set-Location $env:GITHUB_WORKSPACE |
| 95 | + |
| 96 | + $Changelog = '.\docs\changelog.md' |
| 97 | + $ChangeLogLines = Get-Content $Changelog |
| 98 | + $ChangelogStartline = $null |
| 99 | + $ChangelogEndline = $null |
| 100 | + $ReleaseTagDate = $(Get-Date -Format 'yyyy-MM-dd') |
| 101 | + for ($i = 0; $i -lt $ChangeLogLines.count; $i++) { |
| 102 | + if (-not $ChangelogStartline) { |
| 103 | + if ($ChangeLogLines[$i] -match ("^##\s*(\[$ReleaseTag\] - $ReleaseTagDate\s*|$ReleaseTag - $ReleaseTagDate\s*|$ReleaseTag - $ReleaseTagDate$)|>$ReleaseTag</a> - $ReleaseTagDate\s*")) { |
| 104 | + $ChangelogStartline = $i |
| 105 | + continue |
| 106 | + } |
| 107 | + } else { |
| 108 | + if (($ChangeLogLines[$i]).startswith('## ')) { |
| 109 | + $ChangelogEndline = $i - 1 |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + if (-not $ChangelogStartline) { |
| 115 | + $ReleaseMarkdown = "# **Tag '$ReleaseTag - $ReleaseTagDate' not found in '$Changelog', using first entry.**`r`n" |
| 116 | + $ChangelogStartline = $null |
| 117 | + $ChangelogEndline = $null |
| 118 | + for ($i = 0; $i -lt $ChangeLogLines.count; $i++) { |
| 119 | + if (-not $ChangelogStartline) { |
| 120 | + if (($ChangeLogLines[$i]).startswith('## ')) { |
| 121 | + $ChangelogStartline = $i |
| 122 | + } |
| 123 | + } else { |
| 124 | + if (($ChangeLogLines[$i]).startswith('## ')) { |
| 125 | + $ChangelogEndline = $i - 1 |
| 126 | + break |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } else { |
| 131 | + if (-not $Changelogendline) { $ChangelogEndline = $ChangelogLines.count - 1 } |
| 132 | + } |
| 133 | + for ($i = $ChangelogStartline; $i -le $ChangelogEndline; $i++) { |
| 134 | + $ChangeLogLines[$i] = $ChangeLogLines[$i] -replace '^##', '#' |
| 135 | + } |
| 136 | + $ReleaseMarkdown = $ReleaseMarkdown + ($($ChangeLogLines[$ChangelogStartline..$ChangelogEndline]) -join "`r`n") |
| 137 | + |
| 138 | + if ($RegExMatches = [regex]::matches($ReleaseMarkdown, '\[(.*?)\]')) { |
| 139 | + for ($i = 0; $i -lt $ChangeLogLines.count; $i++) { |
| 140 | + foreach ($m in $RegExMatches) { |
| 141 | + if ($ChangeLogLines[$i].StartsWith("$($m.value):")) { |
| 142 | + $ReleaseMarkdown = $ReleaseMarkdown + "`r`n$($ChangeLogLines[$i])" |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + $ReleaseMarkdown = $ReleaseMarkdown + @" |
| 149 | +`r`n# File hashes |
| 150 | +- SHA256 hash of '$ReleaseFile': $((Get-FileHash $ReleaseFile -Algorithm SHA256).hash) |
| 151 | +- See 'hashes.txt' in '$ReleaseFile' for hash value of every single file in the release. |
| 152 | +"@ |
| 153 | + |
| 154 | + Write-Output 'ReleaseMarkdown:' |
| 155 | + Write-Output $ReleaseMarkdown |
| 156 | + $ReleaseMarkdown | Out-File -FilePath .\build\CHANGELOG.md -Encoding utf8 -Force |
| 157 | +} |
| 158 | + |
| 159 | +if ($env:GITHUB_WORKSPACE) { |
| 160 | + main |
| 161 | +} else { |
| 162 | + throw 'This script is designed to run as part of a GitHub workflow, and does not work elsewhere.' |
| 163 | +} |
0 commit comments