Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active May 1, 2024 06:37
Show Gist options
  • Save jdhitsolutions/d34b2974817f043f4a468549a18939c1 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/d34b2974817f043f4a468549a18939c1 to your computer and use it in GitHub Desktop.
Presentation material for PSUGInnSalzach 9 Nov 2023
return "This is a demo script file."
#demo.ps1
#about me
https://jdhitsolutions.github.io
#Module layout
psedit c:\scripts\new-project.ps1
. c:\scripts\new-project.ps1
Help New-PSProject
#create new project in demo folder
New-PSProject -Name PSDemo -Path . -CommandNames "Get-Foo","Set-Foo" -Description "demo module" -NoGitHub
dir -Recurse
psedit .\PSDemo.psm1
psedit .\PSDemo.psd1
git status
# Install-Module PSFunctionTools
# https://github.com/jdhitsolutions/PSFunctionTools
Get-PSFunctionTools
#creating the layout
help Import-ModuleLayout -Examples
Help New-ModuleFromFiles -examples
help New-ModuleFromLayout -Examples
psedit C:\scripts\PSFunctionTools\samples\ModuleLayout.json
https://github.com/jdhitsolutions/PSFunctionTools/blob/main/samples/ModuleLayout.json
#adding commands
Export-FunctionFromFile -Path C:\scripts\JDH-Functions.ps1 -OutputPath .\functions\ -WhatIf
#alternatives
# Plaster and Stucco are popular PowerShell modules
#project status
# Install-Module PSProjectStatus
# https://github.com/jdhitsolutions/PSProjectStatus
help New-PSProjectStatus
New-PSProjectStatus -version 0.1.0
psedit .\psproject.json
git checkout -b 0.2.0
Set-PSProjectStatus -LastUpdate (Get-Date) -ProjectVersion 0.2.0 -tasks "help documentation","pester tests","update readme"
Get-PSProjectStatus
Get-PSProjectGitStatus
#look at updated JSON file
#adding help
# Install-Module platyps
# setup script copies my default tasks.json file
(Get-Content .\.vscode\tasks.json | ConvertFrom-Json).Tasks
#add help
#update help
#external help
#change log
# Install-Module ChangelogManagement
# https://github.com/natescherer/ChangelogManagement
Get-Command -module ChangeLogManagement
New-Changelog
psedit .\Changelog.md
help Add-ChangelogData
Add-ChangelogData -Type Added -Data "Added help documentation."
#a sample
psedit C:\scripts\PSWorkItem\changelog.md
# https://github.com/jdhitsolutions/PSWorkItem/blob/main/changelog.md
#publishing
psedit C:\scripts\Publish-Project.ps1
#deploy to github
psedit c:\scripts\gitreleasepush.ps1
#keep track of tasks
# Install-Module PSWorkItem
# https://github.com/jdhitsolutions/PSWorkItem
Get-PSWorkItem
New-PSWorkItem -Name "PSDemo update" -DaysDue 3 -Category Project -WhatIf
#run in a PS7 window
Open-PSWorkItemConsole -Path C:\work\PSWorkItem.db
#My favorite projects
# https://github.com/jdhitsolutions?tab=repositories
#PSScriptTools
# https://github.com/jdhitsolutions/PSScriptTools
#PSTypeExtensionTools
# https://github.com/jdhitsolutions/PSTypeExtensionTools
#PSReleaseTools
# https://github.com/jdhitsolutions/PSReleaseTools
MIT License
Copyright (c) 2023 JDH Information Technology Solutions, Inc.
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#requires -version 5.1
#requires -module Pester
#requires -module Platyps
<#
TODO:
split this up into different commands and create a module
validate pester test stub
Add parameters to specify PSEdition
#>
#use the gh.exe cli tool
# . C:\scripts\New-GitHubRepo.ps1
Function New-PSProject {
<# PSFunctionInfo
Version 2.0.0
Author Jeff Hicks
CompanyName JDH Information Technology Solutions, Inc.
Copyright 2021-2023
Description Create a PowerShell module project
Guid 8b92e54d-4e02-48a3-b811-62e27bda6d3a
Tags module
LastUpdate 7/14/2023 1:11 PM
Source C:\Scripts\New-Project.ps1
#>
<#
.SYNOPSIS
Create a PowerShell project folder.
.DESCRIPTION
Use this command to create a new PowerShell project folder. You specify a top level folder and the name of a project. The command will create a Tests sub-folder, a Pester script outline and set the project up in Git. The command will also create a Docs folder and a culture-specific folder for localized help.
The Git commands will initialize the folder, create a README.md file, a LICENSE.txt file, create a 0.0.1 branch and checkout that branch. The command assumes you have the Git command in your path.
.EXAMPLE
.NOTES
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
.LINK
git
.LINK
New-Item
.INPUTS
[string]
.OUTPUTS
None
#>
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(
Position = 0,
Mandatory,
HelpMessage = "The name of the project"
)]
[ValidateNotNullOrEmpty()]
#Specify the project name. This will become the directory name as well and the name of the GitHub repository.
[string]$Name,
[Parameter(HelpMessage = "The top level path for the new project")]
[ValidateNotNullOrEmpty()]
[ValidateScript( {
if (Test-Path $_) {
$True
}
else {
Throw "Cannot validate path $_"
}
})]
#Specify the top level path for the new project
[string]$Path = "C:\Scripts",
#names of commands you intend to start with. These will be exported by default.
[string[]]$CommandNames,
[Parameter(HelpMessage = "Specify the user or company for the MIT license")]
[ValidateNotNullOrEmpty()]
#Specify the user or company for the MIT license
[string]$License = "JDH Information Technology Solutions, Inc.",
[Parameter(Mandatory,HelpMessage ="Specify a project description" )]
[string]$Description,
[switch]$Passthru,
[parameter(HelpMessage = "Do not create a Github repository")]
[switch]$NoGitHub
)
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.MyCommand)"
Write-Verbose "[PROCESS] Creating project $name under $path"
$newPath = Join-Path -Path $Path -ChildPath $Name
If (-Not (Test-Path -Path $newPath) ) {
[void](New-Item -Path $path -Name $Name -ItemType Directory)
#create localized folder
Write-Verbose "[PROCESS] Creating folder $PSCulture under $path"
[void](New-Item -path $newPath -Name $PSCulture -ItemType Directory)
#create functions folder
Write-Verbose "[PROCESS] Creating folder functions under $path"
[void](New-Item -path $newPath -Name functions -ItemType Directory)
#create formats folder
Write-Verbose "[PROCESS] Creating folder formats under $path"
[void](New-Item -path $newPath -Name formats -ItemType Directory)
#create images folder
Write-Verbose "[PROCESS] Creating folder images under $path"
[void](New-Item -path $newPath -Name images -ItemType Directory)
#create docs folder
Write-Verbose "[PROCESS] Creating folder docs under $path"
$docs = New-Item -path $newPath -Name docs -ItemType Directory
#create .vscode folder
Write-Verbose "[PROCESS] Creating folder .vscode under $path"
$code = New-Item -path $newPath -Name .vscode -ItemType Directory
Copy-Item -path c:\scripts\tasks.json -Destination $code
#create module file
Write-Verbose "[PROCESS] Creating $name.psm1"
if ($CommandNames) {
$cmd = @()
foreach ($item in $CommandNames) {
Write-Verbose "[PROCESS] creating function scaffold for $item"
$f = @"
Function $item {
[cmdletbinding()]
Param(
)
Begin {
Write-Verbose "[BEGIN ] Starting: `$(`$MyInvocation.MyCommand)"
} #begin
Process {
Write-Verbose "[PROCESS] Processing"
} #process
End {
Write-Verbose "[END ] Ending: `$(`$MyInvocation.MyCommand)"
} #end
} #close $item
"@
#create the file
$file = Join-Path -path $newPath -ChildPath "functions\$item.ps1"
Set-Content -Value $f -Path $file
} #foreach item
} #if command names
$myScript = @"
#region Main
Get-ChildItem -path `$PSScriptRoot\functions\*.ps1 |
ForEach-Object { . `$_.FullName}
#endregion
"@
Set-Content -Value $myScript -Path "$newPath\$name.psm1"
#create manifest
Write-Verbose "[PROCESS] Creating $name.psd1"
$manHash = @{
Path = "$newPath\$name.psd1"
RootModule = "$name.psm1"
Description = $Description
CompanyName = $License
Copyright = "$((Get-Date).year) $Lic
ense"
}
$cmdString = $CommandNames -join ','
If ($CommandNames) {
$manHash.Add("FunctionsToExport", $cmdString)
}
New-ModuleManifest @manHash
Write-Verbose "[PROCESS] Creating About help topic outline"
if ($pscmdlet.ShouldProcess($docs.FullName, "New Markdown About Help")) {
New-MarkdownAboutHelp -OutputFolder $docs.FullName -AboutName $Name
}
#create Changelog.txt
Write-Verbose "[PROCESS] Creating Changelog.md"
Set-Content -Path $newPath\Changelog.md -Value "# Changelog for $Name"
#create README.md
Write-Verbose "[PROCESS] Creating README.md"
$readme = @"
# $Name
{INSERT CONTENT}
Last Updated _$(Get-Date -format u)_
"@
Set-Content -Value $readme -Path "$newPath\README.md"
#create License file
$lic = @"
Copyright (c) $((Get-Date).Year) $License
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"@
Write-Verbose "[PROCESS] Creating LICENSE.txt"
Set-Content -Value $lic -Path "$newPath\LICENSE.txt"
#create scratch files which my git config is set to ignore
Write-Verbose "[PROCESS] Creating scratch.ps1"
Set-Content -Value "#scratch file for $Name" -Path "$newPath\scratch.ps1"
Write-Verbose "[PROCESS] Creating scratch-change.md"
Set-Content -Value "# $Name" -Path "$newPath\scratch-change.md"
#create Test Folder
Write-Verbose "[PROCESS] Creating Tests folder"
$tests = New-Item -Path $newPath -Name tests -ItemType Directory
#Pester Init
Write-Verbose "[PROCESS] Running Pester setup"
#body of Test script
$TestInit = @"
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
}
`$here = Split-Path -Path `$MyInvocation.MyCommand.Path
`$parent = Split-Path -Path `$here
`$module = Split-Path -Path `$parent -Leaf
Write-Host "Importing module `$module from `$parent" -ForegroundColor Cyan
Import-Module `$parent -force
InModuleScope `$Module {
Describe $Name {
It "does something useful" {
`$true | Should Be `$true
}
}
}
"@
if ($PSCmdlet.ShouldProcess("newPath", "Pester setup")) {
$PesterTest = Join-Path -Path $Tests.FullName -ChildPath "$name.tests.ps1"
Write-Verbose "[PROCESS] Creating $pestertest"
Set-Content -Value $TestInit -Path $PesterTest
Write-Verbose "[PROCESS] Git commit $pestertest"
}
#Git Init
Write-Verbose "[PROCESS] Running Git setup"
if ($PSCmdlet.ShouldProcess($newPath, "Git Init")) {
Write-Verbose "[PROCESS] Git: Init"
git init -q $newpath
Set-Location $newPath
#create Main and add all files
Write-Verbose "[PROCESS] Git: Add base Files"
git add .
Write-Verbose "[PROCESS] Git: Initial Commit"
git commit -a -q -m "Initial commit"
# $r = New-GitHubRepository -Name $name -NoWiki -Private -Raw -Description $Description
#give repo a chance to finish setting up online
#Start-Sleep -Seconds 5
#Write-Verbose "[PROCESS] Git: git remote add origin $($r.clone_url)"
#git remote add origin "$($r.clone_url)"
#Start-Sleep -Seconds 5
#create the Github remote using the GitHub CLI
if (-Not $NoGitHub) {
Try {
[void](Get-Command gh.exe -ErrorAction stop)
Write-Verbose "[PROCESS] Creating a new private GitHub repository $Name"
gh repo create $name --private --disable-wiki --description $Description
Write-Verbose "[PROCESS] Git: git push --set-upstream origin main"
git push --set-upstream origin main
#create initial branch
Write-Verbose "[PROCESS] Git: Create 0.0.1 branch"
git branch -q 0.0.1
#checkout initial
Write-Verbose "[PROCESS] Git: Checkout 0.0.1 branch"
git checkout -q 0.0.1
}
Catch {
Write-Warning "Can't find gh.exe. You will need to manually finish setting up the Github repo."
}
} #setup Github repo
}
else {
#simulate WhatIf processing for Git commands
Write-Host "What if: performing the operation git init -q $newPath"
Write-Host "What if: performing the operation git commit -a -q -m 'Initial commit'"
if (-not $NoGitHub) {
Write-Host "What if: Creating a new private GitHub repository $Name"
Write-Host "What if: performing the operation gh repo create $name --private --enable-wiki=false --description $Description --confirm"
Write-Host "What if: performing the operation git push --set-upstream origin main"
Write-Host "What if: performing the operation git branch -q 0.0.1"
Write-Host "What if: performing the operation git checkout -q 0.0.1"
}
}
}
else {
Write-Warning "$NewPath appears to already exist."
}
if (Test-Path -path $newPath) {
Set-Location $newPath
}
if ($Passthru) {
Get-ChildItem -Recurse
}
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #close function
#This script requires the Github CLI client (gh.exe)
[cmdletbinding(SupportsShouldProcess)]
Param([switch]$Preview)
#make sure nothing is un-stage or committed
Write-Host "Processing project in $PWD" -ForegroundColor Green
$st = git status --porcelain
if ($st) {
Write-Warning "There are uncommitted or un-staged items"
Write-Host ($st | Out-String) -ForegroundColor Yellow
return
}
#get current branch
$br = git branch --show-current
Write-Host "On branch $br" -ForegroundColor Green
if ($br -match "^(\d+\.)+") {
#Strip off a leading v if included
if ($br -match "^v") {
$br = $br.Replace("v", "")
}
$tag = "v$br"
<#
Need to accommodate that some repos use main instead of master
#>
if ((git branch -l master) -AND $PSCmdlet.ShouldProcess("master", "Git checkout")) {
git checkout master
}
elseif ((git branch -l main) -AND $PSCmdlet.ShouldProcess("main", "Git checkout")) {
git checkout main
}
elseif (-Not ($WhatIfPreference)) {
Write-Warning "Can't determine master or main branch"
#bail out
Return
}
if ($PSCmdlet.ShouldProcess($br, "Git merge")) {
git merge $br
}
if ($PSCmdlet.ShouldProcess("master/main", "Git push")) {
git push
}
if ($PSCmdlet.ShouldProcess($tag, "Git tag")) {
git tag $tag
}
if ($PSCmdlet.ShouldProcess("--tag", "Git push")) {
git push --tag
$origin = (git remote get-url --push origin) -replace "\.git$", ""
#create release
#gh release create TAG-F D:\r.md -p -t TITLE
if (Test-Path "$pwd\scratch-change.md") {
$title = "{0}_{1}" -f (Split-Path $pwd -leaf),$tag
Write-Host "Creating release $title from $pwd\scratch-change.md" -fore yellow
if ($Preview) {
write-Host "PREVIEW" -ForegroundColor Cyan
gh release create $tag -F "$pwd\scratch-change.md" -p -t $title
}
else {
gh release create $tag -F "$pwd\scratch-change.md" -t $title
}
}
else {
#configure release online
Write-Host "Creating release online" -ForegroundColor green
Write-Host "Open and edit the release on GitHub" -ForegroundColor Green
Write-Host "$origin/releases/tag/$tag" -ForegroundColor green
Start-Process "$origin/releases/tag/$tag"
}
}
}
else {
Write-Warning "The branch $br does not appear to be a version number"
}
#requires -version 5.1
#requires -module Pester
#requires -module Platyps
<#
TODO:
split this up into different commands and create a module
validate pester test stub
Add parameters to specify PSEdition
#>
#use the gh.exe cli tool
# . C:\scripts\New-GitHubRepo.ps1
Function New-PSProject {
<# PSFunctionInfo
Version 2.0.0
Author Jeff Hicks
CompanyName JDH Information Technology Solutions, Inc.
Copyright 2021-2023
Description Create a PowerShell module project
Guid 8b92e54d-4e02-48a3-b811-62e27bda6d3a
Tags module
LastUpdate 7/14/2023 1:11 PM
Source C:\Scripts\New-Project.ps1
#>
<#
.SYNOPSIS
Create a PowerShell project folder.
.DESCRIPTION
Use this command to create a new PowerShell project folder. You specify a top level folder and the name of a project. The command will create a Tests sub-folder, a Pester script outline and set the project up in Git. The command will also create a Docs folder and a culture-specific folder for localized help.
The Git commands will initialize the folder, create a README.md file, a LICENSE.txt file, create a 0.0.1 branch and checkout that branch. The command assumes you have the Git command in your path.
.EXAMPLE
.NOTES
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
.LINK
git
.LINK
New-Item
.INPUTS
[string]
.OUTPUTS
None
#>
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(
Position = 0,
Mandatory,
HelpMessage = "The name of the project"
)]
[ValidateNotNullOrEmpty()]
#Specify the project name. This will become the directory name as well and the name of the GitHub repository.
[string]$Name,
[Parameter(HelpMessage = "The top level path for the new project")]
[ValidateNotNullOrEmpty()]
[ValidateScript( {
if (Test-Path $_) {
$True
}
else {
Throw "Cannot validate path $_"
}
})]
#Specify the top level path for the new project
[string]$Path = "C:\Scripts",
#names of commands you intend to start with. These will be exported by default.
[string[]]$CommandNames,
[Parameter(HelpMessage = "Specify the user or company for the MIT license")]
[ValidateNotNullOrEmpty()]
#Specify the user or company for the MIT license
[string]$License = "JDH Information Technology Solutions, Inc.",
[Parameter(Mandatory,HelpMessage ="Specify a project description" )]
[string]$Description,
[switch]$Passthru,
[parameter(HelpMessage = "Do not create a Github repository")]
[switch]$NoGitHub
)
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.MyCommand)"
Write-Verbose "[PROCESS] Creating project $name under $path"
$newPath = Join-Path -Path $Path -ChildPath $Name
If (-Not (Test-Path -Path $newPath) ) {
[void](New-Item -Path $path -Name $Name -ItemType Directory)
#create localized folder
Write-Verbose "[PROCESS] Creating folder $PSCulture under $path"
[void](New-Item -path $newPath -Name $PSCulture -ItemType Directory)
#create functions folder
Write-Verbose "[PROCESS] Creating folder functions under $path"
[void](New-Item -path $newPath -Name functions -ItemType Directory)
#create formats folder
Write-Verbose "[PROCESS] Creating folder formats under $path"
[void](New-Item -path $newPath -Name formats -ItemType Directory)
#create images folder
Write-Verbose "[PROCESS] Creating folder images under $path"
[void](New-Item -path $newPath -Name images -ItemType Directory)
#create docs folder
Write-Verbose "[PROCESS] Creating folder docs under $path"
$docs = New-Item -path $newPath -Name docs -ItemType Directory
#create .vscode folder
Write-Verbose "[PROCESS] Creating folder .vscode under $path"
$code = New-Item -path $newPath -Name .vscode -ItemType Directory
Copy-Item -path c:\scripts\tasks.json -Destination $code
#create module file
Write-Verbose "[PROCESS] Creating $name.psm1"
if ($CommandNames) {
$cmd = @()
foreach ($item in $CommandNames) {
Write-Verbose "[PROCESS] creating function scaffold for $item"
$f = @"
Function $item {
[cmdletbinding()]
Param(
)
Begin {
Write-Verbose "[BEGIN ] Starting: `$(`$MyInvocation.MyCommand)"
} #begin
Process {
Write-Verbose "[PROCESS] Processing"
} #process
End {
Write-Verbose "[END ] Ending: `$(`$MyInvocation.MyCommand)"
} #end
} #close $item
"@
#create the file
$file = Join-Path -path $newPath -ChildPath "functions\$item.ps1"
Set-Content -Value $f -Path $file
} #foreach item
} #if command names
$myScript = @"
#region Main
Get-ChildItem -path `$PSScriptRoot\functions\*.ps1 |
ForEach-Object { . `$_.FullName}
#endregion
"@
Set-Content -Value $myScript -Path "$newPath\$name.psm1"
#create manifest
Write-Verbose "[PROCESS] Creating $name.psd1"
$manHash = @{
Path = "$newPath\$name.psd1"
RootModule = "$name.psm1"
Description = $Description
CompanyName = $License
Copyright = "$((Get-Date).year) $Lic
ense"
}
$cmdString = $CommandNames -join ','
If ($CommandNames) {
$manHash.Add("FunctionsToExport", $cmdString)
}
New-ModuleManifest @manHash
Write-Verbose "[PROCESS] Creating About help topic outline"
if ($pscmdlet.ShouldProcess($docs.FullName, "New Markdown About Help")) {
New-MarkdownAboutHelp -OutputFolder $docs.FullName -AboutName $Name
}
#create Changelog.txt
Write-Verbose "[PROCESS] Creating Changelog.md"
Set-Content -Path $newPath\Changelog.md -Value "# Changelog for $Name"
#create README.md
Write-Verbose "[PROCESS] Creating README.md"
$readme = @"
# $Name
{INSERT CONTENT}
Last Updated _$(Get-Date -format u)_
"@
Set-Content -Value $readme -Path "$newPath\README.md"
#create License file
$lic = @"
Copyright (c) $((Get-Date).Year) $License
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"@
Write-Verbose "[PROCESS] Creating LICENSE.txt"
Set-Content -Value $lic -Path "$newPath\LICENSE.txt"
#create scratch files which my git config is set to ignore
Write-Verbose "[PROCESS] Creating scratch.ps1"
Set-Content -Value "#scratch file for $Name" -Path "$newPath\scratch.ps1"
Write-Verbose "[PROCESS] Creating scratch-change.md"
Set-Content -Value "# $Name" -Path "$newPath\scratch-change.md"
#create Test Folder
Write-Verbose "[PROCESS] Creating Tests folder"
$tests = New-Item -Path $newPath -Name tests -ItemType Directory
#Pester Init
Write-Verbose "[PROCESS] Running Pester setup"
#body of Test script
$TestInit = @"
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
}
`$here = Split-Path -Path `$MyInvocation.MyCommand.Path
`$parent = Split-Path -Path `$here
`$module = Split-Path -Path `$parent -Leaf
Write-Host "Importing module `$module from `$parent" -ForegroundColor Cyan
Import-Module `$parent -force
InModuleScope `$Module {
Describe $Name {
It "does something useful" {
`$true | Should Be `$true
}
}
}
"@
if ($PSCmdlet.ShouldProcess("newPath", "Pester setup")) {
$PesterTest = Join-Path -Path $Tests.FullName -ChildPath "$name.tests.ps1"
Write-Verbose "[PROCESS] Creating $pestertest"
Set-Content -Value $TestInit -Path $PesterTest
Write-Verbose "[PROCESS] Git commit $pestertest"
}
#Git Init
Write-Verbose "[PROCESS] Running Git setup"
if ($PSCmdlet.ShouldProcess($newPath, "Git Init")) {
Write-Verbose "[PROCESS] Git: Init"
git init -q $newpath
Set-Location $newPath
#create Main and add all files
Write-Verbose "[PROCESS] Git: Add base Files"
git add .
Write-Verbose "[PROCESS] Git: Initial Commit"
git commit -a -q -m "Initial commit"
# $r = New-GitHubRepository -Name $name -NoWiki -Private -Raw -Description $Description
#give repo a chance to finish setting up online
#Start-Sleep -Seconds 5
#Write-Verbose "[PROCESS] Git: git remote add origin $($r.clone_url)"
#git remote add origin "$($r.clone_url)"
#Start-Sleep -Seconds 5
#create the Github remote using the GitHub CLI
if (-Not $NoGitHub) {
Try {
[void](Get-Command gh.exe -ErrorAction stop)
Write-Verbose "[PROCESS] Creating a new private GitHub repository $Name"
gh repo create $name --private --disable-wiki --description $Description
Write-Verbose "[PROCESS] Git: git push --set-upstream origin main"
git push --set-upstream origin main
#create initial branch
Write-Verbose "[PROCESS] Git: Create 0.0.1 branch"
git branch -q 0.0.1
#checkout initial
Write-Verbose "[PROCESS] Git: Checkout 0.0.1 branch"
git checkout -q 0.0.1
}
Catch {
Write-Warning "Can't find gh.exe. You will need to manually finish setting up the Github repo."
}
} #setup Github repo
}
else {
#simulate WhatIf processing for Git commands
Write-Host "What if: performing the operation git init -q $newPath"
Write-Host "What if: performing the operation git commit -a -q -m 'Initial commit'"
if (-not $NoGitHub) {
Write-Host "What if: Creating a new private GitHub repository $Name"
Write-Host "What if: performing the operation gh repo create $name --private --enable-wiki=false --description $Description --confirm"
Write-Host "What if: performing the operation git push --set-upstream origin main"
Write-Host "What if: performing the operation git branch -q 0.0.1"
Write-Host "What if: performing the operation git checkout -q 0.0.1"
}
}
}
else {
Write-Warning "$NewPath appears to already exist."
}
if (Test-Path -path $newPath) {
Set-Location $newPath
}
if ($Passthru) {
Get-ChildItem -Recurse
}
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #close function
@jdhitsolutions
Copy link
Author

Everything in this gist is offered as-is for educational purposes only. The script files are written around my personal routines. You are welcome to use them as models for your own work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment