Skip to content

Commit ad87d43

Browse files
committed
- Rename files to __NewModuleName__
- Use replacement tokens in all of the template files - Update initialization script to use tokens and new file structure
1 parent c1b3306 commit ad87d43

File tree

8 files changed

+174
-41
lines changed

8 files changed

+174
-41
lines changed

_InitializeRepository.ps1

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,40 @@
33
Process
44
{
55
Write-Host -ForegroundColor Green "
6-
This script will replace the default template repository files and values with ones specific to your module.
7-
If you have made changes to any files, you may want to commit them before continuing, as this script may overwrite them.
6+
This script will replace the files in this repo with template files specific to your module.
7+
If you have made changes to any files you may want to commit them before continuing, as this script will likely overwrite them.
88
"
99

1010
[string] $moduleName = Read-Host -Prompt "Enter the name of your module (e.g. 'YourModuleName')"
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+
Remove-AllRepositoryFilesExceptTemplateModuleFiles
15+
Copy-TemplateFilesToRepositoryRoot
1416
Remove-TemplateModuleFiles
15-
New-ModuleFiles -ModuleName $moduleName -OrganizationName $organizationName
16-
Set-RepositoryDefaultFiles -ModuleName $moduleName -OrganizationName $organizationName
17-
Remove-TemplateDefaultFiles
17+
Set-ModuleFileNames -moduleName $moduleName
18+
Set-TemplateTokenValuesInAllRepoFiles -moduleName $moduleName -organizationName $organizationName
1819
}
1920

2021
Begin
2122
{
2223
$InformationPreference = 'Continue'
2324
[string] $RepositoryRoot = $PSScriptRoot
2425

26+
function Remove-AllRepositoryFilesExceptTemplateModuleFiles
27+
{
28+
Remove-Item -Path $RepositoryRoot\* -Recurse -Force -Exclude '_InitializeRepository.ps1','src\Template.PowerShell.ScriptModule'
29+
}
30+
31+
function Copy-TemplateFilesToRepositoryRoot
32+
{
33+
[string] $templateModuleDirectoryPath = "$RepositoryRoot\src\Template.PowerShell.ScriptModule"
34+
if (Test-Path -Path $templateModuleDirectoryPath -PathType Container)
35+
{
36+
Copy-Item -Path $templateModuleDirectoryPath\* -Destination $RepositoryRoot -Recurse -Force
37+
}
38+
}
39+
2540
function Remove-TemplateModuleFiles
2641
{
2742
[string] $templateModuleDirectoryPath = "$RepositoryRoot\src\Template.PowerShell.ScriptModule"
@@ -31,52 +46,45 @@ Begin
3146
}
3247
}
3348

34-
function New-ModuleFiles([string] $moduleName, [string] $organizationName)
49+
function Set-ModuleFileNames([string] $moduleName)
3550
{
36-
[string] $moduleDirectoryPath = "$RepositoryRoot\src\$moduleName"
37-
[string] $moduleFilePath = "$moduleDirectoryPath\$moduleName.psm1"
38-
[string] $moduleManifestFilePath = "$moduleDirectoryPath\$moduleName.psd1"
39-
[string] $moduleTestsFilePath = "$moduleDirectoryPath\$moduleName.Tests.ps1"
51+
[string] $moduleDirectoryPath = "$RepositoryRoot\src\__NewModuleName__"
52+
[string] $moduleFilePath = "$moduleDirectoryPath\__NewModuleName__.psm1"
53+
[string] $moduleManifestFilePath = "$moduleDirectoryPath\__NewModuleName__.psd1"
54+
[string] $moduleTestsFilePath = "$moduleDirectoryPath\__NewModuleName__.Tests.ps1"
4055

41-
# Create the module directory.
42-
if (-Not (Test-Path -Path $moduleDirectoryPath -PathType Container))
56+
if (Test-Path -Path $moduleDirectoryPath -PathType Container)
4357
{
44-
New-Item -Path $moduleDirectoryPath -ItemType Directory
58+
Rename-Item -Path $moduleDirectoryPath -NewName $moduleName -Force
4559
}
4660

47-
# Create the module file.
48-
if (-Not (Test-Path -Path $moduleFilePath -PathType Leaf))
61+
if (Test-Path -Path $moduleFilePath -PathType Leaf)
4962
{
50-
63+
Rename-Item -Path $moduleFilePath -NewName "$moduleName.psm1" -Force
5164
}
5265

53-
# Create the module manifest file.
54-
if (-Not (Test-Path -Path $moduleManifestFilePath -PathType Leaf))
55-
{
56-
57-
}
58-
else
66+
if (Test-Path -Path $moduleManifestFilePath -PathType Leaf)
5967
{
60-
Write-Host "Module manifest file already exists at '$moduleManifestFilePath'. Skipping creation."
68+
Rename-Item -Path $moduleManifestFilePath -NewName "$moduleName.psd1" -Force
6169
}
6270

63-
# Create the module tests file.
64-
if (-Not (Test-Path -Path $moduleTestsFilePath -PathType Leaf))
65-
{
66-
67-
}
68-
else
71+
if (Test-Path -Path $moduleTestsFilePath -PathType Leaf)
6972
{
70-
Write-Host "Module tests file already exists at '$moduleTestsFilePath'. Skipping creation."
73+
Rename-Item -Path $moduleTestsFilePath -NewName "$moduleName.Tests.ps1" -Force
7174
}
7275
}
7376

