in pratica è il nonno di quantum-core-report

# Script per generare report versioni e struttura progetto

# Uso: .\Generate-ProjectReport.ps1 -Path "C:\path\to\project" -OutputFile "report.txt"

  

param(

    [Parameter(Mandatory=$false)]

    [string]$Path = ".",

    [Parameter(Mandatory=$false)]

    [string]$OutputFile = "project_report.txt",

    [Parameter(Mandatory=$false)]

    [string[]]$Extensions = @("*.txt", "*.py"),

    [Parameter(Mandatory=$false)]

    [string[]]$ExcludedDirs = @("venv", "env", ".venv", ".env", "node_modules", "__pycache__", ".git")

)

  

function Get-FileVersionInfo {

    param([string]$FilePath)

    try {

        $firstLine = Get-Content $FilePath -First 1 -ErrorAction SilentlyContinue

        if ($firstLine -and ($firstLine.StartsWith("#") -or $firstLine.StartsWith("//"))) {

            return $firstLine.Trim()

        }

    }

    catch {

        return "Errore lettura file"

    }

    return "Nessuna info versione"

}

  

function Get-DirectoryTree {

    param(

        [string]$Path,

        [int]$Level = 0,

        [string[]]$Extensions,

        [string[]]$ExcludedDirs,

        [bool[]]$IsLast = @()

    )

    $treeOutput = @()

    # Ottieni nome directory corrente

    $dirName = Split-Path $Path -Leaf

    if ($Level -eq 0) {

        $treeOutput += "$dirName/"

    }

    try {

        # Ottieni tutti gli elementi nella directory

        $items = Get-ChildItem -Path $Path | Sort-Object @{Expression={$_.PSIsContainer}; Descending=$true}, Name

        for ($i = 0; $i -lt $items.Count; $i++) {

            $item = $items[$i]

            $isLastItem = ($i -eq $items.Count - 1)

            # Costruisci il prefisso per la riga corrente

            $prefix = ""

            for ($j = 0; $j -lt $Level; $j++) {

                if ($j -lt $IsLast.Count -and $IsLast[$j]) {

                    $prefix += "    "

                } else {

                    $prefix += "|   "

                }

            }

            # Aggiungi il simbolo per l elemento corrente

            if ($isLastItem) {

                $prefix += "\-- "

            } else {

                $prefix += "|-- "

            }

            if ($item.PSIsContainer) {

                # Controlla se la directory è da escludere

                $isExcluded = $ExcludedDirs -contains $item.Name

                if ($isExcluded) {

                    # Mostra la directory ma con indicazione di esclusione

                    $treeOutput += "$prefix$($item.Name)/         # [ESCLUSA - ambiente virtuale/cache]"

                } else {

                    # E una directory normale

                    $treeOutput += "$prefix$($item.Name)/"

                    # Ricorsione per sottodirectory

                    $newIsLast = $IsLast + @($isLastItem)

                    $subTree = Get-DirectoryTree -Path $item.FullName -Level ($Level + 1) -Extensions $Extensions -ExcludedDirs $ExcludedDirs -IsLast $newIsLast

                    $treeOutput += $subTree

                }

            }

            else {

                # E un file - controlla se corrisponde alle estensioni

                $matchesExtension = $false

                foreach ($ext in $Extensions) {

                    if ($item.Name -like $ext) {

                        $matchesExtension = $true

                        break

                    }

                }

                if ($matchesExtension) {

                    $versionInfo = Get-FileVersionInfo -FilePath $item.FullName

                    # Formatta il commento di versione in modo piu pulito

                    $cleanVersionInfo = $versionInfo.Replace("#", "").Trim()

                    if ($cleanVersionInfo -ne "Nessuna info versione") {

                        $treeOutput += "$prefix$($item.Name)         # $cleanVersionInfo"

                    } else {

                        $treeOutput += "$prefix$($item.Name)"

                    }

                }

                else {

                    $treeOutput += "$prefix$($item.Name)"

                }

            }

        }

    }

    catch {

        $prefix = ""

        for ($j = 0; $j -lt $Level; $j++) {

            if ($j -lt $IsLast.Count -and $IsLast[$j]) {

                $prefix += "    "

            } else {

                $prefix += "|   "

            }

        }

        $treeOutput += "$prefix\-- [ERROR] Errore accesso directory"

    }

    return $treeOutput

}

  

function Should-ExcludeDirectory {

    param(

        [string]$DirectoryPath,

        [string[]]$ExcludedDirs

    )

    $dirName = Split-Path $DirectoryPath -Leaf

    return $ExcludedDirs -contains $dirName

}

  

