Skip to content

Commit 4ab9b34

Browse files
committed
feat: Move appropriate functions into template module for creating the new repo
1 parent f01cee4 commit 4ab9b34

File tree

3 files changed

+95
-66
lines changed

3 files changed

+95
-66
lines changed

_InitializeRepository.ps1

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,96 +11,43 @@ If you have made changes to any files you may want to commit them before continu
1111

1212
[string] $organizationName = Read-Host -Prompt "Enter your name, or the the name of your organization (e.g. 'My Company'). This will be used in the module manifest and repository license"
1313

14+
Import-Module -Name $TemplateModuleDirectoryPath -Force
1415
Remove-AllRepositoryFilesExceptTemplateModuleFiles
15-
Copy-TemplateFilesToRepositoryRoot
16+
New-PowerShellModuleRepository -RepositoryDirectoryPath $RepositoryDirectoryPath -ModuleName $moduleName -OrganizationName $organizationName
1617
Remove-TemplateModuleFiles
17-
Set-ModuleFileNames -moduleName $moduleName
18-
Set-TemplateTokenValuesInAllRepoFiles -moduleName $moduleName -organizationName $organizationName
1918

2019
Write-Host -ForegroundColor Green "Repo initialization complete. You can now commit the changes to your repository."
2120
}
2221

2322
Begin
2423
{
2524
$InformationPreference = 'Continue'
26-
[string] $RepositoryRoot = Resolve-Path -Path $PSScriptRoot
25+
[string] $RepositoryDirectoryPath = Resolve-Path -Path $PSScriptRoot
26+
[string] $TemplateModuleDirectoryPath = "$RepositoryDirectoryPath\src\Template.PowerShell.ScriptModule"
2727

2828
function Remove-AllRepositoryFilesExceptTemplateModuleFiles
2929
{
3030
# Delete all files except the ones we want to keep.
31-
Get-ChildItem -Path $RepositoryRoot -Recurse -File |
31+
Get-ChildItem -Path $RepositoryDirectoryPath -Recurse -File |
3232
Where-Object {
33-
$_.FullName -notlike "$RepositoryRoot\.git\*" -and # Don't delete the .git directory.
34-
$_.FullName -notlike "$RepositoryRoot\_InitializeRepository.ps1" -and # Don't delete this script.
35-
$_.FullName -notlike "$RepositoryRoot\src\Template.PowerShell.ScriptModule\*" # Don't delete the template module files.
33+
$_.FullName -notlike "$RepositoryDirectoryPath\.git\*" -and # Don't delete the .git directory.
34+
$_.FullName -notlike "$RepositoryDirectoryPath\_InitializeRepository.ps1" -and # Don't delete this script.
35+
$_.FullName -notlike "$TemplateModuleDirectoryPath\*" # Don't delete the template module files.
3636
} |
3737
Remove-Item -Force
3838

3939
# Delete all empty directories that were left behind.
40-
Get-ChildItem -Path $RepositoryRoot -Recurse -Force -Directory |
40+
Get-ChildItem -Path $RepositoryDirectoryPath -Recurse -Force -Directory |
4141
Sort-Object -Property FullName -Descending | # Delete child directories before parent directories.
4242
Where-Object { $_.GetFileSystemInfos().Count -eq 0 } |
4343
Remove-Item -Force
4444
}
4545

46-
function Copy-TemplateFilesToRepositoryRoot
47-
{
48-
[string] $templateModuleDirectoryPath = "$RepositoryRoot\src\Template.PowerShell.ScriptModule\TemplateRepoFiles"
49-
if (Test-Path -Path $templateModuleDirectoryPath -PathType Container)
50-
{
51-
Copy-Item -Path $templateModuleDirectoryPath\* -Destination $RepositoryRoot -Recurse -Force
52-
}
53-
}
54-
5546
function Remove-TemplateModuleFiles
5647
{
57-
[string] $templateModuleDirectoryPath = "$RepositoryRoot\src\Template.PowerShell.ScriptModule"
58-
if (Test-Path -Path $templateModuleDirectoryPath -PathType Container)
59-
{
60-
Remove-Item -Path $templateModuleDirectoryPath -Recurse -Force
61-
}
62-
}
63-
64-
function Set-ModuleFileNames([string] $moduleName)
65-
{
66-
[string] $moduleDirectoryPath = "$RepositoryRoot\src\__NewModuleName__"
67-
[string] $moduleFilePath = "$moduleDirectoryPath\__NewModuleName__.psm1"
68-
[string] $moduleManifestFilePath = "$moduleDirectoryPath\__NewModuleName__.psd1"
69-
[string] $moduleTestsFilePath = "$moduleDirectoryPath\__NewModuleName__.Tests.ps1"
70-
71-
if (Test-Path -Path $moduleFilePath -PathType Leaf)
72-
{
73-
Rename-Item -Path $moduleFilePath -NewName "$moduleName.psm1" -Force
74-
}
75-
76-
if (Test-Path -Path $moduleManifestFilePath -PathType Leaf)
77-
{
78-
Rename-Item -Path $moduleManifestFilePath -NewName "$moduleName.psd1" -Force
79-
}
80-
81-
if (Test-Path -Path $moduleTestsFilePath -PathType Leaf)
82-
{
83-
Rename-Item -Path $moduleTestsFilePath -NewName "$moduleName.Tests.ps1" -Force
84-
}
85-
86-
# Rename the directory last.
87-
if (Test-Path -Path $moduleDirectoryPath -PathType Container)
88-
{
89-
Rename-Item -Path $moduleDirectoryPath -NewName $moduleName -Force
90-
}
91-
}
92-
93-
function Set-TemplateTokenValuesInAllRepoFiles([string] $moduleName, [string] $organizationName)
94-
{
95-
$repositoryFiles = Get-ChildItem -Path $RepositoryRoot -Recurse -File -Exclude '_InitializeRepository.ps1'
96-
foreach ($file in $repositoryFiles)
48+
if (Test-Path -Path $TemplateModuleDirectoryPath -PathType Container)
9749
{
98-
$filePath = $file.FullName
99-
$contents = Get-Content -Path $filePath
100-
$contents = $contents -replace '__NewModuleName__', $moduleName
101-
$contents = $contents -replace '__IndividualOrOrganizationName__', $organizationName
102-
$contents = $contents -replace '__NewModuleGuid__', (New-Guid).ToString()
103-
Set-Content -Path $filePath -Value $contents
50+
Remove-Item -Path $TemplateModuleDirectoryPath -Recurse -Force
10451
}
10552
}
10653
}