74-
function Remove-TemplateDefaultFiles
77+
function Set-TemplateTokenValuesInAllRepoFiles([string] $moduleName, [string] $organizationName)
7578
{
76-
[string] $templateDefaultFilesDirectoryPath = "$RepositoryRoot\_TemplateDefaultFiles"
77-
if (Test-Path -Path $templateDefaultFilesDirectoryPath -PathType Container)
79+
$repositoryFiles = Get-ChildItem -Path $RepositoryRoot -Recurse -File
80+
foreach ($file in $repositoryFiles)
7881
{
79-
Remove-Item -Path $templateDefaultFilesDirectoryPath -Recurse -Force
82+
$filePath = $file.FullName
83+
$contents = Get-Content -Path $filePath
84+
$contents = $contents -replace '__NewModuleName__', $moduleName
85+
$contents = $contents -replace '__IndividualOrOrganizationName__', $organizationName
86+
$contents = $contents -replace '__NewModuleGuid__', (New-Guid).ToString()
87+
Set-Content -Path $filePath -Value $contents
8088
}
8189
}
8290
}

src/Template.PowerShell.ScriptModule/TemplateRepoFiles/.github/workflows/build-and-test-powershell-module.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ on:
3131
value: ${{ jobs.build-and-test.outputs.deployFilesArtifactName }}
3232

3333
env:
34-
powerShellModuleName: 'NewModuleName'
35-
powerShellModuleDirectoryPath: './src/NewModuleName'
34+
powerShellModuleName: '__NewModuleName__'
35+
powerShellModuleDirectoryPath: './src/__NewModuleName__'
3636
deployFilesDirectoryPath: './deploy'
3737
prereleaseModuleArtifactName: 'PrereleaseModuleArtifact'
3838
prereleaseModuleArtifactDirectoryPath: './artifacts/Prerelease'

src/Template.PowerShell.ScriptModule/TemplateRepoFiles/License.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Daniel Schroeder
3+
Copyright (c) 2024 __IndividualOrOrganizationName__
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

src/Template.PowerShell.ScriptModule/TemplateRepoFiles/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NewModuleName PowerShell Module
1+
# __NewModuleName__ PowerShell Module
22

33
## 💬 Description
44

src/Template.PowerShell.ScriptModule/TemplateRepoFiles/deploy/Invoke-SmokeTests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
# To run these tests on your local machine, see the comments in the BeforeAll block.
66

77
BeforeAll {
8-
Import-Module -Name 'NewModuleName' -Force
8+
Import-Module -Name '__NewModuleName__' -Force
99

1010
# To run these tests on your local machine, comment out the Import-Module command above and uncomment the one below.
1111
# Do this to use the module version from source code, not the installed version.
1212
# This is necessary to test functionality that you've added to the module, but have not yet published and installed.
13-
# Import-Module "$PSScriptRoot\..\src\NewModuleName" -Force
13+
# Import-Module "$PSScriptRoot\..\src\__NewModuleName__" -Force
1414
}
1515

