Skip to content

Commit

Permalink
Merge pull request #887 from ctaggart/tab
Browse files Browse the repository at this point in the history
adds PowerShell argument tab completion for Paket-Add
  • Loading branch information
forki committed Jun 21, 2015
2 parents b978d48 + b0e7f1a commit 18f5cb4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 60 deletions.
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

$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

0 comments on commit 18f5cb4

Please sign in to comment.