Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Function Export-VsCodeConfig {
# Get list of installed extensions
try {
$command = {
& code --list-extensions 2>$null
& code --list-extensions --show-versions 2>$null
}
$extensionsOutput = Invoke-Command -ScriptBlock $command
if ($LASTEXITCODE -ne 0) {
Expand Down
191 changes: 133 additions & 58 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,89 @@
[CmdletBinding()]
param()

Function Write-StatusMessage {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, Position=0)]
[string]$Message,
[Parameter(Mandatory=$false)]
[string]$ForegroundColor = "Gray",
[Parameter(Mandatory=$false)]
[int]$Indent = 0,
[Parameter(Mandatory=$false)]
[ValidateSet("Default", "Verbose", "Debug", "Warning", "Error")]
[string]$Verbosity = "Default",
[Parameter(Mandatory=$false)]
[int]$Width = 0,
[Parameter(Mandatory=$false)]
[switch]$NoNewLine
)

if ($Indent -gt 0) {
$Message = "$(' ' * $Indent)$Message"
}

if ($Width -gt 0) {
if($Message.Length -gt $Width) {
$Message = $Message.Substring(0, $Width - 3) + "...";
} else {
$Message = $Message.PadRight($Width, " ");
}
}

$messageParams = @{ }

if($Verbosity -eq "Default") {
$messageParams.Object = $Message
$messageParams.ForegroundColor = $ForegroundColor
$messageParams.NoNewLine = $NoNewLine.IsPresent
} else {
$messageParams.Message = $Message
}
#$messageParams.Object = $Message

switch($Verbosity) {
"Verbose" {
Write-Verbose @messageParams
}
"Debug" {
Write-Debug @messageParams
}
"Warning" {
Write-Warning @messageParams
}
"Error" {
Write-Error @messageParams
}
"Default" {
Write-Host @messageParams
}
}
}

function Center-Text($text, $width) {
$text = "$text"
$pad = $width - $text.Length
if ($pad -le 0) { return $text }
$left = [math]::Floor($pad / 2)
$right = $pad - $left
(' ' * $left) + $text + (' ' * $right)
}

function Left-Text($text, $width) {
$text = " $text"
if ($text.Length -ge $width) { return $text }
return $text + (' ' * ($width - $text.Length))
}

function Right-Text($text, $width) {
$text = "$text "
if ($text.Length -ge $width) { return $text }
return (' ' * ($width - $text.Length)) + $text
}

$successCheck = [char]0x2713

Write-Host "DevSetup Module Installer" -ForegroundColor Cyan
Write-Host "=========================" -ForegroundColor Cyan

Expand All @@ -28,148 +111,138 @@ try {

Write-Debug "DevSetup module found and verified."

# Determine the correct user modules path based on PowerShell version
$UserModulesPath = $null

if ($PSVersionTable.PSVersion.Major -ge 6) {
# PowerShell 6+ (Core)
# In PS 6+, $IsWindows, $IsLinux, $IsMacOS variables are available
if ((Get-Variable -Name "IsWindows" -ErrorAction SilentlyContinue) -and $IsWindows) {
# Windows
$UserModulesPath = Join-Path -Path $env:USERPROFILE -ChildPath "Documents\PowerShell\Modules"
} elseif ((Get-Variable -Name "IsLinux" -ErrorAction SilentlyContinue) -and $IsLinux) {
# Linux
$UserModulesPath = Join-Path -Path $env:HOME -ChildPath ".local/share/powershell/Modules"
} elseif ((Get-Variable -Name "IsMacOS" -ErrorAction SilentlyContinue) -and $IsMacOS) {
# macOS
$UserModulesPath = Join-Path -Path $env:HOME -ChildPath ".local/share/powershell/Modules"
} else {
# Fallback - assume Windows for PowerShell 6+ if platform detection fails
$UserModulesPath = Join-Path -Path $env:USERPROFILE -ChildPath "Documents\PowerShell\Modules"
}
} else {
# PowerShell 5.1 (Windows PowerShell) - always Windows
$UserModulesPath = Join-Path -Path $env:USERPROFILE -ChildPath "Documents\WindowsPowerShell\Modules"
}
# Get the PSModulePath environment variable
$psModulePath = $Env:PSModulePath

# Split the string into an array of individual paths using the platform-specific path separator
$modulePaths = $psModulePath -split [System.IO.Path]::PathSeparator

# Determine the correct user modules path based on env:PSModulePath
$UserModulesPath = ($modulePaths | Select-Object -First 1)

if (-not $UserModulesPath) {
throw "Unable to determine user modules path for PowerShell version $($PSVersionTable.PSVersion)"
}

Write-Host "PowerShell Version: $($PSVersionTable.PSVersion)" -ForegroundColor Yellow
Write-Host "PowerShell Edition: $($PSVersionTable.PSEdition)" -ForegroundColor Yellow
Write-Debug "Target user modules path: $UserModulesPath"

# Show current PSModulePath for debugging
Write-Debug "Current PSModulePath:"
($env:PSModulePath -split [IO.Path]::PathSeparator) | ForEach-Object { Write-Debug " $_" }

# Get the module version from the manifest
$ModuleVersion = $null
Write-Debug "Reading module version from manifest..."
try {
$ManifestData = Import-PowerShellDataFile -Path $ModuleManifest
$ModuleVersion = $ManifestData.ModuleVersion
if (-not $ModuleVersion) {
throw "ModuleVersion not found in manifest"
}
Write-Host "Installing DevSetup Module version: $ModuleVersion" -ForegroundColor Green
} catch {
Write-Warning "Failed to read module version from manifest: $_"
Write-Host "Using default version: 1.0.0" -ForegroundColor Yellow
$ModuleVersion = "1.0.0"
}

Write-Host "Installing DevSetup Module version: $ModuleVersion..." -ForegroundColor Yellow

Write-StatusMessage "- Checking PowerShell Version..." -Width 60 -NoNewLine -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$($PSVersionTable.PSVersion)]" 20) -ForegroundColor Green
Write-StatusMessage "- Checking PowerShell Edition..." -Width 60 -NoNewLine -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$($PSVersionTable.PSEdition)]" 20) -ForegroundColor Green