src/Template.PowerShell.ScriptModule/Template.PowerShell.ScriptModule.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Checkout the template repository at https://github.com/deadlydog/Template.PowerS
6969
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
7070
FunctionsToExport = @(
7171
'Get-TemplateDescription'
72+
'New-PowerShellModuleRepository'
7273
)
7374

7475
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,95 @@
11
# This is just example code. Run the _InitializeRepository.ps1 script to replace this file with your module.
22

3-
function Get-TemplateDescription {
3+
function Get-TemplateDescription
4+
{
45
[CmdletBinding()]
56
Param ()
67

78
[string] $description = @'
8-
This module does nothing, but is part of a template git repository that you can clone to create new PowerShell script modules quickly and easily with boilerplate files and CI/CD workflows already defined.
9+
This module is part of a template git repository that you can use to create new PowerShell script module repos quickly and easily with boilerplate files and CI/CD workflows already defined.
910
1011
For more information, visit the repository at https://github.com/deadlydog/Template.PowerShell.ScriptModule.
1112
'@
1213

1314
Write-Output $description
1415
}
16+
17+
function New-PowerShellModuleRepository
18+
{
19+
[CmdletBinding()]
20+
Param
21+
(
22+
[Parameter(Mandatory = $true, HelpMessage = "The path to the directory where the module repository should be created.")]
23+
[ValidateNotNullOrEmpty()]
24+
[string] $RepositoryDirectoryPath,
25+
26+
[Parameter(Mandatory = $true, HelpMessage = "The name of the module to create.")]
27+
[ValidateNotNullOrEmpty()]
28+
[string] $ModuleName,
29+
30+
[Parameter(Mandatory = $true, HelpMessage = "The name of the individual or organization that owns the module.")]
31+
[ValidateNotNullOrEmpty()]
32+
[string] $OrganizationName
33+
)
34+
35+
Copy-TemplateFilesToRepositoryRoot -repositoryDirectoryPath $RepositoryDirectoryPath
36+
Set-ModuleFileNames -repositoryDirectoryPath $RepositoryDirectoryPath -moduleName $ModuleName
37+
Set-TemplateTokenValuesInAllRepositoryFiles -repositoryDirectoryPath $RepositoryDirectoryPath -moduleName $ModuleName -organizationName $OrganizationName
38+
}
39+
40+
function Copy-TemplateFilesToRepositoryRoot([string] $repositoryDirectoryPath)
41+
{
42+
if (-not (Test-Path -Path $repositoryDirectoryPath -PathType Container))
43+
{
44+
New-Item -Path $repositoryDirectoryPath -ItemType Directory > $null
45+
}
46+
47+
[string] $templateModuleDirectoryPath = "$repositoryDirectoryPath\src\Template.PowerShell.ScriptModule\TemplateRepoFiles"
48+
if (Test-Path -Path $templateModuleDirectoryPath -PathType Container)
49+
{
50+
Copy-Item -Path $templateModuleDirectoryPath\* -Destination $repositoryDirectoryPath -Recurse -Force
51+
}
52+
}
53+
54+
function Set-ModuleFileNames([string] $repositoryDirectoryPath, [string] $moduleName)
55+
{
56+
[string] $moduleDirectoryPath = "$repositoryDirectoryPath\src\__NewModuleName__"
57+
[string] $moduleFilePath = "$moduleDirectoryPath\__NewModuleName__.psm1"
58+
[string] $moduleManifestFilePath = "$moduleDirectoryPath\__NewModuleName__.psd1"
59+
[string] $moduleTestsFilePath = "$moduleDirectoryPath\__NewModuleName__.Tests.ps1"
60+
61+
if (Test-Path -Path $moduleFilePath -PathType Leaf)
62+
{
63+
Rename-Item -Path $moduleFilePath -NewName "$moduleName.psm1" -Force
64+
}
65+
66+
if (Test-Path -Path $moduleManifestFilePath -PathType Leaf)
67+
{
68+
Rename-Item -Path $moduleManifestFilePath -NewName "$moduleName.psd1" -Force
69+
}
70+
71+
if (Test-Path -Path $moduleTestsFilePath -PathType Leaf)
72+
{
73+
Rename-Item -Path $moduleTestsFilePath -NewName "$moduleName.Tests.ps1" -Force
74+
}
75+
76+
# Rename the directory last.
77+
if (Test-Path -Path $moduleDirectoryPath -PathType Container)
78+
{
79+
Rename-Item -Path $moduleDirectoryPath -NewName $moduleName -Force
80+
}
81+
}
82+
83+
function Set-TemplateTokenValuesInAllRepositoryFiles([string] $repositoryDirectoryPath, [string] $moduleName, [string] $organizationName)
84+
{
85+
$repositoryFiles = Get-ChildItem -Path $RepositoryRoot -Recurse -File -Exclude '_InitializeRepository.ps1'
86+
foreach ($file in $repositoryFiles)
87+
{
88+
$filePath = $file.FullName
89+
$contents = Get-Content -Path $filePath
90+
$contents = $contents -replace '__NewModuleName__', $moduleName
91+
$contents = $contents -replace '__IndividualOrOrganizationName__', $organizationName
92+
$contents = $contents -replace '__NewModuleGuid__', (New-Guid).ToString()
93+
Set-Content -Path $filePath -Value $contents
94+
}
95+
}

0 commit comments

Comments
 (0)