Skip to content

Commit

Permalink
Merge pull request #94 from aldrichtr/refactor/build-driver-functions
Browse files Browse the repository at this point in the history
Reduce the build driver script by using stitch module functions
  • Loading branch information
aldrichtr authored Jan 3, 2024
2 parents 61184be + fe9097b commit 566ff4f
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 601 deletions.
320 changes: 11 additions & 309 deletions .build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,148 +14,8 @@ using namespace System.Diagnostics.CodeAnalysis
#-------------------------------------------------------------------------------
#region Rule suppression

[SuppressMessage('PSReviewUnusedParameter','ProfilePath',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','BuildProfile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','DefaultBuildProfile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','BuildConfigRoot',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','BuildConfigFile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','Source',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','Staging',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','Tests',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','Artifact',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','Docs',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','SourceTypeMap',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','SkipModuleTaskImport',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','BuildInfo',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ExcludePathFromClean',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','SkipDependencyCheck',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','DependencyTags',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','RequiredModules',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','CodeCov',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','CodeCovFormat',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','CodeCovDirectory',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','CodeCovFile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','TestResultFormat',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','TestResultDirectory',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','TestResultFile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','PesterOutput',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','PesterResultDirectory',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','PesterResultFile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','CopyAdditionalItems',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','CopyEmptySourceDirs',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','SkipManifestArrayFormat',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ModuleFileIncludeTypes',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ModuleFilePrefix',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ModuleFileSuffix',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ModuleNamespace',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ManifestBackupPath',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','KeepManifestBackup',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ManifestVersionField',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','SuppressManifestComments',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ExcludeFunctionsFromExport',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ExcludeAliasFromExport',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','FormatPsXmlDirectory',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','FormatPsXmlFileFilter',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','TypePsXmlDirectory',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','TypePsXmlFileFilter',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','HelpDocsCultureDirectory',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','HelpDocLogFile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','FormatSettings',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','AnalyzerSettings',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ProjectVersionSource',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ProjectVersionField',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ChangelogPath',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ChangelogBackupPath',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','KeepChangelogBackup',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ChangelogVersionField',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','GitTagVersionField',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','GitStashMessage',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','IncludeUntrackedInGitStash',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ProjectPSRepoName',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','PublishPsRepoName',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','PublishToPsRepo',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','PublishActionIfUncommitted',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','NugetApiKey',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ReleaseNotesFormat',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','ReleaseNotesFile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','InstallSaveToPath',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','InstallSaveToModules',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','InstallModuleFromPsRepo',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','LogPath',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','LogFile',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','Output',
Justification = 'Parameters used in separate task files' )]
[SuppressMessage('PSReviewUnusedParameter','SkipBuildHeader',
Justification = 'Parameters used in separate task files' )]

[SuppressMessage('PSReviewUnusedParameter','', Justification = 'Parameters used in separate task files' )]

#endregion Rule suppression
#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -377,7 +237,7 @@ param(
[Parameter()][String]$ModuleFileSuffix,

