Skip to content

Commit d4ca4bb

Browse files
committed
Rename CopyDirectories to CopyPaths because it supports files
Add an alias for backward compatibility Add a test for functionality and parameter existence
1 parent cfc29c0 commit d4ca4bb

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

Source/Public/Build-Module.ps1

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Build-Module {
1717
The optimization process:
1818
1. The OutputDirectory is created
1919
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
20+
3. If specified, $CopyPaths (relative to the Source) will be copied to the output
2121
4. The ModuleName.psm1 will be generated (overwritten completely) by concatenating all .ps1 files in the $SourceDirectories subdirectories
2222
5. The ModuleVersion and ExportedFunctions in the ModuleName.psd1 may be updated (depending on parameters)
2323
@@ -88,7 +88,8 @@ function Build-Module {
8888
# Folders which should be copied intact to the module output
8989
# Can be relative to the module folder
9090
[AllowEmptyCollection()]
91-
[string[]]$CopyDirectories = @(),
91+
[Alias("CopyDirectories")]
92+
[string[]]$CopyPaths = @(),
9293

9394
# Folders which contain source .ps1 scripts to be concatenated into the module
9495
# Defaults to Enum, Classes, Private, Public
@@ -194,9 +195,9 @@ function Build-Module {
194195
Write-Verbose "Copy files to $OutputDirectory"
195196
# Copy the files and folders which won't be processed
196197
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
198+
if ($ModuleInfo.CopyPaths) {
199+
Write-Verbose "Copy Entire Directories: $($ModuleInfo.CopyPaths)"
200+
Copy-Item -Path $ModuleInfo.CopyPaths -Recurse -Destination $OutputDirectory -Force
200201
}
201202

202203
Write-Verbose "Combine scripts to $RootModule"

Tests/Public/Build-Module.Tests.ps1

+45-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ Describe "Build-Module" {
4949
$parameters["Target"].Attributes.Where{$_ -is [ValidateSet]}.ValidValues | Should -Be "Clean", "Build", "CleanBuild"
5050
}
5151

52+
It "supports an optional string array parameter CopyPaths (which used to be CopyDirectories)" {
53+
$parameters.ContainsKey("CopyPaths") | Should -Be $true
54+
55+
# Techincally we could implement this a few other ways ...
56+
$parameters["CopyPaths"].ParameterType | Should -Be ([string[]])
57+
$parameters["CopyPaths"].Aliases | Should -Contain "CopyDirectories"
58+
}
59+
5260
It "has an Passthru switch parameter" {
5361
$parameters.ContainsKey("Passthru") | Should -Be $true
5462
$parameters["Passthru"].ParameterType | Should -Be ([switch])
@@ -229,7 +237,7 @@ Describe "Build-Module" {
229237
OutputDirectory = "TestDrive:\$Version"
230238
Name = "MyModule"
231239
ModuleBase = "TestDrive:\MyModule\"
232-
CopyDirectories = @()
240+
CopyPaths = @()
233241
Encoding = "UTF8"
234242
PublicFilter = "Public\*.ps1"
235243
}
@@ -344,7 +352,7 @@ Describe "Build-Module" {
344352
OutputDirectory = "TestDrive:\$Version"
345353
Name = "MyModule"
346354
ModuleBase = "TestDrive:\MyModule\"
347-
CopyDirectories = @()
355+
CopyPaths = @()
348356
Encoding = "UTF8"
349357
PublicFilter = "Public\*.ps1"
350358
}
@@ -455,7 +463,7 @@ Describe "Build-Module" {
455463
OutputDirectory = "TestDrive:\$Version"
456464
Name = "MyModule"
457465
ModuleBase = "TestDrive:\MyModule\"
458-
CopyDirectories = @()
466+
CopyPaths = @()
459467
Encoding = "UTF8"
460468
PublicFilter = "Public\*.ps1"
461469
}
@@ -551,4 +559,38 @@ Describe "Build-Module" {
551559
'TestDrive:\output\MyModule.psm1' | Should -FileContentMatch 'MATCHING TEST CONTENT'
552560
}
553561
}
562+
563+
Context "Copies additional items specified in CopyPaths" {
564+
565+
$null = New-Item "TestDrive:\build.psd1" -Type File -Force -Value "@{}"
566+
$null = New-ModuleManifest "TestDrive:\MyModule.psd1" -ModuleVersion "1.0.0" -Author "Tester"
567+
$null = New-Item "TestDrive:\Public\Test.ps1" -Type File -Value 'MATCHING TEST CONTENT' -Force
568+
$null = New-Item "TestDrive:\MyModule.format.ps1xml" -Type File -Value '<Configuration />' -Force
569+
$null = New-Item "TestDrive:\lib\imaginary1.dll" -Type File -Value '1' -Force
570+
$null = New-Item "TestDrive:\lib\subdir\imaginary2.dll" -Type File -Value '2' -Force
571+
572+
Mock GetBuildInfo -ModuleName ModuleBuilder {
573+
[PSCustomObject]@{
574+
SourcePath = "TestDrive:\MyModule.psd1"
575+
Version = [Version]"1.0.0"
576+
OutputDirectory = "./output"
577+
CopyPaths = "./lib", "./MyModule.format.ps1xml"
578+
Encoding = 'UTF8'
579+
SourceDirectories = @('Public')
580+
}
581+
}
582+
583+
$Result = Build-Module -SourcePath 'TestDrive:\build.psd1' -OutputDirectory '.\output' -Passthru -Target Build
584+
585+
It "Copies single files that are in CopyPaths" {
586+
$Result.ModuleBase | Should -Be ("TestDrive:\output" | Convert-Path).TrimEnd('\')
587+
'TestDrive:\output\MyModule.format.ps1xml' | Should -Exist
588+
'TestDrive:\output\MyModule.format.ps1xml' | Should -FileContentMatch '<Configuration />'
589+
}
590+
591+
It "Recursively copies all the files in folders that are in CopyPaths" {
592+
'TestDrive:\output\lib\imaginary1.dll' | Should -FileContentMatch '1'
593+
'TestDrive:\output\lib\subdir\imaginary2.dll' | Should -FileContentMatch '2'
594+
}
595+
}
554596
}

nuget.config

+2-3
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>

0 commit comments

Comments
 (0)