Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created February 10, 2022 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinJump/173af5500103db85fffa712479066606 to your computer and use it in GitHub Desktop.
Save KevinJump/173af5500103db85fffa712479066606 to your computer and use it in GitHub Desktop.
Umbraco setup and tear down powershell module.
function New-Umbraco {
# statup umbraco, and add some packages if you want
#
# e.g numbraco MyTestSite uSync Jumoo.TranslationManager vendr -NoStarter
#
# extras!!!
#
# open vscode in the folder as part of the build
# numbraco MyTestSite -code
#
# don't run the site
# numbraco MyTestSite -doNotRun
#
# Required dbatools powershell scripts and local SQLExpress install.
param(
[Parameter(Mandatory)]
[Alias("n")]
[string]
$Name,
[string[]]
[Alias("p")]
$Packages,
[string]
[Alias("f")]
$framework = "net5.0",
# dont install the starter kit.
[switch]
$NoStarter,
# open vscode in the folder
[switch]
$Code,
# don't run the site at the end.
[switch]
$DoNotRun,
# add any packages with the --prerelease flag.
[switch]
$PreRelease
)
# check if you don't have DBATools it will still work
Function Test-CommandExists {
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = ‘stop’
try { if (Get-Command $command) { RETURN $true } }
Catch { Write-Host “$command does not exist”; RETURN $false }
Finally { $ErrorActionPreference = $oldPreference }
} #end function test-CommandExists
$dbname = 'umb-' + $name
$connectionString = "Server=$env:COMPUTERNAME\SQLExpress;Database=$dbname;Integrated Security=true"
# if you don't have dbatools, remove this, Umbraco will create the DB for you (will take 10-15 seconds longer)
if (Test-CommandExists new-dbaDatabase) {
Write-Host "Creating Database" -ForegroundColor Blue
new-dbaDatabase -SqlInstance $env:COMPUTERNAME\sqlExpress -name $dbname
}
else {
Write-Host "DBA Tools not installed, going for auto creation" -ForegroundColor Yellow
}
dotnet new umbraco -n $Name --connection-string $connectionString -F $framework
Set-Location $Name
# you can put username and password here, but if you set UMB_USER, UMB_EMAIL and UMB_PASSWORD on your local pc, then you don't have to.
Set-Item Env:\UMBRACO__CMS__GLOBAL__INSTALLMISSINGDATABASE true
Set-Item Env:\UMBRACO__CMS__UNATTENDED__INSTALLUNATTENDED true
Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSERNAME $env:UMB_USER
Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSEREMAIL $env:UMB_EMAIL
Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSERPASSWORD $env:UMB_PASSWORD
if (!$NoStarter) {
Write-Host "Adding the starter kit" -ForegroundColor Blue
dotnet add package Umbraco.TheStarterKit -s https://api.nuget.org/v3/index.json
}
foreach ($package in $Packages) {
Write-Host "Adding $package" -ForegroundColor Blue
if ($preRelease) {
dotnet add package $package --prerelease
}
else {
dotnet add package $package
}
}
if ($Code) {
code .
}
$project = ".\$Name\$Name.csproj";
if ($DoNotRun) {
dotnet build
Set-Location ..
}
else {
Write-Host "Running Site" -ForegroundColor Blue
Set-Location ..
dotnet run --project $project
}
if (Test-Path -Path *.sln) {
dotnet sln add $project
}
}
function Remove-Umbraco {
param(
[Parameter(Mandatory)]
[Alias("n")]
[string]
$Name,
[switch]
$RemoveFolder
)
$dbname = 'umb-' + $name
Write-Host ">> Removing database $dbname"
remove-dbaDatabase -SqlInstance $env:COMPUTERNAME\sqlExpress -database $dbname -Confirm:$false
if ($RemoveFolder) {
# check to see if we are in the site folder,
$currentFolderName = Split-PAth -path (Get-Location) -leaf
if ($currentFolderName -eq $Name) {
# if we are go up a folder.
Write-Host ">> Moving to parent folder of $Name"
Set-Location ..
}
Write-Host ">> Removing folder $Name"
if (Test-Path $Name) {
Remove-Item -Path $Name -Recurse
}
}
}
function Clear-UmbracoDbs {
Get-DbaDatabase -SqlInstance $env:COMPUTERNAME\SqlExpress | Where-Object {$_.Name -like 'umb-*'} | Remove-DbaDatabase -Confirm:$false
}
Export-ModuleMember New-Umbraco
Export-ModuleMember Remove-Umbraco
Export-ModuleMember Clear-UmbracoDbs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment