2017-11-26 18:51:31 +08:00
|
|
|
[CmdletBinding()]
|
|
|
|
param()
|
|
|
|
|
|
|
|
function findExistingImportModuleDirectives([Parameter(Mandatory=$true)][string]$path)
|
|
|
|
{
|
|
|
|
if (!(Test-Path $path))
|
|
|
|
{
|
2017-12-20 08:11:20 +08:00
|
|
|
return
|
2017-11-26 18:51:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$fileContents = Get-Content $path
|
2017-12-20 08:11:20 +08:00
|
|
|
$fileContents -match 'Import-Module.+?(?=posh-vcpkg)'
|
|
|
|
return
|
2017-11-26 18:51:31 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 10:18:53 +08:00
|
|
|
$scriptsDir = split-path -parent $script:MyInvocation.MyCommand.Definition
|
2017-11-26 18:51:31 +08:00
|
|
|
|
|
|
|
$profileEntry = "Import-Module '$scriptsDir\posh-vcpkg'"
|
2020-10-28 11:48:14 +08:00
|
|
|
$profilePath = $PROFILE # Implicit PowerShell variable
|
2018-05-05 19:23:19 +08:00
|
|
|
$profileDir = Split-Path $profilePath -Parent
|
|
|
|
if (!(Test-Path $profileDir))
|
2017-11-26 18:51:31 +08:00
|
|
|
{
|
2018-05-05 19:23:19 +08:00
|
|
|
New-Item -ItemType Directory -Path $profileDir | Out-Null
|
2017-11-26 18:51:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host "`nAdding the following line to ${profilePath}:"
|
|
|
|
Write-Host " $profileEntry"
|
|
|
|
|
|
|
|
# @() Needed to force Array in PowerShell 2.0
|
|
|
|
[Array]$existingImports = @(findExistingImportModuleDirectives $profilePath)
|
|
|
|
if ($existingImports.Count -gt 0)
|
|
|
|
{
|
|
|
|
$existingImportsOut = $existingImports -join "`n "
|
|
|
|
Write-Host "`nposh-vcpkg is already imported to your PowerShell profile. The following entries were found:"
|
|
|
|
Write-Host " $existingImportsOut"
|
2020-10-28 11:48:14 +08:00
|
|
|
Write-Host "`nPlease make sure you have started a new PowerShell window for the changes to take effect."
|
2017-11-26 18:51:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-05 19:23:19 +08:00
|
|
|
# Modifying the profile will invalidate any signatures.
|
2017-11-26 18:51:31 +08:00
|
|
|
# Posh-git does the following check, so we should too.
|
|
|
|
# https://github.com/dahlbyk/posh-git/blob/master/src/Utils.ps1
|
|
|
|
# If the profile script exists and is signed, then we should not modify it
|
|
|
|
if (Test-Path $profilePath)
|
|
|
|
{
|
|
|
|
$sig = Get-AuthenticodeSignature $profilePath
|
|
|
|
if ($null -ne $sig.SignerCertificate)
|
|
|
|
{
|
|
|
|
Write-Warning "Skipping add of posh-vcpkg import to profile; '$profilePath' appears to be signed."
|
|
|
|
Write-Warning "Please manually add the line '$profileEntry' to your profile and resign it."
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Add-Content $profilePath -Value "`n$profileEntry" -Encoding UTF8
|
2020-10-28 11:48:14 +08:00
|
|
|
Write-Host "`nSuccessfully added posh-vcpkg to your PowerShell profile. Please start a new PowerShell window for the changes to take effect."
|