$nugetProvider = Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue

Write-StatusMessage "- Installing NuGet Package Provider..." -Width 60 -NoNewLine -ForegroundColor Gray
if ($nugetProvider) {
Write-Host "NuGet PackageProvider is already installed (version: $($nugetProvider.Version))" -ForegroundColor Green
Write-StatusMessage (Right-Text "[$($nugetProvider.Version)]" 20) -ForegroundColor Green
} else {
Write-Host "Installing NuGet PackageProvider..." -ForegroundColor Cyan
$env:__SuppressAutoNuGetProviderPrompt = 'true'
$env:POWERSHELL_UPDATECHECK = 'Off'
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser -ForceBootstrap
Install-PackageProvider -Name NuGet -Force -ForceBootstrap

# Verify installation
$nugetProvider = Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue
if ($nugetProvider) {
Write-Host "NuGet PackageProvider successfully installed (version: $($nugetProvider.Version))" -ForegroundColor Green
Write-StatusMessage (Right-Text "[$($nugetProvider.Version)]" 20) -ForegroundColor Green
} else {
throw "Failed to install NuGet PackageProvider"
}
}

# Install required module dependencies
Write-Host "Installing module dependencies..." -ForegroundColor Cyan
try {
$RequiredModules = $ManifestData.RequiredModules
if ($RequiredModules -and $RequiredModules.Count -gt 0) {
foreach ($RequiredModule in $RequiredModules) {
Write-Host "- Checking dependency: $RequiredModule" -ForegroundColor Gray

Write-StatusMessage "- Installing powershell dependency $RequiredModule..." -Width 60 -NoNewLine -ForegroundColor Gray
# Check if the module is already installed
$InstalledModule = Get-Module -ListAvailable -Name $RequiredModule -ErrorAction SilentlyContinue
if ($InstalledModule) {
Write-Host " - Already installed: $RequiredModule (version: $($InstalledModule[0].Version))" -ForegroundColor Green
Write-StatusMessage (Right-Text "[$($InstalledModule[0].Version)]" 20) -ForegroundColor Green
} else {
Write-Host " - Installing: $RequiredModule" -ForegroundColor Yellow
try {
Install-Module -Name $RequiredModule -Scope CurrentUser -Force -AllowClobber -ErrorAction Stop
Write-Host " - Successfully installed: $RequiredModule" -ForegroundColor Green
$InstalledModule = Get-Module -ListAvailable -Name $RequiredModule -ErrorAction SilentlyContinue
Write-StatusMessage (Right-Text "[$($InstalledModule[0].Version)]" 20) -ForegroundColor Green
} catch {
Write-Warning " - Failed to install $RequiredModule`: $_"
Write-Host " - You may need to install this module manually later" -ForegroundColor Yellow
Write-StatusMessage (Right-Text "[FAILED]" 20) -ForegroundColor Red
}
}
}
} else {
Write-Host "- No required modules specified in manifest" -ForegroundColor Gray
Write-StatusMessage "- No required modules specified in manifest..." -Width 60 -NoNewLine -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
}
} catch {
Write-Warning "Failed to process required modules from manifest: $_"
Write-Host "Continuing with installation..." -ForegroundColor Yellow
Write-Host "- Continuing with installation..." -ForegroundColor Gray
}

