Skip to content

Commit 599a8f3

Browse files
committed
fix: Prefix dot-files and dot-directories with an underscore so they are included in the module package, and remove the underscore prefix when copying files to a new repo
docs: Add info about dot-files underscore prefix workaround test: Add tests to ensure underscore prefix is removed from dot-files and dot-directories
1 parent c5a9ecf commit 599a8f3

14 files changed

+51
-2
lines changed

docs/Contributing.md

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ If you want to increment the Major or Minor version number, you have 2 options:
2323
Builds are not triggered on tags, and thus the version tag will be used as the starting point for the next version.
2424
e.g. Creating a new tag of `v2.4.0` will produce a new version of `2.4.1` on the next commit to the `main` branch.
2525

26+
## Why are the template dot-files filenames prefixed with an underscore?
27+
28+
`Publish-Module` has a bug where it does not include any files or directories starting with `.` in the module NuGet package.
29+
The newer `Publish-PSResource` has fixed this issue somewhat so the directories and some of the files are included, but it still leaves out some dot-files, like the `.gitignore` and `.editorconfig` files.
30+
To work around these issues, we prefix the files with an underscore (e.g. `_.gitignore`) so that they are included in the module package, and then remove the underscore prefix during the file copy process of the `New-PowerShellScriptModuleRepository` cmdlet.
31+
2632
## ⁉ Why was a specific decision made
2733

2834
Curious about some of the choices made in this project?

src/ScriptModuleRepositoryTemplate/ScriptModuleRepositoryTemplate.Tests.ps1

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
using module './ScriptModuleRepositoryTemplate.psm1'
22

33
Describe 'New-PowerShellScriptModuleRepository' {
4-
It 'Should create a new directory with the module repository files' {
4+
BeforeEach {
5+
[string] $TemporaryRepoPath = "$TestDrive\NewModule"
6+
if (Test-Path -Path $TemporaryRepoPath) {
7+
Remove-Item -Path $TemporaryRepoPath -Recurse -Force
8+
}
9+
}
10+
11+
It 'Should create a new directory with the module repository files using the specified module name' {
512
# Arrange.
6-
$repositoryDirectoryPath = "$TestDrive\NewModule"
13+
$repositoryDirectoryPath = $TemporaryRepoPath
714
$moduleName = 'NewModule'
815
$organizationName = 'My Organization'
916

@@ -21,4 +28,30 @@ Describe 'New-PowerShellScriptModuleRepository' {
2128
$expectedModuleManifestFilePath | Should -Exist
2229
$expectedModuleTestsFilePath | Should -Exist
2330
}
31+
32+
It 'Should replace all dot-files and directories prefixed with an underscore to remove the underscore' {
33+
# Arrange.
34+
$repositoryDirectoryPath = $TemporaryRepoPath
35+
$moduleName = 'NewModule'
36+
$organizationName = 'My Organization'
37+
38+
$expectedDotDirectoryPath = Join-Path -Path $repositoryDirectoryPath -ChildPath ".vscode"
39+
$expectedDotFilePath = Join-Path -Path $repositoryDirectoryPath -ChildPath ".gitignore"
40+
41+
# Act.
42+
New-PowerShellScriptModuleRepository -RepositoryDirectoryPath $repositoryDirectoryPath -ModuleName $moduleName -OrganizationName $organizationName
43+
44+
# Assert.
45+
46+
# No files should start with '_.'
47+
$repoFilePaths = Get-ChildItem -Path $repositoryDirectoryPath -Recurse -Force
48+
$repoFilePaths | ForEach-Object {
49+
[string] $fileName = $_.Name
50+
$fileName | Should -Not -Match '^_\.'
51+
}
52+
53+
# Verify at least one Dot directory and file were renamed properly.
54+
$expectedDotDirectoryPath | Should -Exist
55+
$expectedDotFilePath | Should -Exist
56+
}
2457
}

src/ScriptModuleRepositoryTemplate/ScriptModuleRepositoryTemplate.psm1

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ function CopyTemplateFilesToRepositoryRoot([string] $repositoryDirectoryPath)
7171
Write-Verbose "Copying the template repository files from '$templateModuleDirectoryPath' to the repository directory '$repositoryDirectoryPath'."
7272
Copy-Item -Path $templateModuleDirectoryPath\* -Destination $repositoryDirectoryPath -Recurse -Force
7373
}
74+
75+
# Rename all dot-files prefixed with an underscore to remove the underscore.
76+
# The underscore prefix is a workaround to a bug with Publish-Module and Publish-PSResource the excludes dot-files
77+
# and dot-directories from being included in the module package.
78+
$repoDotFiles = Get-ChildItem -Path $repositoryDirectoryPath -Recurse -Force -Filter '_.*'
79+
$repoDotFiles | ForEach-Object {
80+
[string] $filePath = $_.FullName
81+
[string] $newFileName = $_.Name -replace '^_\.', '.'
82+
Rename-Item -Path $filePath -NewName $newFileName -Force
83+
}
7484
}
7585

7686
function SetModuleFileNames([string] $repositoryDirectoryPath, [string] $moduleName)

0 commit comments

Comments
 (0)