Build-ModuleManifest.ps1

# Build-ModuleManifest.ps1
[CmdletBinding()]
param(
    [Parameter(Mandatory = $false)]
    [string]$ModulePath = $PSScriptRoot
)

$psd1Path = Join-Path $ModulePath "HyperShell.psd1"
$psm1Path = Join-Path $ModulePath "HyperShell.psm1"
$publicPath = Join-Path $ModulePath "Public"

$allFunctions = @()

# Function to extract function names from a file
function Get-FunctionsFromFile {
    param([string]$FilePath)
    
    $ast = [System.Management.Automation.Language.Parser]::ParseFile(
        $FilePath,
        [ref]$null,
        [ref]$null
    )
    
    $functions = $ast.FindAll({
        $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]
    }, $true) | ForEach-Object { $_.Name }
    
    return $functions
}

# Check .psm1 file
if (Test-Path $psm1Path) {
    Write-Host "Scanning HyperShell.psm1..." -ForegroundColor Cyan
    $psm1Functions = Get-FunctionsFromFile -FilePath $psm1Path
    if ($psm1Functions) {
        Write-Host " Found $($psm1Functions.Count) functions in .psm1" -ForegroundColor Gray
        $allFunctions += $psm1Functions
    }
}

# Check Public folder
if (Test-Path $publicPath) {
    Write-Host "Scanning Public folder..." -ForegroundColor Cyan
    $publicFiles = Get-ChildItem -Path $publicPath -Filter "*.ps1" -File
    
    foreach ($file in $publicFiles) {
        Write-Host " Scanning $($file.Name)..." -ForegroundColor Gray
        $fileFunctions = Get-FunctionsFromFile -FilePath $file.FullName
        if ($fileFunctions) {
            Write-Host " Found: $($fileFunctions -join ', ')" -ForegroundColor DarkGray
            $allFunctions += $fileFunctions
        }
    }
}

# Remove duplicates and sort
$allFunctions = $allFunctions | Select-Object -Unique | Sort-Object

Write-Host "`nTotal functions found: $($allFunctions.Count)" -ForegroundColor Cyan
$allFunctions | ForEach-Object { Write-Host " - $_" -ForegroundColor White }

if ($allFunctions.Count -eq 0) {
    Write-Warning "No functions found! Check your module structure."
    Write-Host "`nSearched in:" -ForegroundColor Yellow
    Write-Host " - $psm1Path (Exists: $(Test-Path $psm1Path))" -ForegroundColor Gray
    Write-Host " - $publicPath (Exists: $(Test-Path $publicPath))" -ForegroundColor Gray
    exit 1
}

# Update the manifest
Write-Host "`nUpdating module manifest..." -ForegroundColor Cyan
$manifestData = Import-PowerShellDataFile -Path $psd1Path

Update-ModuleManifest -Path $psd1Path `
    -FunctionsToExport $allFunctions `
    -AliasesToExport @('*') `
    -RootModule $manifestData.RootModule `
    -ModuleVersion $manifestData.ModuleVersion `
    -GUID $manifestData.GUID `
    -Author $manifestData.Author `
    -CompanyName $manifestData.CompanyName `
    -Copyright $manifestData.Copyright `
    -Description $manifestData.Description `
    -PowerShellVersion $manifestData.PowerShellVersion