Expand-Token.ps1

#Set-StrictMode -Version Latest
#####################################################
# Expand-Token
#####################################################
<#PSScriptInfo
 
.VERSION 0.1
 
.GUID bfd55243-60dd-4394-a80e-835718187e1f
 
.AUTHOR David Walker, Sitecore Dave, Radical Dave
 
.COMPANYNAME David Walker, Sitecore Dave, Radical Dave
 
.COPYRIGHT David Walker, Sitecore Dave, Radical Dave
 
.TAGS powershell token regex
 
.LICENSEURI https://github.com/Radical-Dave/Expand-Token/blob/main/LICENSE
 
.PROJECTURI https://github.com/Radical-Dave/Expand-Token
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS Expand-TokenString,Set-EnvToken
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
 
.DESCRIPTION
 PowerShell Script to expand tokens in file/folder/strings using RegEx, EnvVar EnvironmentVariables, .env files and .with
 
.PARAMETER source
Source path to process
 
.PARAMETER destination
Destination path for output - if not provided, overwrites
 
.PARAMETER regex
Regex pattern for finding tokens - default to powershell format:
#>

[OutputType('System.String')]
[CmdletBinding(SupportsShouldProcess)]
Param(
    [Parameter(Mandatory=$false)]
    [string] $source,
    [Parameter(Mandatory=$false)]
    [string] $destination,
    [Parameter(Mandatory=$false)]
    [string] $regex = '(\$\()([a-zA-Z0-9\.\-_]*)(\))'
)
begin {
    $Global:ProgressPreference = 'SilentlyContinue'
    $ErrorActionPreference = 'Stop'
    #$commandPath = $MyInvocation.MyCommand.Path
    $commandPath = $PSCommandPath
    Write-Verbose "commandPath:$commandPath"
    $PSScriptName = (Split-Path $commandPath -Leaf).Replace('.ps1','')
    $PSCallingScript = if ($MyInvocation.PSCommandPath) { $MyInvocation.PSCommandPath | Split-Path -Parent } else { $null }
    $currLocation = Get-Location
    Write-Verbose "#####################################################`n# $PSScriptName $source $destination called by:$PSCallingScript from $currLocation"
}
process {
    if (!$source) { $source = "$currLocation" }
    if ($source -eq $PSScriptRoot) { throw "$PSScriptName ERROR - will not run in root of $PSScriptRoot"}
    if (-not (Test-Path $source)) {
        if (-not (Test-Path "$currLocation\$source")) {
            if (-not (Test-Path "$currLocation\tests\$source")) {
                throw "$PSScriptName invalid source:$source"
            } else {
                $source = "$currLocation\tests\$source"
            }
        } else {
            $source ="$currLocation\$source"
        }
    }
    Write-Output "source:$source"
    #Write-Verbose "destination:$destination"
    if (!$destination) {$destination = $source} elseif ($destination.IndexOf(':') -eq -1 -and $destination.Substring(0,1) -ne '\') {$destination = Join-Path $currLocation $destination}
    Write-Verbose "destination:$destination"
    if ($destination) {
        if (-not (Test-Path $source -PathType Leaf)) {
            if (-not (Test-Path $destination)) { New-Item -Path $destination -ItemType Directory | Out-Null}
        } else {
            $destParent = Split-Path $destination -Parent
            if (-not (Test-Path $destParent)) { New-Item -Path $destParent -ItemType Directory | Out-Null}
        }
    }

    if (-not (Get-Command -Name 'Set-EnvToken')) {Install-Script -Name 'Set-EnvToken' -Confirm:$False -Force}
    Set-EnvToken @((Split-Path $profile -Parent),$PSScriptRoot,("$currLocation" -ne "$PSScriptRoot" ? $currLocation : ''),(Split-Path $source -Parent),(($destination -ne $source -and $destParent) ? $destParent : ''