generated from deadlydog/Template.NewGitRepo
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathScriptModuleRepositoryTemplate.psm1
130 lines (107 loc) · 5.41 KB
/
ScriptModuleRepositoryTemplate.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
function New-PowerShellScriptModuleRepository
{
<#
.SYNOPSIS
Creates a new PowerShell script module repository directory with boilerplate files and CI/CD workflows already defined.
.DESCRIPTION
This function creates a new PowerShell script module repository with boilerplate files and CI/CD workflows already defined. This allows you to create new PowerShell script modules quickly and easily.
Once the directory is created, you can run `git init` in it to initialize it as a git repository, and then push it to your own git server.
You will then need to follow the instructions in the ReadMe.md file to finish setting up the repository.
.PARAMETER RepositoryDirectoryPath
The path to the new directory that should be created for the module repository.
.PARAMETER ModuleName
The name of the module to create.
.PARAMETER OrganizationName
The name of the individual or organization that owns the module.
.EXAMPLE
PS> New-PowerShellScriptModuleRepository -RepositoryDirectoryPath 'C:\MyNewModule' -ModuleName 'MyNewModule' -OrganizationName 'My Name'
Creates a new module repository at 'C:\MyNewModule' with the module name 'MyNewModule' and the organization name 'My Name'.
.INPUTS
None. You cannot pipe objects to New-PowerShellScriptModuleRepository.
.OUTPUTS
None. New-PowerShellScriptModuleRepository does not return any output.
It creates a new directory with the module repository files.
.LINK
https://github.com/deadlydog/PowerShell.ScriptModuleRepositoryTemplate
#>
[CmdletBinding(SupportsShouldProcess)]
[Alias('New-PSRepository')]
Param
(
[Parameter(Mandatory = $true, HelpMessage = "The path to the new directory that should be created for the module repository.")]
[ValidateNotNullOrEmpty()]
[string] $RepositoryDirectoryPath,
[Parameter(Mandatory = $true, HelpMessage = "The name of the module to create.")]
[ValidateNotNullOrEmpty()]
[string] $ModuleName,
[Parameter(Mandatory = $true, HelpMessage = "The name of the individual or organization that owns the module.")]
[ValidateNotNullOrEmpty()]
[string] $OrganizationName
)
CopyTemplateFilesToRepositoryRoot -repositoryDirectoryPath $RepositoryDirectoryPath
SetModuleFileNames -repositoryDirectoryPath $RepositoryDirectoryPath -moduleName $ModuleName
SetTemplateTokenValuesInAllRepositoryFiles -repositoryDirectoryPath $RepositoryDirectoryPath -moduleName $ModuleName -organizationName $OrganizationName
}
function CopyTemplateFilesToRepositoryRoot([string] $repositoryDirectoryPath)
{
if (-not (Test-Path -Path $repositoryDirectoryPath -PathType Container))
{
Write-Verbose "Creating the repository directory '$repositoryDirectoryPath'."
New-Item -Path $repositoryDirectoryPath -ItemType Directory > $null
}
[string] $templateModuleDirectoryPath = "$PSScriptRoot\TemplateRepoFiles"
if (Test-Path -Path $templateModuleDirectoryPath -PathType Container)
{
Write-Verbose "Copying the template repository files from '$templateModuleDirectoryPath' to the repository directory '$repositoryDirectoryPath'."
Copy-Item -Path $templateModuleDirectoryPath\* -Destination $repositoryDirectoryPath -Recurse -Force
}
# Rename all dot-files prefixed with an underscore to remove the underscore.
# The underscore prefix is a workaround to a bug with Publish-Module and Publish-PSResource that exclude dot-files
# and dot-directories from being included in the module package.
$repoDotFiles = Get-ChildItem -Path $repositoryDirectoryPath -Recurse -Force -Filter '_.*'
$repoDotFiles | ForEach-Object {
[string] $filePath = $_.FullName
[string] $parentDirectory = Split-Path -Path $filePath -Parent
[string] $newFileName = $_.Name -replace '^_\.', '.'
[string] $newFilePath = Join-Path -Path $parentDirectory -ChildPath $newFileName
Move-Item -Path $filePath -Destination $newFilePath -Force
}
}
function SetModuleFileNames([string] $repositoryDirectoryPath, [string] $moduleName)
{
[string] $moduleDirectoryPath = "$repositoryDirectoryPath\src\__NewModuleName__"
[string] $moduleFilePath = "$moduleDirectoryPath\__NewModuleName__.psm1"
[string] $moduleManifestFilePath = "$moduleDirectoryPath\__NewModuleName__.psd1"
[string] $moduleTestsFilePath = "$moduleDirectoryPath\__NewModuleName__.Tests.ps1"
if (Test-Path -Path $moduleFilePath -PathType Leaf)
{
Rename-Item -Path $moduleFilePath -NewName "$moduleName.psm1" -Force
}
if (Test-Path -Path $moduleManifestFilePath -PathType Leaf)
{
Rename-Item -Path $moduleManifestFilePath -NewName "$moduleName.psd1" -Force
}
if (Test-Path -Path $moduleTestsFilePath -PathType Leaf)
{
Rename-Item -Path $moduleTestsFilePath -NewName "$moduleName.Tests.ps1" -Force
}
# Rename the directory last.
if (Test-Path -Path $moduleDirectoryPath -PathType Container)
{
Rename-Item -Path $moduleDirectoryPath -NewName $moduleName -Force
}
}
function SetTemplateTokenValuesInAllRepositoryFiles([string] $repositoryDirectoryPath, [string] $moduleName, [string] $organizationName)
{
$repositoryFiles = Get-ChildItem -Path $repositoryDirectoryPath -Recurse -File
foreach ($file in $repositoryFiles)
{
$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()
$contents = $contents -replace '__CurrentYear__', (Get-Date).Year
Set-Content -Path $filePath -Value $contents
}
}