Public/New-UDScaffold.ps1
<#
.SYNOPSIS Create a scaffold directory structure for use with UniversalDashboard .DESCRIPTION Generates the files and folders required for a working dashboard. Inspired by (and built upon) UDTemplate. https://www.powershellgallery.com/packages/UDTemplate .EXAMPLE PS C:\> New-UDScaffold -ProjectName UDName -Destination .\Dashboards #> function New-UDScaffold { [CmdletBinding(SupportsShouldProcess)] Param ( [Parameter(Mandatory)] [string]$ProjectName, [Parameter(Mandatory)] [ValidateScript( { If (-not(Test-Path $_ -PathType Container)) { Throw "Destination must be a directory" } return $true } )] [System.IO.FileInfo]$Destination ) Begin { # Generate the required variables. $TemplateRoot = (Split-Path $PSScriptRoot -Parent) $ProjectRoot = (Join-Path $Destination $ProjectName) } Process { # Make sure that the dashboard isn't already created. If (Test-Path $ProjectRoot) { Write-Error "Project exists at '$ProjectRoot'. Please choose a different path." break; } # Copy the scaffold directory into the new project path. Copy-Item -Path (Join-Path -Path $TemplateRoot -ChildPath "\Scaffold\") -Destination $ProjectRoot -Recurse -Force # Rename the module file as appropriate. Move-Item -Path (Join-Path -Path $ProjectRoot -ChildPath "_MODULE_.psm1") -Destination (Join-Path -Path $ProjectRoot -ChildPath "$ProjectName.psm1" ) # Build the config file $Config = @{ 'Title' = $ProjectName 'RootModule' = "{0}.psm1" -f $ProjectName } $Config | ConvertTo-Json | Set-Content -Path (Join-Path -Path $ProjectRoot -ChildPath config.json) # Generate the module manifest. $ModuleManifestSplat = @{ 'Path' = "{0}.psd1" -f (Join-Path -Path $ProjectRoot -ChildPath $ProjectName) 'RootModule' = "{0}.psm1" -f $ProjectName } New-ModuleManifest @ModuleManifestSplat Write-Verbose ([PSCustomObject]@{ 'Name' = $ProjectName 'Root Module' = $Configuration.rootmodule 'Configuration File' = (Join-Path $ProjectRoot config.json) }) } } |