From d26a6b067c24e324b111849c320bdc4cf681e713 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Sun, 26 Nov 2017 02:51:31 -0800 Subject: Add `vcpkg integrate powershell` for tab completion --- scripts/addPoshVcpkgToPowershellProfile.ps1 | 55 +++++++++++++++++++++++++++++ toolsrc/src/vcpkg/commands.integrate.cpp | 12 +++++-- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 scripts/addPoshVcpkgToPowershellProfile.ps1 diff --git a/scripts/addPoshVcpkgToPowershellProfile.ps1 b/scripts/addPoshVcpkgToPowershellProfile.ps1 new file mode 100644 index 000000000..92a7573e4 --- /dev/null +++ b/scripts/addPoshVcpkgToPowershellProfile.ps1 @@ -0,0 +1,55 @@ +[CmdletBinding()] +param() + +function findExistingImportModuleDirectives([Parameter(Mandatory=$true)][string]$path) +{ + if (!(Test-Path $path)) + { + return $false + } + + $fileContents = Get-Content $path + return $fileContents -match 'Import-Module.+?(?=posh-vcpkg)' +} + +$scriptsDir = split-path -parent $MyInvocation.MyCommand.Definition +. "$scriptsDir\VcpkgPowershellUtils.ps1" + +$profileEntry = "Import-Module '$scriptsDir\posh-vcpkg'" +$profilePath = $PROFILE # Implicit powershell variable +if (!(Test-Path $profilePath)) +{ + $profileDir = Split-Path $profilePath -Parent + vcpkgCreateDirectoryIfNotExists $profileDir +} + +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" + Write-Host "`nPlease make sure you have started a new Powershell window for the changes to take effect." + return +} + +# 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 +Write-Host "`nSuccessfully added posh-vcpkg to your PowerShell profile. Please start a new Powershell window for the changes to take effect." diff --git a/toolsrc/src/vcpkg/commands.integrate.cpp b/toolsrc/src/vcpkg/commands.integrate.cpp index 66c5eb5a9..460e99b88 100644 --- a/toolsrc/src/vcpkg/commands.integrate.cpp +++ b/toolsrc/src/vcpkg/commands.integrate.cpp @@ -324,18 +324,20 @@ With a project open, go to Tools->NuGet Package Manager->Package Manager Console " vcpkg integrate install Make installed packages available user-wide. Requires admin privileges on " "first use\n" " vcpkg integrate remove Remove user-wide integration\n" - " vcpkg integrate project Generate a referencing nuget package for individual VS project use\n"; + " vcpkg integrate project Generate a referencing nuget package for individual VS project use\n" + " vcpkg integrate powershell Enable PowerShell Tab-Completion\n"; namespace Subcommand { static const std::string INSTALL = "install"; static const std::string REMOVE = "remove"; static const std::string PROJECT = "project"; + static const std::string POWERSHELL = "powershell"; } static std::vector valid_arguments(const VcpkgPaths&) { - return {Subcommand::INSTALL, Subcommand::REMOVE, Subcommand::PROJECT}; + return {Subcommand::INSTALL, Subcommand::REMOVE, Subcommand::PROJECT, Subcommand::POWERSHELL}; } const CommandStructure COMMAND_STRUCTURE = { @@ -365,6 +367,12 @@ With a project open, go to Tools->NuGet Package Manager->Package Manager Console { return integrate_project(paths); } + if (args.command_arguments[0] == Subcommand::POWERSHELL) + { + System::powershell_execute("PowerShell Tab-Completion", + paths.scripts / "addPoshVcpkgToPowershellProfile.ps1"); + Checks::exit_success(VCPKG_LINE_INFO); + } #endif Checks::exit_with_message(VCPKG_LINE_INFO, "Unknown parameter %s for integrate", args.command_arguments[0]); -- cgit v1.2.3 From 1a9a14b3ce456289c2a4523079e0104dc17a9758 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Mon, 4 Dec 2017 16:01:16 -0800 Subject: [autocomplete] Add info in README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index bf7d7b0ea..9ff217564 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,14 @@ Finally, create a New Project (or open an existing one) in Visual Studio 2017 or For CMake projects, simply include our toolchain file. See our [using a package](docs/examples/using-sqlite.md) example for the specifics. +## Tab-Completion / Auto-Completion +`Vcpkg` supports auto-completion of commands, package names, options etc. To enable tab-completion in Powershell, use +``` +.\vcpkg integrate powershell +``` +and restart Powershell. + + ## Examples See the [documentation](docs/index.md) for specific walkthroughs, including [using a package](docs/examples/using-sqlite.md) and [adding a new package](docs/examples/packaging-zlib.md). -- cgit v1.2.3