openthebox/init.ps1

120 lines
5.3 KiB
PowerShell
Raw Permalink Normal View History

# Open The Box - Initialization Script
# Run this after cloning to download dependencies and verify the environment.
#
# Usage: .\init.ps1
$ErrorActionPreference = "Stop"
$LorelineVersion = "0.7.1"
$LorelineZipUrl = "https://github.com/jeremyfa/loreline/releases/download/v$LorelineVersion/loreline-csharp.zip"
$LibDir = Join-Path $PSScriptRoot "lib"
$LorelineDll = Join-Path $LibDir "Loreline.dll"
$TempZip = Join-Path $LibDir "loreline-csharp.zip"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Open The Box - Project Setup" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# ── Step 1: Check .NET SDK ────────────────────────────────────────────
Write-Host "[1/3] Checking .NET SDK..." -ForegroundColor Yellow
$sdks = & dotnet --list-sdks 2>$null
$hasNet10 = $sdks | Where-Object { $_ -match "^10\." }
if (-not $hasNet10) {
Write-Host " .NET 10 SDK not found." -ForegroundColor Red
Write-Host " Install it with: winget install Microsoft.DotNet.SDK.10" -ForegroundColor Red
Write-Host " Or download from: https://dotnet.microsoft.com/download/dotnet/10.0" -ForegroundColor Red
Write-Host ""
$response = Read-Host " Attempt to install via winget? (y/N)"
if ($response -eq "y" -or $response -eq "Y") {
Write-Host " Installing .NET 10 SDK..." -ForegroundColor Yellow
winget install Microsoft.DotNet.SDK.10 --accept-source-agreements --accept-package-agreements
Write-Host " Installed. You may need to restart your terminal for PATH changes." -ForegroundColor Green
} else {
Write-Host " Skipping .NET 10 SDK install. Build may fail without it." -ForegroundColor Red
}
} else {
$version = ($hasNet10 | Select-Object -First 1) -replace '\s+\[.*$', ''
Write-Host " .NET SDK $version found." -ForegroundColor Green
}
Write-Host ""
# ── Step 2: Download Loreline ─────────────────────────────────────────
Write-Host "[2/3] Checking Loreline dependency..." -ForegroundColor Yellow
if (-not (Test-Path $LibDir)) {
New-Item -ItemType Directory -Path $LibDir -Force | Out-Null
}
if (Test-Path $LorelineDll) {
Write-Host " Loreline.dll already present." -ForegroundColor Green
} else {
Write-Host " Downloading Loreline v$LorelineVersion from GitHub..." -ForegroundColor Yellow
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $LorelineZipUrl -OutFile $TempZip -UseBasicParsing
Write-Host " Extracting..." -ForegroundColor Yellow
$extractDir = Join-Path $LibDir "_loreline_extract"
Expand-Archive -Path $TempZip -DestinationPath $extractDir -Force
# Find and copy the DLL, XML, and deps.json
$dllSource = Get-ChildItem -Path $extractDir -Recurse -Filter "Loreline.dll" | Select-Object -First 1
if ($dllSource) {
$sourceDir = $dllSource.DirectoryName
Copy-Item (Join-Path $sourceDir "Loreline.dll") $LibDir -Force
$xmlFile = Join-Path $sourceDir "Loreline.xml"
if (Test-Path $xmlFile) { Copy-Item $xmlFile $LibDir -Force }
$depsFile = Join-Path $sourceDir "Loreline.deps.json"
if (Test-Path $depsFile) { Copy-Item $depsFile $LibDir -Force }
Write-Host " Loreline v$LorelineVersion installed." -ForegroundColor Green
} else {
Write-Host " ERROR: Could not find Loreline.dll in downloaded archive." -ForegroundColor Red
exit 1
}
# Cleanup
Remove-Item $extractDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $TempZip -Force -ErrorAction SilentlyContinue
}
catch {
Write-Host " ERROR: Failed to download Loreline." -ForegroundColor Red
Write-Host " $($_.Exception.Message)" -ForegroundColor Red
Write-Host ""
Write-Host " Manual install:" -ForegroundColor Yellow
Write-Host " 1. Download from: $LorelineZipUrl" -ForegroundColor Yellow
Write-Host " 2. Extract Loreline.dll to: $LibDir" -ForegroundColor Yellow
exit 1
}
}
Write-Host ""
# ── Step 3: Restore NuGet packages ───────────────────────────────────
Write-Host "[3/3] Restoring NuGet packages..." -ForegroundColor Yellow
dotnet restore
if ($LASTEXITCODE -eq 0) {
Write-Host " Packages restored." -ForegroundColor Green
} else {
Write-Host " WARNING: Package restore failed. Try running 'dotnet restore' manually." -ForegroundColor Red
}
Write-Host ""
# ── Done ──────────────────────────────────────────────────────────────
Write-Host "========================================" -ForegroundColor Green
Write-Host " Setup complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host " To build: dotnet build" -ForegroundColor Cyan
Write-Host " To run: dotnet run --project src/OpenTheBox" -ForegroundColor Cyan
Write-Host ""