-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathISEGit.psm1
223 lines (197 loc) · 6.43 KB
/
ISEGit.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Needed for Enable/Disable-ISEGitPrompt
$oldPrompt = Get-Content Function:\prompt
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
Set-Content Function:\prompt -Value $oldPrompt
}
$isISE = $host.Name -eq 'Windows PowerShell ISE Host'
if ($isISE) {
if (-not ($Menu = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus |
Where-Object { $_.DisplayName -eq 'ISEGit' })) {
$Menu = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('ISEGit', $null, $null)
}
}
function Resolve-ISEPath {
param (
[switch]$Child
)
$status = {
[CmdletBinding()]
param (
$Path = '.'
)
Push-Location $Path
git.exe status --porcelain
Pop-Location
}
if ($psISE.CurrentFile) {
$FileOpen = $true
$FilePath = Split-Path $psISE.CurrentFile.FullPath
$FileName = Split-Path $psISE.CurrentFile.FullPath -Leaf
& $status -ErrorVariable ScriptPane -ErrorAction SilentlyContinue -Path $FilePath |
Out-Null
}
& $status -ErrorVariable ConsolePane -ErrorAction SilentlyContinue -Path $pwd.ProviderPath |
Out-Null
if ($Child) {
# Start with edited file...
if ((-not $ScriptPane) -and $FileOpen) {
New-Object PSObject -Property @{
Name = $FileName
Path = $FilePath
}
} elseif (-not $ConsolePane) {
New-Object PSobject -Property @{
Name = '*'
Path = $pwd.ProviderPath
}
}
} else {
if (-not $ConsolePane) {
New-Object PSObject -Property @{
Name = '*'
Path = $pwd.ProviderPath
}
} elseif ((-not $ScriptPane) -and $FileOpen) {
New-Object PSobject -Property @{
Name = $FileName
Path = $FilePath
}
}
}
}
function Get-IseInput {
[OutputType('System.String')]
param (
[string]$Prompt,
[string]$Title = 'Enter data requested'
)
# We are in ISE - so WPF is already "there"... ;)
$XAML = [xml](Get-Content $PSScriptRoot\Input.xaml)
$Reader = New-Object System.Xml.XmlNodeReader $XAML
$Dialog = [Windows.Markup.XamlReader]::Load($Reader)
$Dialog.Title = $Title
foreach ($Name in (
Select-Xml '//*/@Name' -Xml $XAML |
foreach { $_.Node.Value}
)) {
New-Variable -Name "Control_$Name" -Value $Dialog.FindName($Name)
}
$Control_Prompt.Text = $Prompt
$Control_OK.Add_Click({
Set-Variable -Scope 1 -Name Output -Value $Control_Value.Text
$Dialog.Close()
})
$Control_Cancel.Add_Click({
$Dialog.Close()
})
$Dialog.ShowDialog() | Out-Null
$Output
}
function Get-ISEGitStatus {
$Path = (Resolve-IsePath).Path
if ($Path) {
Get-GitStatus -Path $Path
}
}
function Add-GitMenuItem {
param (
[string]$DisplayName,
[scriptblock]$ScriptBlock,
[string]$Key
)
if (-not ($Menu.Submenus | Where-Object { $_.DisplayName -eq $DisplayName }) -and $isISE) {
try {
$Menu.Submenus.Add(
$DisplayName,
$ScriptBlock,
$Key
)
} catch {
$Menu.Submenus.Add(
$DisplayName,
$ScriptBlock,
$null
)
}
}
}
function Add-ISEGitItem {
$Data = Resolve-IsePath -Child
if ($Data.Path) {
Add-GitItem -Path $Data.Path -Name $Data.Name
}
}
function Checkpoint-ISEGitProject {
$Data = Resolve-IsePath -Child
if ($Data.Path) {
$Message = Get-IseInput -Prompt (
'Please enter commit message. File(s): {0} Project: {1}' -f $Data.Name, $Data.Path
) -Title 'Commit message required!'
Checkpoint-GitProject -Path $Data.Path -Name $Data.Name -Message $Message
}
}
function New-ISEGitBranch {
$Path = (Resolve-IsePath).Path
if ($Path) {
$Name = Get-IseInput -Title 'Name of branch needed' -Prompt 'Provide the name for new branch'
if ($Name) {
New-GitBranch -Name $Name -Path $Path
}
}
}
function Get-ISEGitBranch {
$Path = (Resolve-IsePath).Path
if ($Path) {
Get-GitBranch -Name * -Path $Path
}
}
function Merge-ISEGitBranch {
$Path = (Resolve-IsePath).Path
if ($Path) {
$Name = Get-IseInput -Title 'Name of branch needed' -Prompt 'Provide the name of the branch to merge'
if ($Name) {
Merge-GitBranch -Name $Name -Path $Path
}
}
}
function Remove-ISEGitBranch {
param ([switch]$Force)
$Path = (Resolve-IsePath).Path
if ($Path) {
$Name = Get-IseInput -Title 'Name of branch needed' -Prompt 'Provide the name of the branch to be removed'
if ($Name) {
Remove-GitBranch -Name $Name -Path $Path -Force:$Force
}
}
}
function Push-ISEGitProject {
$Path = (Resolve-IsePath).Path
if ($Path) {
Push-GitProject -Path $Path
}
}
function Enable-ISEGitPrompt {
function global:prompt {
Write-Host '<# ' -ForegroundColor Gray -NoNewline
Write-GitPrompt
Write-host ' #>' -NoNewLine -ForegroundColor Gray
& ([scriptblock]::Create($oldPrompt))
}
}
function Disable-ISEGitPrompt {
function global:prompt {
& ([scriptblock]::Create($oldPrompt))
}
}
Add-GitMenuItem -DisplayName Status -ScriptBlock {Get-ISEGitStatus} -Key CTRL+SHIFT+S
Add-GitMenuItem -DisplayName Add -ScriptBlock {Add-ISEGitItem} -Key CTRL+SHIFT+A
Add-GitMenuItem -DisplayName Commit -ScriptBlock {Checkpoint-ISEGitProject} -Key CTRL+SHIFT+C
Add-GitMenuItem -DisplayName 'New branch' -ScriptBlock {New-ISEGitBranch} -Key CTRL+SHIFT+B
Add-GitMenuItem -DisplayName 'Get branches' -ScriptBlock {Get-ISEGitBranch} -Key $null
Add-GitMenuItem -DisplayName 'Merge branch' -ScriptBlock {Merge-ISEGitBranch} -Key $null
Add-GitMenuItem -DisplayName 'Remove branch' -ScriptBlock {Remove-ISEGitBranch} -Key $null
Add-GitMenuItem -DisplayName 'Remove branch (forced)' -ScriptBlock {Remove-ISEGitBranch -Force} -Key $null
Add-GitMenuItem -DisplayName Push -ScriptBlock {Push-ISEGitProject} -Key CTRL+SHIFT+P
Add-GitMenuItem -DisplayName 'Enable prompt' -ScriptBlock {Enable-ISEGitPrompt} -Key CTRL+E
Add-GitMenuItem -DisplayName 'Disable prompt' -ScriptBlock {Disable-ISEGitPrompt} -Key CTRL+SHIFT+E
Export-ModuleMember -Function * -Alias *