DevXToolkit-Installer-v1.0.ps1

<#
.SYNOPSIS
Verifies a source checkout of DevXToolkit and optionally registers it in a
PowerShell profile.
 
.DESCRIPTION
Imports the module manifest beside this script and returns verification
details. The default invocation does not modify profiles, execution policy,
or the filesystem. Use -AddToProfile to add an idempotent import line to a
specific profile. PowerShell Gallery is the preferred persistent installation
path.
#>

[CmdletBinding(SupportsShouldProcess = $true)]
param(
    [switch]$AddToProfile,

    [string]$ProfilePath = $PROFILE.CurrentUserAllHosts
)

$modulePath = Join-Path -Path $PSScriptRoot -ChildPath 'DevXToolkit.psd1'
if (-not (Test-Path -LiteralPath $modulePath -PathType Leaf)) {
    throw "DevXToolkit module manifest not found: $modulePath"
}

Import-Module -Name $modulePath -Force -ErrorAction Stop
$module = Get-Module -Name DevXToolkit
$profileUpdated = $false

if ($AddToProfile) {
    if ([string]::IsNullOrWhiteSpace($ProfilePath)) {
        throw 'ProfilePath must be provided when AddToProfile is used.'
    }

    $resolvedProfilePath = [System.IO.Path]::GetFullPath($ProfilePath)
    $profileDirectory = Split-Path -Path $resolvedProfilePath -Parent
    if (-not (Test-Path -LiteralPath $profileDirectory -PathType Container) -and
        $PSCmdlet.ShouldProcess($profileDirectory, 'Create profile directory')) {
        New-Item -ItemType Directory -Path $profileDirectory -Force | Out-Null
    }

    if (-not (Test-Path -LiteralPath $resolvedProfilePath -PathType Leaf) -and
        $PSCmdlet.ShouldProcess($resolvedProfilePath, 'Create PowerShell profile')) {
        New-Item -ItemType File -Path $resolvedProfilePath -Force | Out-Null
    }

    $escapedModulePath = $modulePath.Replace("'", "''")
    $importLine = "Import-Module -Name '$escapedModulePath'"
    $profileContent = if (Test-Path -LiteralPath $resolvedProfilePath -PathType Leaf) {
        @(Get-Content -LiteralPath $resolvedProfilePath)
    }
    else {
        @()
    }

    if ($profileContent -notcontains $importLine -and
        $PSCmdlet.ShouldProcess($resolvedProfilePath, 'Add DevXToolkit import')) {
        Add-Content -LiteralPath $resolvedProfilePath -Value $importLine -Encoding UTF8
        $profileUpdated = $true
    }
}

[pscustomobject]@{
    Name           = $module.Name
    Version        = $module.Version.ToString()
    ModulePath     = $modulePath
    ProfileUpdated = $profileUpdated
}