Skip to content

Commit

Permalink
Supporting Posh.Profiles
Browse files Browse the repository at this point in the history
Fixes #40, Fixes #57, Fixes #58, Fixes #59, Fixes #60
  • Loading branch information
James Brundage committed Jul 20, 2023
1 parent 64e8c24 commit 9200a2d
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Formatting/Posh/Posh.Profiles.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Write-FormatView -TypeName Posh.Profiles -Action {
Write-FormatViewExpression -ScriptBlock {
Show-Markdown -InputObject $_.README
}

Write-FormatViewExpression -Newline
Write-FormatViewExpression -Newline

Write-FormatViewExpression -Text "The current profile is:"

Write-FormatViewExpression -Newline
Write-FormatViewExpression -Newline


Write-FormatViewExpression -Style Italic -Property Current

Write-FormatViewExpression -Newline
Write-FormatViewExpression -Newline


Write-FormatViewExpression -Style 'Foreground.Cyan' -ScriptBlock {
"To add to your profile, use `$posh.Profile.Add()"
}

Write-FormatViewExpression -Newline

Write-FormatViewExpression -Style 'Foreground.Cyan' -ScriptBlock {
"To remove from your profile, use `$posh.Profile.Remove()"
}
}
36 changes: 36 additions & 0 deletions Types/Posh.Profiles/Add.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<#
.SYNOPSIS
Adds content to a profile
.DESCRIPTION
Adds content to a PowerShell profile
#>
param(
# The content to add to your profile.
[ScriptBlock]
$Content,

# Which profile should the content be added to?
# The default is the current user's current host.
[ValidateSet('AllUsersAllHosts','AllUsersCurrentHosts','CurrentUserAllHosts','CurrentUserCurrentHost')]
[string]
$WhichProfile = 'CurrentUserCurrentHost'
)

$profilePath = $PROFILE.$WhichProfile

$profileContent =
if (Test-Path $profilePath) {
Get-Content -Raw $profilePath
} else {
''
}

if ($profileContent -like "*$Content*") {
return
}

$profileContent += [Environment]::NewLine
$profileContent += "$content"
$profileContent | Set-Content -Path $profilePath -PassThru


7 changes: 7 additions & 0 deletions Types/Posh.Profiles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PowerShell has profiles!

Profiles run whenever PowerShell is loaded.

They're great if there's a module you want to always load, or a variable you always want defined.

To see how to manipulate PowerShell profiles with Posh, use $posh.Profiles | Get-Member
41 changes: 41 additions & 0 deletions Types/Posh.Profiles/Remove.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<#
.SYNOPSIS
Removes content from a profile
.DESCRIPTION
Removes content from a PowerShell profile.
#>
param(
# The content to remove.
# If the content is a regular expression pattern, any matches will be removed.
# Otherwise, any case-insensitive occurances of the string will be removed.
$Content,

# Which profile should the content be removed from?
# The default is the current user's current host.
[ValidateSet('AllUsersAllHosts','AllUsersCurrentHosts','CurrentUserAllHosts','CurrentUserCurrentHost')]
[string]
$WhichProfile = 'CurrentUserCurrentHost'
)

$profilePath = $PROFILE.$WhichProfile

$profileContent =
if (Test-Path $profilePath) {
Get-Content -Raw $profilePath
} else {
''
}

if ($Content -is [regex]) {
$profileContent = $Content.Replace($profileContent, '')
} else {
$Content = "$content"
if ($Content) {
$profileContent =
$profileContent.Replace(
$content, '', [StringComparison]::OrdinalIgnoreCase
)
}
}

$profileContent | Set-Content -Path $profilePath -PassThru
9 changes: 9 additions & 0 deletions Types/Posh.Profiles/get_Current.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<#
.SYNOPSIS
Gets the current Profile
.DESCRIPTION
Gets the current PowerShell Profile
.EXAMPLE
$posh.Profile.Current
#>
"$profile"
13 changes: 13 additions & 0 deletions Types/Posh.Profiles/get_Files.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<#
.SYNOPSIS
Gets the profile files
.DESCRIPTION
Gets the FileInfo objects for each profile that exists.
.EXAMPLE
$posh.Profile.Files
#>
foreach ($potentialPath in $profile.CurrentUserCurrentHost, $profile.CurrentUserAllHosts, $profile.AllUsersAllHosts, $profile.AllUsersCurrentHost) {
if ([IO.File]::Exists($potentialPath)) {
[IO.FileInfo]$potentialPath
}
}

0 comments on commit 9200a2d

Please sign in to comment.