# Script pour copier les fichiers de configuration vers le projet # Lit files-config.txt et copie chaque fichier vers configs/{system}/ param( [string]$ConfigFile = "files-config.txt", [string]$MappingFile = "path-mapping.json" ) $ErrorActionPreference = "Stop" # Vérifier que le fichier de configuration existe if (-not (Test-Path $ConfigFile)) { Write-Error "Le fichier de configuration '$ConfigFile' n'existe pas." exit 1 } # Créer les répertoires de destination $configsDir = "configs" $windowsDir = Join-Path $configsDir "windows" $wslDir = Join-Path $configsDir "wsl" foreach ($dir in @($configsDir, $windowsDir, $wslDir)) { if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null Write-Host "Créé le répertoire: $dir" } } # Charger le mapping existant ou créer un nouveau $mapping = @{ windows = @{} wsl = @{} } if (Test-Path $MappingFile) { try { $existingMapping = Get-Content $MappingFile -Raw | ConvertFrom-Json if ($existingMapping.windows) { $mapping.windows = @{} $existingMapping.windows.PSObject.Properties | ForEach-Object { $mapping.windows[$_.Name] = $_.Value } } if ($existingMapping.wsl) { $mapping.wsl = @{} $existingMapping.wsl.PSObject.Properties | ForEach-Object { $mapping.wsl[$_.Name] = $_.Value } } } catch { Write-Warning "Impossible de charger le mapping existant, création d'un nouveau mapping." } } # Fonction pour résoudre un chemin Windows function Resolve-WindowsPath { param([string]$path) # Remplacer les variables d'environnement $resolved = $path $resolved = $resolved -replace '%USERPROFILE%', $env:USERPROFILE $resolved = $resolved -replace '%APPDATA%', $env:APPDATA $resolved = $resolved -replace '%LOCALAPPDATA%', $env:LOCALAPPDATA $resolved = $resolved -replace '%HOME%', $env:HOME $resolved = $resolved -replace '%USERPROFILE%', $env:USERPROFILE # Remplacer les autres variables d'environnement $resolved = [System.Environment]::ExpandEnvironmentVariables($resolved) return $resolved } # Fonction pour résoudre un chemin WSL function Resolve-WSLPath { param([string]$path) # Expansion de ~ if ($path -match '^~') { $homePath = wsl bash -c "echo ~" $path = $path -replace '^~', $homePath } return $path } # Fonction pour obtenir le chemin relatif dans le projet function Get-ProjectPath { param( [string]$system, [string]$originalPath ) # Nettoyer chaque partie du chemin $parts = $originalPath -split '[\\/]' $safeParts = @() foreach ($part in $parts) { # Nettoyer chaque partie : remplacer les caractères invalides et les variables $safePart = $part -replace '[\\/:*?"<>|]', '_' $safePart = $safePart -replace '^%', 'var_' -replace '%$', '' -replace '%', '_' $safePart = $safePart -replace '^~', 'home' # Si la partie est vide après nettoyage, utiliser un nom par défaut if ([string]::IsNullOrWhiteSpace($safePart)) { $safePart = "root" } $safeParts += $safePart } # Rejoindre toutes les parties avec des underscores $safeName = $safeParts -join '_' # Construire le chemin final $basePath = Join-Path "configs" $system return Join-Path $basePath $safeName } # Lire et traiter le fichier de configuration $lines = Get-Content $ConfigFile | Where-Object { $_ -notmatch '^\s*#' -and $_.Trim() -ne '' } $copiedCount = 0 $skippedCount = 0 $errorCount = 0 foreach ($line in $lines) { $line = $line.Trim() if ($line -eq '' -or $line -match '^\s*#') { continue } # Parser la ligne: système|chemin if ($line -notmatch '^(\w+)\|(.+)$') { Write-Warning "Ligne ignorée (format invalide): $line" continue } $system = $matches[1] $originalPath = $matches[2] if ($system -notin @('windows', 'wsl')) { Write-Warning "Système invalide '$system' dans la ligne: $line" continue } try { # Résoudre le chemin selon le système if ($system -eq 'windows') { $resolvedPath = Resolve-WindowsPath $originalPath } else { # Pour WSL, utiliser wsl pour résoudre le chemin $wslPath = $originalPath if ($wslPath -match '^~') { $wslPath = wsl bash -c "echo $wslPath" } $resolvedPath = $wslPath } # Vérifier que le fichier existe if ($system -eq 'windows') { if (-not (Test-Path $resolvedPath)) { Write-Warning "Fichier introuvable: $resolvedPath (original: $originalPath)" $skippedCount++ continue } } else { # Pour WSL, vérifier via wsl wsl test -f "$resolvedPath" 2>$null | Out-Null if ($LASTEXITCODE -ne 0) { Write-Warning "Fichier introuvable dans WSL: $resolvedPath (original: $originalPath)" $skippedCount++ continue } } # Obtenir le chemin de destination dans le projet $projectPath = Get-ProjectPath $system $originalPath # Créer le répertoire parent si nécessaire $projectDir = Split-Path $projectPath -Parent if (-not (Test-Path $projectDir)) { New-Item -ItemType Directory -Path $projectDir -Force | Out-Null } # Copier le fichier if ($system -eq 'windows') { Copy-Item -Path $resolvedPath -Destination $projectPath -Force } else { # Pour WSL, utiliser PowerShell pour copier depuis WSL # Méthode plus fiable : lire le contenu depuis WSL et l'écrire avec PowerShell try { $content = wsl cat "$resolvedPath" 2>&1 if ($LASTEXITCODE -eq 0) { # Convertir le contenu en chaîne si c'est un tableau if ($content -is [array]) { $content = $content -join "`n" } Set-Content -Path $projectPath -Value $content -Encoding UTF8 -Force } else { throw "Erreur lors de la lecture du fichier WSL: $content" } } catch { Write-Error "Impossible de copier le fichier WSL '$resolvedPath': $_" throw } } # Enregistrer le mapping $mapping[$system][$originalPath] = $projectPath Write-Host "Copié: $originalPath -> $projectPath" $copiedCount++ } catch { Write-Error "Erreur lors du traitement de '$line': $_" $errorCount++ } } # Sauvegarder le mapping $mappingJson = @{ windows = $mapping.windows wsl = $mapping.wsl } | ConvertTo-Json -Depth 10 Set-Content -Path $MappingFile -Value $mappingJson -Encoding UTF8 Write-Host "`nRésumé:" Write-Host " Fichiers copiés: $copiedCount" Write-Host " Fichiers ignorés: $skippedCount" Write-Host " Erreurs: $errorCount" Write-Host " Mapping sauvegardé dans: $MappingFile"