Skip to content

Commit d873623

Browse files
committed
Switch to searching for module manifests
After discussion on #89 it seems this will "just work" in more scenarios, without breaking things that already work.
1 parent 59dd957 commit d873623

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

Source/Private/GetBuildInfo.ps1

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ function GetBuildInfo {
1313

1414
$BuildInfo = if ($BuildManifest -and (Test-Path $BuildManifest)) {
1515
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-
}
2116
# Read the Module Manifest configuration file for default parameter values
2217
Write-Debug "Load Build Manifest $BuildManifest"
2318
Import-Metadata -Path $BuildManifest
@@ -77,20 +72,34 @@ function GetBuildInfo {
7772

7873
$BuildInfo = $BuildInfo | Update-Object $ParameterValues
7974

80-
# Resolve Module manifest if not defined in Build.psd1
81-
if (-Not $BuildInfo.SourcePath -and $BuildManifestParent) {
82-
# Resolve Build Manifest's parent folder to find the Absolute path
83-
$ModuleName = Split-Path -Leaf $BuildManifestParent
75+
$BuildManifestParent = if ($BuildManifest) {
76+
Split-Path -Parent $BuildManifest
77+
} else {
78+
Get-Location -PSProvider FileSystem
79+
}
8480

85-
# If we're in a "well known" source folder, look higher for a name
86-
if ($ModuleName -in 'Source', 'src') {
87-
$ModuleName = Split-Path (Split-Path -Parent $BuildManifestParent) -Leaf
81+
# Resolve Module manifest if not defined in Build.psd1 or there's no Build.psd1
82+
if (-Not $BuildInfo.SourcePath) {
83+
# Find a module manifest (or maybe several)
84+
$ModuleInfo = Get-ChildItem $BuildManifestParent -Recurse -Filter *.psd1 -ErrorAction SilentlyContinue |
85+
ImportModuleManifest -ErrorAction SilentlyContinue
86+
# If we found more than one module info, the only way we have of picking just one is if it matches a folder name
87+
if (@($ModuleInfo).Count -gt 1) {
88+
# Resolve Build Manifest's parent folder to find the Absolute path
89+
$ModuleName = Split-Path -Leaf $BuildManifestParent
90+
# If we're in a "well known" source folder, look higher for a name
91+
if ($ModuleName -in 'Source', 'src') {
92+
$ModuleName = Split-Path (Split-Path -Parent $BuildManifestParent) -Leaf
93+
}
94+
$ModuleInfo = @($ModuleInfo).Where{ $_.Name -eq $ModuleName }
95+
}
96+
if (@($ModuleInfo).Count -eq 1) {
97+
Write-Debug "Updating BuildInfo SourcePath to $SourcePath"
98+
$BuildInfo = $BuildInfo | Update-Object @{ SourcePath = $ModuleInfo.Path }
99+
}
100+
if (-Not $BuildInfo.SourcePath) {
101+
throw "Can't find a module manifest in $BuildManifestParent"
88102
}
89-
90-
# As the Module Manifest did not specify the Module manifest, we expect the Module manifest in same folder
91-
$SourcePath = Join-Path $BuildManifestParent "$ModuleName.psd1"
92-
Write-Debug "Updating BuildInfo SourcePath to $SourcePath"
93-
$BuildInfo = $BuildInfo | Update-Object @{ SourcePath = $SourcePath }
94103
}
95104

96105
# Make sure the Path is set and points at the actual manifest, relative to Build.psd1 or absolute

Source/Private/InitializeBuild.ps1

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,11 @@ function InitializeBuild {
2929
Write-Debug "BuildCommand: $($BuildCommandInvocation.MyCommand | Out-String)"
3030
$BuildInfo = GetBuildInfo -BuildManifest $BuildManifest -BuildCommandInvocation $BuildCommandInvocation
3131

32-
# These errors are caused by trying to parse valid module manifests without compiling the module first
33-
$ErrorsWeIgnore = "^" + @(
34-
"Modules_InvalidRequiredModulesinModuleManifest"
35-
"Modules_InvalidRootModuleInModuleManifest"
36-
) -join "|^"
37-
3832
# Finally, add all the information in the module manifest to the return object
39-
$ModuleInfo = Get-Module (Get-Item $BuildInfo.SourcePath).FullName -ListAvailable -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable Problems
40-
41-
# If there are any problems that count, fail
42-
if ($Problems = $Problems.Where({ $_.FullyQualifiedErrorId -notmatch $ErrorsWeIgnore })) {
43-
foreach ($problem in $Problems) {
44-
Write-Error $problem
45-
}
46-
throw "Unresolvable problems in module manifest"
33+
if ($ModuleInfo = ImportModuleManifest $BuildInfo.SourcePath) {
34+
# Update the module manifest with our build configuration and output it
35+
Update-Object -InputObject $ModuleInfo -UpdateObject $BuildInfo
36+
} else {
37+
throw "Unresolvable problems in module manifest: '$($BuildInfo.SourcePath)'"
4738
}
48-
49-
# Workaround the fact that Get-Module returns the DefaultCommandPrefix as Prefix
50-
$ModuleInfo = Update-Object -InputObject $ModuleInfo -UpdateObject @{ DefaultCommandPrefix = $ModuleInfo.Prefix; Prefix = "" }
51-
# Update the module manifest with our build configuration
52-
$ModuleInfo = Update-Object -InputObject $ModuleInfo -UpdateObject $BuildInfo
53-
54-
$ModuleInfo
5539
}

0 commit comments

Comments
 (0)