# Create the user modules directory if it doesn't exist
if (-not (Test-Path $UserModulesPath)) {
Write-Host "Creating user modules directory: $UserModulesPath" -ForegroundColor Cyan
Write-StatusMessage "- Creating user modules directory..." -Width 60 -NoNewLine -ForegroundColor Gray
New-Item -Path $UserModulesPath -ItemType Directory -Force | Out-Null
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
}

# Define the target installation path with version
$TargetModuleBasePath = Join-Path -Path $UserModulesPath -ChildPath "DevSetup"
$TargetModulePath = Join-Path -Path $TargetModuleBasePath -ChildPath $ModuleVersion

Write-Host "- Install Path: $TargetModulePath" -ForegroundColor Gray

# Remove existing installation if it exists
if (Test-Path $TargetModuleBasePath) {
Write-Host "- Removing existing DevSetup module versions..." -ForegroundColor Gray
Write-StatusMessage "- Removing existing DevSetup module versions..." -Width 60 -NoNewLine -ForegroundColor Gray
Remove-Item -Path $TargetModuleBasePath -Recurse -Force | Out-Null
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
}

# Create the versioned directory structure
Write-Host "- Creating versioned directory structure..." -ForegroundColor Gray
Write-StatusMessage "- Creating versioned directory structure..." -Width 60 -NoNewLine -ForegroundColor Gray
New-Item -Path $TargetModulePath -ItemType Directory -Force | Out-Null
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green

# Copy the DevSetup module contents to the versioned path
Write-Host "- Installing DevSetup module..." -ForegroundColor Gray
Write-StatusMessage "- Installing DevSetup module..." -Width 60 -NoNewLine -ForegroundColor Gray
Copy-Item -Path "$DevSetupModulePath\*" -Destination $TargetModulePath -Recurse -Force | Out-Null

Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green

# Verify the installation
Write-Host "- Verifying installation..." -ForegroundColor Gray
Write-StatusMessage "- Verifying installation..." -Width 60 -NoNewLine -ForegroundColor Gray

# Check if the module is now in PSModulePath
$ModuleFound = Get-Module -ListAvailable -Name "DevSetup" -ErrorAction SilentlyContinue
if ($ModuleFound) {
Write-Host "- Installation Verified..." -ForegroundColor Gray
#Write-Host "- Installation Verified..." -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
$ModuleFound | ForEach-Object {
Write-Debug " - Version: $($_.Version) at $($_.ModuleBase)"
}
} else {
Write-StatusMessage (Right-Text "[FAILED]" 20) -ForegroundColor Red
Write-Warning "DevSetup module not found in module search paths!"
Write-Host "Manual verification - checking installation path..." -ForegroundColor Yellow
Write-Host "- Manual verification - checking installation path..." -ForegroundColor Yellow
if (Test-Path (Join-Path $TargetModulePath "DevSetup.psd1")) {
Write-Host "Module files exist at target path: $TargetModulePath" -ForegroundColor Green
Write-Host " - Module files exist at target path: $TargetModulePath" -ForegroundColor Green
} else {
Write-Error "Module files not found at target path!"
}
Expand Down Expand Up @@ -199,6 +272,7 @@ try {
}

Write-Host "`nInstallation completed successfully!" -ForegroundColor Green
#Write-Host "Install Path:`n- $TargetModulePath`n" -ForegroundColor Gray
Write-Host "You can now use DevSetup commands in any PowerShell session." -ForegroundColor White

# Add the module to the current session's auto-import
Expand All @@ -209,9 +283,9 @@ try {
Write-Debug "DevSetup module loaded in current session."
}

Write-Host "`nTo get started, try:" -ForegroundColor Cyan
Write-Host " Use-DevSetup -Init" -ForegroundColor White
Write-Host " # or use the alias:" -ForegroundColor Gray
Write-Host "`nTo get started, run:" -ForegroundColor Cyan
#Write-Host " Use-DevSetup -Init" -ForegroundColor White
#Write-Host " # or use the alias:" -ForegroundColor Gray
Write-Host " devsetup -Init" -ForegroundColor White

Write-Host "`nNote: If the command isn't found in new sessions, run:" -ForegroundColor Yellow
Expand All @@ -226,5 +300,6 @@ try {
exit 1
}

Write-Host "`nPress any key to continue..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
#Write-Host "`nPress any key to continue..." -ForegroundColor Gray
#$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")