FilesystemCache.ps1

$linuxFileshare = $env:CI_CACHE_LINUX_FS_PATH ?? "/data/ci_cache"
$windowsFileshare = $env:CI_CACHE_WINDOWS_FS_PATH ?? "C:\ci_cache"

if ($IsLinux) {
    $script:storePath = $linuxFileshare
} else {
    $script:storePath = $windowsFileshare
}

if (-not(Test-Path $script:storePath)) {
    New-Item -ItemType Directory -Path $script:storePath
}

function Save-FileSystemCache {
    [CmdletBinding()]
    param (
        [ValidatePattern('[a-zA-Z_]+')]
        [Parameter(Mandatory=$true)]
        [string] $Id,
        [Parameter(Mandatory=$true)][string[]] $Paths,
        [string[]] $PathsToComputeHash = @(),
        [int] $RepeatCount = 3
    )

    return Save-CacheInternal `
        -Id $Id `
        -Paths $Paths `
        -PathsToComputeHash $PathsToComputeHash `
        -TestHandler {
            param (
                [string]$key
            )
            $archivePath = (Join-Path $script:storePath $key)
            if (-not(Test-Path $archivePath)) {
                throw "CI_CACHE_NOT_FOUND: $key does not exist in $($script:storePath)"
            }
        } `
        -SaveHandler {
            param (
                [string]$path
            )
        
            $fileName = Split-Path -Path $path -Leaf
            $archivePath = (Join-Path $script:storePath $fileName)
            Copy-Item -Path $path -Destination $archivePath -Force
        } `
        -RepeatCount $RepeatCount
}

function Test-FileSystemCache {
    [CmdletBinding()]
    param (
        [ValidatePattern('[a-zA-Z_]+')]
        [Parameter(Mandatory=$true)]
        [string] $Id,
        [string[]] $PathsToComputeHash = @(),
        [int] $RepeatCount = 3
    )

    return Test-CacheInternal `
        -Id $Id `
        -PathsToComputeHash $PathsToComputeHash `
        -TestHandler {
            param (
                [string]$key
            )
            $archivePath = (Join-Path $script:storePath $key)
            if (-not(Test-Path $archivePath)) {
                throw "CI_CACHE_NOT_FOUND: $key does not exist in $($script:storePath)"
            }
        } `
        -RepeatCount $RepeatCount
}

function Restore-FileSystemCache {
    [CmdletBinding()]
    param (
        [ValidatePattern('[a-zA-Z_]+')]
        [Parameter(Mandatory=$true)]
        [string] $Id,
        [string[]] $PathsToComputeHash = @(),
        [string] $RestoreHashKey,
        [string] $OutputPath,
        [int] $RepeatCount = 3
    )

   return Restore-CacheInternal `
        -Id $Id `
        -PathsToComputeHash $PathsToComputeHash `
        -RestoreHashKey $RestoreHashKey `
        -OutputPath $OutputPath `
        -RestoreHandler {
            param (
                [string] $key
            )
            $archivePath = (Join-Path $script:storePath $key)
            Copy-Item -Path $archivePath -Destination $key -Force
            if (-not(Test-Path $key)) {
                throw "CI_CACHE_NOT_FOUND: $key does not exist"
            }
        } `
        -RepeatCount $RepeatCount
}

Export-ModuleMember -Function Save-FileSystemCache, Test-FileSystemCache, Restore-FileSystemCache