Files
Report-showcase/.template/development/tools/use-starter.ps1
T
2026-10-09 01:45:17 +00:00

64 lines
2.8 KiB
PowerShell

param(
[Parameter(Mandatory = $true)]
[ValidateSet('report', 'letter', 'commercial-offer', 'contract')]
[string]$Name
)
$ErrorActionPreference = 'Stop'
$workspaceRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..\..\..'))
$starterRoot = [System.IO.Path]::GetFullPath(
(Join-Path $workspaceRoot "docs\examples\documents\$Name")
)
$mainTarget = [System.IO.Path]::GetFullPath((Join-Path $workspaceRoot 'main.typ'))
$chaptersTarget = [System.IO.Path]::GetFullPath((Join-Path $workspaceRoot 'chapters'))
$assetsTarget = [System.IO.Path]::GetFullPath((Join-Path $workspaceRoot 'assets'))
foreach ($path in @($starterRoot, $mainTarget, $chaptersTarget, $assetsTarget)) {
if (-not $path.StartsWith(
$workspaceRoot + [System.IO.Path]::DirectorySeparatorChar,
[System.StringComparison]::OrdinalIgnoreCase
)) {
throw "Небезопасный путь за пределами проекта: $path"
}
}
if (-not (Test-Path -LiteralPath (Join-Path $starterRoot 'main.typ'))) {
throw "Не найден пример документа: $Name"
}
# Выбор типа разрешён даже при незакоммиченных изменениях: перед заменой
# создаётся локальная резервная копия, которая не попадает в Git.
$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$backupRoot = [System.IO.Path]::GetFullPath(
(Join-Path $workspaceRoot ".private\starter-backups\$timestamp")
)
if (-not $backupRoot.StartsWith(
$workspaceRoot + [System.IO.Path]::DirectorySeparatorChar,
[System.StringComparison]::OrdinalIgnoreCase
)) {
throw "Небезопасный путь резервной копии: $backupRoot"
}
New-Item -ItemType Directory -Path $backupRoot -Force | Out-Null
if (Test-Path -LiteralPath $mainTarget) {
Copy-Item -LiteralPath $mainTarget -Destination (Join-Path $backupRoot 'main.typ')
}
if (Test-Path -LiteralPath $chaptersTarget) {
Copy-Item -LiteralPath $chaptersTarget -Destination (Join-Path $backupRoot 'chapters') -Recurse
Remove-Item -LiteralPath $chaptersTarget -Recurse -Force
}
Copy-Item -LiteralPath (Join-Path $starterRoot 'main.typ') -Destination $mainTarget -Force
Copy-Item -LiteralPath (Join-Path $starterRoot 'chapters') -Destination $chaptersTarget -Recurse
$starterAssets = Join-Path $starterRoot 'assets'
if (Test-Path -LiteralPath $starterAssets) {
New-Item -ItemType Directory -Path $assetsTarget -Force | Out-Null
Copy-Item -Path (Join-Path $starterAssets '*') -Destination $assetsTarget -Recurse -Force
}
Write-Host "Установлен тип документа '$Name'."
Write-Host "Предыдущие main.typ и chapters/ сохранены в $backupRoot"
Write-Host 'Откройте main.typ и последовательно проверьте параметры.'