Skip to content

Commit

Permalink
- Rename files to __NewModuleName__
Browse files Browse the repository at this point in the history
- Use replacement tokens in all of the template files
- Update initialization script to use tokens and new file structure
  • Loading branch information
deadlydog committed Feb 26, 2024
1 parent c1b3306 commit ad87d43
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 41 deletions.
76 changes: 42 additions & 34 deletions _InitializeRepository.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,40 @@
Process
{
Write-Host -ForegroundColor Green "
This script will replace the default template repository files and values with ones specific to your module.
If you have made changes to any files, you may want to commit them before continuing, as this script may overwrite them.
This script will replace the files in this repo with template files specific to your module.
If you have made changes to any files you may want to commit them before continuing, as this script will likely overwrite them.
"

[string] $moduleName = Read-Host -Prompt "Enter the name of your module (e.g. 'YourModuleName')"

[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"

Remove-AllRepositoryFilesExceptTemplateModuleFiles
Copy-TemplateFilesToRepositoryRoot
Remove-TemplateModuleFiles
New-ModuleFiles -ModuleName $moduleName -OrganizationName $organizationName
Set-RepositoryDefaultFiles -ModuleName $moduleName -OrganizationName $organizationName
Remove-TemplateDefaultFiles
Set-ModuleFileNames -moduleName $moduleName
Set-TemplateTokenValuesInAllRepoFiles -moduleName $moduleName -organizationName $organizationName
}

Begin
{
$InformationPreference = 'Continue'
[string] $RepositoryRoot = $PSScriptRoot

function Remove-AllRepositoryFilesExceptTemplateModuleFiles
{
Remove-Item -Path $RepositoryRoot\* -Recurse -Force -Exclude '_InitializeRepository.ps1','src\Template.PowerShell.ScriptModule'
}

function Copy-TemplateFilesToRepositoryRoot
{
[string] $templateModuleDirectoryPath = "$RepositoryRoot\src\Template.PowerShell.ScriptModule"
if (Test-Path -Path $templateModuleDirectoryPath -PathType Container)
{
Copy-Item -Path $templateModuleDirectoryPath\* -Destination $RepositoryRoot -Recurse -Force
}
}

function Remove-TemplateModuleFiles
{
[string] $templateModuleDirectoryPath = "$RepositoryRoot\src\Template.PowerShell.ScriptModule"
Expand All @@ -31,52 +46,45 @@ Begin
}
}

function New-ModuleFiles([string] $moduleName, [string] $organizationName)
function Set-ModuleFileNames([string] $moduleName)
{
[string] $moduleDirectoryPath = "$RepositoryRoot\src\$moduleName"
[string] $moduleFilePath = "$moduleDirectoryPath\$moduleName.psm1"
[string] $moduleManifestFilePath = "$moduleDirectoryPath\$moduleName.psd1"
[string] $moduleTestsFilePath = "$moduleDirectoryPath\$moduleName.Tests.ps1"
[string] $moduleDirectoryPath = "$RepositoryRoot\src\__NewModuleName__"
[string] $moduleFilePath = "$moduleDirectoryPath\__NewModuleName__.psm1"
[string] $moduleManifestFilePath = "$moduleDirectoryPath\__NewModuleName__.psd1"
[string] $moduleTestsFilePath = "$moduleDirectoryPath\__NewModuleName__.Tests.ps1"

# Create the module directory.
if (-Not (Test-Path -Path $moduleDirectoryPath -PathType Container))
if (Test-Path -Path $moduleDirectoryPath -PathType Container)
{
New-Item -Path $moduleDirectoryPath -ItemType Directory
Rename-Item -Path $moduleDirectoryPath -NewName $moduleName -Force
}

# Create the module file.
if (-Not (Test-Path -Path $moduleFilePath -PathType Leaf))
if (Test-Path -Path $moduleFilePath -PathType Leaf)
{

Rename-Item -Path $moduleFilePath -NewName "$moduleName.psm1" -Force
}

# Create the module manifest file.
if (-Not (Test-Path -Path $moduleManifestFilePath -PathType Leaf))
{

}
else
if (Test-Path -Path $moduleManifestFilePath -PathType Leaf)
{
Write-Host "Module manifest file already exists at '$moduleManifestFilePath'. Skipping creation."
Rename-Item -Path $moduleManifestFilePath -NewName "$moduleName.psd1" -Force
}

# Create the module tests file.
if (-Not (Test-Path -Path $moduleTestsFilePath -PathType Leaf))
{

}
else
if (Test-Path -Path $moduleTestsFilePath -PathType Leaf)
{
Write-Host "Module tests file already exists at '$moduleTestsFilePath'. Skipping creation."
Rename-Item -Path $moduleTestsFilePath -NewName "$moduleName.Tests.ps1" -Force
}
}

function Remove-TemplateDefaultFiles
function Set-TemplateTokenValuesInAllRepoFiles([string] $moduleName, [string] $organizationName)
{
[string] $templateDefaultFilesDirectoryPath = "$RepositoryRoot\_TemplateDefaultFiles"
if (Test-Path -Path $templateDefaultFilesDirectoryPath -PathType Container)
$repositoryFiles = Get-ChildItem -Path $RepositoryRoot -Recurse -File
foreach ($file in $repositoryFiles)
{
Remove-Item -Path $templateDefaultFilesDirectoryPath -Recurse -Force
$filePath = $file.FullName
$contents = Get-Content -Path $filePath
$contents = $contents -replace '__NewModuleName__', $moduleName
$contents = $contents -replace '__IndividualOrOrganizationName__', $organizationName
$contents = $contents -replace '__NewModuleGuid__', (New-Guid).ToString()
Set-Content -Path $filePath -Value $contents
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ on:
value: ${{ jobs.build-and-test.outputs.deployFilesArtifactName }}

env:
powerShellModuleName: 'NewModuleName'
powerShellModuleDirectoryPath: './src/NewModuleName'
powerShellModuleName: '__NewModuleName__'
powerShellModuleDirectoryPath: './src/__NewModuleName__'
deployFilesDirectoryPath: './deploy'
prereleaseModuleArtifactName: 'PrereleaseModuleArtifact'
prereleaseModuleArtifactDirectoryPath: './artifacts/Prerelease'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Daniel Schroeder
Copyright (c) 2024 __IndividualOrOrganizationName__

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:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NewModuleName PowerShell Module
# __NewModuleName__ PowerShell Module

## 💬 Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# To run these tests on your local machine, see the comments in the BeforeAll block.

BeforeAll {
Import-Module -Name 'NewModuleName' -Force
Import-Module -Name '__NewModuleName__' -Force

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

Describe 'Get-HelloWorld' {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module './NewModuleName.psm1'
using module './__NewModuleName__.psm1'

# UPDATE ME: This is just example code. Replace the code below with your module's tests.
Describe 'Get-HelloWorld' {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
@{

# Script module or binary module file associated with this manifest.
RootModule = '__NewModuleName__.psm1'

# Version number of this module.
ModuleVersion = '0.0.0'

# Supported PSEditions
# CompatiblePSEditions = @()

# ID used to uniquely identify this module
GUID = '__NewModuleGuid__'

# Author of this module
Author = '__IndividualOrOrganizationName__'

# Company or vendor of this module
CompanyName = '__IndividualOrOrganizationName__'

# Copyright statement for this module
Copyright = '(c) __IndividualOrOrganizationName__. All rights reserved.'

# Description of the functionality provided by this module
# Description = ''

# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# 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.
FunctionsToExport = @(
'Get-HelloWorld'
)

# 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.
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = '*'

# 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.
AliasesToExport = @()

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# 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.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

0 comments on commit ad87d43

Please sign in to comment.