1616
Describe 'Get-HelloWorld' {

src/Template.PowerShell.ScriptModule/TemplateRepoFiles/src/NewModuleName/NewModuleName.Tests.ps1 renamed to src/Template.PowerShell.ScriptModule/TemplateRepoFiles/src/__NewModuleName__/__NewModuleName__.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using module './NewModuleName.psm1'
1+
using module './__NewModuleName__.psm1'
22

33
# UPDATE ME: This is just example code. Replace the code below with your module's tests.
44
Describe 'Get-HelloWorld' {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
@{
2+
3+
# Script module or binary module file associated with this manifest.
4+
RootModule = '__NewModuleName__.psm1'
5+
6+
# Version number of this module.
7+
ModuleVersion = '0.0.0'
8+
9+
# Supported PSEditions
10+
# CompatiblePSEditions = @()
11+
12+
# ID used to uniquely identify this module
13+
GUID = '__NewModuleGuid__'
14+
15+
# Author of this module
16+
Author = '__IndividualOrOrganizationName__'
17+
18+
# Company or vendor of this module
19+
CompanyName = '__IndividualOrOrganizationName__'
20+
21+
# Copyright statement for this module
22+
Copyright = '(c) __IndividualOrOrganizationName__. All rights reserved.'
23+
24+
# Description of the functionality provided by this module
25+
# Description = ''
26+
27+
# Minimum version of the PowerShell engine required by this module
28+
# PowerShellVersion = ''
29+
30+
# Name of the PowerShell host required by this module
31+
# PowerShellHostName = ''
32+
33+
# Minimum version of the PowerShell host required by this module
34+
# PowerShellHostVersion = ''
35+
36+
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
37+
# DotNetFrameworkVersion = ''
38+
39+
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
40+
# ClrVersion = ''
41+
42+
# Processor architecture (None, X86, Amd64) required by this module
43+
# ProcessorArchitecture = ''
44+
45+
# Modules that must be imported into the global environment prior to importing this module
46+
# RequiredModules = @()
47+
48+
# Assemblies that must be loaded prior to importing this module
49+
# RequiredAssemblies = @()
50+
51+
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
52+
# ScriptsToProcess = @()
53+
54+
# Type files (.ps1xml) to be loaded when importing this module
55+
# TypesToProcess = @()
56+
57+
# Format files (.ps1xml) to be loaded when importing this module
58+
# FormatsToProcess = @()
59+
60+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
61+
# NestedModules = @()
62+
63+
# 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.
64+
FunctionsToExport = @(
65+
'Get-HelloWorld'
66+
)
67+
68+
# 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.
69+
CmdletsToExport = @()
70+
71+
# Variables to export from this module
72+
VariablesToExport = '*'
73+
74+
# Aliases 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 aliases to export.
75+
AliasesToExport = @()
76+
77+
# DSC resources to export from this module
78+
# DscResourcesToExport = @()
79+
80+
# List of all modules packaged with this module
81+
# ModuleList = @()
82+
83+
# List of all files packaged with this module
84+
# FileList = @()
85+
86+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
87+
PrivateData = @{
88+
89+
PSData = @{
90+
91+
# Tags applied to this module. These help with module discovery in online galleries.
92+
# Tags = @()
93+
94+
# A URL to the license for this module.
95+
# LicenseUri = ''
96+
97+
# A URL to the main website for this project.
98+
# ProjectUri = ''
99+
100+
# A URL to an icon representing this module.
101+
# IconUri = ''
102+
103+
# ReleaseNotes of this module
104+
# ReleaseNotes = ''
105+
106+
# Prerelease string of this module
107+
# Prerelease = ''
108+
109+
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
110+
# RequireLicenseAcceptance = $false
111+
112+
# External dependent modules of this module
113+
# ExternalModuleDependencies = @()
114+
115+
} # End of PSData hashtable
116+
117+
} # End of PrivateData hashtable
118+
119+
# HelpInfo URI of this module
120+
# HelpInfoURI = ''
121+
122+
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
123+
# DefaultCommandPrefix = ''
124+
125+
}

src/Template.PowerShell.ScriptModule/TemplateRepoFiles/src/NewModuleName/NewModuleName.psm1 renamed to src/Template.PowerShell.ScriptModule/TemplateRepoFiles/src/__NewModuleName__/__NewModuleName__.psm1

File renamed without changes.

0 commit comments

Comments
 (0)