Skip to content

Commit e7fdc76

Browse files
committed
Import PowerShell profile scripts.
1 parent d9f773d commit e7fdc76

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

PowerShellProfile/newProfile.ps1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2023 Matthias Wolf, Mawosoft.
2+
3+
<#
4+
.SYNOPSIS
5+
Creates a new Powershell profile of the selected type and dot-sources the given files or copies their content.
6+
.OUTPUTS
7+
[FileInfo] object of the profile.
8+
#>
9+
10+
#Requires -Version 5.1
11+
12+
[CmdletBinding(SupportsShouldProcess)]
13+
[OutputType([System.IO.FileInfo])]
14+
param(
15+
# The paths to the scripts to dot source or copy.
16+
# If not specified, the scripts are determined automatically
17+
[ValidateNotNullOrEmpty()]
18+
[string[]]$Path,
19+
20+
# The scope of the profile to create.
21+
[ValidateSet('AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost')]
22+
[string]$Scope = 'CurrentUserAllHosts',
23+
24+
# Whether to dot source or copy the script into the profile.
25+
[ValidateSet('DotSource', 'SetContent')]
26+
[string]$Mode = 'DotSource',
27+
28+
# Force overwriting an existing script
29+
[switch]$Force
30+
)
31+
32+
Set-StrictMode -Version 3.0
33+
34+
if (-not $Path) {
35+
$Path = @("$PSScriptRoot/profileCommon.ps1")
36+
if (Test-Path 'env:VBOX_MSI_INSTALL_PATH') {
37+
$Path += "$PSScriptRoot/profileVBoxHost.ps1"
38+
}
39+
if (Test-Path 'C:\Program Files\Microsoft Visual Studio\2022') {
40+
$Path += "$PSScriptRoot/profileDeveloper.ps1"
41+
}
42+
43+
}
44+
45+
[System.Collections.ArrayList]$contents = @()
46+
47+
if ($Mode -eq 'DotSource') {
48+
foreach ($_path in $Path) {
49+
$_path = Resolve-Path $_path
50+
Write-Verbose "Dot-sourcing $_path"
51+
$null = $contents.Add(". '$_path'")
52+
}
53+
}
54+
else {
55+
foreach ($_path in $Path) {
56+
# Also ensures $Path is valid
57+
$_path = Resolve-Path $_path
58+
Write-Verbose "Copying content $_path"
59+
$null = $contents.Add((Get-Content $_path -Raw))
60+
}
61+
}
62+
63+
[string]$profilePath = $PROFILE.$($Scope)
64+
65+
New-Item -Path $profilePath -Value ($contents -join [System.Environment]::NewLine) -Force:$Force

