Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds PowerShell argument tab completion for Paket-Add #887

Merged
merged 4 commits into from
Jun 21, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ let gitRaw = environVarOrDefault "gitRaw" "https://raw.github.com/fsprojects"
let buildDir = "bin"
let tempDir = "temp"
let buildMergedDir = buildDir @@ "merged"
let buildMergedDirPS = buildDir @@ "Paket.PowerShell"


// Read additional information from the release notes document
Expand Down Expand Up @@ -140,7 +141,7 @@ Target "BuildPowerShell" (fun _ ->
let result =
ExecProcess (fun info ->
info.FileName <- Path.Combine(Environment.SystemDirectory, @"WindowsPowerShell\v1.0\powershell.exe")
info.Arguments <- "-executionpolicy bypass -noprofile -file src/Paket.PowerShell/System.Management.Automation.ps1") System.TimeSpan.MaxValue
info.Arguments <- "-executionpolicy bypass -noprofile -file src/Paket.PowerShell/System.Management.Automation.ps1") System.TimeSpan.MaxValue
if result <> 0 then failwithf "Error copying System.Management.Automation.dll"

!! solutionFilePowerShell
Expand Down Expand Up @@ -181,7 +182,7 @@ Target "MergePaketTool" (fun _ ->
)

Target "MergePowerShell" (fun _ ->
CreateDir buildMergedDir
CreateDir buildMergedDirPS

let toPack =
["paket.exe"; "Paket.Core.dll"; "FSharp.Core.dll"; "Newtonsoft.Json.dll"; "UnionArgParser.dll"; "Paket.PowerShell.dll"]
Expand All @@ -191,10 +192,19 @@ Target "MergePowerShell" (fun _ ->
let result =
ExecProcess (fun info ->
info.FileName <- currentDirectory @@ "packages" @@ "ILRepack" @@ "tools" @@ "ILRepack.exe"
info.Arguments <- sprintf "/verbose /lib:%s /out:%s %s" buildDir (buildMergedDir @@ "Paket.PowerShell.dll") toPack
info.Arguments <- sprintf "/verbose /lib:%s /out:%s %s" buildDir (buildMergedDirPS @@ "Paket.PowerShell.dll") toPack
) (TimeSpan.FromMinutes 5.)

if result <> 0 then failwithf "Error during ILRepack execution."

// copy psd1 & set version
CopyFile (buildMergedDirPS @@ "ArgumentTabCompletion.ps1") "src/Paket.PowerShell/ArgumentTabCompletion.ps1"
let psd1 = buildMergedDirPS @@ "Paket.PowerShell.psd1"
CopyFile psd1 "src/Paket.PowerShell/Paket.PowerShell.psd1"
use psd = File.AppendText psd1
psd.WriteLine ""
psd.WriteLine (sprintf "ModuleVersion = '%s'" release.AssemblyVersion)
psd.WriteLine "}"
)

Target "SignAssemblies" (fun _ ->
Expand Down
36 changes: 36 additions & 0 deletions src/Paket.PowerShell/ArgumentTabCompletion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

# http://stackoverflow.com/questions/30923696/add-custom-argument-completer-for-cmdlet

# https://github.com/mariuszwojcik/RabbitMQTools/blob/master/TabExpansions.ps1
function createCompletionResult([string]$text, [string]$value, [string]$tooltip) {
if ([string]::IsNullOrEmpty($value)) { return }
if ([string]::IsNullOrEmpty($text)) { $text = $value }
if ([string]::IsNullOrEmpty($tooltip)) { $tooltip = $value }
$completionText = @{$true="'$value'"; $false=$value }[$value -match "\W"]
$completionText = $completionText -replace '\[', '``[' -replace '\]', '``]'
New-Object System.Management.Automation.CompletionResult $completionText, $text, 'ParameterValue', $tooltip | write
}

$findPackages = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
Paket-FindPackages -SearchText $wordToComplete -Max 100 | % {
createCompletionResult $_ $_ $_ | write
}
}

$findPackageVersions = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
if (-not $fakeBoundParameter.NuGet){ return }
Paket-FindPackageVersions -Name $fakeBoundParameter.NuGet -Max 100 | % {
createCompletionResult $_ $_ $_ | write
}
}

# create and add $global:options to the list of completers
# http://www.powertheshell.com/dynamicargumentcompletion/
if (-not $global:options) { $global:options = @{CustomArgumentCompleters = @{};NativeArgumentCompleters = @{}}}

$global:options['CustomArgumentCompleters']['Paket-Add:NuGet'] = $findPackages
$global:options['CustomArgumentCompleters']['Paket-Add:Version'] = $findPackageVersions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More argument tab completers can be added following this pattern. It must be done in PowerShell. The output of another command is used to do the tab completion.


$function:tabexpansion2 = $function:tabexpansion2 -replace 'End\r\n{','End { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'
2 changes: 2 additions & 0 deletions src/Paket.PowerShell/Paket.PowerShell.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
<None Include="paket.references" />
<None Include="paket.template" />
<Compile Include="AssemblyInfo.fs" />
<None Include="Paket.PowerShell.psd1" />
<None Include="ArgumentTabCompletion.ps1" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib" />
Expand Down
12 changes: 12 additions & 0 deletions src/Paket.PowerShell/Paket.PowerShell.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@{
GUID = 'e7bdf748-93e9-4e24-95a2-ace98df3d687'
Author = 'Paket'
Copyright = '(c) 2015 Paket. All rights reserved.'
PowerShellVersion = '3.0'
CLRVersion = '4.0'
RootModule = 'Paket.PowerShell.dll'
HelpInfoUri = 'http://fsprojects.github.io/Paket/'
ScriptsToProcess = 'ArgumentTabCompletion.ps1'
# the build will append these lines
# ModuleVersion = '1.0.0'
# }
Loading