function Generate-ProjectReport {

    param(

        [string]$ProjectPath,

        [string[]]$Extensions,

        [string[]]$ExcludedDirs

    )

    $report = @()

    $report += "=" * 80

    $report += "REPORT STRUTTURA E VERSIONI PROGETTO"

    $report += "Generato: $(Get-Date -Format 'dd/MM/yyyy HH:mm:ss')"

    $report += "Percorso: $ProjectPath"

    $report += "Estensioni monitorate: $($Extensions -join ', ')"

    $report += "Directory escluse: $($ExcludedDirs -join ', ')"

    $report += "=" * 80

    $report += ""

    # Struttura ad albero

    $report += "STRUTTURA AD ALBERO:"

    $report += "-" * 40

    $treeStructure = Get-DirectoryTree -Path $ProjectPath -Extensions $Extensions -ExcludedDirs $ExcludedDirs

    $report += $treeStructure

    $report += ""

    # Dettaglio versioni (escludendo le directory specificate)

    $report += "DETTAGLIO VERSIONI:"

    $report += "-" * 40

    $allFiles = @()

    foreach ($ext in $Extensions) {

        $files = Get-ChildItem -Path $ProjectPath -Filter $ext -Recurse -File | Where-Object {

            $shouldExclude = $false

            foreach ($excludedDir in $ExcludedDirs) {

                if ($_.FullName -like "*\$excludedDir\*" -or $_.FullName -like "*/$excludedDir/*") {

                    $shouldExclude = $true

                    break

                }

            }

            return -not $shouldExclude

        }

        $allFiles += $files

    }

    if ($allFiles.Count -eq 0) {

        $report += "Nessun file trovato con le estensioni specificate (escluse le directory filtrate)."

    }

    else {

        foreach ($file in $allFiles | Sort-Object FullName) {

            $relativePath = $file.FullName.Replace($ProjectPath, "").TrimStart("\")

            $versionInfo = Get-FileVersionInfo -FilePath $file.FullName

            $lastModified = $file.LastWriteTime.ToString("dd/MM/yyyy HH:mm")

            $fileSize = [math]::Round($file.Length / 1KB, 2)

            $report += "FILE: $relativePath"

            $report += "   Versione: $versionInfo"

            $report += "   Modificato: $lastModified"

            $report += "   Dimensione: $fileSize KB"

            $report += ""

        }

    }

    # Statistiche

    $report += "STATISTICHE:"

    $report += "-" * 40

    $totalFiles = $allFiles.Count

    # Conta solo le directory non escluse

    $allDirs = Get-ChildItem -Path $ProjectPath -Recurse -Directory | Where-Object {

        $shouldExclude = $false

        foreach ($excludedDir in $ExcludedDirs) {

            if ($_.FullName -like "*\$excludedDir\*" -or $_.FullName -like "*/$excludedDir/*" -or $_.Name -eq $excludedDir) {

                $shouldExclude = $true

                break

            }

        }

        return -not $shouldExclude

    }

    $totalDirs = $allDirs.Count

    $totalSize = [math]::Round(($allFiles | Measure-Object Length -Sum).Sum / 1MB, 2)

    $report += "File totali monitorati: $totalFiles"

    $report += "Directory totali (escluse quelle filtrate): $totalDirs"

    $report += "Dimensione totale: $totalSize MB"

    $report += ""

    # Raggruppamento per estensione

    $extensionStats = $allFiles | Group-Object Extension | Sort-Object Count -Descending

    $report += "Distribuzione per estensione:"

    foreach ($stat in $extensionStats) {

        $report += "  $($stat.Name): $($stat.Count) file(s)"

    }

    # Mostra le directory principali escluse (non le sottodirectory)

    $excludedFound = Get-ChildItem -Path $ProjectPath -Recurse -Directory | Where-Object {

        $ExcludedDirs -contains $_.Name

    } | Where-Object {

        # Escludi le sottodirectory delle directory già escluse

        $currentDir = $_.FullName

        $isSubdirectory = $false

        foreach ($excludedDir in $ExcludedDirs) {

            $parentExcluded = Get-ChildItem -Path $ProjectPath -Recurse -Directory | Where-Object {

                $_.Name -eq $excludedDir -and $currentDir.StartsWith($_.FullName + "\")

            }

            if ($parentExcluded) {

                $isSubdirectory = $true

                break

            }

        }

        return -not $isSubdirectory

    }

    if ($excludedFound.Count -gt 0) {

        $report += ""

        $report += "Directory principali escluse trovate:"

        foreach ($dir in $excludedFound | Sort-Object FullName) {

            $relativePath = $dir.FullName.Replace($ProjectPath, "").TrimStart("\")

            $report += "  $relativePath"

        }

    }

    $report += ""

    $report += "=" * 80

    $report += "Fine report"

    $report += "=" * 80

    return $report

}

  

# Esecuzione principale

Write-Host "Generazione report progetto..." -ForegroundColor Green

Write-Host "Percorso: $Path" -ForegroundColor Cyan

Write-Host "Output: $OutputFile" -ForegroundColor Cyan

Write-Host "Directory escluse: $($ExcludedDirs -join ', ')" -ForegroundColor Yellow

  

if (-not (Test-Path $Path)) {

    Write-Error "Il percorso specificato non esiste: $Path"

    exit 1

}

  

try {

    $reportContent = Generate-ProjectReport -ProjectPath $Path -Extensions $Extensions -ExcludedDirs $ExcludedDirs

    # Salva il report

    $reportContent | Out-File -FilePath $OutputFile -Encoding UTF8

    Write-Host "Report generato con successo!" -ForegroundColor Green

    Write-Host "File salvato: $OutputFile" -ForegroundColor Yellow

    # Mostra un'anteprima

    Write-Host ""

    Write-Host "ANTEPRIMA REPORT:" -ForegroundColor Magenta

    Write-Host ("-" * 50) -ForegroundColor Gray

    $reportContent | Select-Object -First 20 | ForEach-Object { Write-Host $_ }

    if ($reportContent.Count -gt 20) {

        Write-Host "... (resto del report nel file $OutputFile)" -ForegroundColor Gray

    }

}

catch {

    Write-Error "Errore durante la generazione del report: $($_.Exception.Message)"

    exit 1

}

  

Write-Host ""

Write-Host "Per vedere il report completo:" -ForegroundColor Yellow

Write-Host "   Get-Content '$OutputFile'" -ForegroundColor White