DevXToolkit-Installer-v1.0.ps1

<#
===========================================================
 DevXToolkit Installer v1.0
 Author: Jess
 Purpose:
   - Install or repair DevXToolkit module
   - Align PowerShell profiles across all hosts
   - Ensure DevXToolkit auto-loads everywhere
   - Set execution policy
   - Verify installation
===========================================================
#>


Write-Host "`n=== DevXToolkit Installer v1.0 ===`n" -ForegroundColor Cyan

# ---------------------------------------------------------
# 1. Define module path
# ---------------------------------------------------------
$DevXToolkitPath = Join-Path $PSScriptRoot "DevXToolkit.psd1"
$ImportLine     = "Import-Module `"$DevXToolkitPath`" -Force"

# ---------------------------------------------------------
# 2. Ensure module exists
# ---------------------------------------------------------
if (-not (Test-Path $DevXToolkitPath)) {
    Write-Host "ERROR: DevXToolkit module not found at $DevXToolkitPath" -ForegroundColor Red
    exit 1
}

Write-Host "DevXToolkit module found." -ForegroundColor Green

# ---------------------------------------------------------
# 3. Determine profile paths
# ---------------------------------------------------------
$Profile_Main     = $PROFILE
$Profile_AllHosts = $PROFILE.CurrentUserAllHosts

Write-Host "Main profile: $Profile_Main"
Write-Host "AllHosts profile: $Profile_AllHosts"

# ---------------------------------------------------------
# 4. Ensure profile directories exist
# ---------------------------------------------------------
$ProfileDir = Split-Path $Profile_Main -Parent
if (-not (Test-Path $ProfileDir)) {
    New-Item -ItemType Directory -Path $ProfileDir -Force | Out-Null
    Write-Host "Created profile directory: $ProfileDir"
}

# ---------------------------------------------------------
# 5. Ensure profile files exist
# ---------------------------------------------------------
if (-not (Test-Path $Profile_Main)) {
    New-Item -ItemType File -Path $Profile_Main -Force | Out-Null
    Write-Host "Created main profile file."
}

if (-not (Test-Path $Profile_AllHosts)) {
    New-Item -ItemType File -Path $Profile_AllHosts -Force | Out-Null
    Write-Host "Created AllHosts profile file."
}

# ---------------------------------------------------------
# 6. Insert import line if missing
# ---------------------------------------------------------
function Ensure-Line {
    param(
        [string]$File,
        [string]$Line
    )

    $Content = Get-Content $File -ErrorAction SilentlyContinue

    if ($Content -notcontains $Line) {
        Add-Content -Path $File -Value $Line
        Write-Host "Added import line to: $File"
    }
    else {
        Write-Host "Import line already present in: $File"
    }
}

Ensure-Line -File $Profile_Main     -Line $ImportLine
Ensure-Line -File $Profile_AllHosts -Line $ImportLine

# ---------------------------------------------------------
# 7. Set execution policy
# ---------------------------------------------------------
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force
Write-Host "Execution policy set to RemoteSigned."

# ---------------------------------------------------------
# 8. Reload profiles
# ---------------------------------------------------------
. $Profile_Main
. $Profile_AllHosts

Write-Host "Profiles reloaded."

# ---------------------------------------------------------
# 9. Verify DevXToolkit
# ---------------------------------------------------------
Write-Host "`nVerifying DevXToolkit..." -ForegroundColor Cyan

try {
    mni installer_test.md
    if (Test-Path "installer_test.md") {
        Write-Host "SUCCESS: mni executed and created installer_test.md" -ForegroundColor Green
    }
    else {
        Write-Host "ERROR: mni did not create the file." -ForegroundColor Red
    }
}
catch {
    Write-Host "ERROR: DevXToolkit did not import correctly." -ForegroundColor Red
}

Write-Host "`n=== DevXToolkit Installer v1.0 Complete ===`n" -ForegroundColor Cyan