Press.psm1
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 |
using namespace Microsoft.PowerShell.Commands using namespace System.IO using namespace System.Management.Automation function Build-ReleaseNotes { <# .SYNOPSIS Build release notes from commit logs using Keep a Changelog format #> [CmdletBinding()] param( #Path to the project folder where the git repository is located [Parameter(Mandatory)][String]$Path, #Version for the release notes. If not specified will be unreleased [String]$Version, #Output Path for the Changelog. If not specified it will be output directly as a string [String]$Destination, #By default, only builds the release notes since the last tag. Specify full to process the full operation. [Switch]$Full ) [String]$markdownResult = Get-MessagesSinceLastTag -Path $Path -All:$Full | Add-CommitType | ConvertTo-ReleaseNotesMarkdown -Version $Version if ($Destination) { Write-Verbose "Release Notes saved to $Destination" Out-File -FilePath $Destination -InputObject $markdownResult } else { return $markdownResult } } function Get-MessagesSinceLastTag ([String]$Path, [Switch]$All) { try { Push-Location -StackName GetMessagesSinceLastTag -Path $Path # Unicode (emoji) output from native commands cause issues on Windows $lastOutputEncoding = [console]::OutputEncoding [console]::OutputEncoding = [Text.Encoding]::UTF8 try { $LastErrorActionPreference = $ErrorActionPreference $ErrorActionPreference = 'Stop' [String]$currentCommitTag = & git describe --exact-match --tags 2>$null } catch { if ($PSItem -match 'no tag exactly matches') { #If this is not a direct tag that's fine $currentCommitTag = $null } elseif ($PSItem -match 'no names found, cannot describe anything.') { #This just means there are no tags $currentCommitTag = $null } else { throw } } finally { $ErrorActionPreference = $LastErrorActionPreference } #If this is a release tag, the release notes should be everything since the last release tag [String]$lastVersionTag = & git tag --list 'v*' --sort="version:refname" --merged | Where-Object { $PSItem -ne $currentCommitTag } | Select-Object -Last 1 if (-not $lastVersionTag) { Write-Verbose 'No version tags (vX.X.X) found in this repository, using all commits to generate release notes' $lastVersionCommit = $null } else { [String]$lastVersionCommit = (& git rev-list -n 1 $lastVersionTag) + '..' } if ($All) { $lastVersionCommit = $null } [String]$gitLogResult = (& git log --pretty=format:"|||%h||%B||%aL||%cL" $lastVersionCommit) -join "`n" } catch { throw } finally { [console]::OutputEncoding = $lastOutputEncoding Pop-Location -StackName GetMessagesSinceLastTag } $gitLogResult.Split('|||').where{ $PSItem }.foreach{ $logItem = $PSItem.Split('||') [PSCustomObject]@{ CommitId = $logItem[0] Message = $logItem[1].trim() Author = $logItem[2].trim() Committer = $logItem[3].trim() CommitType = $null } } } function Add-CommitIdIfNotPullRequest { [CmdletBinding()] param ( [Parameter(Mandatory,ValueFromPipeline)]$logEntry ) process { if ($logEntry.Message -notmatch '#\d+') { $logEntry.Message = ($logEntry.Message + ' ({0})') -f $logEntry.CommitId } $logEntry } } function Add-PullRequestContributorThanks { [CmdletBinding()] param ( [Parameter(Mandatory,ValueFromPipeline)]$logEntry ) process { #TODO: Make ignored committer configurable #TODO: Make PR match configurable if ($logEntry.Committer -ne 'noreply' -and #This is the default Github Author $logEntry.Author -ne $logEntry.Committer -and $logEntry.Message -match '#\d+') { [string[]]$multiLineMessage = $logEntry.Message.trim().split("`n") $multiLineMessage[0] = ($multiLineMessage[0] + ' - Thanks @{0}!') -f $logEntry.Author $logEntry.Message = $multiLineMessage -join "`n" } $logEntry } } function Add-CommitType { [CmdletBinding()] param ( [Parameter(ValueFromPipeline)]$logMessage ) begin { #TODO: Move this to PSSettings $commitTypes = @( @{ Name = 'Breaking Changes' Regex = '💥|:boom:|BREAKING CHANGE:|\+semver:\s?(breaking|major)' } @{ Name = 'New Features' Regex = '✨|:(feat|tada):|^feat:|\+semver:\s?(feature|minor)' } @{ Name = 'Minor Updates and Bug Fixes' Regex = '[📌🐛🩹🚑♻️🗑️🔥⚡🔒➕➖🔗⚙️]|:(bug|refactor|perf|security|add|remove|deps|config):|^(fix|refactor|perf|security|style|deps):|\+semver:\s?(fix|patch)' } @{ Name = 'Documentation Updates' Regex = '📝' } ) } process { foreach ($logItem in $logMessage) { foreach ($commitTypeItem in $commitTypes) { if ($LogItem -match $commitTypeItem.Regex) { $LogItem.CommitType = $commitTypeItem.Name break } #Last Resort $LogItem.CommitType = 'Other' } Write-Output $logItem } } } function ConvertTo-ReleaseNotesMarkdown { [CmdletBinding()] param ( #Log Item with commit Type [Parameter(ValueFromPipeline)]$InputObject, #Version to use [String]$Version ) begin { $messages = [Collections.ArrayList]::new() $markdown = [Text.StringBuilder]::new() #Top header $baseHeader = if ($Version) { $currentDate = Get-Date -Format 'yyyy-MM-dd' "## [$Version] - $currentDate" } else { '## [Unreleased]' } [void]$markdown.AppendLine($baseHeader) } process { [void]$messages.add($InputObject) } end { $sortOrder = 'Breaking Changes', 'New Features', 'Minor Updates and Bug Fixes', 'Documentation Updates' $messageGroups = $messages | Add-PullRequestContributorThanks | Add-CommitIdIfNotPullRequest | Group-Object CommitType | Sort-Object { #Sort by our custom sort order. Anything that doesn't match moves to the end $index = $sortOrder.IndexOf($PSItem.Name) if ($index -eq -1) { $index = [int]::MaxValue } $index } foreach ($messageGroupItem in $messageGroups) { #Header First [void]$markdown.AppendLine("### $($messageGroupItem.Name)") #Then the issue lines #TODO: Create links for PRs $messageGroupItem.Group.Message.foreach{ #Multiline List format, removing extra newlines [String]$ListBody = ($PSItem -split "`n").where{ $PSItem } -join " `n " [String]$ChangeItem = '- ' + $ListBody [void]$markdown.AppendLine($ChangeItem) } #Spacer [Void]$markdown.AppendLine() } return ([String]$markdown).trim() } } function Compress-Module { [CmdletBinding()] param( #Path to the directory to archive [Parameter(Mandatory)]$Path, #Output for Zip File Name [Parameter(Mandatory)]$Destination ) $CompressArchiveParams = @{ Path = $Path DestinationPath = $Destination } Compress-Archive @CompressArchiveParams Write-Verbose ('Zip File Output:' + $CompressArchiveParams.DestinationPath) } <# .SYNOPSIS This function prepares a powershell module from a source powershell module directory .DESCRIPTION This function can also optionally "compile" the module, which is place all relevant powershell code in a single .psm1 file. This improves module load performance. If you choose to compile, place any script lines you use to dot-source the other files in your .psm1 file into a #region SourceInit region block, and this function will replace it with the "compiled" scriptblock #> function Copy-ModuleFiles { [CmdletBinding()] param ( #Path to the Powershell Module Manifest representing the file you wish to compile [Parameter(Mandatory)]$PSModuleManifest, #Path to the build destination. This should be non-existent or deleted by Clean prior [Parameter(Mandatory)]$Destination, #By Default this command expects a nonexistent destination, specify this to allow for a "Dirty" copy [Switch]$Force, #By default, the build will consolidate all relevant module files into a single .psm1 file. This enables the module to load faster. Specify this if you want to instead copy the files as-is [Switch]$NoCompile, #If you chose compile, specify this for the region block in your .psm1 file to replace with the compiled code. If not specified, it will just append to the end of the file. Defaults to 'SourceInit' for #region SourceInit [String]$SourceRegionName = 'SourceInit', #Files that are considered for inclusion to the 'compiled' module. This by default includes .ps1 files only. Uses Filesystem Filter syntax [String[]]$PSFileInclude = '*.ps1', #Files that are considered for exclusion to the 'compiled' module. This excludes any files that have two periods before ps1 (e.g. .build.ps1, .tests.ps1). Uses Filesystem Filter syntax [String[]]$PSFileExclude = '*.*.ps1', #If a prerelease tag exists, the build will touch a prerelease warning file into the root of the module folder. Specify this parameter to disable this behavior. [Switch]$NoPreReleaseFile = (-not $PressSetting.PreRelease), #Additional files to include in the folder. These will be dropped directly into the resulting module folder. Paths should be relative to the module root. [String[]]$Include ) $SourceModuleDir = Split-Path $PSModuleManifest #Verify a clean build folder try { $DestinationDirectory = New-Item -ItemType Directory -Path $Destination -ErrorAction Stop } catch [IO.IOException] { if ($PSItem.exception.message -match 'already exists\.$') { if (-not $Force) { throw "Folder $Destination already exists. Make sure that you cleaned your Build Output directory. To override this behavior, specify -Force" } else { #Downgrade error to warning Write-Warning $PSItem } } else { throw $PSItem } } #TODO: Use this one command and sort out the items later #$FilesToCopy = Get-ChildItem -Path $PSModuleManifestDirectory -Filter '*.ps*1' -Exclude '*.tests.ps1' -Recurse $SourceManifest = Import-PowerShellDataFile -Path $PSModuleManifest #TODO: Allow .psm1 to be blank and generate it on-the-fly if (-not $SourceManifest.RootModule) { throw "The source manifest at $PSModuleManifest does not have a RootModule specified. This is required to build the module." } $SourceRootModulePath = Join-Path $SourceModuleDir $sourceManifest.RootModule $SourceRootModule = Get-Content -Raw $SourceRootModulePath #Cannot use Copy-Item Directly because the filtering isn't advanced enough (can't exclude) $SourceFiles = Get-ChildItem -Path $SourceModuleDir -Include $PSFileInclude -Exclude $PSFileExclude -File -Recurse if (-not $NoCompile) { #TODO: Apply ordering if important (e.g. classes) #Collate the files, pulling out using lines because these have to go first [String[]]$UsingLines = @() [String]$CombinedSourceFiles = ((Get-Content -Raw $SourceFiles) -split '\r?\n' | Where-Object { if ($_ -match '^using .+$') { $UsingLines += $_ return $false } return $true }) -join [Environment]::NewLine #If a SourceInit region was set, inject the files there, otherwise just append to the end. $sourceRegionRegex = "(?s)#region $SourceRegionName.+#endregion $SourceRegionName" if ($SourceRootModule -match $sourceRegionRegex) { #Need to escape the $ in the replacement string $RegexEscapedCombinedSourceFiles = [String]$CombinedSourceFiles.replace('$','$$') $SourceRootModule = $SourceRootModule -replace $sourceRegionRegex,$RegexEscapedCombinedSourceFiles } else { #Just add them to the end of the file $SourceRootModule += [Environment]::NewLine + $CombinedSourceFiles } #Use a stringbuilder to piece the portions of the config back together, with using statements up-front [Text.StringBuilder]$OutputRootModule = '' if ($UsingLines) { $UsingLines.trim() | Sort-Object -Unique | ForEach-Object { [void]$OutputRootModule.AppendLine($PSItem) } } [void]$OutputRootModule.AppendLine($SourceRootModule) [String]$SourceRootModule = $OutputRootModule #Strip non-help-related comments and whitespace #[String]$SourceRootModule = Remove-CommentsAndWhiteSpace $SourceRootModule } else { #TODO: Track all files in the source directory to ensure none get missed on the second step try { #In order to get relative paths we have to be in the directory we want to be relative to Push-Location (Split-Path $PSModuleManifest) $SourceFiles | ForEach-Object { #Powershell 6+ Preferred way. #TODO: Enable when dropping support for building on 5.x #$RelativePath = [io.path]::GetRelativePath($SourceModuleDir,$PSItem.fullname) #Powershell 3.x compatible "Ugly" Regex method #$RelativePath = $PSItem.FullName -replace [Regex]::Escape($SourceModuleDir),'' $RelativePath = Resolve-Path $PSItem.FullName -Relative #Copy-Item doesn't automatically create directory structures when copying files vs. directories $DestinationPath = Join-Path $DestinationDirectory $RelativePath $DestinationDir = Split-Path $DestinationPath if (-not (Test-Path $DestinationDir)) { New-Item -ItemType Directory $DestinationDir > $null } $copiedItems = Copy-Item -Path $PSItem -Destination $DestinationPath -PassThru #Update file timestamps for Invoke-Build Incremental Build detection $copiedItems.foreach{ $PSItem.LastWriteTime = [DateTime]::Now } } } catch { throw } finally { #Return after processing relative paths Pop-Location } } #Output the (potentially) modified Root Module $SourceRootModule | Out-File -FilePath (Join-Path $DestinationDirectory $SourceManifest.RootModule) #If there is a "lib" folder, copy that as-is if (Test-Path "$SourceModuleDir\lib") { Write-Verbose 'lib folder detected, copying entire contents' $copiedItems = Copy-Item -Recurse -Force -Path "$SourceModuleDir\lib" -Destination $DestinationDirectory -PassThru $copiedItems.foreach{ $PSItem.LastWriteTime = [DateTime]::Now } } #Copy the Module Manifest $OutputModuleManifest = Copy-Item -PassThru -Path $PSModuleManifest -Destination $DestinationDirectory $OutputModuleManifest.foreach{ $PSItem.LastWriteTime = [DateTime]::Now } $OutputModuleManifest = [String]$OutputModuleManifest #Additional files to include if ($Include) { $copiedItems = $Include | Copy-Item -Destination $DestinationDirectory -PassThru $copiedItems.foreach{ $PSItem.LastWriteTime = [DateTime]::Now } } #Add a prerelease if (-not $NoPreReleaseFile) { 'This is a prerelease build and not meant for deployment!' | Out-File -FilePath (Join-Path $DestinationDirectory "PRERELEASE-$($PressSetting.VersionLabel)") } return [PSCustomObject]@{ OutputModuleManifest = $OutputModuleManifest } } <# .SYNOPSIS Fetch the names of public functions in the specified folder using AST .DESCRIPTION This is a better method than grabbing the names of the .ps1 file and "hoping" they line up. This also only gets parent functions, child functions need not apply #> #TODO: Better Function handling: Require function to be the same name as the file. Accessory private functions are OK. function Get-PublicFunctions { [CmdletBinding()] param( #The path to the public module directory containing the modules. Defaults to the "Public" folder where the source module manifest resides. [Parameter(Mandatory)][String[]]$PublicModulePath ) $publicFunctionFiles = Get-ChildItem $PublicModulePath -Filter '*.ps1' | Where-Object Name -NotMatch '\.\w+?\.ps1$' #Exclude Tests.ps1, etc. #TODO: Make this a PSSetting foreach ($fileItem in $publicFunctionFiles) { $scriptContent = Get-Content -Raw $fileItem $functionNames = [ScriptBlock]::Create($scriptContent).AST.EndBlock.Statements | Where-Object { $PSItem -is [Management.Automation.Language.FunctionDefinitionAst] } | ForEach-Object Name $functionName = $FileItem.BaseName if ($functionName -notin $functionNames) { Write-Warning "$fileItem`: There is no function named $functionName in $fileItem, please ensure your public function is named the same as the file. Discovered functions: $functionNames" continue } Write-Verbose "Discovered public function $functionName in $fileItem" Write-Output $functionName } } function Get-Setting { [CmdletBinding()] param ( #Base Configuration Path to search for configurations [Parameter(Mandatory)][String]$ConfigBase, #Additional YAML configurations to add to the configuration [String[]]$YamlConfigurationPath, #Build Output Directory Name. Defaults to Get-BuildEnvironment Default which is 'BuildOutput' $BuildOutput = 'BuildOutput' ) $Settings = [ordered]@{} $Settings.BuildEnvironment = (Get-BuildEnvironment -BuildOutput $BuildOutput -As Hashtable).AsReadOnly() $Settings.General = [ordered]@{ # Root directory for the project ProjectRoot = $Settings.BuildEnvironment.ProjectPath # Root directory for the module SrcRootDir = $Settings.BuildEnvironment.ModulePath # The name of the module. This should match the basename of the PSD1 file ModuleName = $Settings.BuildEnvironment.ProjectName # Module version ModuleVersion = (Import-PowerShellDataFile -Path $Settings.BuildEnvironment.PSModuleManifest).ModuleVersion # Module manifest path ModuleManifestPath = $Settings.BuildEnvironment.PSModuleManifest } $Settings.Build = [ordered]@{ Dependencies = @('StageFiles', 'BuildHelp') # Default Output directory when building a module OutDir = $Settings.BuildEnvironment.BuildOutput # Module output directory # override the top-level 'OutDir' above and compute the full path to the module internally ModuleOutDir = Join-Path $Settings.BuildEnvironment.BuildOutput $Settings.BuildEnvironment.ProjectName # Controls whether to "compile" module into single PSM1 or not CompileModule = $true # List of files to exclude from output directory Exclude = @() } $Settings.Test = [ordered]@{ # Enable/disable Pester tests Enabled = $true # Directory containing Pester tests RootDir = Join-Path -Path $Settings.BuildEnvironment.ProjectPath -ChildPath tests # Specifies an output file path to send to Invoke-Pester's -OutputFile parameter. # This is typically used to write out test results so that they can be sent to a CI # system like AppVeyor. OutputFile = ([IO.Path]::Combine($Settings.Environment.BuildOutput,"$($Settings.Environment.ProjectName)-TestResults_PS$($psversiontable.psversion)`_$(get-date -format yyyyMMdd-HHmmss).xml")) # Specifies the test output format to use when the TestOutputFile property is given # a path. This parameter is passed through to Invoke-Pester's -OutputFormat parameter. OutputFormat = 'NUnitXml' ScriptAnalysis = [ordered]@{ # Enable/disable use of PSScriptAnalyzer to perform script analysis Enabled = $true # When PSScriptAnalyzer is enabled, control which severity level will generate a build failure. # Valid values are Error, Warning, Information and None. "None" will report errors but will not # cause a build failure. "Error" will fail the build only on diagnostic records that are of # severity error. "Warning" will fail the build on Warning and Error diagnostic records. # "Any" will fail the build on any diagnostic record, regardless of severity. FailBuildOnSeverityLevel = 'Error' # Path to the PSScriptAnalyzer settings file. SettingsPath = Join-Path $PSScriptRoot -ChildPath ScriptAnalyzerSettings.psd1 } CodeCoverage = [ordered]@{ # Enable/disable Pester code coverage reporting. Enabled = $false # Fail Pester code coverage test if below this threshold Threshold = .75 # CodeCoverageFiles specifies the files to perform code coverage analysis on. This property # acts as a direct input to the Pester -CodeCoverage parameter, so will support constructions # like the ones found here: https://github.com/pester/Pester/wiki/Code-Coverage. Files = @( Join-Path -Path $Settings.BuildEnvironment.ModulePath -ChildPath '*.ps1' Join-Path -Path $Settings.BuildEnvironment.ModulePath -ChildPath '*.psm1' ) } } $Settings.Help = [ordered]@{ # Path to updateable help CAB UpdatableHelpOutDir = Join-Path -Path $Settings.Build.ModuleOutDir -ChildPath 'UpdatableHelp' # Default Locale used for help generation, defaults to en-US DefaultLocale = (Get-UICulture).Name # Convert project readme into the module about file ConvertReadMeToAboutHelp = $false } $Settings.Docs = [ordered]@{ # Directory PlatyPS markdown documentation will be saved to RootDir = Join-Path -Path $Settings.Build.ModuleOutDir -ChildPath 'docs' } $Settings.Publish = [ordered]@{ # PowerShell repository name to publish modules to PSRepository = 'PSGallery' # API key to authenticate to PowerShell repository with PSRepositoryApiKey = $env:PSGALLERY_API_KEY # Credential to authenticate to PowerShell repository with PSRepositoryCredential = $null } # Enable/disable generation of a catalog (.cat) file for the module. # [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')] # $catalogGenerationEnabled = $true # # Select the hash version to use for the catalog file: 1 for SHA1 (compat with Windows 7 and # # Windows Server 2008 R2), 2 for SHA2 to support only newer Windows versions. # [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')] # $catalogVersion = 2 $PowerConfig = New-PowerConfig | Add-PowerConfigObject -Object $Settings -WarningAction silentlyContinue #TODO: Find and suppress the json depth warning | Add-PowerConfigYamlSource -Path (Join-Path (Resolve-Path $ConfigBase) '.config/press.yml') foreach ($path in $yamlConfigurationPath) { $ErrorActionPreference = 'Stop' $PowerConfig = $PowerConfig | Add-PowerConfigYamlSource -Mandatory -Path $path -ErrorAction Stop } #Environment variables are added last to take the highest precedence $PowerConfig = $PowerConfig | Add-PowerConfigEnvironmentVariableSource -Prefix 'PRESS_' return Get-PowerConfig $PowerConfig } function Get-Version { [CmdletBinding()] param( [Parameter(Mandatory)][String]$ProjectPath, [Version]$GitVersionVersion = '5.6.6', [String]$GitVersionConfigPath = $(Resolve-Path (Join-Path $MyInvocation.MyCommand.Module.ModuleBase '.\GitVersion.default.yml')) ) Write-Verbose "Using Gitversion Configuration $GitVersionConfigPath" $gitCommand = Get-Command -CommandType Application -Name 'git' if (-not $gitCommand) { throw 'git was not found in your path. Ensure git is installed and can be found.' } if (-not (Test-Path "$ProjectPath\.git")) { throw "No Git repository (.git folder) found in $ProjectPath. Please specify a folder with a git repository" } if (-not (Test-Path "$ProjectPath\.config\dotnet-tools.json")) { [String]$dotnetNewManifestStatus = & dotnet new tool-manifest -o $ProjectPath *>&1 if ($dotnetNewManifestStatus -notlike '*was created successfully*') { throw "There was an error creating a dotnet tool manifest: $dotnetNewManifestStatus" } if (-not (Get-Item "$ProjectPath\.config\dotnet-tools.json")) { throw "The manifest command completed successfully but $ProjectPath\.config\dotnet-tools.json still could not be found. This is probably a bug." } } #TODO: Direct Json check maybe? [String]$gitVersionToolCheck = & dotnet tool list if ($gitVersionToolCheck -notlike "*gitversion.tool*$GitVersionVersion*") { & dotnet tool uninstall gitversion.tool | Out-Null [String]$gitversionInstall = & dotnet tool install gitversion.tool --version $GitVersionVersion if ($gitVersionInstall -notlike '*gitversion.tool*was successfully installed*') { throw "Failed to install gitversion local tool with dotnet: $gitVersionInstall" } } #Output from a command is String in Windows and Object[] in Linux. Cast to string to normalize. [String]$dotnetToolRestoreStatus = & dotnet tool restore *>&1 $dotnetToolMatch = "*Tool 'gitversion.tool' (version '$GitVersionVersion') was restored.*" if ($dotnetToolRestoreStatus -notlike $dotnetToolMatch) { throw "GitVersion dotnet tool was not found. Ensure you have a .NET manifest. Message: $dotnetToolRestoreStatus" } #Reference Dotnet Local Tool directly rather than trying to go through .NET EXE #This appears to be an issue where dotnet is installed but the tools aren't added to the path for Linux # $GitVersionExe = "$HOME\.dotnet\tools/dotnet-gitversion" $DotNetExe = "dotnet" [String[]]$GitVersionParams = 'gitversion',$ProjectPath,'/nofetch' if (-not ( $ProjectPath -and (Test-Path ( Join-Path $ProjectPath 'GitVersion.yml' )) )) { #Use the Press Builtin $GitVersionParams += '/config' $GitVersionParams += $GitVersionConfigPath } try { if ($DebugPreference -eq 'Continue') { $GitVersionParams += '/diag' } [String[]]$GitVersionOutput = & $DotNetExe @GitVersionParams *>&1 if (-not $GitVersionOutput) { throw 'GitVersion returned no output. Are you sure it ran successfully?' } if ($LASTEXITCODE -ne 0) { if ($GitVersionOutput -like '*GitVersion.GitVersionException: No commits found on the current branch*') { #TODO: Auto-version calc maybe? throw 'There are no commits on your current git branch. Please make at least one commit before trying to calculate the version' } throw "GitVersion returned exit code $LASTEXITCODE. Output:`n$GitVersionOutput" } #Split Diagnostic Messages from Regex $i = 0 foreach ($lineItem in $GitVersionOutput) { if ($GitVersionOutput[$i] -eq '{') { break } $i++ } if ($i -ne 0) { [String[]]$diagMessages = $GitVersionOutput[0..($i - 1)] $diagMessages | Write-Debug } #Should not normally get this far if there are errors if ($diagMessages -match 'ERROR \[') { throw "An error occured when running GitVersion.exe in $ProjectPath. Diag Message: `n$diagMessages" } #There is some trailing debug info sometimes $jsonResult = $GitVersionOutput[$i..($GitVersionOutput.count - 1)] | Where-Object { $_ -notmatch 'Info.+Done writing' } $GitVersionInfo = $jsonResult | ConvertFrom-Json -ErrorAction stop #Fixup prerelease tag for Powershell modules if ($GitVersionInfo.NuGetPreReleaseTagV2 -match '[-.]') { Write-Verbose 'Detected invalid characters for Powershell Gallery Prerelease Tag. Fixing it up.' $GitVersionInfo.NuGetPreReleaseTagV2 = $GitVersionInfo.NuGetPreReleaseTagV2 -replace '[\-\.]','' $GitVersionInfo.NuGetVersionV2 = ($GitVersionInfo.MajorMinorPatch,$GitVersionInfo.NuGetPreReleaseTagV2).Where{$PSItem} -join '-' } $GitVersionResult = $GitVersionInfo | Select-Object BranchName, MajorMinorPatch, NuGetVersionV2, NuGetPreReleaseTagV2 | Format-List | Out-String Write-Verbose "Gitversion Result: `n$($GitVersionResult | Format-List | Out-String)" } catch { throw "There was an error when running GitVersion.exe $buildRoot`: $PSItem. The output of the command (if any) is below...`r`n$GitVersionOutput" } return $GitVersionInfo } function Invoke-Clean { [CmdletBinding()] param ( [Parameter(Mandatory)]$BuildOutputPath, [Parameter(Mandatory)]$buildProjectName, [Switch]$Prerequisites ) #Taken from Invoke-Build because it does not preserve the command in the scope this function normally runs #Copyright (c) Roman function Remove-BuildItem { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Private Function')] param( [Parameter(Mandatory)][string[]]$Path ) if ($Path -match '^[.*/\\]*$') { *Die 'Not allowed paths.' 5 } $v = $PSBoundParameters['Verbose'] try { foreach ($_ in $Path) { if (Get-Item $_ -Force -ErrorAction 0) { if ($v) { Write-Verbose "remove: removing $_" -Verbose } Remove-Item $_ -Force -Recurse -ErrorAction stop } elseif ($v) { Write-Verbose "remove: skipping $_" -Verbose } } } catch { throw $_ } } #Reset the BuildOutput Directory if (Test-Path $buildOutputPath) { Write-Verbose "Removing and resetting Build Output Path: $buildOutputPath" Remove-BuildItem -Path $buildOutputPath } if ($Prerequisites) { $PrerequisitePath = (Join-Path ([Environment]::GetFolderpath('LocalApplicationData')) 'Press') Write-Verbose "Removing and resetting Press Prerequisites: $PrerequisitePath" Remove-BuildItem $PrerequisitePath -Verbose:$false } New-Item -Type Directory $BuildOutputPath > $null #META: Force reload Press if building press if ($buildProjectName -eq 'Press') { Write-Verbose 'Detected Press Meta-Build' Import-Module -Force -Global -Verbose -Name (Join-Path $MyInvocation.MyCommand.Module.ModuleBase 'Press.psd1') } else { #Unmount any modules named the same as our module Remove-Module $buildProjectName -Verbose:$false -ErrorAction SilentlyContinue } } function New-NugetPackage { <# .SYNOPSIS Creates a Nuget Package from a Powershell Module .OUTPUTS System.String. The path to the generated nuget package file #> [CmdletBinding(SupportsShouldProcess)] param ( #Path to the module manifest to build [Parameter(Mandatory)][String]$Path, #Where to output the new module package. Specify a folder [Parameter(Mandatory)][String]$Destination, #Whether the license must be accepted to use this package [Switch]$RequireLicenseAcceptance ) $ErrorActionPreference = 'Stop' $ModuleName = (Get-Item $Path).basename $ModuleMetaData = Import-PowerShellDataFile $Path $ModuleDir = Split-Path $Path #Fast Method but skips some metadata. Doesn't matter for non-powershell gallery publishes #Call private method in PSGetv2 #Also replicates and simplifies some logic from https://github1s.com/PowerShell/PowerShellGetv2/src/PowerShellGet/private/functions/Publish-PSArtifactUtility.ps1 if ($PSCmdlet.ShouldProcess($Destination, "Build Nuget Package for $ModuleName")) { $psGetv2 = Import-Module PowershellGet -PassThru -MaximumVersion 2.99.99 -MinimumVersion 2.2.5 $version = @($ModuleMetaData.ModuleVersion, $ModuleMetaData.PrivateData.PSData.Prerelease) | Where-Object { $_ } | Join-String -Separator '-' $newNuSpecFileParams = @{ OutputPath = $ModuleDir Id = $ModuleName Version = $version Description = $ModuleMetaData.Description Authors = $ModuleMetaData.Author } $PSData = $ModuleMetaData.PrivateData.PSData $metadata = @{ Owners = $PSData.CompanyName Files = $ModuleMetaData.FileList LicenseUrl = $PSData.LicenseUrl ProjectUrl = $PSData.ProjectUrl IconUrl = $PSData.IconUrl ReleaseNotes = $PSData.ReleaseNotes Tags = $PSData.Tags RequireLicenseAcceptance = $PSData.RequireLicenseAcceptance } $metadata.Tags += $ModuleMetaData.FunctionsToExport.foreach{ if ($_ -ne '*') { "PSCommand_$PSItem" } } $metadata.Tags += $ModuleMetaData.CmdletsToExport.foreach{ if ($_ -ne '*') { "PSCmdlet_$PSItem" } } $metadata.Tags += $ModuleMetaData.DscResourcesToExport.foreach{ if ($_ -ne '*') { "DscResource_$PSItem" } } $metadata.Dependencies = $ModuleMetaData.RequiredModules.foreach{ $spec = [ModuleSpecification]::new($PSItem) [PSCustomObject]@{ id = $spec.Name version = ConvertTo-NugetVersion $Spec } } $metadata. GetEnumerator(). where{ $null -ne $PSItem.Value }. foreach{ $newNuSpecFileParams[$PSItem.Name] = $PSItem.Value } $NuSpecPath = & ($psGetv2) New-NuSpecFile @newNuSpecFileParams $newNugetPackageParams = @{ UseDotnetCli = $true NuSpecPath = $NuSpecPath NuGetPackageRoot = Split-Path $Path OutputPath = $Destination } try { $nuGetPackagePath = & ($PSGetv2) New-NugetPackage @newNugetPackageParams Write-Verbose "Created NuGet Package at $nuGetPackagePath" return $nuGetPackagePath } catch { throw } finally { Remove-Item $NuSpecPath } } } #Adapted from Publish-PSArtifactUtility function ConvertTo-NugetVersion { param( [ModuleSpecification]$spec ) switch ($true) { $spec.RequiredVersion { '[{0}]' -f $spec.RequiredVersion break } ($spec.MinimumVersion -and $spec.MaximumVersion) { '[{0},{1}]' -f $spec.RequiredVersion, $spec.MaximumVersion break } $spec.MaximumVersion { '(,{0}]' -f $spec.MaximumVersion break } $spec.MinimumVersion { $spec.MinimumVersion } } } <# .SYNOPSIS Retrieves the dotnet dependencies for a powershell module .NOTES This process basically builds a C# Powershell Standard Library and identifies the resulting assemblies. There is probably a more lightweight way to do this. .EXAMPLE Get-PSModuleNugetDependencies @{'System.Text.Json'='4.6.0'} #> function Restore-NugetPackages { [CmdletBinding(SupportsShouldProcess,DefaultParameterSetName = 'String')] param ( #A list of nuget packages to include. You can specify a nuget-style version with a / separator e.g. yamldotnet/3.2.* [Parameter(ParameterSetName = 'String',Mandatory,Position = 0)][String[]]$PackageName, #Which packages and their associated versions to include, in hashtable form. Supports Nuget Versioning: https://docs.microsoft.com/en-us/nuget/concepts/package-versioning#version-ranges-and-wildcards [Parameter(ParameterSetName = 'Hashtable',Mandatory,Position = 0)][HashTable]$Packages, #Which .NET Framework target to use. Defaults to .NET Standard 2.0 and is what you should use for PS5+ compatible modules [String]$Target = 'netstandard2.0', #Where to output the resultant assembly files. Default is a new folder 'lib' in the current directory. [Parameter(Position = 1)][String]$Destination, #Which PS Standard library to use. Defaults to 7.0.0-preview.1. [String]$PowershellTarget = '7.0.0-preview.1', [String]$BuildPath = (Join-Path ([io.path]::GetTempPath()) "PSModuleDeps-$((New-Guid).Guid)"), #Name of the build project. You normally don't need to change this. [String]$BuildProjectName = 'PSModuleDeps', #Whether to output the resultant copied file paths [Switch]$PassThru, #Whether to do an online restore check of the dependencies. Disable this to speed up the process at the risk of compatibility. [Switch]$NoRestore ) if ($PSCmdlet.ParameterSetName -eq 'String') { $Packages = @{} $PackageName.Foreach{ $PackageVersion = $PSItem -split '/' if ($PackageVersion.count -eq 2) { $Packages[$PackageVersion[0]] = $PackageVersion[1] } else { $Packages[$PSItem] = '*' } } } #Add Powershell Standard Library $Packages['PowerShellStandard.Library'] = $PowershellTarget if (-not ([version](dotnet --version) -ge 2.2)) { throw 'dotnet 2.2 or later is required. Make sure you have the .net core SDK 2.x+ installed' } #Add starter Project for netstandard 2.0 $BuildProjectFile = Join-Path $BuildPath "$BuildProjectName.csproj" New-Item -ItemType Directory $BuildPath -Force > $null @" <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>$Target</TargetFramework> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> </PropertyGroup> <ItemGroup> <PackageReference Include="PowerShellStandard.Library" Version="$PowerShellTarget"> <PrivateAssets>All</PrivateAssets> </PackageReference> </ItemGroup> </Project> "@ | Out-File -FilePath $BuildProjectFile foreach ($ModuleItem in $Packages.keys) { $dotnetArgs = 'add',$BuildProjectFile,'package',$ModuleItem if ($Packages[$ModuleItem] -ne $true) { if ($NoRestore) { $dotNetArgs += '--no-restore' } $dotnetArgs += '--version' $dotnetArgs += $Packages[$ModuleItem] } Write-Verbose "Executing: dotnet $dotnetArgs" & dotnet $dotnetArgs | Write-Verbose } & dotnet publish -o $BuildPath $BuildProjectFile | Write-Verbose function ConvertFromModuleDeps ($Path) { $runtimeDeps = Get-Content -Raw $Path | ConvertFrom-Json $depResult = [ordered]@{} $TargetFullName = $runtimeDeps.targets[0].psobject.properties.name $runtimeDeps.targets.$TargetFullName.psobject.Properties.name | Where-Object { $PSItem -notlike "$BuildProjectName*" } | Sort-Object | ForEach-Object { $depInfo = $PSItem -split '/' $depResult[$depInfo[0]] = $depInfo[1] } return $depResult } #Use return to end script here and don't actually copy the files $ModuleDeps = ConvertFromModuleDeps -Path $BuildPath/obj/project.assets.json if (-not $Destination) { #Output the Module Dependencies and end here Remove-Item $BuildPath -Force -Recurse return $ModuleDeps } if ($PSCmdlet.ShouldProcess($Destination,'Copy Resultant DLL Assemblies')) { New-Item -ItemType Directory $Destination -Force > $null $CopyItemParams = @{ Path = "$BuildPath/*.dll" Exclude = "$BuildProjectName.dll" Destination = $Destination Force = $true } if ($PassThru) { $CopyItemParams.PassThru = $true } Copy-Item @CopyItemParams Remove-Item $BuildPath -Force -Recurse } } function Set-ReleaseNotes { [CmdletBinding(SupportsShouldProcess)] param( #Path to the module manifest [Parameter(Mandatory, Position = 0)][String]$Path, #Content to replace the release notes with [Parameter(Mandatory, ValueFromPipeline)][String]$Content ) process { $ErrorActionPreference = 'Stop' [string]$ReleaseNotes = (Import-PowerShellDataFile $Path).PrivateData.PSData.ReleaseNotes $ReleaseNotesCompare = [text.encoding]::UTF8.GetBytes($ReleaseNotes) | Where-Object { $_ -notin 10, 13 } $ReleaseNotesNewCompare = [text.encoding]::UTF8.GetBytes($Content) | Where-Object { $_ -notin 10, 13 } if (-not $ReleaseNotes -or (Compare-Object $ReleaseNotesCompare $ReleaseNotesNewCompare)) { #BUG: Do not use update-modulemanifest #Reference: https://github.com/PowerShell/PowerShellGetv2/issues/294 Write-Verbose "Detected Release Notes update, updating ReleaseNotes property of $Path" try { Update-Metadata -Path $Path -Property ReleaseNotes -Value $Content.Trim() } catch [ItemNotFoundException] { if ($PSItem -notmatch "Can't find") { throw } #TODO: Automatically add it if not present 'Your module .psd1 template must have a PrivateData.PSData.ReleaseNotes property ' + 'uncommented for Set-ReleaseNotes to work' | Write-Error return } } else { Write-Verbose "Release Notes in $Path do not require updating" } } } <# .SYNOPSIS Sets the version on a powershell Module #> function Set-Version { [CmdletBinding(SupportsShouldProcess)] param ( #Path to the module manifest to update [String][Parameter(Mandatory)]$Path, #Version to set for the module [Version][Parameter(Mandatory)]$Version, #Prerelease tag to add to the module, if any [AllowEmptyString()][String]$PreRelease ) #Default is to update version so no propertyname specified $Manifest = Import-PowerShellDataFile $Path $currentVersion = $Manifest.ModuleVersion # $currentVersion = Get-Metadata -Path $Path -PropertyName 'ModuleVersion' if ($currentVersion -ne $Version) { Write-Verbose "Current Manifest Version $currentVersion doesn't match $Version. Updating..." if ($PSCmdlet.ShouldProcess($Path, "Set ModuleVersion to $Version")) { Update-Metadata -Path $Path -PropertyName ModuleVersion -Value $Version } } $currentPreRelease = $Manifest.privatedata.psdata.prerelease if ($PreRelease) { if ($currentPreRelease -ne $PreRelease) { Write-Verbose "Current Manifest Prerelease Tag $currentPreRelease doesn't match $PreRelease. Updating..." #HACK: Do not use update-modulemanifest because https://github.com/PowerShell/PowerShellGetv2/issues/294 #TODO: AutoCreate prerelease metadata try { if ($PSCmdlet.ShouldProcess($Path, "Set Prerelease Version to $PreRelease")) { Update-Metadata -Path $Path -PropertyName PreRelease -Value $PreRelease } } catch { if ($PSItem -like "Can't find*") { throw 'Could not find the Prerelease field in your source manifest file. You must add this under PrivateData/PSData first' } } } } elseif ($CurrentPreRelease -ne '') { if ($PSCmdlet.ShouldProcess($Path, "Remove $PreRelease if it is present")) { Update-Metadata -Path $Path -PropertyName PreRelease -Value '' } } } function Test-Pester { [CmdletBinding(DefaultParameterSetName = 'Default')] param ( #Root path to search for tests to run [Parameter(Mandatory, ParameterSetName = 'Default')][String]$Path, #Path where the coverage files should be output. Defaults to the build output path. [Parameter(Mandatory, ParameterSetName = 'Default')][String]$OutputPath, #Test files that should be excluded from the run, for example, Pester Mock Tests [Parameter(ParameterSetName = 'Default')][String[]]$ExcludePath, #A PesterConfiguration hashtable to use instead of the intelligent defaults. For advanced usage only. [Parameter(ParameterSetName = 'Configuration')][hashtable]$Configuration, #Run the result as a separate job, useful if testing modules with assemblies since they cannot be removed from #state. This is different than '-AsJob' because -AsJob typically drops to a job and this just runs it in a job [Switch]$InJob, #Run the job in Windows Powershell 5.1, if available. It will warn and proceed if it is not available (e.g. on Linux) [Switch]$UseWindowsPowershell, #No Write-Host Output, just produce the passthru and output file [Switch]$Quiet ) #Load Pester Just-In-Time style, cannot use a requires because of module compilation #TODO: Allow Pester Version to be customizable Get-Module Pester -ErrorAction SilentlyContinue | Where-Object Version -LT '5.2.0' | Remove-Module -Force Import-Module Pester -MinimumVersion '5.2.0' -PassThru | Write-Verbose #FIXME: Allow for custom configurations once we figure out how to serialize them into a job. For now we just do a hashtable # if ($Configuration) { throw [NotSupportedException]'Custom Pester Configurations temporarily disabled while sorting out best way to run them in isolated job' } if (-not $Configuration) { $Configuration = @{ Output = @{ Verbosity = 'Detailed' } Run = @{ PassThru = $true Path = $Path } TestResult = @{ Enabled = $true OutputPath = "$OutputPath/TEST-Results.xml" } } #If we are in vscode, add the VSCodeMarkers if ($host.name -match 'Visual Studio Code') { Write-Host -Fore Green '===Detected Visual Studio Code, Displaying Pester Test Links===' $Configuration.Debug = @{ ShowNavigationMarkers = $true } } } if ($Quiet) { $Configuration.Output.Verbosity = 'None' } if ($ExcludePath) { $Configuration.Run.ExcludePath = $ExcludePath } #Validate the configuration before we pass it to the job try { [void][PesterConfiguration]::new($Configuration) } catch [MethodInvocationException] { if ($PSItem.FullyQualifiedErrorId -eq 'NullReferenceException') { throw 'An invalid Pester Configuration was provided. An entry was present that doesnt match the Pester configuration schema. Check for typos and make sure that all entries match what is in [PesterConfiguration]::Default' } } #Workaround for PesterConfiguration/hashtables not serializing correctly #Reference: https://github.com/pester/Pester/issues/1977 $configurationJson = $Configuration | ConvertTo-Json -Compress $TestResults = if ($InJob) { $StartJobParams = @{ ScriptBlock = { #Workaround for PSIC Bug #Reference: https://github.com/PowerShell/vscode-powershell/issues/3399 $winPSUserModPath = "$HOME/Documents/WindowsPowershell/Modules" if ( $PSEdition -eq 'Desktop' -and (Test-Path $winPSUserModPath) -and $ENV:PSModulePath.split([IO.Path]::PathSeparator) -notcontains $(Resolve-Path $winPSUserModPath) ) { $ENV:PSModulePath = @($(Resolve-Path $winPSUserModPath), $ENV:PSModulePath). Where{ $PSItem } -join [IO.Path]::PathSeparator } #Require a modern version of Pester on 5.1 try { #TODO: Centralize the Pester Dependency version Import-Module -Name Pester -MinimumVersion 5.2.0 } catch { #TODO: Search for it in pwsh folders or via virtual environment throw 'You must have Pester 5.2 or greater installed in your Windows Powershell 5.1 session. Hint: Install-Module Pester -MinimumVersion 5.2.0 -Scope CurrentUser' } Set-Location $USING:PWD $configuration = [PesterConfiguration]$($USING:configurationJson | ConvertFrom-Json) Invoke-Pester -Configuration $configuration } } if ($UseWindowsPowershell) { try { Get-Command powershell.exe -ErrorAction stop | Out-Null } catch { Write-Error [NotSupportedException]'Test-Pester: -UseWindowsPowershell was specified but Powershell 5.1 is not available on this system.' return } $StartJobParams.PSVersion = '5.1' } Start-Job @StartJobParams | Receive-Job -Wait } else { Invoke-Pester -Configuration $Configuration } if ($Configuration.Run.PassThru) { if ($TestResults.Result -ne 'Passed') { throw "Failed $($TestResults.FailedCount) tests" } } Write-Output $TestResults } #requires -version 7 #because it uses System.Management.Automation.SemanticVersion function Update-GithubRelease { [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory)][String]$Owner, [Parameter(Mandatory)][String]$Repository, [Parameter(Mandatory)][SemVer]$Version, [Parameter(Mandatory)][String]$AccessToken, [Parameter(Mandatory)][String]$Body, [String[]]$ArtifactPath, #Skip cleanup of old drafts within the same major version [Switch]$NoCleanup ) $Tag = "v$Version" $gitHubParams = @{ OwnerName = $Owner RepositoryName = $Repository AccessToken = $accessToken Verbose = $Verbose } #Get all releases of the same major version [Collections.ArrayList]$existingReleases = @( Get-GitHubRelease @gitHubParams | Where-Object { #Skip nulls if ($null -eq $PSItem) { continue } #TODO: Allow custom version prefix [Semver]$releaseVersion = $PSItem.tag_name -replace 'v','' ($releaseVersion.Major -eq $Version.Major) } ) #Cleanup older drafts within the same major release if they exist if (-not $NoCleanup) { if ($PSCmdlet.ShouldProcess('Existing Github Releases', 'Remove all existing draft releases with same major version')) { $removedReleases = $existingReleases | Where-Object Draft | Where-Object tag_name -NE $Tag | ForEach-Object { Write-Verbose "Detected Older Draft Release for $($PSItem.tag_name) older minor version than $Version, removing" $PSItem | Remove-GitHubRelease @gitHubParams -ErrorAction Stop -Force Write-Output $PSItem } } } $removedReleases.foreach{ $existingReleases.Remove($removedReleases) } [Collections.ArrayList]$taggedRelease = @( $existingReleases | Where-Object tag_name -EQ $Tag ) #There should be only one release per tag if ($taggedRelease.count -gt 1) { Write-Warning "Multiple Releases found for $Tag. Will attempt to arrive at a single candidate release" #Attempt to resolve the "best" release and remove the rest $removedReleases = $taggedRelease | Sort-Object published_at,draft,created_at -Descending | Select-Object -Skip 1 | ForEach-Object { Write-Verbose "Detected duplicate Release for $($PSItem.tag_name) with ID $($PSItem.ReleaseID), removing" $PSItem | Remove-GitHubRelease @gitHubParams -ErrorAction Stop -Force Write-Output $PSItem } $removedReleases.foreach{ $taggedRelease.Remove($removedReleases) } #Should be zero or one at this point if ($taggedRelease.count -gt 1) { throw "Unable to resolve to a single release for tag $Tag. This is probably a bug. Items: $taggedRelease" } } $ghReleaseParams = $gitHubParams.Clone() $ghReleaseParams.Body = $Body $ghReleaseParams.Name = "$Repository $Tag" #If a release exists, update the existing one, otherwise create a new draft release #TODO: Add option to re-create so that the date of the release creation updates $releaseResult = if ($taggedRelease -and $taggedRelease.count -eq 1) { #Update Release Notes $taggedRelease | Set-GitHubRelease @ghReleaseParams -Tag $Tag -PassThru } else { New-GitHubRelease @ghReleaseParams -Draft -Tag $Tag } Write-Output $releaseResult #Update artifacts if required by pulling the existing artifacts and replacing them Write-Verbose "Github Artifacts: $artifactPath" if ($artifactPath) { Write-Verbose "Uploading Github Artifacts: $artifactPath" $releaseResult | Get-GitHubReleaseAsset @gitHubParams | Remove-GitHubReleaseAsset @gitHubParams -Force $artifactPath | New-GitHubReleaseAsset @gitHubParams -Release $releaseResult.releaseID | Out-Null } } <# .SYNOPSIS This function sets a module manifest for the various function exports that are present in a module such as private/public functions, classes, etc. #> function Update-PublicFunctions { [CmdletBinding(SupportsShouldProcess)] param( #Path to the module manifest to update [Parameter(Mandatory)][String]$Path, #Paths to the module public function files [String]$PublicFunctionPath, #Optionally Specify the list of functions to override auto-detection [String[]]$Functions ) if (-not $Functions) { $Functions = Get-PublicFunctions $PublicFunctionPath } if (-not $Functions) { write-warning "No functions found in the powershell module. Did you define any yet? Create a new one called something like New-MyFunction.ps1 in the Public folder" return } $currentFunctions = Get-Metadata -Path $Path -PropertyName FunctionsToExport if (Compare-Object $currentFunctions $functions) { Write-Verbose "Current Function List in manifest doesn't match. Current: $currentFunctions New: $Functions. Updating." #HACK: Don't use Update-ModuleManifest because of https://github.com/PowerShell/PowerShellGetv2/issues/294 if ($PSCmdlet.ShouldProcess($Path, "Add Functions $($Functions -join ', ')")) { BuildHelpers\Update-Metadata -Path $Path -PropertyName FunctionsToExport -Value $Functions } } } $taskAliasName = '.Tasks' Set-Alias -Name $taskAliasName -Value (Join-Path $PSScriptRoot 'Press.tasks.ps1') Export-ModuleMember -Alias $taskAliasName |