Public/Invoke-DevVm.ps1

<#
.SYNOPSIS
Main DevVm command dispatcher - provides a simplified CLI interface.
 
.DESCRIPTION
Provides a user-friendly command-line interface for DevVm operations.
Similar to tools like nvm, sdkman, etc.
 
.PARAMETER Command
The subcommand to execute (use, current, list, install, available, activate, cache, system, help).
 
.PARAMETER Runtime
The runtime to operate on (node, java, maven, etc.).
 
.PARAMETER Version
The version to use or install.
 
.PARAMETER Persist
Where to persist the configuration (session, project, user, global).
 
.PARAMETER All
Show all available versions (not just latest of each release line).
 
.PARAMETER Update
Force fresh download of available versions, bypassing cache.
 
.PARAMETER Verify
Verify runtime commands after activation (show version output).
 
.EXAMPLE
devvm use node 18.9.1
devvm use java 11.0.2 -Persist project
devvm current node
devvm list node
devvm install node 20.0.0
devvm available java
devvm available java -All
devvm available node -Update
devvm system
devvm cache
devvm cache clean
devvm activate
devvm activate -Verify
 
#>

function Invoke-DevVm {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0)]
        [ValidateSet('use', 'current', 'list', 'install', 'available', 'activate', 'cache', 'system', 'help')]
        [string]$Command = 'help',

        [Parameter(Position = 1)]
        [string]$Runtime,

        [Parameter(Position = 2)]
        [string]$Version,

        [Parameter()]
        [ValidateSet('session', 'project', 'user', 'global')]
        [string]$Persist = 'session',

        [switch]$All,

        [switch]$Update,

        [switch]$Verify
    )

    switch ($Command.ToLower()) {
        'use' {
            if ([string]::IsNullOrWhiteSpace($Runtime)) {
                Write-Error "Runtime is required. Usage: devvm use <runtime> <version>"
                return
            }
            if ([string]::IsNullOrWhiteSpace($Version)) {
                Write-Error "Version is required. Usage: devvm use <runtime> <version>"
                return
            }
            Set-DevVmVersion -Runtime $Runtime -Version $Version -Persist $Persist
        }

        'current' {
            if ([string]::IsNullOrWhiteSpace($Runtime)) {
                Write-Error "Runtime is required. Usage: devvm current <runtime>"
                return
            }
            Get-DevVmCurrent -Runtime $Runtime
        }

        'list' {
            if ([string]::IsNullOrWhiteSpace($Runtime)) {
                Write-Error "Runtime is required. Usage: devvm list <runtime>"
                return
            }
            Get-DevVmVersion -Runtime $Runtime
        }

        'install' {
            if ([string]::IsNullOrWhiteSpace($Runtime)) {
                Write-Error "Runtime is required. Usage: devvm install <runtime> <version>"
                return
            }
            if ([string]::IsNullOrWhiteSpace($Version)) {
                Write-Error "Version is required. Usage: devvm install <runtime> <version>"
                return
            }
            Install-DevVmVersion -Runtime $Runtime -Version $Version
        }

        'available' {
            if ([string]::IsNullOrWhiteSpace($Runtime)) {
                Write-Error "Runtime is required. Usage: devvm available <runtime> [-All] [-Update]"
                return
            }
            Find-DevVmAvailable -Runtime $Runtime -All:$All -Update:$Update
        }

        'activate' {
            if ([string]::IsNullOrWhiteSpace($Runtime)) {
                Invoke-DevVmActivate -Verify:$Verify
            } else {
                Invoke-DevVmActivate -Runtime $Runtime -Verify:$Verify
            }
        }

        'cache' {
            # Handle cache management: devvm cache [clean]
            if ([string]::IsNullOrWhiteSpace($Runtime) -or $Runtime -eq 'clean') {
                # devvm cache or devvm cache clean
                $cleanAll = if ($Runtime -eq 'clean') { $true } else { $false }
                if ($cleanAll) {
                    Clear-DevVmCache -All -Confirm:$false
                } else {
                    Clear-DevVmCache
                }
            } else {
                # devvm cache info or other subcommand
                Clear-DevVmCache
            }
        }

        'system' {
            # Show complete system information
            Get-DevVmSystem
        }

        'help' {
            Write-Output @"
 
DevVm - Version Manager for Windows
=================================================
 
Usage: devvm <command> [options]
 
Commands:
  use <runtime> <version> Set and activate a runtime version
                                Example: devvm use node 18.9.1
                                         devvm use node 18.9.1 -Persist project
 
  current <runtime> Show currently active runtime version
                                Example: devvm current node
 
  list <runtime> List installed versions of a runtime
                                Example: devvm list node
 
  install <runtime> <version> Download and install a runtime version
                                Example: devvm install node 20.0.0
 
  available <runtime> Show available versions for download
                                Example: devvm available java
                                         devvm available java -All
                                         devvm available node -Update
 
  cache [clean] Manage cache of downloaded version lists
                                Example: devvm cache (show cache info)
                                         devvm cache clean (clear all cache)
 
  system Show complete system information
                                (configuration + storage + cache)
                                Example: devvm system
 
  activate [runtime] Activate versions from .devvm files
                                Example: devvm activate
                                         devvm activate node
                                         devvm activate -Verify
 
  help Show this help message
 
Persist Options:
  -Persist session (default) Only for current PowerShell session
  -Persist project Save to .devvm file in current directory
  -Persist user Save to user profile (~/.devvm)
  -Persist global Save to global configuration
 
Config Precedence (highest wins):
  session > project > user > global
 
Supported Runtimes:
  node, java, maven and lein
 
Examples:
  devvm use node 18.9.1 # Use Node.js 18.9.1 in current session
  devvm use node 18.9.1 -Persist project # Save Node.js 18.9.1 to project .devvm
  devvm use java 11.0.2 -Persist user # Set Java 11.0.2 for user profile
  devvm current node # Show active Node.js version
  devvm list node # List installed Node.js versions
  devvm install node 20.0.0 # Install Node.js 20.0.0
  devvm available node # Show available Node.js versions (uses cache)
  devvm available java -All # Show ALL available Java versions
  devvm available node -Update # Force fresh download, bypass cache
  devvm cache # Show cache information
  devvm cache clean # Clear all cache
  devvm system # Show complete system information
  devvm activate -Verify # Verify runtime command output after activation
 
"@

        }

        default {
            Write-Error "Unknown command: $Command. Use 'devvm help' for usage information."
        }
    }
}