121 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			PowerShell
		
	
	
	
	
	
			
		
		
	
	
			121 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			PowerShell
		
	
	
	
	
	
<#
 | 
						|
.SYNOPSIS
 | 
						|
    Construit l'image Docker avec toutes les dépendances requises via PowerShell et WSL (Debian).
 | 
						|
.DESCRIPTION
 | 
						|
    Ce script prépare les dépendances et construit l'image Docker sous WSL (Debian) avec BuildKit activé pour des builds plus rapides.
 | 
						|
.PARAMETRES
 | 
						|
    -full : Si présent, effectue une construction complète de l'image Docker (équivalent à --no-cache).
 | 
						|
.EXEMPLE
 | 
						|
    .\build-img.ps1        # Construction rapide (avec cache)
 | 
						|
    .\build-img.ps1 -full  # Construction complète sans cache
 | 
						|
.RETOURNE
 | 
						|
    Affiche le succès ou les erreurs pendant la construction de l'image Docker.
 | 
						|
#>
 | 
						|
 | 
						|
param(
 | 
						|
    [switch]$full
 | 
						|
)
 | 
						|
 | 
						|
$ErrorActionPreference = 'Stop'
 | 
						|
 | 
						|
try {
 | 
						|
    # Determine paths
 | 
						|
    $scriptDir = $PSScriptRoot
 | 
						|
    $projectRoot = (Get-Item (Join-Path $scriptDir '..')).FullName
 | 
						|
 | 
						|
    # Convert Windows path to WSL path (manual, no wslpath)
 | 
						|
    $wslProjectRoot = $projectRoot -replace '^([A-Za-z]):\\','/mnt/$1/' -replace '\\','/'
 | 
						|
    $wslProjectRoot = $wslProjectRoot.ToLower()
 | 
						|
 | 
						|
    # Check if Docker is available in WSL Debian
 | 
						|
    Write-Host "Vérification de Docker dans WSL Debian..." -ForegroundColor Cyan
 | 
						|
    $dockerCheck = wsl -d Debian bash -c "docker --version" 2>&1
 | 
						|
    if ($LASTEXITCODE -ne 0) {
 | 
						|
        Write-Error "Docker n'est pas disponible dans WSL Debian : $dockerCheck"
 | 
						|
        exit 1
 | 
						|
    }
 | 
						|
    Write-Host "Docker détecté : $dockerCheck" -ForegroundColor Green
 | 
						|
 | 
						|
    # Check if the Dockerfile exists
 | 
						|
    $dockerfilePath = Join-Path $scriptDir "Dockerfile"
 | 
						|
    if (-not (Test-Path $dockerfilePath)) {
 | 
						|
        Write-Error "Le fichier Dockerfile n'existe pas dans $scriptDir"
 | 
						|
        exit 1
 | 
						|
    }
 | 
						|
 | 
						|
    # Check if .dockerignore exists, warn if not
 | 
						|
    $dockerignorePath = Join-Path $projectRoot ".dockerignore"
 | 
						|
    if (-not (Test-Path $dockerignorePath)) {
 | 
						|
        Write-Warning "Aucun fichier .dockerignore trouvé. Créez-en un pour optimiser le build."
 | 
						|
        Write-Host "Exemple de contenu :" -ForegroundColor Yellow
 | 
						|
        Write-Host @"
 | 
						|
node_modules
 | 
						|
dist
 | 
						|
.git
 | 
						|
.angular
 | 
						|
.vscode
 | 
						|
*.log
 | 
						|
tmp
 | 
						|
db/*.db
 | 
						|
.env
 | 
						|
"@ -ForegroundColor DarkGray
 | 
						|
    }
 | 
						|
 | 
						|
    # Compose build command with BuildKit enabled
 | 
						|
    $noCache = if ($full) { '--no-cache' } else { '' }
 | 
						|
    
 | 
						|
    # Escape spaces in path for bash
 | 
						|
    $escapedPath = $wslProjectRoot -replace ' ','\\ '
 | 
						|
    
 | 
						|
    # Build command parts
 | 
						|
    $cdCmd = "cd `"$wslProjectRoot`""
 | 
						|
    $dockerCmd = "docker build $noCache --progress=plain -t obsiviewer-angular:latest -f docker/Dockerfile ."
 | 
						|
    
 | 
						|
    # Complete command with BuildKit
 | 
						|
    $innerCmd = "export DOCKER_BUILDKIT=1 && $cdCmd && $dockerCmd"
 | 
						|
 | 
						|
    # Run build inside WSL Debian to use Linux Docker daemon
 | 
						|
    Write-Host "Construction de l'image Docker obsiviewer-angular:latest..." -ForegroundColor Cyan
 | 
						|
    Write-Host "BuildKit activé pour un build optimisé avec cache" -ForegroundColor Cyan
 | 
						|
    
 | 
						|
    $buildResult = wsl -d Debian bash -lc $innerCmd 2>&1
 | 
						|
    
 | 
						|
    # Display build output
 | 
						|
    $buildResult | ForEach-Object { Write-Host $_ }
 | 
						|
    
 | 
						|
    if ($LASTEXITCODE -ne 0) {
 | 
						|
        Write-Error "Erreur lors de la construction de l'image Docker (code: $LASTEXITCODE)"
 | 
						|
        exit 1
 | 
						|
    }
 | 
						|
 | 
						|
    # Verify the image was built successfully
 | 
						|
    $verifyCmd = "docker image inspect obsiviewer-angular:latest --format='{{.Size}}'"
 | 
						|
    Write-Host "`nVérification de l'image construite..." -ForegroundColor Cyan
 | 
						|
    $imageSize = wsl -d Debian bash -lc $verifyCmd 2>&1
 | 
						|
    
 | 
						|
    if ($LASTEXITCODE -ne 0) {
 | 
						|
        Write-Error "L'image Docker n'a pas été construite correctement : $imageSize"
 | 
						|
        exit 1
 | 
						|
    }
 | 
						|
 | 
						|
    # Convert size to MB
 | 
						|
    $sizeMB = [math]::Round([long]$imageSize / 1MB, 2)
 | 
						|
    
 | 
						|
    Write-Host "`n================================================" -ForegroundColor Green
 | 
						|
    Write-Host "✓ Image Docker construite avec succès" -ForegroundColor Green
 | 
						|
    Write-Host "  Nom: obsiviewer-angular:latest" -ForegroundColor Green
 | 
						|
    Write-Host "  Taille: $sizeMB MB" -ForegroundColor Green
 | 
						|
    Write-Host "================================================" -ForegroundColor Green
 | 
						|
    
 | 
						|
    # Display tips for faster rebuilds
 | 
						|
    if (-not $full) {
 | 
						|
        Write-Host "`nConseils pour les prochains builds :" -ForegroundColor Cyan
 | 
						|
        Write-Host "  • Les dépendances npm sont mises en cache" -ForegroundColor Gray
 | 
						|
        Write-Host "  • Le cache Angular est préservé entre les builds" -ForegroundColor Gray
 | 
						|
        Write-Host "  • Seuls les fichiers modifiés déclencheront un rebuild" -ForegroundColor Gray
 | 
						|
    }
 | 
						|
}
 | 
						|
catch {
 | 
						|
    Write-Error "Erreur lors de la construction de l'image Docker : $_"
 | 
						|
    exit 1
 | 
						|
} |