PowerShellProfile/profileCommon.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) 2023 Matthias Wolf, Mawosoft.
2+
3+
<#
4+
.SYNOPSIS
5+
Lists all Powershell profiles and tests if they exist.
6+
.OUTPUTS
7+
[PSObject[]] with profile infos
8+
#>
9+
function global:Get-Profile {
10+
[CmdletBinding()]
11+
[OutputType([psobject[]])]
12+
param()
13+
14+
foreach ($scope in ('AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost')) {
15+
[pscustomobject]@{
16+
Scope = $scope
17+
Path = $PROFILE.$($scope)
18+
Exists = Test-Path -Path $PROFILE.$($scope) -PathType Leaf
19+
}
20+
}
21+
}
22+
23+
<#
24+
.SYNOPSIS
25+
# Invokes a batch file (.bat, .cmd) and imports the environment variables it has set.
26+
.NOTES
27+
TODO
28+
- proper use of %comspec% or cmd definition
29+
- preserve working dir, psmodulepath
30+
- Batch parameters and other goodies
31+
#>
32+
function global:Import-BatchEnvironment {
33+
param (
34+
# The batch file to run
35+
[Parameter(Mandatory)]
36+
[ValidateNotNullOrEmpty()]
37+
[string]$Path
38+
)
39+
40+
[string]$tempfile = New-TemporaryFile
41+
cmd /c " ""$Path"" && set >""$tempfile"" "
42+
Get-Content $tempfile | ForEach-Object {
43+
[int]$i = $_.IndexOf('=', [System.StringComparison]::Ordinal)
44+
if ($i -gt 0) {
45+
Set-Content "env:$($_.Substring(0, $i))" $_.Substring($i + 1)
46+
}
47+
}
48+
49+
Remove-Item $tempfile -ErrorAction Ignore
50+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright (c) 2023 Matthias Wolf, Mawosoft.
2+
3+
# Visual Studio alias
4+
5+
if (Test-Path 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\devenv.exe') {
6+
Set-Alias -Scope Global -Name 'vstudio' -Value 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\devenv.exe'
7+
}
8+
9+
if (Test-Path 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat') {
10+
Set-Alias -Scope Global -Name 'vsdevcmd' -Value 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat'
11+
Set-Alias -Scope Global -Name 'ipvsd' -Value 'Import-VsDevCmd'
12+
13+
function global:Import-VsDevCmd {
14+
<#
15+
.SYNOPSIS
16+
Imports the VsDevCmd environment variables
17+
#>
18+
[CmdletBinding()] param()
19+
Import-BatchEnvironment (Get-Alias 'vsdevcmd').Definition
20+
}
21+
}
22+
23+
# dotnet Tab completion
24+
# See https://learn.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete
25+
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
26+
param($commandName, $wordToComplete, $cursorPosition)
27+
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
28+
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
29+
}
30+
}
31+
32+
33+
<#
34+
.SYNOPSIS
35+
Sets MSBuild environment variables to auto-generate binlogs
36+
#>
37+
function global:Set-BinlogEnvironment {
38+
[CmdletBinding()]
39+
param(
40+
# Binlog will include all environment variables, not just used ones.
41+
[switch]$LogAllEnvVars,
42+
43+
# Directory path to store the binlogs, defaults to .\MSBuild_Logs
44+
[ValidateNotNullOrEmpty()]
45+
[string]$Path
46+
)
47+
48+
$env:MSBuildDebugEngine = 1
49+
# Fix incomplete binlogs in MSBuild <=17.3.x. See https://github.com/mawosoft/Mawosoft.Extensions.BenchmarkDotNet/issues/146
50+
$env:MSBUILDLOGTASKINPUTS = 1
51+
$env:MSBUILDTARGETOUTPUTLOGGING = 'true'
52+
$env:MSBUILDLOGIMPORTS = 1
53+
$env:MSBUILDLOGALLENVIRONMENTVARIABLES = if ($LogAllEnvVars) { 'true' } else { $null }
54+
$env:MSBUILDDEBUGPATH = if ($Path) { $Path } else { $null }
55+
}
56+
57+
<#
58+
.SYNOPSIS
59+
Clears the MSBuild environment variables for binlog auto-generation
60+
#>
61+
function global:Clear-BinlogEnvironment {
62+
[CmdletBinding()]
63+
param()
64+
65+
$env:MSBuildDebugEngine = $null
66+
$env:MSBUILDLOGTASKINPUTS = $null
67+
$env:MSBUILDTARGETOUTPUTLOGGING = $null
68+
$env:MSBUILDLOGIMPORTS = $null
69+
$env:MSBUILDLOGALLENVIRONMENTVARIABLES = $null
70+
$env:MSBUILDDEBUGPATH = $null
71+
}

PowerShellProfile/profileVBoxHost.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2023 Matthias Wolf, Mawosoft.
2+
3+
# VirtualBox aliases
4+
5+
if (Test-Path 'env:VBOX_MSI_INSTALL_PATH') {
6+
Set-Alias -Scope 'Global' -Name 'vbi' -Value (Join-Path $env:VBOX_MSI_INSTALL_PATH 'vbox-img.exe')
7+
Set-Alias -Scope 'Global' -Name 'vbm' -Value (Join-Path $env:VBOX_MSI_INSTALL_PATH 'VBoxManage.exe')
8+
}

0 commit comments

Comments
 (0)