-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Submit-Gist.ps1
118 lines (95 loc) · 2.74 KB
/
Submit-Gist.ps1
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
<#
.Synopsis
Submits a file to its GitHub gist repository.
Author: Roman Kuzmin
.Description
Requirements:
* The gist exists and you are the owner.
* Git client is installed, configured, and available in the path.
* Use PowerShell.exe, git may require some console based interaction.
The script uses the local gist repository $HOME\gist-{GistId}. If it does
not exist then it is cloned. Then the file is copied to this repository.
Then git `add`, `status`, `commit`, and `push` are invoked.
A just cloned local gist repository is removed after submission unless the
switch Keep is used. An existing local repository is not removed.
See also Update-Gist.ps1 which has its pros and cons.
.Parameter FileName
The file to be submitted (existing is updated, new is added).
.Parameter GistId
The existing gist ID. If it is not specified then the script searches
for a gist URL in the file, the first matching URL is used for the ID.
The expected URL is either
https://gist.github.com/user/gist-id
or
https://gist.github.com/gist-id
.Parameter Keep
Tells to keep the local gist repository.
.Inputs
None.
.Outputs
git output.
.Link
https://github.com/nightroman/PowerShelf
#>
param(
[Parameter(Mandatory=1)]
[string]$FileName,
[string]$GistId,
[switch]$Keep
)
trap {$PSCmdlet.ThrowTerminatingError($_)}
$ErrorActionPreference = 'Stop'
function exec($command) {
. $command
if ($LASTEXITCODE) {throw "Command {$command} exited with code $LASTEXITCODE."}
}
$FileName = Resolve-Path -LiteralPath $FileName
# extract the gist ID from the file
if (!$GistId) {
foreach($_ in Get-Content -LiteralPath $FileName) {
if ($_ -match 'https://gist.github.com/\w+/(\w+)' -or $_ -match 'https://gist.github.com/(\w+)') {
$GistId = $matches[1]
break
}
}
if (!$GistId) {throw 'GistId is not specified and the file does not contain the gist URL.'}
}
$repo = "gist-$GistId"
Push-Location -LiteralPath $HOME
try {
# clone the repository
if (Test-Path -LiteralPath $repo) {
$Keep = $true
}
else {
exec { git clone "https://gist.github.com/$GistId.git" $repo }
}
Push-Location -LiteralPath $repo
try {
# copy the file to the repository
Copy-Item -LiteralPath $FileName . -Force
# add
exec { git add . }
# status
$status = exec { git status -s }
# nothing?
if (!$status) {
Write-Host -ForegroundColor Cyan "Nothing is changed."
return
}
# commit
exec { git commit -m ([System.IO.Path]::GetFileName($FileName)) }
# push
exec { git push }
}
finally {
Pop-Location
# remove the local repository
if (!$Keep) {
Remove-Item -LiteralPath $repo -Force -Recurse
}
}
}
finally {
Pop-Location
}