Public/Set-DevVmAlias.ps1

function Set-DevVmAlias {
    <#
    .SYNOPSIS
    Creates a custom alias for the devvm command.
 
    .DESCRIPTION
    Adds a custom alias to the global .devvm configuration file. The alias will be loaded
    automatically in future PowerShell sessions.
 
    .PARAMETER Name
    The name of the alias to create. Must start with a letter and contain only alphanumeric
    characters, hyphens, or underscores.
 
    .EXAMPLE
    Set-DevVmAlias -Name "vm"
    Creates an alias 'vm' that points to Invoke-DevVm.
 
    .EXAMPLE
    Set-DevVmAlias -Name "dev"
    Creates an alias 'dev' that points to Invoke-DevVm.
 
    .NOTES
    The alias is saved to the global .devvm file in your user profile and will persist across
    PowerShell sessions. To use the alias in the current session, restart PowerShell or
    run: New-Alias -Name 'yourAlias' -Value 'Invoke-DevVm' -Force
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidatePattern('^[a-zA-Z][a-zA-Z0-9_-]*$')]
        [string]$Name
    )

    $homeDir = Get-HomeDirectory
    $globalConfigPath = Join-Path -Path $homeDir -ChildPath '.devvm'

    # Load existing config or create new
    $config = @{
        versions = @{}
        runtimes = @{}
        alias = @()
    }

    if (Test-Path $globalConfigPath) {
        try {
            $rawContent = Get-Content -Path $globalConfigPath -Raw
            if ($rawContent -and $rawContent.Trim()) {
                $parsed = $rawContent | ConvertFrom-Json

                # Preserve existing versions
                if ($parsed.versions) {
                    $config.versions = @{}
                    foreach ($property in $parsed.versions.PSObject.Properties) {
                        $config.versions[$property.Name] = $property.Value
                    }
                }

                # Preserve existing runtimes
                if ($parsed.runtimes) {
                    $config.runtimes = @{}
                    foreach ($property in $parsed.runtimes.PSObject.Properties) {
                        $config.runtimes[$property.Name] = $property.Value
                    }
                }

                # Load existing aliases
                if ($parsed.alias) {
                    $config.alias = @($parsed.alias)
                }
            }
        }
        catch {
            Write-Verbose "Failed to load existing config: $_"
        }
    }

    # Check if alias already exists
    if ($config.alias -contains $Name) {
        Write-Host "Alias '$Name' already exists in configuration." -ForegroundColor Yellow
        return
    }

    # Add the new alias
    $config.alias += $Name

    # Save the configuration
    try {
        $json = $config | ConvertTo-Json -Depth 5
        $json | Set-Content -Path $globalConfigPath -Encoding UTF8 -ErrorAction Stop
        Write-Host "Alias '$Name' added successfully." -ForegroundColor Green
        Write-Host "To use the alias in this session, run: " -NoNewline
        Write-Host "New-Alias -Name '$Name' -Value 'Invoke-DevVm' -Force" -ForegroundColor Cyan
        Write-Host "Or restart PowerShell to load it automatically."

        # Create the alias in the current session
        New-Alias -Name $Name -Value 'Invoke-DevVm' -Scope Global -Force -ErrorAction SilentlyContinue
    }
    catch {
        Write-Error "Failed to save alias: $_"
    }
}