Public/Initialize-DevVmProfile.ps1
|
<#
.SYNOPSIS Initializes DevVm auto-activation in the PowerShell profile. .DESCRIPTION Adds DevVm auto-activation code to the PowerShell profile after installing from PowerShell Gallery. This function should be run once after installing the DevVm module from PowerShell Gallery. Supports profile versioning starting from version 0.1.1, which automatically tracks the module version in the profile block for better compatibility management. .PARAMETER ProfilePath The PowerShell profile path to update. Default: $PROFILE (current user's profile) .PARAMETER Force Force update if DevVm block already exists in profile. .EXAMPLE Initialize-DevVmProfile Initialize-DevVmProfile -Force .NOTES Profile Version Support: 0.1.1+ The profile block now includes version tracking for improved maintenance and debugging. #> function Initialize-DevVmProfile { [CmdletBinding()] param( [string]$ProfilePath = $PROFILE, [switch]$Force ) $ErrorActionPreference = 'Stop' Write-Information "DevVm Profile Initialization" -InformationAction Continue Write-Information "===========================" -InformationAction Continue Write-Information "" -InformationAction Continue # Ensure profile directory exists $profileDir = Split-Path -Parent $profilePath if (-not (Test-Path $profileDir)) { New-Item -ItemType Directory -Path $profileDir -Force | Out-Null Write-Information " Created profile directory: $profileDir" -InformationAction Continue } # Create profile if it doesn't exist if (-not (Test-Path $profilePath)) { New-Item -ItemType File -Path $profilePath -Force | Out-Null Write-Information " Created profile file: $profilePath" -InformationAction Continue } # Get the DevVm module path $devVmModule = Get-Module DevVm -ErrorAction SilentlyContinue if (-not $devVmModule) { Write-Error "DevVm module not loaded. Please import it first." Write-Information " Import-Module DevVm" -InformationAction Continue exit 1 } $moduleBase = $devVmModule.ModuleBase $moduleVersion = $devVmModule.Version # Build the profile block with proper escaping # Use here-string with careful escaping for all special characters $devVmBlock = @" # BEGIN DEVVM # DevVm - Version Manager for runtimes (v$moduleVersion) `$devVmModulePath = '$moduleBase' if (Test-Path `$devVmModulePath) { `$devVmModuleManifest = Join-Path -Path `$devVmModulePath -ChildPath 'DevVm.psd1' if (Test-Path `$devVmModuleManifest) { Import-Module `$devVmModuleManifest -ErrorAction SilentlyContinue Invoke-DevVmActivate -ErrorAction SilentlyContinue # Load custom aliases from global config `$homeDir = if (`$env:USERPROFILE) { `$env:USERPROFILE } elseif (`$env:HOME) { `$env:HOME } else { [System.IO.Path]::GetTempPath() } `$globalConfigPath = Join-Path -Path `$homeDir -ChildPath '.devvm' if (Test-Path `$globalConfigPath) { try { `$configContent = Get-Content -Path `$globalConfigPath -Raw | ConvertFrom-Json if (`$configContent.alias -and `$configContent.alias.Count -gt 0) { foreach (`$customAlias in `$configContent.alias) { if (`$customAlias -and `$customAlias -match "^[a-zA-Z][a-zA-Z0-9_-]*`$") { New-Alias -Name `$customAlias -Value "Invoke-DevVm" -Force -ErrorAction SilentlyContinue } } } } catch { # Silently ignore errors loading custom aliases } } } } # END DEVVM "@ $profileContent = Get-Content -Path $profilePath -Raw -ErrorAction SilentlyContinue if (-not $profileContent) { $profileContent = "" } $devVmBlockPattern = '(?s)# BEGIN DEVVM.*?# END DEVVM\r?\n?' if ($profileContent -match $devVmBlockPattern) { if ($Force) { # Update existing block $escapedDevVmBlock = $devVmBlock -replace '\$', '$$' $updatedContent = [regex]::Replace( $profileContent, $devVmBlockPattern, $escapedDevVmBlock, [System.Text.RegularExpressions.RegexOptions]::Singleline ) Write-Information " DevVm profile block already exists, updating..." -InformationAction Continue } else { Write-Warning "DevVm profile block already exists in profile" Write-Information " Use -Force to update it" -InformationAction Continue return } } else { # Add new block at the beginning $cleanedContent = $profileContent -replace '(?m)^\s*(# DevVm - Version Manager for runtimes|Import-Module DevVm.*|Invoke-DevVmActivate.*)\s*$\r?\n?', '' $cleanedContent = $cleanedContent -replace '(\r?\n){3,}', "`r`n`r`n" # Preserve BOM if present $hasBom = $profileContent.StartsWith([char]0xFEFF) if ($hasBom) { $cleanedContent = $cleanedContent.TrimStart([char]0xFEFF) $updatedContent = [char]0xFEFF + $devVmBlock + $cleanedContent } else { $updatedContent = $devVmBlock + $cleanedContent } Write-Information " Adding DevVm profile block..." -InformationAction Continue } # Use WriteAllText with UTF8NoBOM to avoid adding BOM # For PS5.1 compatibility, we need to use this approach $utf8NoBom = New-Object System.Text.UTF8Encoding $false [System.IO.File]::WriteAllText($profilePath, $updatedContent, $utf8NoBom) Write-Information " [OK] Profile updated successfully: $profilePath" -InformationAction Continue Write-Information "" -InformationAction Continue Write-Information "Setup completed!" -InformationAction Continue Write-Information "" -InformationAction Continue Write-Information "Next steps:" -InformationAction Continue Write-Information " 1. Close and reopen PowerShell to apply changes" -InformationAction Continue Write-Information " 2. Run 'devvm current' to verify" -InformationAction Continue Write-Information " 3. Run 'devvm help' for usage information" -InformationAction Continue Write-Information "" -InformationAction Continue } |