WinGetManifestFetcher.psm1

#Requires -Version 5.1

<#
.SYNOPSIS
    WinGet Manifest Fetcher - Retrieve installer information from WinGet manifests without WinGet client
.DESCRIPTION
    This module provides functionality to query the microsoft/winget-pkgs repository and retrieve
    installer information from WinGet manifests without requiring the WinGet client to be installed.
.NOTES
    Author: WinGet Manifest Fetcher Contributors
    Version: 1.6.0
#>


$ErrorActionPreference = 'Stop'

# Module-level variables
$script:WinGetRepoOwner = 'microsoft'
$script:WinGetRepoName = 'winget-pkgs'
$script:ManifestPath = 'manifests'

# Windows PowerShell uses ServicePointManager. Preserve OS-managed
# SystemDefault (0); older .NET defaults need TLS 1.2 enabled once at import.
if ($PSEdition -eq 'Desktop' -and [int][Net.ServicePointManager]::SecurityProtocol -ne 0) {
    [Net.ServicePointManager]::SecurityProtocol =
        [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
}

# Cache configuration
$script:CacheEnabled = $true
# Cross-platform cache directory configuration
if ($IsWindows -or (-not (Test-Path Variable:IsWindows) -and $env:OS -eq 'Windows_NT')) {
    # Windows
    $script:CacheDirectory = Join-Path -Path ([Environment]::GetFolderPath('LocalApplicationData')) -ChildPath (Join-Path -Path 'WinGetManifestFetcher' -ChildPath 'Cache')
} elseif ($IsMacOS -or (-not (Test-Path Variable:IsMacOS) -and $env:OS -ne 'Windows_NT' -and (uname) -eq 'Darwin')) {
    # macOS
    $script:CacheDirectory = Join-Path -Path $HOME -ChildPath (Join-Path -Path 'Library' -ChildPath (Join-Path -Path 'Caches' -ChildPath 'WinGetManifestFetcher'))
} else {
    # Linux and other Unix-like systems
    if ($env:XDG_CACHE_HOME) {
        $script:CacheDirectory = Join-Path -Path $env:XDG_CACHE_HOME -ChildPath 'WinGetManifestFetcher'
    } else {
        $script:CacheDirectory = Join-Path -Path (Join-Path -Path $HOME -ChildPath '.cache') -ChildPath 'WinGetManifestFetcher'
    }
}
$script:CacheExpirationMinutes = 60  # Default cache expiration time
$script:CacheVersion = '1.0'  # Cache version for invalidation

if (-not $env:GITHUB_TOKEN) {
    Write-Warning "No GitHub authentication configured. API rate limits will apply. Set the GITHUB_TOKEN environment variable to authenticate."
}

# Initialize cache directory
if ($script:CacheEnabled -and -not (Test-Path -Path $script:CacheDirectory)) {
    try {
        $null = New-Item -ItemType Directory -Path $script:CacheDirectory -Force
        Write-Verbose "Created cache directory: $script:CacheDirectory"
    } catch {
        Write-Warning "Failed to create cache directory: $_"
        $script:CacheEnabled = $false
    }
}

# NOTE: PowerShellBuild will automatically include all functions from src/Private and src/Public
# Private functions will be available within the module
# Public functions will be exported

# When running from source (not built), manually dot-source the functions
$moduleRoot = $PSScriptRoot
if (-not $moduleRoot) {
    $moduleRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
}

# Check if we're running from source by looking for the src folder structure
$srcPath = $moduleRoot
if (Test-Path (Join-Path $srcPath 'Private') -PathType Container) {
    Write-Verbose "Running from source - loading functions manually"
    
    # Dot source the private functions
    $privateFunctions = Get-ChildItem -Path (Join-Path $srcPath 'Private') -Filter '*.ps1' -Recurse
    foreach ($function in $privateFunctions) {
        Write-Verbose "Loading private function: $($function.Name)"
        . $function.FullName
    }
    
    # Dot source the public functions
    $publicFunctions = Get-ChildItem -Path (Join-Path $srcPath 'Public') -Filter '*.ps1' -Recurse
    foreach ($function in $publicFunctions) {
        Write-Verbose "Loading public function: $($function.Name)"
        . $function.FullName
    }
    
    # Export public functions
    $publicFunctionNames = $publicFunctions.BaseName
    Export-ModuleMember -Function $publicFunctionNames
}