Skip to content

Commit

Permalink
Fix missing commands in bootstrap script (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Feb 2, 2024
1 parent c017b49 commit 2178a3c
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 57 deletions.
46 changes: 39 additions & 7 deletions .build/tasks/Update_Bootstrap_Script.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ param

# Synopsis: Updates the bootstrap script before deploy
task Update_Bootstrap_Script {
function Get-StartPSResourceGetBootstrapFunctionDefinition
function Get-FunctionDefinition
{
[CmdletBinding()]
param ()
param
(
[Parameter(Mandatory = $true)]
[System.String]
$CommandName
)

# Get the script content
$moduleContent = Get-Content -Path "$BuiltModuleBase/PSResourceGet.Bootstrap.psm1" -Raw
$moduleContent = (Get-Command $CommandName).Module.Definition

# Parse the script into an AST
$ast = [System.Management.Automation.Language.Parser]::ParseInput($moduleContent, [ref]$null, [ref]$null)
Expand All @@ -55,7 +60,7 @@ task Update_Bootstrap_Script {
param($node)

return $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
$node.Name -eq 'Start-PSResourceGetBootstrap'
$node.Name -in $CommandName
}, $true)

return $functionDefinition
Expand All @@ -79,7 +84,7 @@ task Update_Bootstrap_Script {
$builtBootstrapScript = $builtBootstrapScript.Replace('yyyy-MM-dd', (Get-Date -Format 'yyyy-MM-dd'))

Write-Build -Color 'DarkGray' -Text "`tGet the function definition for the Start-PSResourceGetBootstrap function."
$functionDefinition = Get-StartPSResourceGetBootstrapFunctionDefinition
$functionDefinition = Get-FunctionDefinition -CommandName 'Start-PSResourceGetBootstrap'

Write-Build -Color 'DarkGray' -Text "`t`tGet the parameter block for the Start-PSResourceGetBootstrap function."
$parameterBlockString = "[CmdletBinding(DefaultParameterSetName = 'Scope')]`n" + $functionDefinition.Body.ParamBlock.Extent.Text
Expand All @@ -101,10 +106,35 @@ task Update_Bootstrap_Script {
$commentBasedHelp = ($functionDefinition.GetHelpContent()).GetCommentBlock()
$functionDefinitionString = $functionDefinition.Extent.Text

Write-Build -Color 'DarkGray' -Text "`tSet comment-based help in the bootstrap script."
Write-Build -Color 'DarkGray' -Text "`tAdd the command Start-PSResourceGetBootstrap to the bootstrap script."
$builtBootstrapScript = $builtBootstrapScript.Replace('#placeholder Start-PSResourceGetBootstrap', "$($commentBasedHelp)$($functionDefinitionString)")

Write-Debug -Message "Updated bootstrap script:`n$builtBootstrapScript"
Write-Build -Color 'DarkGray' -Text "`tAdd private helper commands to the bootstrap script."

$commands = @(
'Get-EnvironmentVariable'
'Get-PSModulePath'
'New-Exception'
'New-ErrorRecord'
)

$functionDefinitionString = ''

foreach ($command in $commands)
{
Write-Build -Color 'DarkGray' -Text "`t`tGet definition for command $Command."

$functionDefinition = Get-FunctionDefinition -CommandName $command

$functionDefinitionString += $functionDefinition.Extent.Text
$functionDefinitionString += "`n"
}

Write-Build -Color 'DarkGray' -Text "`tAdding helper commands to the bootstrap script."
$builtBootstrapScript = $builtBootstrapScript.Replace('#placeholder helpers', $functionDefinitionString)

Write-Build -Color 'DarkGray' -Text "`tExport only command Start-PSResourceGetBootstrap from the bootstrap script."
$builtBootstrapScript = $builtBootstrapScript.Replace('#placeholder export', "Export-ModuleMember -Function 'Start-PSResourceGetBootstrap'")

Write-Build -Color 'DarkGray' -Text "`tNormalize line endings."
$builtBootstrapScript = $builtBootstrapScript -replace '\r?\n', "`n"
Expand Down Expand Up @@ -144,6 +174,8 @@ task Update_Bootstrap_Script {
Write-Build -Color 'DarkGray' -Text "`tFormat the bootstrap script."
$builtBootstrapScript = Invoke-Formatter -ScriptDefinition $builtBootstrapScript -Settings $settings

Write-Debug -Message "Updated bootstrap script:`n$builtBootstrapScript"

Write-Build -Color 'DarkGray' -Text "`tWrite the bootstrap script to the build output folder."
Set-Content -Path "$OutputDirectory/bootstrap.ps1" -Value $builtBootstrapScript
}
2 changes: 1 addition & 1 deletion .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ main ]
workflow_dispatch:

# cSpell: ignore potatoqualitee codeql SARIF
# cSpell: ignore codeql SARIF
jobs:
pssa:
name: PSScriptAnalyzer
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Status badges updated.
- Update integration tests for the bootstrap script.
- Fix missing commands in bootstrap script.

### Changed

Expand Down
30 changes: 23 additions & 7 deletions Resolve-Dependency.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
.PARAMETER PSResourceGetVersion
String specifying the module version for PSResourceGet if the `UsePSResourceGet` switch is utilized.
.PARAMETER PSResourceGetThrottleLimit
Specifies the maximum number of concurrent requests to the PowerShell Gallery
when using PSResourceGet.
.NOTES
Load defaults for parameters values from Resolve-Dependency.psd1 if not
provided as parameter.
Expand Down Expand Up @@ -137,6 +141,10 @@ param
[System.String]
$PSResourceGetVersion,

[Parameter()]
[System.UInt16]
$PSResourceGetThrottleLimit,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$UsePowerShellGetCompatibilityModule,
Expand Down Expand Up @@ -945,13 +953,9 @@ try

Write-Progress -Activity 'PSResourceGet:' -PercentComplete $progressPercent -CurrentOperation 'Restoring Build Dependencies'

$modulesToSave | ForEach-Object -ThrottleLimit 5 -Parallel {
$modulesToSave | ForEach-Object -ThrottleLimit $PSResourceGetThrottleLimit -Parallel {
$currentModule = $_

$syncProgressCopy = $using:syncProgress

$progressPercent = $syncProgressCopy.AddOrUpdate('ProgressPercentage', { param($key) return 0 }, { param($key, $value) return $value + $using:percentagePerModule })

$savePSResourceParameters = @{
Path = $using:PSDependTarget
TrustRepository = $true
Expand All @@ -964,6 +968,8 @@ try
# Modules that Sampler depend on that cannot be refreshed without a new session.
$skipModule = @('PowerShell-Yaml')

$savedModule = $false

if ($savePSResourceParameters.Name -in $skipModule -and (Get-Module -Name $savePSResourceParameters.Name))
{
Write-Progress -Activity 'PSResourceGet:' -PercentComplete $progressPercent -CurrentOperation 'Restoring Build Dependencies' -Status ('Skipping module {0}' -f $savePSResourceParameters.Name)
Expand All @@ -972,8 +978,6 @@ try
}
else
{
Write-Progress -Activity 'PSResourceGet:' -PercentComplete $progressPercent -CurrentOperation 'Restoring Build Dependencies' -Status ('Saving module {0}' -f $savePSResourceParameters.Name)

# Clear all module from the current session so any new version fetched will be re-imported.
Get-Module -Name $savePSResourceParameters.Name | Remove-Module -Force

Expand All @@ -983,9 +987,21 @@ try
{
Write-Warning -Message 'Save-PSResource could not save (replace) one or more dependencies. This can be due to the module is loaded into the session (and referencing assemblies). Close the current session and open a new session and try again.'
}
else
{
$savedModule = $true
}
}

$syncProgressCopy = $using:syncProgress

#$syncProgressCopy.progressPercentage += $using:percentagePerModule
$progressPercent = $syncProgressCopy.AddOrUpdate('ProgressPercentage', { param($key) return 0 }, { param($key, $value) return $value + $using:percentagePerModule })

if ($savedModule)
{
Write-Progress -Activity 'PSResourceGet:' -PercentComplete $progressPercent -CurrentOperation 'Restoring Build Dependencies' -Status ('Saved module {0}' -f $savePSResourceParameters.Name)
}
}

Write-Progress -Activity 'PSResourceGet:' -PercentComplete 100 -CurrentOperation 'Restoring Build Dependencies' -Completed
Expand Down
1 change: 1 addition & 0 deletions Resolve-Dependency.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

UsePSResourceGet = $true
#PSResourceGetVersion = '1.0.1'
PSResourceGetThrottleLimit = 2

UsePowerShellGetCompatibilityModule = $true
UsePowerShellGetCompatibilityModuleVersion = '3.0.23-beta23'
Expand Down
77 changes: 56 additions & 21 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ stages:
testRunTitle: 'HQRM'

- job: test_linux
displayName: 'Linux'
displayName: 'Unit Linux'
timeoutInMinutes: 0
pool:
vmImage: 'ubuntu-latest'
Expand Down Expand Up @@ -136,7 +136,7 @@ stages:
parallel: true

- job: test_windows_core
displayName: 'Windows (PowerShell Core)'
displayName: 'Unit Windows (PowerShell Core)'
timeoutInMinutes: 0
pool:
vmImage: 'windows-latest'
Expand Down Expand Up @@ -170,7 +170,7 @@ stages:
parallel: true

- job: test_windows_ps
displayName: 'Windows (Windows PowerShell)'
displayName: 'Unit Windows (Windows PowerShell)'
timeoutInMinutes: 0
pool:
vmImage: 'windows-latest'
Expand Down Expand Up @@ -204,7 +204,7 @@ stages:
parallel: true

- job: test_macos
displayName: 'macOS'
displayName: 'Unit macOS'
timeoutInMinutes: 0
pool:
vmImage: 'macos-latest'
Expand Down Expand Up @@ -296,15 +296,28 @@ stages:
bash <(curl -s https://codecov.io/bash) -f "./$(buildFolderName)/$(testResultFolderName)/JaCoCo_coverage.xml" -F unit
displayName: 'Publish Code Coverage to Codecov.io'
- job: Test_Integration
displayName: 'Integration'
strategy:
matrix:
SQL2016_WIN2019:
WIN2019_PWSH:
JOB_VMIMAGE: 'windows-2019'
PWSH: true
WIN2019:
JOB_VMIMAGE: 'windows-2019'
SQL2016_WIN2022:
PWSH: false
WIN2022_PWSH:
JOB_VMIMAGE: 'windows-2022'
PWSH: true
WIN2022:
JOB_VMIMAGE: 'windows-2022'
PWSH: false
UBUNTU:
JOB_VMIMAGE: 'ubuntu-latest'
PWSH: true
MACOS:
JOB_VMIMAGE: 'macos-latest'
PWSH: true
pool:
vmImage: $(JOB_VMIMAGE)
timeoutInMinutes: 0
Expand All @@ -315,33 +328,52 @@ stages:
buildType: 'current'
artifactName: $(buildArtifactName)
targetPath: '$(Build.SourcesDirectory)/$(buildFolderName)'
# - task: PowerShell@2
# name: configureWinRM
# displayName: 'Configure WinRM'
# inputs:
# targetType: 'inline'
# script: 'winrm quickconfig -quiet'
# pwsh: false
- task: PowerShell@2
name: configureWinRM
displayName: 'Configure WinRM'
inputs:
targetType: 'inline'
script: 'winrm quickconfig -quiet'
pwsh: false
- powershell: |
./build.ps1 -Tasks test -CodeCoverageThreshold 0 -PesterPath 'tests/Integration' -PesterExcludeTag 'BootstrapScript'
name: test
displayName: 'Run Integration Test'
inputs:
filePath: './build.ps1'
arguments: "-Tasks test -CodeCoverageThreshold 0 -PesterPath 'tests/Integration' -PesterExcludeTag 'BootstrapScript'"
pwsh: $(PWSH)
- task: PublishTestResults@2
displayName: 'Publish Test Results'
condition: succeededOrFailed()
inputs:
testResultsFormat: 'NUnit'
testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml'
testRunTitle: 'Integration ($(JOB_VMIMAGE))'
testRunTitle: 'Integration ($(Agent.JobName))'

- job: Test_Integration_Bootstrap_Script
displayName: 'Integration Bootstrap Script'
dependsOn:
- Test_Integration
strategy:
matrix:
SQL2016_WIN2019:
WIN2019_PWSH:
JOB_VMIMAGE: 'windows-2019'
PWSH: true
WIN2019:
JOB_VMIMAGE: 'windows-2019'
SQL2016_WIN2022:
PWSH: false
WIN2022_PWSH:
JOB_VMIMAGE: 'windows-2022'
PWSH: true
WIN2022:
JOB_VMIMAGE: 'windows-2022'
PWSH: false
UBUNTU:
JOB_VMIMAGE: 'ubuntu-latest'
PWSH: true
MACOS:
JOB_VMIMAGE: 'macos-latest'
PWSH: true
pool:
vmImage: $(JOB_VMIMAGE)
timeoutInMinutes: 0
Expand All @@ -352,17 +384,20 @@ stages:
buildType: 'current'
artifactName: $(buildArtifactName)
targetPath: '$(Build.SourcesDirectory)/$(buildFolderName)'
- powershell: |
./build.ps1 -Tasks test -CodeCoverageThreshold 0 -PesterPath 'tests/Integration' -PesterTag 'BootstrapScript'
- task: PowerShell@2
name: test
displayName: 'Run Integration Test'
inputs:
filePath: './build.ps1'
arguments: "-Tasks test -CodeCoverageThreshold 0 -PesterPath 'tests/Integration' -PesterTag 'BootstrapScript'"
pwsh: $(PWSH)
- task: PublishTestResults@2
displayName: 'Publish Test Results'
condition: succeededOrFailed()
inputs:
testResultsFormat: 'NUnit'
testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml'
testRunTitle: 'Integration Bootstrap Script ($(JOB_VMIMAGE))'
testRunTitle: 'Integration Bootstrap Script ($(Agent.JobName))'

- stage: Deploy
dependsOn: Test
Expand Down
5 changes: 0 additions & 5 deletions source/Public/Start-PSResourceGetBootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,8 @@ function Start-PSResourceGetBootstrap
ErrorAction = 'Stop'
}

# $previousProgressPreference = $ProgressPreference
# $ProgressPreference = 'SilentlyContinue'

Invoke-WebRequest @invokeWebRequestParameters

# $ProgressPreference = $previousProgressPreference

$moduleAvailable = $true
}
catch
Expand Down
4 changes: 4 additions & 0 deletions source/Scripts/bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue'
New-Module -Name $script:moduleName -ScriptBlock {
#placeholder localization

#placeholder helpers

#placeholder Start-PSResourceGetBootstrap

#placeholder export
} | Import-Module

Start-PSResourceGetBootstrap @PSBoundParameters
Loading

0 comments on commit 2178a3c

Please sign in to comment.