Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active April 3, 2022 13:10
Show Gist options
  • Save jdhitsolutions/56ad9efa54d37c720c911a35819fc56e to your computer and use it in GitHub Desktop.
Save jdhitsolutions/56ad9efa54d37c720c911a35819fc56e to your computer and use it in GitHub Desktop.
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.
#requires -version 4.0
#requires -module Pester
Function New-Project {
<#
.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 Dev branch and checkout that branch. The command assumes you have the Git command in your path.
.PARAMETER Name
The name of the project.
.PARAMETER Path
The top level path for the new project.
.PARAMETER Passthru
Display the new files and directories.
.EXAMPLE
PS C:\> New-Project -name PiedPiperBox -path C:\Scripts
PS C:\scripts\PiedPiperBox> dir -Recurse
Directory: C:\scripts\PiedPiperBox
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 7/15/2016 5:33 PM Docs
d----- 7/15/2016 5:33 PM en-US
d----- 7/15/2016 5:33 PM Tests
-a---- 7/15/2016 5:33 PM 29 Changelog.txt
-a---- 7/15/2016 5:33 PM 1154 LICENSE.txt
-a---- 7/15/2016 5:33 PM 103 PiedPiperBox.psm1
-a---- 7/15/2016 5:33 PM 392 README.md
-a---- 7/15/2016 5:33 PM 32 scratch.ps1
Directory: C:\scripts\PiedPiperBox\en-US
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 7/15/2016 5:33 PM 132 About_PiedPiperBox.help.txt
Directory: C:\scripts\PiedPiperBox\Tests
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 7/15/2016 5:33 PM 260 PiedPiperBox.tests.ps1
PS C:\Scripts\PiedPiperBox> git status
On branch dev
nothing to commit, working directory clean
The first command will create a project folder C:\Scripts\PiedPiperBox and initialize as a Git repository.
The remaining commands show what was added.
.NOTES
NAME : New-Project
VERSION : 2.1
LAST UPDATED: July 15, 2016
AUTHOR : Jeff Hicks (@jeffHicks)
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.LINK
git
.LINK
New-Item
.INPUTS
[string]
.OUTPUTS
None
#>
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(
Position=0,
Mandatory,
HelpMessage="The name of the project")]
[ValidateNotNullorEmpty()]
[string]$Name,
[Parameter(HelpMessage="The top level path for the new project")]
[ValidateNotNullorEmpty()]
[ValidateScript({
if (Test-Path $_) {
$True
}
else {
Throw "Cannot validate path $_"
}
})]
[string]$Path = "C:\Scripts",
[switch]$Passthru
)
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) ) {
$parent = New-Item -Path $path -Name $Name -ItemType Directory
#create Test Folder
$tests = New-Item -Path $newPath -Name Tests -ItemType Directory
#create localized folder
Write-Verbose "[PROCESS] Creating folder $psculture under $path"
New-Item -path $newPath -Name $PSCulture -ItemType Directory | Out-Null
#create About help topic
$about = @"
TOPIC
About_$Name
SHORT DESCRIPTION
under development
LONG DESCRIPTION
under development
SEE ALSO
"@
Write-Verbose "[PROCESS] Creating About help topic outline"
Set-Content -Value $about -path "$newPath\$PSCulture\About_$Name.help.txt"
#create docs folder
Write-Verbose "[PROCESS] Creating folder Docs under $path"
New-Item -path $newPath -Name Docs -ItemType Directory | Out-Null
#create Changelog.txt
Write-Verbose "[PROCESS] Creating Changelog.txt"
Set-Content -Path $newPath\Changelog.txt -Value "#Changelog for $Name"
#create README.md
Write-Verbose "[PROCESS] Creating README.md"
$readme = @"
# $Name #
****************************************************************
DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED
THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF YOU DO
NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, DO NOT USE
OUTSIDE OF A SECURE, TEST SETTING.
****************************************************************
"@
Set-Content -Value $readme -Path "$newPath\README.md"
#create script file
Write-Verbose "[PROCESS] Creating $name.psm1"
$myScript = @"
#requires -version $($PSVersionTable.psversion.Major).$($PSVersionTable.psversion.Minor)
#region Main
#endregion
#define aliases
#export module members
"@
Set-Content -Value $myScript -Path "$newPath\$name.psm1"
#create License file
$lic = @"
Copyright (c) 2016 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.
"@
Write-Verbose "[PROCESS] Creating LICENSE.txt"
Set-Content -Value $lic -Path "$newPath\LICENSE.txt"
#create a scratch file 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"
#Pester Init
Write-Verbose "[PROCESS] Running Pester setup"
#body of Test script
$TestInit = @"
`$here = Split-Path -Parent `$MyInvocation.MyCommand.Path
`$sut = (Split-Path -Leaf `$MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. `$here\..\`$sut
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
cd $newPath
#create Master 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"
#create Dev branch
Write-Verbose "[PROCESS] Git: Create Dev branch"
git branch -q dev
#checkout dev
Write-Verbose "[PROCESS] Git: Checkout Dev branch"
git checkout -q dev
}
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'"
Write-Host "What if: performing the operation git branch -q dev"
Write-Host "What if: performing the operation git checkout -q dev"
}
}
else {
Write-Warning "$NewPath appears to already exist."
}
if (Test-Path -path $newPath) {
cd $newPath
}
if ($Passthru) {
Get-ChildItem -Recurse
}
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #close function
@jdhitsolutions
Copy link
Author

Fixed a bug in the here string setting up the Pester test file.

@jdhitsolutions
Copy link
Author

v2.1
Now adds an MIT license file.
Added a -passthru parameter
Added an outline for an about_project help file
Added a scratch.ps1 file.

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