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
Prev Previous commit
Next Next commit
Fix output path #31
  • Loading branch information
Jaykul committed Oct 24, 2018
commit 72eb13ddc06587bc85a8aeb839516f02c7a80314
12 changes: 5 additions & 7 deletions Source/Private/InitializeBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ function InitializeBuild {
# Update the ModuleManifest with our build configuration
$ModuleInfo = Update-Object -InputObject $ModuleInfo -UpdateObject $BuildInfo

# Ensure the OutputDirectory makes sense
if (!$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
# Ensure the OutputDirectory makes sense (it's never blank anymore)
if (![IO.Path]::IsPathRooted($ModuleInfo.OutputDirectory)) {
# Relative paths are relative to the build.psd1 now
$OutputDirectory = Join-Path $ModuleSource $ModuleInfo.OutputDirectory
$ModuleInfo.OutputDirectory = $OutputDirectory
}

$ModuleInfo
Expand Down
28 changes: 28 additions & 0 deletions Source/Private/ResolveOutputFolder.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function ResolveOutputFolder {
[CmdletBinding()]
param(
# Where to build the module.
# Defaults to an \output folder, adjacent to the "SourcePath" folder
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$OutputDirectory,

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

# specifies the module version for use in the output path if -VersionedOutputDirectory is true
[Parameter(ValueFromPipelineByPropertyName)]
[Alias("ModuleVersion")]
[string]$Version
)
process {
Write-Verbose "Resolve OutputDirectory path: $OutputDirectory"
# Make sure the OutputDirectory exists (assumes we're in the module source directory)
$OutputDirectory = New-Item $OutputDirectory -ItemType Directory -Force | Convert-Path
if ($VersionedOutputDirectory -and $OutputDirectory.TrimEnd("/\") -notmatch "\d+\.\d+\.\d+$") {
$OutputDirectory = New-Item (Join-Path $OutputDirectory $Version) -ItemType Directory -Force | Convert-Path
Write-Verbose "Added ModuleVersion to OutputDirectory path: $OutputDirectory"
}
$OutputDirectory
}
}
19 changes: 11 additions & 8 deletions Source/Public/Build-Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ function Build-Module {
[string]$SourcePath = $(Get-Location -PSProvider FileSystem),

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

# If set (true) adds a folder named after the version number to the OutputDirectory
[switch]$VersionedOutputDirectory,
Expand Down Expand Up @@ -149,18 +149,21 @@ function Build-Module {
Write-Verbose "Building $($ModuleInfo.Name)"

# Output file names
$OutputDirectory = $ModuleInfo.OutputDirectory
$OutputDirectory = $ModuleInfo | ResolveOutputFolder
$RootModule = Join-Path $OutputDirectory "$($ModuleInfo.Name).psm1"
$OutputManifest = Join-Path $OutputDirectory "$($ModuleInfo.Name).psd1"

Write-Progress "Building $($ModuleInfo.Name)" -Status "Use -Verbose for more information"
Write-Verbose "Building $($ModuleInfo.Name)"
Write-Verbose "Output to: $OutputDirectory"

Wait-Debugger
if ($Target -match "Clean") {
Write-Verbose "Cleaning $OutputDirectory"
if (Test-Path $OutputDirectory) {
Remove-Item $OutputDirectory -Recurse -Force
if (Test-Path $OutputDirectory -PathType Leaf) {
throw "Unable to build. There is a file in the way at $OutputDirectory"
}
if (Test-Path $OutputDirectory -PathType Container) {
if (Get-ChildItem $OutputDirectory\*) {
Remove-Item $OutputDirectory\* -Recurse -Force
}
}
if ($Target -notmatch "Build") {
return # No build, just cleaning
Expand Down
2 changes: 2 additions & 0 deletions Source/build.psd1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@{
Path = "ModuleBuilder.psd1"
OutputDirectory = "..\"
VersionedOutputDirectory = $true
}
53 changes: 53 additions & 0 deletions Tests/Private/ResolveOutputFolder.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Describe "ResolveOutputFolder" {
Import-Module ModuleBuilder -DisableNameChecking -Verbose:$False

Context "Given an OutputDirectory only" {

$Result = InModuleScope -ModuleName ModuleBuilder {
ResolveOutputFolder -OutputDirectory TestDrive:\Output
}

It "Creates the Output directory" {
$Result | Should -Be "TestDrive:\Output"
$Result | Should -Exist
}
}

Context "Given an OutputDirectory and ModuleVersion but no switch" {

$Result = InModuleScope -ModuleName ModuleBuilder {
ResolveOutputFolder -OutputDirectory TestDrive:\Output -ModuleVersion "1.0.0"
}

It "Creates the Output directory" {
"TestDrive:\Output" | Should -Exist
}

It "Returns the Output directory" {
$Result | Should -Be "TestDrive:\Output"
}

It "Does not creates children in Output" {
Get-ChildItem $Result | Should -BeNullOrEmpty
}
}

Context "Given an OutputDirectory, ModuleVersion and switch" {

$Result = InModuleScope -ModuleName ModuleBuilder {
ResolveOutputFolder -OutputDirectory TestDrive:\Output -ModuleVersion "1.0.0" -VersionedOutput
}

It "Creates the Output directory" {
"TestDrive:\Output" | Should -Exist
}

It "Creates the version directory" {
"TestDrive:\Output\1.0.0" | Should -Exist
}

It "Returns the Output directory" {
$Result | Should -Be "TestDrive:\Output\1.0.0"
}
}
}