<#
If the module should be part of a larger namespace, set the namespace here. ModuleNamespace is
If the module should be part of a larger namespace, set the namespace here. ModuleNamespace is
a hashtable where the key is the module name and the value is the namespace like:
@{
Module1 = 'Fabricam.Automation'
Expand Down Expand Up @@ -658,174 +518,14 @@ begin {
Write-Debug " - Version $($stitchModule.Version) loaded"
}

Write-Verbose "Using $($stitchModule.Version) of stitch"
Write-Debug "Stitch loaded from $($stitchModule.Path)"
Write-Debug "`n$('-' * 80)>"

#endregion Load Stitch module
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#region set BuildConfigPath
Write-Debug "`n<$('-' * 80)"

<#
Here we need to ensure that at the least, we have found a useable build configuration root directory
`$BuildConfigRoot` This could be one of a few directories under Invoke-Build's `$BuildRoot`
#>


<#
We will drill down from StartingDirectory -> Build Configuration Root -> Profile Root -> Current Profile
We are going to try to find the build configuration path. This relies on either
the ProfilePath or BuildConfigRoot being set to a valid path.
#>
$buildRootIsSet = (-not ([string]::IsNullorEmpty($BuildRoot)))
#-------------------------------------------------------------------------------
#region Starting Directory

Write-Debug 'Resolving the starting directory'
$startingDirectory = Resolve-ProjectRoot -ErrorAction SilentlyContinue
if ($null -eq $startingDirectory) {
if ($buildRootIsSet) {
Write-Debug "- BuildRoot was set. Looking in $BuildRoot"
$startingDirectory = $BuildRoot
} else {
$startingDirectory = (Get-Location).Path
Write-Debug '- BuildRoot was not set. Using current directory'
}
} else {
Write-Debug '- Resolved Project Root'
}

# abort if we can't find the starting directory
if ($null -eq $startingDirectory) {
throw 'Something went wrong, could not determine starting directory'
} else {
Write-Verbose "Starting in $startingDirectory"
}

#endregion Starting Directory
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#region Build Configuration Root
$buildConfigRootIsSet = (-not ([string]::IsNullorEmpty($BuildConfigRoot)))

Write-Debug 'Resolving the Build Configuration Root Directory'
if ($buildConfigRootIsSet) {
Write-Debug "- BuildConfigRoot was set to '$BuildConfigRoot' by Parameters"
} else {
# Now that we have a starting point, see if we can find the BuildConfigRoot
$possibleBuildConfigRoot = ($startingDirectory | Find-BuildConfigurationRootDirectory)
Write-Debug "- found BuildConfigRoot in '$possibleBuildConfigRoot'"
if ($null -ne $possibleBuildConfigRoot) {
$BuildConfigRoot = $possibleBuildConfigRoot
Write-Debug "- BuildConfigRoot is a $($BuildConfigRoot.GetType().FullName)"
Write-Debug "- BuildConfigRoot is now set to '$BuildConfigRoot'"
Remove-Variable possibleBuildConfigRoot -ErrorAction SilentlyContinue
}
}

#! abort if we cannot find the Build Configuration Root
if ($null -eq $BuildConfigRoot) {
throw 'Could not find the Build Configuration Root Directory (.build or .stitch by default)'
} elseif (-not (Test-Path $BuildConfigRoot)) {
throw "BuildConfigRoot points to an invalid path '$BuildConfigRoot'"
}
#endregion Build Configuration Root
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#region Profile path


# if we made it here, then BuildConfigRoot is a valid path
# the best option is that $ProfilePath and $BuildProfile are set and that results in a valid path

# the next option is that $ProfilePath is set, and $DefaultBuildProfile are set and valid

# ProfilePath isn't set, we use BuildConfigRoot to look for runbooks

if ([string]::IsNullorEmpty($ProfilePath)) {
Write-Debug 'ProfilePath was not set. Looking for a profile path'

$possibleProfileRoot = $BuildConfigRoot | Find-BuildProfileRootDirectory

if ($null -ne $possibleProfileRoot) {
Write-Debug "- Found profile root directory '$possibleProfileRoot'"
$ProfilePath = $possibleProfileRoot
}
Remove-Variable possibleProfileRoot -ErrorAction SilentlyContinue
Write-Verbose "ProfilePath set to $ProfilePath"
}

# Either it was already set or we just found the ProfilePath
if ([string]::IsNullorEmpty($ProfilePath)) {
if (Test-Path $ProfilePath) {
Write-Debug "ProfilePath was set to $ProfilePath by parameter"
if (-not ([string]::IsNullorEmpty($BuildProfile))) {
$BuildConfigPath = (Join-Path $ProfilePath $BuildProfile)
} elseif (-not ([string]::IsNullorEmpty($DefaultBuildProfile))) {
$BuildConfigPath = (Join-Path $ProfilePath $DefaultBuildProfile)
} else {
$foundRunbooks = Select-BuildRunBook -Path $ProfilePath
if ($null -ne $foundRunbooks) {
$BuildConfigPath = Split-Path -Path $foundRunbooks -Parent
Write-Verbose "No Profiles were set, but found runbook in $ProfilePath"
}
}
} else {
throw "ProfilePath was set to an invalid path '$ProfilePath'"
}
}
#endregion Profile path
#-------------------------------------------------------------------------------

if ([string]::IsNullorEmpty($BuildConfigPath)) {
# we didn't find a valid configurtion path yet, see if we can find a runbook in the config root

#! it shouldn't be possible to get here without it, but let's make sure
if ($null -ne $BuildConfigRoot) {
if (Test-Path $BuildConfigRoot) {
$runbookOptions = @{
Path = $BuildConfigRoot
}
if (-not ([string]::IsNullorEmpty($BuildProfile))) {
$runbookOptions['BuildProfile'] = $BuildProfile
} elseif (-not ([string]::IsNullorEmpty($DefaultBuildProfile))) {
$runbookOptions['BuildProfile'] = $DefaultBuildProfile
}
$foundRunbooks = Select-BuildRunBook -Path $ProfilePath
if ($null -ne $foundRunbooks) {
$BuildConfigPath = Split-Path -Path $foundRunbooks -Parent
$Runbook = $foundRunbooks
Write-Verbose "A Runbook was found in '$BuildConfigRoot'"
}
} else {
throw "Build Configuration Root was set to an invalid path '$BuildConfigRoot'"
}
}
}

Remove-Variable runbookOptions, foundRunbooks -ErrorAction SilentlyContinue

<#
All of this was to set BuildConfigPath. If we made it here and it still isnt set, we are in big trouble, we should
just quit and let the user know why
#>
if ($null -eq $BuildConfigPath) {
if ($errorMessage.Count -gt 0) {
throw ($errorMessage -join "`n")
} else {
throw "**Can't continue** Could not find the build configuration path"
}
} else {
Write-Verbose "Build configuration Path : $BuildConfigPath"
}
Write-Debug "`n$('-' * 80)>"
#endregion set BuildConfigPath
#-------------------------------------------------------------------------------

$BuildConfigPath = Find-BuildConfigurationDirectory -BuildProfile $BuildProfile
#-------------------------------------------------------------------------------
#region Load stitch config
Write-Debug "`n<$('-' * 80)"
Expand Down Expand Up @@ -854,9 +554,7 @@ begin {
. $StitchConfigFile
}
} else {
if ($errorMessage.Count -gt 0) {
throw ( $errorMessage -join "`n")
}

}
Write-Debug "`n$('-' * 80)>"
#endregion Load stitch config
Expand Down Expand Up @@ -931,14 +629,17 @@ begin {
#region Load the runbook
Write-Debug "`n<$('-' * 80)"


$Runbook = $BuildConfigPath | Get-BuildRunBook

if ($null -ne $Runbook) {
if (Test-Path $Runbook) {
Write-Debug "Importing runbook $Runbook"
. $Runbook
Write-Debug ' - Complete'
}
} else {
Write-Debug "Runbook was not set, looking in BuildConfigPath"
Write-Debug "Runbook was not found, looking in BuildConfigPath"
$foundRunbooks = Select-BuildRunBook -Path $BuildConfigPath
foreach ($runbook in $foundRunbooks) {
Write-Debug "Importing runbook $Runbook"
Expand All @@ -960,3 +661,4 @@ process {
end {
Write-Debug "`n$('-' * 80)`n-- End $($MyInvocation.MyCommand.Name)`n$('-' * 80)"
}

5 changes: 4 additions & 1 deletion .build/profiles/clean/runbook.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
Use this file to manage the phases and tasks.
#>

task . 'clean.staging', 'clean.artifacts'
#Synopsis: Clean the staging and artifacts directories
task Clean 'clean.staging', 'clean.artifacts'

task . Clean
Loading

0 comments on commit 566ff4f

Please sign in to comment.