Skip to content

Commit 8de7520

Browse files
authored
Change CopyDirectories to CopyPaths
- Also stop warning about the "Build" verb - "Complete" the progress display after the build finishes Merge pull request #91 from PoshCode/fix/CopyPaths
2 parents cfc29c0 + f1becd5 commit 8de7520

File tree

9 files changed

+82
-32
lines changed

9 files changed

+82
-32
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
[0-9]*/
33
obj/
44
Output/
5-
Tools/
5+
Tools/
6+
coverage.xml

Source/Private/GetBuildInfo.ps1

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@ function GetBuildInfo {
1111
$BuildCommandInvocation
1212
)
1313

14-
$BuildInfo = if ($BuildManifest -and (Test-Path $BuildManifest)) {
15-
if ((Split-path -Leaf $BuildManifest) -eq 'build.psd1') {
16-
# Read the Module Manifest configuration file for default parameter values
17-
Write-Debug "Load Build Manifest $BuildManifest"
18-
Import-Metadata -Path $BuildManifest
19-
} else {
20-
Write-Debug "Use SourcePath $BuildManifest"
21-
@{ SourcePath = $BuildManifest }
22-
}
14+
$BuildInfo = if ($BuildManifest -and (Test-Path $BuildManifest) -and (Split-path -Leaf $BuildManifest) -eq 'build.psd1') {
15+
# Read the build.psd1 configuration file for default parameter values
16+
Write-Debug "Load Build Manifest $BuildManifest"
17+
Import-Metadata -Path $BuildManifest
2318
} else {
2419
@{}
2520
}

Source/Public/Build-Module.ps1

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
if (!(Get-Verb Build) -and $MyInvocation.Line -notmatch "DisableNameChecking") {
2-
Write-Warning "The verb 'Build' was approved recently, but PowerShell $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor) doesn't know. You will be warned about Build-Module."
3-
}
4-
51
function Build-Module {
62
<#
73
.Synopsis
@@ -17,7 +13,7 @@ function Build-Module {
1713
The optimization process:
1814
1. The OutputDirectory is created
1915
2. All psd1/psm1/ps1xml files (except build.psd1) in the Source will be copied to the output
20-
3. If specified, $CopyDirectories (relative to the Source) will be copied to the output
16+
3. If specified, $CopyPaths (relative to the Source) will be copied to the output
2117
4. The ModuleName.psm1 will be generated (overwritten completely) by concatenating all .ps1 files in the $SourceDirectories subdirectories
2218
5. The ModuleVersion and ExportedFunctions in the ModuleName.psd1 may be updated (depending on parameters)
2319
@@ -88,7 +84,8 @@ function Build-Module {
8884
# Folders which should be copied intact to the module output
8985
# Can be relative to the module folder
9086
[AllowEmptyCollection()]
91-
[string[]]$CopyDirectories = @(),
87+
[Alias("CopyDirectories")]
88+
[string[]]$CopyPaths = @(),
9289

9390
# Folders which contain source .ps1 scripts to be concatenated into the module
9491
# Defaults to Enum, Classes, Private, Public
@@ -194,9 +191,9 @@ function Build-Module {
194191
Write-Verbose "Copy files to $OutputDirectory"
195192
# Copy the files and folders which won't be processed
196193
Copy-Item *.psm1, *.psd1, *.ps1xml -Exclude "build.psd1" -Destination $OutputDirectory -Force
197-
if ($ModuleInfo.CopyDirectories) {
198-
Write-Verbose "Copy Entire Directories: $($ModuleInfo.CopyDirectories)"
199-
Copy-Item -Path $ModuleInfo.CopyDirectories -Recurse -Destination $OutputDirectory -Force
194+
if ($ModuleInfo.CopyPaths) {
195+
Write-Verbose "Copy Entire Directories: $($ModuleInfo.CopyPaths)"
196+
Copy-Item -Path $ModuleInfo.CopyPaths -Recurse -Destination $OutputDirectory -Force
200197
}
201198

202199
Write-Verbose "Combine scripts to $RootModule"
@@ -275,5 +272,6 @@ function Build-Module {
275272
} finally {
276273
Pop-Location -StackName Build-Module -ErrorAction SilentlyContinue
277274
}
275+
Write-Progress "Building $($ModuleInfo.Name)" -Completed
278276
}
279277
}

Source/Public/Convert-LineNumber.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ function Convert-LineNumber {
3939
$SourceFile = $Invocation.SourceFile
4040
$SourceLineNumber = $Invocation.SourceLineNumber
4141
}
42-
$PSScriptRoot = Split-Path $SourceFile
43-
4442
if(!(Test-Path $SourceFile)) {
4543
throw "'$SourceFile' does not exist"
4644
}
45+
$PSScriptRoot = Split-Path $SourceFile
4746

4847
Push-Location $PSScriptRoot
4948
try {

Tests/Public/Build-Module.Tests.ps1

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,37 @@ Describe "Build-Module" {
99
$parameters["SourcePath"].ParameterType | Should -Be ([string])
1010
$parameters["SourcePath"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
1111
}
12+
It "throws if the SourcePath doesn't exist" {
13+
{ Build-Module -SourcePath TestDrive:\NoSuchPath } | Should -Throw "Source must point to a valid module"
14+
}
1215

1316
It "has an optional string parameter for the OutputDirectory" {
1417
$parameters.ContainsKey("OutputDirectory") | Should -Be $true
1518
$parameters["OutputDirectory"].ParameterType | Should -Be ([string])
1619
$parameters["OutputDirectory"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
1720
}
1821

19-
It "has an optional parameter for setting the Version"{
22+
It "has an optional parameter for setting the Version" {
2023
$parameters.ContainsKey("Version") | Should -Be $true
2124
$parameters["Version"].ParameterType | Should -Be ([version])
2225
$parameters["Version"].ParameterSets.Keys | Should -Not -Be "__AllParameterSets"
2326
}
2427

25-
It "has an optional parameter for setting the Encoding"{
28+
It "has an optional parameter for setting the Encoding" {
2629
$parameters.ContainsKey("Encoding") | Should -Be $true
2730
# Note that in PS Core, we can't use encoding types for parameters
2831
$parameters["Encoding"].ParameterType | Should -Be ([string])
2932
$parameters["Encoding"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
3033
}
3134

32-
It "has an optional string parameter for a Prefix"{
35+
It "Warns if you set the encoding to anything but UTF8" {
36+
$warns = @()
37+
# Note: Using WarningAction Stop just to avoid testing anything else here ;)
38+
try { Build-Module -Encoding ASCII -WarningAction Stop -WarningVariable +warns } catch {}
39+
$warns.Message | Should -Match "recommend you build your script modules with UTF8 encoding"
40+
}
41+
42+
It "has an optional string parameter for a Prefix" {
3343
$parameters.ContainsKey("Prefix") | Should -Be $true
3444
$parameters["Prefix"].ParameterType | Should -Be ([string])
3545
$parameters["Prefix"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
@@ -49,6 +59,14 @@ Describe "Build-Module" {
4959
$parameters["Target"].Attributes.Where{$_ -is [ValidateSet]}.ValidValues | Should -Be "Clean", "Build", "CleanBuild"
5060
}
5161

62+
It "supports an optional string array parameter CopyPaths (which used to be CopyDirectories)" {
63+
$parameters.ContainsKey("CopyPaths") | Should -Be $true
64+
65+
# Techincally we could implement this a few other ways ...
66+
$parameters["CopyPaths"].ParameterType | Should -Be ([string[]])
67+
$parameters["CopyPaths"].Aliases | Should -Contain "CopyDirectories"
68+
}
69+
5270
It "has an Passthru switch parameter" {
5371
$parameters.ContainsKey("Passthru") | Should -Be $true
5472
$parameters["Passthru"].ParameterType | Should -Be ([switch])
@@ -229,7 +247,7 @@ Describe "Build-Module" {
229247
OutputDirectory = "TestDrive:\$Version"
230248
Name = "MyModule"
231249
ModuleBase = "TestDrive:\MyModule\"
232-
CopyDirectories = @()
250+
CopyPaths = @()
233251
Encoding = "UTF8"
234252
PublicFilter = "Public\*.ps1"
235253
}
@@ -344,7 +362,7 @@ Describe "Build-Module" {
344362
OutputDirectory = "TestDrive:\$Version"
345363
Name = "MyModule"
346364
ModuleBase = "TestDrive:\MyModule\"
347-
CopyDirectories = @()
365+
CopyPaths = @()
348366
Encoding = "UTF8"
349367
PublicFilter = "Public\*.ps1"
350368
}
@@ -455,7 +473,7 @@ Describe "Build-Module" {
455473
OutputDirectory = "TestDrive:\$Version"
456474
Name = "MyModule"
457475
ModuleBase = "TestDrive:\MyModule\"
458-
CopyDirectories = @()
476+
CopyPaths = @()
459477
Encoding = "UTF8"
460478
PublicFilter = "Public\*.ps1"
461479
}
@@ -551,4 +569,38 @@ Describe "Build-Module" {
551569
'TestDrive:\output\MyModule.psm1' | Should -FileContentMatch 'MATCHING TEST CONTENT'
552570
}
553571
}
572+
573+
Context "Copies additional items specified in CopyPaths" {
574+
575+
$null = New-Item "TestDrive:\build.psd1" -Type File -Force -Value "@{}"
576+
$null = New-ModuleManifest "TestDrive:\MyModule.psd1" -ModuleVersion "1.0.0" -Author "Tester"
577+
$null = New-Item "TestDrive:\Public\Test.ps1" -Type File -Value 'MATCHING TEST CONTENT' -Force
578+
$null = New-Item "TestDrive:\MyModule.format.ps1xml" -Type File -Value '<Configuration />' -Force
579+
$null = New-Item "TestDrive:\lib\imaginary1.dll" -Type File -Value '1' -Force
580+
$null = New-Item "TestDrive:\lib\subdir\imaginary2.dll" -Type File -Value '2' -Force
581+
582+
Mock GetBuildInfo -ModuleName ModuleBuilder {
583+
[PSCustomObject]@{
584+
SourcePath = "TestDrive:\MyModule.psd1"
585+
Version = [Version]"1.0.0"
586+
OutputDirectory = "./output"
587+
CopyPaths = "./lib", "./MyModule.format.ps1xml"
588+
Encoding = 'UTF8'
589+
SourceDirectories = @('Public')
590+
}
591+
}
592+
593+
$Result = Build-Module -SourcePath 'TestDrive:\build.psd1' -OutputDirectory '.\output' -Passthru -Target Build
594+
595+
It "Copies single files that are in CopyPaths" {
596+
$Result.ModuleBase | Should -Be ("TestDrive:\output" | Convert-Path).TrimEnd('\')
597+
'TestDrive:\output\MyModule.format.ps1xml' | Should -Exist
598+
'TestDrive:\output\MyModule.format.ps1xml' | Should -FileContentMatch '<Configuration />'
599+
}
600+
601+
It "Recursively copies all the files in folders that are in CopyPaths" {
602+
'TestDrive:\output\lib\imaginary1.dll' | Should -FileContentMatch '1'
603+
'TestDrive:\output\lib\subdir\imaginary2.dll' | Should -FileContentMatch '2'
604+
}
605+
}
554606
}

Tests/Public/Convert-LineNumber.Tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Describe "Convert-LineNumber" {
2626
}
2727
}
2828

29+
It "Should throw if the SourceFile doesn't exist" {
30+
{ Convert-LineNumber -SourceFile TestDrive:\NoSuchFile -SourceLineNumber 10 } | Should Throw "'TestDrive:\NoSuchFile' does not exist"
31+
}
32+
2933
It 'Should work with an error PositionMessage' {
3034
$line = Select-String -Path $ModulePath 'function ParseLineNumber {' | % LineNumber
3135

build.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ param(
1313
[string]$SemVer
1414
)
1515
# Sanitize parameters to pass to Build-Module
16-
$null = $PSBoundParameters.Remove('Test')
1716
$ErrorActionPreference = "Stop"
1817
Push-Location $PSScriptRoot -StackName BuildBuildModule
1918

nuget.config

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<!-- install packages to a custom path -->
44
<config>
5-
<add key="globalPackagesFolder" value="Tools"/>
5+
<add key="globalPackagesFolder" value="Tools" />
66
</config>
77
<packageSources>
88
<clear />
9-
<add key="powershell" value="https://www.Preview.PowerShellGallery.Com/api/v2" />
109
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
1110
</packageSources>
1211
</configuration>

test.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ Write-Host "Invoke-Pester for Module $($ModuleUnderTest) version $($ModuleUnderT
3333

3434
if (-not $SkipCodeCoverage) {
3535
# Get code coverage for the psm1 file to a coverage.xml that we can mess with later
36-
Invoke-Pester ./Tests -CodeCoverage $ModuleUnderTest.Path -CodeCoverageOutputFile ./coverage.xml -Show $Show -PesterOption @{ IncludeVSCodeMarker = $IncludeVSCodeMarker }
36+
Invoke-Pester ./Tests -Show $Show -PesterOption @{
37+
IncludeVSCodeMarker = $IncludeVSCodeMarker
38+
} -CodeCoverage $ModuleUnderTest.Path -CodeCoverageOutputFile ./coverage.xml -PassThru |
39+
Convert-CodeCoverage -SourceRoot ./Source -Relative
3740
} else {
3841
Invoke-Pester ./Tests -Show $Show -PesterOption @{ IncludeVSCodeMarker = $IncludeVSCodeMarker }
3942
}

0 commit comments

Comments
 (0)