Skip to content

Commit 7d519fa

Browse files
committed
Finally make the build.psd1 file optional per #44
1 parent 3dc7800 commit 7d519fa

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

Source/Private/GetBuildInfo.ps1

+18-15
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@ function GetBuildInfo {
22
[CmdletBinding()]
33
param(
44
# The path to the Build Manifest Build.psd1
5-
[Parameter(Mandatory)]
6-
[ValidateScript( {
7-
if ((Test-Path $_) -and (Split-path -Leaf $_) -eq 'build.psd1') {
8-
$true
9-
}
10-
else {
11-
throw "The Module Manifest must point to a valid build.psd1 Data file"
12-
}
13-
})]
5+
[Parameter()][AllowNull()]
146
[string]$BuildManifest,
157

168
# Pass MyInvocation from the Build-Command so we can read parameter values
@@ -19,9 +11,23 @@ function GetBuildInfo {
1911
$BuildCommandInvocation
2012
)
2113

22-
# Read the Module Manifest configuration file for default parameter values
23-
Write-Debug "Load Build Manifest $BuildManifest"
24-
$BuildInfo = Import-Metadata -Path $BuildManifest
14+
$BuildInfo = if ($BuildManifest -and (Test-Path $BuildManifest)) {
15+
if ((Split-path -Leaf $BuildManifest) -eq 'build.psd1') {
16+
$BuildManifestParent = if ($BuildManifest) {
17+
Split-Path -Parent $BuildManifest
18+
} else {
19+
Get-Location -PSProvider FileSystem
20+
}
21+
# Read the Module Manifest configuration file for default parameter values
22+
Write-Debug "Load Build Manifest $BuildManifest"
23+
Import-Metadata -Path $BuildManifest
24+
} else {
25+
@{ SourcePath = $BuildManifest }
26+
}
27+
} else {
28+
@{}
29+
}
30+
2531
$CommonParameters = [System.Management.Automation.Cmdlet]::CommonParameters +
2632
[System.Management.Automation.Cmdlet]::OptionalCommonParameters
2733
$BuildParameters = $BuildCommandInvocation.MyCommand.Parameters
@@ -71,9 +77,6 @@ function GetBuildInfo {
7177

7278
$BuildInfo = $BuildInfo | Update-Object $ParameterValues
7379

74-
# Resolve Build Manifest's parent folder to find the Absolute path
75-
$BuildManifestParent = (Split-Path -Parent $BuildManifest)
76-
7780
# Resolve Module manifest if not defined in Build.psd1
7881
if (-Not $BuildInfo.SourcePath -and $BuildManifestParent) {
7982
# Resolve Build Manifest's parent folder to find the Absolute path

Source/Private/ResolveBuildManifest.ps1

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function ResolveBuildManifest {
99
if ((Split-Path $SourcePath -Leaf) -eq 'build.psd1') {
1010
$BuildManifest = $SourcePath
1111
} elseif (Test-Path $SourcePath -PathType Leaf) {
12-
# When you pass the ModuleManifest as parameter, you must have the Build Manifest in the same folder
12+
# When you pass the SourcePath as parameter, you must have the Build Manifest in the same folder
1313
$BuildManifest = Join-Path (Split-Path -Parent $SourcePath) [Bb]uild.psd1
1414
} else {
1515
# It's a container, assume the Build Manifest is directly under
@@ -19,9 +19,7 @@ function ResolveBuildManifest {
1919
# Make sure we are resolving the absolute path to the manifest, and test it exists
2020
$ResolvedBuildManifest = (Resolve-Path $BuildManifest -ErrorAction SilentlyContinue).Path
2121

22-
if (-Not ($ResolvedBuildManifest)) {
23-
throw "Couldn't resolve the Build Manifest at $BuildManifest"
24-
} else {
22+
if ($ResolvedBuildManifest) {
2523
$ResolvedBuildManifest
2624
}
2725

Tests/Integration/Source1.Tests.ps1

+33
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,39 @@ Describe "Build-Module With Source1" {
8686
It "Should update AliasesToExport in the manifest" {
8787
$Metadata.AliasesToExport | Should -Be @("GS","GSou", "SS", "SSou")
8888
}
89+
}
90+
91+
Context "Supports building without a build.psd1" {
92+
Copy-Item $PSScriptRoot\Source1 $PSScriptRoot\Copy1 -Recurse
93+
Remove-Item $PSScriptRoot\Copy1\build.psd1
94+
Rename-Item $PSScriptRoot\Copy1\Source1.psd1 Copy1.psd1
95+
96+
$Build = @{ }
97+
98+
It "No longer fails if there's no build.psd1" {
99+
$BuildParameters = @{
100+
SourcePath = "$PSScriptRoot\Copy1\Copy1.psd1"
101+
OutputDirectory = "..\Result1"
102+
VersionedOutputDirectory = $true
103+
}
104+
105+
$Build.Output = Build-Module @BuildParameters -Passthru
106+
}
107+
108+
Remove-Item -Recurse $PSScriptRoot\Copy1
109+
110+
111+
It "Creates the same module as with a build.psd1" {
112+
$Build.Metadata = Import-Metadata $Build.Output.Path
113+
}
114+
115+
It "Should update AliasesToExport in the manifest" {
116+
$Build.Metadata.AliasesToExport | Should -Be @("GS", "GSou", "SS", "SSou")
117+
}
118+
119+
It "Should update FunctionsToExport in the manifest" {
120+
$Build.Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
121+
}
89122

90123
}
91124
}

0 commit comments

Comments
 (0)