-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate-Profile.ps1
More file actions
145 lines (124 loc) · 4.83 KB
/
Copy pathUpdate-Profile.ps1
File metadata and controls
145 lines (124 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function Update-Profile
{
<#
.SYNOPSIS
Updates PowerShell profile to the latest version.
.DESCRIPTION
Updates the PowerShell profile by performing a git pull from the remote repository.
If Git is not available, provides instructions for manual update using the install script.
Requires a restart of the PowerShell session after updating to reload the profile.
Compatible with PowerShell Desktop 5.1+ on Windows, macOS, and Linux.
.EXAMPLE
PS > Update-Profile
Updating PowerShell profile...
Profile updated successfully! Restart your PowerShell session to reload your profile.
Updates the profile from the git repository.
.NOTES
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/ProfileManagement/Update-Profile.ps1
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/ProfileManagement/Update-Profile.ps1
.LINK
https://github.com/jonlabelle/pwsh-profile
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
[CmdletBinding()]
param(
[Parameter(DontShow = $true)]
[String]$ProfileRoot
)
begin
{
Write-Verbose 'Starting Update-Profile'
}
process
{
Write-Host 'Updating PowerShell profile...' -ForegroundColor Cyan
# Check if Git is available
$gitCommand = Get-Command -Name git -ErrorAction SilentlyContinue
if (-not $gitCommand)
{
$psExecutable = if ($PSVersionTable.PSVersion.Major -lt 6) { 'powershell' } else { 'pwsh' }
Write-Host ''
Write-Host 'Git is not installed or not found in PATH.' -ForegroundColor Yellow
Write-Host 'To update your profile without Git, use the install.ps1 script:' -ForegroundColor Yellow
Write-Host ''
Write-Host " irm 'https://raw.githubusercontent.com/jonlabelle/pwsh-profile/main/install.ps1' |" -ForegroundColor Cyan
Write-Host " $psExecutable -NoProfile -ExecutionPolicy Bypass -" -ForegroundColor Cyan
Write-Host ''
Write-Host 'This will download and install the latest profile version.' -ForegroundColor Gray
return
}
$gitExecutable = if ($gitCommand.Source)
{
$gitCommand.Source
}
elseif ($gitCommand.Path)
{
$gitCommand.Path
}
else
{
$gitCommand.Name
}
# Resolve the repo root from this script's known location (Functions/ProfileManagement/)
$profileDirectory = if ([String]::IsNullOrWhiteSpace($ProfileRoot))
{
[System.IO.Path]::GetFullPath((Join-Path -Path $PSScriptRoot -ChildPath (Join-Path -Path '..' -ChildPath '..')))
}
else
{
[System.IO.Path]::GetFullPath($ProfileRoot)
}
# Verify the directory is a git repository
$gitDir = Join-Path -Path $profileDirectory -ChildPath '.git'
if (-not (Test-Path -Path $gitDir))
{
Write-Error "Profile directory '$profileDirectory' is not a git repository. Cannot update."
return
}
try
{
$commitBefore = & $gitExecutable -C $profileDirectory rev-parse HEAD 2>$null
$gitOutput = & $gitExecutable -C $profileDirectory pull --rebase 2>&1
if ($LASTEXITCODE -ne 0)
{
$gitOutput | ForEach-Object { Write-Host $_ }
Write-Error "git pull failed with exit code $LASTEXITCODE."
return
}
$commitAfter = & $gitExecutable -C $profileDirectory rev-parse HEAD 2>$null
}
catch
{
Write-Error "An error occurred while updating the profile: $($_.Exception.Message)"
return
}
if ($commitBefore -eq $commitAfter)
{
Write-Host 'Profile is already up to date.' -ForegroundColor Green
}
else
{
$gitLog = & $gitExecutable -C $profileDirectory log --oneline "${commitBefore}..${commitAfter}" 2>$null
if ($gitLog)
{
Write-Host ''
Write-Host 'Updates:' -ForegroundColor Cyan
Write-Host ''
foreach ($line in $gitLog)
{
$cleanLine = $line -replace '^\w+\s+', '' -replace '\s*\([^)]+\)\s*', ''
Write-Host " - $cleanLine" -ForegroundColor Gray
}
Write-Host ''
}
Write-Host 'Profile updated successfully! Restart your PowerShell session to reload your profile.' -ForegroundColor Green
}
}
end
{
Write-Verbose 'Update-Profile completed'
}
}