Skip to content

Instantly share code, notes, and snippets.

@ronaldbarendse
Created November 1, 2021 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronaldbarendse/5f8ee683196d753eeade4738376fc34f to your computer and use it in GitHub Desktop.
Save ronaldbarendse/5f8ee683196d753eeade4738376fc34f to your computer and use it in GitHub Desktop.
Umbraco 9 install script
# Optional cleanup for repeated tests
Remove-Item "Umbraco9" -Recurse -ErrorAction SilentlyContinue
Remove-Item "Umbraco9.sln" -ErrorAction SilentlyContinue
# Configure these values to automatically create a back-office user
#Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSERNAME "Administrator"
#Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSEREMAIL "umbraco@example.com"
#Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSERPASSWORD "1234567890"
# Ensure the latest Umbraco templates are installed
dotnet new -i Umbraco.Templates
# Measure duration of the Umbraco installation
$duration = Measure-Command { &{
# Create solution/project
dotnet new sln -n Umbraco9
dotnet new umbraco -n Umbraco9 --connection-string "Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Umbraco.mdf;Integrated Security=True"
dotnet sln add Umbraco9
# Add NuGet packages
dotnet add Umbraco9 package Umbraco.TheStarterKit
dotnet add Umbraco9 package Umbraco.Forms
# Create database file (saves 5 connect attempts with 1 second delays)
$DataDirectory = Join-Path (Resolve-Path .) "Umbraco9\umbraco\Data"
$TempDatabaseName = "Umbraco-$(New-Guid)"
New-Item $DataDirectory -ItemType Directory | Out-Null
&sqlcmd -S "(localdb)\MSSQLLocalDB" -Q "CREATE DATABASE [$TempDatabaseName] ON (NAME=N'$TempDatabaseName', FILENAME='$(Join-Path $DataDirectory "Umbraco.mdf")')"
&sqlcmd -S "(localdb)\MSSQLLocalDB" -Q "ALTER DATABASE [$TempDatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; EXEC sp_detach_db @dbname='$TempDatabaseName'"
# Change logging level
Set-Item Env:\SERILOG__MINIMUMLEVEL__DEFAULT "Warning"
# Configure unattended install
Set-Item Env:\UMBRACO__CMS__UNATTENDED__INSTALLUNATTENDED true
# Ensure the application is stopped after the first run/install
@"
using Microsoft.Extensions.Hosting;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
namespace Umbraco9
{
public class StopApplicationComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.AddNotificationHandler<UmbracoApplicationStartingNotification, StopApplicationHandler>();
}
}
public class StopApplicationHandler : INotificationHandler<UmbracoApplicationStartingNotification>
{
private readonly IHostApplicationLifetime hostApplicationLifetime;
public StopApplicationHandler(IHostApplicationLifetime hostApplicationLifetime)
{
this.hostApplicationLifetime = hostApplicationLifetime;
}
public void Handle(UmbracoApplicationStartingNotification notification)
{
this.hostApplicationLifetime.ApplicationStarted.Register(() =>
{
this.hostApplicationLifetime.StopApplication();
});
}
}
}
"@ | Out-File Umbraco9\StopApplicationHandler.cs
# Run and install
dotnet run --project Umbraco9
# Do not stop application on the next runs
Remove-Item Umbraco9\StopApplicationHandler.cs
} | Out-Default }
Write-Host @"
██ ██ ███ ███ ██████ ██████ █████ ██████ ██████ █████
██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ██████ ██████ ███████ ██ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ██████ ██ ██ ██ ██ ██████ ██████ █████
Installed in $($duration.TotalSeconds) seconds!
Start the application by running: dotnet run --project Umbraco9
"@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment