Public/New-DevTree.ps1
|
function New-DevTree { <# .SYNOPSIS Creates several nested directories at once under a common root. .DESCRIPTION Creates -Root if missing, then creates each entry in -Directories relative to it. Each directory path is rejected if it is rooted or escapes -Root via a '..' segment. Directories that already exist are left untouched. .PARAMETER Directories Relative directory paths to create under -Root, e.g. 'src/core', 'tests/unit'. .PARAMETER Root Root directory the paths are created under. Defaults to the current location. .EXAMPLE New-DevTree -Root MyProject -Directories src/core, src/adapters, tests/unit, docs #> [CmdletBinding(SupportsShouldProcess = $true)] [OutputType([System.IO.DirectoryInfo[]])] param( [Parameter(Mandatory = $true, Position = 0)] [string[]]$Directories, [Parameter()] [string]$Root = (Get-Location).Path ) $resolvedRoot = Resolve-DtFullPath -Path $Root $resolvedPaths = foreach ($directory in $Directories) { Resolve-DtContainedPath -BasePath $resolvedRoot -ChildName $directory } if (-not $PSCmdlet.ShouldProcess($resolvedRoot, "Create tree: $($Directories -join ', ')")) { return } if (-not (Test-Path -LiteralPath $resolvedRoot)) { New-DtDirectory -Path $resolvedRoot -Force -Confirm:$false | Out-Null } foreach ($resolvedPath in $resolvedPaths) { New-DtDirectory -Path $resolvedPath -Force -Confirm:$false | Out-Null Get-Item -LiteralPath $resolvedPath } } |