Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix #32 InitializeBuild and tests
  • Loading branch information
Jaykul committed Oct 24, 2018
commit 27687227a3fec59753aed51d2ad3fe909aa5dd27
37 changes: 21 additions & 16 deletions Source/Private/InitializeBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,8 @@ function InitializeBuild {
$Invocation = $(Get-Variable MyInvocation -Scope 1 -ValueOnly)
)
# NOTE: This reads the parameter values from Build-Module!
$ParameterValues = @{}
foreach($parameter in $Invocation.MyCommand.Parameters.GetEnumerator()) {
$key = $parameter.Key
if($null -ne ($value = Get-Variable -Name $key -ValueOnly -ErrorAction Ignore )) {
if($value -ne ($null -as $parameter.Value.ParameterType)) {
$ParameterValues[$key] = $value
}
}
}
# BUG BUG: needs to prioritize build.psd1 values over build-module *defaults*, but user-provided parameters over build.psd1 values
Write-Debug "Initializing build variables"

$ModuleSource = ResolveModuleSource $SourcePath
Push-Location $ModuleSource -StackName Build-Module
Expand All @@ -44,6 +37,20 @@ function InitializeBuild {
# Read a build.psd1 configuration file for default parameter values
$BuildInfo = Import-Metadata -Path (Join-Path $ModuleSource [Bb]uild.psd1)
# Combine the defaults with parameter values
$ParameterValues = @{}
foreach ($parameter in $Invocation.MyCommand.Parameters.GetEnumerator()) {
$key = $parameter.Key
# set if it doesn't exist, overwrite if the value is bound as a parameter
if (!$BuildInfo.ContainsKey($key) -or $Invocation.BoundParameters.ContainsKey($key)) {
if ($null -ne ($value = Get-Variable -Name $key -ValueOnly -ErrorAction Ignore )) {
if ($value -ne ($null -as $parameter.Value.ParameterType)) {
Write-Debug " $key = $value"
$ParameterValues[$key] = $value
}
}
}
}

$BuildInfo = $BuildInfo | Update-Object $ParameterValues

# Make sure the Path is set and points at the actual manifest
Expand All @@ -63,14 +70,12 @@ function InitializeBuild {
# Update the ModuleManifest with our build configuration
$ModuleInfo = Update-Object -InputObject $ModuleInfo -UpdateObject $BuildInfo

# Ensure OutputDirectory
# Ensure the OutputDirectory makes sense
if (!$ModuleInfo.OutputDirectory) {
$OutputRoot = if($OutputRoot = Split-Path $ModuleSource) { $OutputRoot } else { $ModuleSource}
$OutputDirectory = Join-Path $OutputRoot "$($ModuleInfo.Version)"
Add-Member -Input $ModuleInfo -Type NoteProperty -Name OutputDirectory -Value $OutputDirectory -Force
} elseif (![IO.Path]::IsPathRooted($ModuleInfo.OutputDirectory)) {
$OutputRoot = if($OutputRoot = Split-Path $ModuleSource) { $OutputRoot } else { $ModuleSource}
$OutputDirectory = Join-Path $OutputRoot $ModuleInfo.OutputDirectory
# The assumption here is that the "source" is a subdirectory of the project root (next to, for instance "tests" and "docs")
# If there's no output directory specified (or a relative path) we want to output within the project root, not the "source"
$OutputRoot = if($OutputRoot = Split-Path $ModuleSource) { $OutputRoot } else { $ModuleSource }
$OutputDirectory = Join-Path $OutputRoot "Output"
Add-Member -Input $ModuleInfo -Type NoteProperty -Name OutputDirectory -Value $OutputDirectory -Force
}

Expand Down
10 changes: 8 additions & 2 deletions Source/Public/Build-Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ function Build-Module {
[string]$SourcePath = $(Get-Location -PSProvider FileSystem),

# Where to build the module.
# Defaults to a version number folder, adjacent to the module folder
# Defaults to an \output folder, adjacent to the "SourcePath" folder
[Alias("Destination")]
[string]$OutputDirectory,
[string]$OutputDirectory = "Output",

# If set (true) adds a folder named after the version number to the OutputDirectory
[switch]$VersionedOutputDirectory,

# Semantic version, like 1.0.3-beta01+sha.22c35ffff166f34addc49a3b80e622b543199cc5
# If the SemVer has metadata (after a +), then the full Semver will be added to the ReleaseNotes
Expand Down Expand Up @@ -142,6 +145,9 @@ function Build-Module {

# Push into the module source (it may be a subfolder)
$ModuleInfo = InitializeBuild $SourcePath
Write-Progress "Building $($ModuleInfo.Name)" -Status "Use -Verbose for more information"
Write-Verbose "Building $($ModuleInfo.Name)"

# Output file names
$OutputDirectory = $ModuleInfo.OutputDirectory
$RootModule = Join-Path $OutputDirectory "$($ModuleInfo.Name).psm1"
Expand Down
18 changes: 14 additions & 4 deletions Tests/Private/InitializeBuild.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ Describe "InitializeBuild" {
Mock ResolveModuleSource -ModuleName ModuleBuilder { $SourcePath }
Mock ResolveModuleManifest -ModuleName ModuleBuilder { "TestDrive:\Source\MyModule.psd1" }
Mock Push-Location -ModuleName ModuleBuilder {}
Mock Import-Metadata -ModuleName ModuleBuilder { @{Path = "MyModule.psd1"} }
Mock Import-Metadata -ModuleName ModuleBuilder {
@{
Path = "MyModule.psd1"
SourceDirectories = "Classes", "Public"
}
}
#Mock Update-Object -ModuleName ModuleBuilder { $InputObject }
Mock Get-Variable -ParameterFilter {
$Name -eq "MyInvocation"
Expand Down Expand Up @@ -33,6 +38,8 @@ Describe "InitializeBuild" {
New-Item "TestDrive:\Source\" -Type Directory

$Result = InModuleScope -ModuleName ModuleBuilder {
[CmdletBinding()]
param( $SourceDirectories = @("Enum", "Classes", "Private", "Public") )
InitializeBuild -SourcePath TestDrive:\Source\
}

Expand Down Expand Up @@ -63,7 +70,8 @@ Describe "InitializeBuild" {
It "Returns the ModuleInfo combined with an OutputDirectory and Path" {
$Result.ModuleBase | Should -be "TestDrive:\Source\"
$Result.Path | Should -be "TestDrive:\Source\MyModule.psd1"
$Result.OutputDirectory | Should -be "TestDrive:\1.0.0"
$Result.OutputDirectory | Should -be "TestDrive:\Output"
$Result.SourceDirectories | Should -be @("Classes", "Public")
}
}

Expand Down Expand Up @@ -105,7 +113,8 @@ Describe "InitializeBuild" {
It "Treats the output path as relative to the (parent of the) ModuleSource" {
$Result.ModuleBase | Should -be "TestDrive:\Source\"
$Result.Path | Should -be "TestDrive:\Source\MyModule.psd1"
$Result.OutputDirectory | Should -be "TestDrive:\.\Output"
# Note that Build-Module will call ResolveOutputFolder with this, so the relative path here is ok
$Result.OutputDirectory | Should -be ".\Output"
}
}

Expand Down Expand Up @@ -153,7 +162,8 @@ Describe "InitializeBuild" {
It "Treats the output path as relative to the (parent of the) ModuleSource" {
$Result.ModuleBase | Should -be "TestDrive:\"
$Result.Path | Should -be "TestDrive:\MyModule.psd1"
$Result.OutputDirectory | Should -be "TestDrive:\.\Output"
# Note that Build-Module will call ResolveOutputFolder with this, so the relative path here is ok
$Result.OutputDirectory | Should -be ".\Output"
}
}
}