This repository has been archived by the owner on Aug 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
powershell.go
225 lines (188 loc) · 5.81 KB
/
powershell.go
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
224
225
package shells
import (
"bytes"
"fmt"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
"path"
"path/filepath"
"strings"
)
type PowerShell struct {
AbstractShell
}
type PsWriter struct {
bytes.Buffer
TemporaryPath string
indent int
}
func psQuote(text string) string {
// taken from: http://www.robvanderwoude.com/escapechars.php
text = strings.Replace(text, "`", "``", -1)
// text = strings.Replace(text, "\0", "`0", -1)
text = strings.Replace(text, "\a", "`a", -1)
text = strings.Replace(text, "\b", "`b", -1)
text = strings.Replace(text, "\f", "^f", -1)
text = strings.Replace(text, "\r", "`r", -1)
text = strings.Replace(text, "\n", "`n", -1)
text = strings.Replace(text, "\t", "^t", -1)
text = strings.Replace(text, "\v", "^v", -1)
text = strings.Replace(text, "#", "`#", -1)
text = strings.Replace(text, "'", "`'", -1)
text = strings.Replace(text, "\"", "`\"", -1)
return "\"" + text + "\""
}
func psQuoteVariable(text string) string {
text = psQuote(text)
text = strings.Replace(text, "$", "`$", -1)
return text
}
func (b *PsWriter) Line(text string) {
b.WriteString(strings.Repeat(" ", b.indent) + text + "\r\n")
}
func (b *PsWriter) CheckForErrors() {
b.checkErrorLevel()
}
func (b *PsWriter) Indent() {
b.indent++
}
func (b *PsWriter) Unindent() {
b.indent--
}
func (b *PsWriter) checkErrorLevel() {
b.Line("if(!$?) { Exit $LASTEXITCODE }")
b.Line("")
}
func (b *PsWriter) Command(command string, arguments ...string) {
list := []string{
psQuote(command),
}
for _, argument := range arguments {
list = append(list, psQuote(argument))
}
b.Line("& " + strings.Join(list, " "))
b.checkErrorLevel()
}
func (b *PsWriter) Variable(variable common.BuildVariable) {
if variable.File {
variableFile := b.Absolute(path.Join(b.TemporaryPath, variable.Key))
variableFile = helpers.ToBackslash(variableFile)
b.Line(fmt.Sprintf("md %s -Force | out-null", psQuote(helpers.ToBackslash(b.TemporaryPath))))
b.Line(fmt.Sprintf("Set-Content %s -Value %s -Encoding UTF8 -Force", psQuote(variableFile), psQuoteVariable(variable.Value)))
b.Line("$" + variable.Key + "=" + psQuote(variableFile))
} else {
b.Line("$" + variable.Key + "=" + psQuoteVariable(variable.Value))
}
b.Line("$env:" + variable.Key + "=$" + variable.Key)
}
func (b *PsWriter) IfDirectory(path string) {
b.Line("if(Test-Path " + psQuote(helpers.ToBackslash(path)) + " -PathType Container) {")
b.Indent()
}
func (b *PsWriter) IfFile(path string) {
b.Line("if(Test-Path " + psQuote(helpers.ToBackslash(path)) + " -PathType Leaf) {")
b.Indent()
}
func (b *PsWriter) IfCmd(cmd string, arguments ...string) {
cmd = cmd + " " + strings.Join(arguments, " ")
b.Line(fmt.Sprintf("if(iex %s 2>$null) {", psQuote(cmd)))
b.Indent()
}
func (b *PsWriter) Else() {
b.Unindent()
b.Line("} else {")
b.Indent()
}
func (b *PsWriter) EndIf() {
b.Unindent()
b.Line("}")
}
func (b *PsWriter) Cd(path string) {
b.Line("cd " + psQuote(helpers.ToBackslash(path)))
b.checkErrorLevel()
}
func (b *PsWriter) RmDir(path string) {
path = psQuote(helpers.ToBackslash(path))
b.Line("if( (Get-Command -Name Remove-Item2 -Module NTFSSecurity -ErrorAction SilentlyContinue) -and (Test-Path " + path + " -PathType Container) ) {")
b.Indent()
b.Line("Remove-Item2 -Force -Recurse " + path)
b.Unindent()
b.Line("} elseif(Test-Path " + path + ") {")
b.Indent()
b.Line("Remove-Item -Force -Recurse " + path)
b.Unindent()
b.Line("}")
b.Line("")
}
func (b *PsWriter) RmFile(path string) {
path = psQuote(helpers.ToBackslash(path))
b.Line("if( (Get-Command -Name Remove-Item2 -Module NTFSSecurity -ErrorAction SilentlyContinue) -and (Test-Path " + path + " -PathType Leaf) ) {")
b.Indent()
b.Line("Remove-Item2 -Force " + path)
b.Unindent()
b.Line("} elseif(Test-Path " + path + ") {")
b.Indent()
b.Line("Remove-Item -Force " + path)
b.Unindent()
b.Line("}")
b.Line("")
}
func (b *PsWriter) Print(format string, arguments ...interface{}) {
coloredText := fmt.Sprintf(format, arguments...)
b.Line("echo " + psQuoteVariable(coloredText))
}
func (b *PsWriter) Notice(format string, arguments ...interface{}) {
coloredText := fmt.Sprintf(format, arguments...)
b.Line("echo " + psQuoteVariable(coloredText))
}
func (b *PsWriter) Warning(format string, arguments ...interface{}) {
coloredText := fmt.Sprintf(format, arguments...)
b.Line("echo " + psQuoteVariable(coloredText))
}
func (b *PsWriter) Error(format string, arguments ...interface{}) {
coloredText := fmt.Sprintf(format, arguments...)
b.Line("echo " + psQuoteVariable(coloredText))
}
func (b *PsWriter) EmptyLine() {
b.Line("echo \"\"")
}
func (b *PsWriter) Absolute(dir string) string {
if filepath.IsAbs(dir) {
return dir
}
b.Line("$CurrentDirectory = (Resolve-Path .\\).Path")
return filepath.Join("$CurrentDirectory", dir)
}
func (b *PowerShell) GetName() string {
return "powershell"
}
func (b *PowerShell) GetConfiguration(info common.ShellScriptInfo) (script *common.ShellConfiguration, err error) {
script = &common.ShellConfiguration{
Command: "powershell",
Arguments: []string{"-noprofile", "-noninteractive", "-executionpolicy", "Bypass", "-command"},
PassFile: true,
Extension: "ps1",
}
return
}
func (b *PowerShell) GenerateScript(scriptType common.ShellScriptType, info common.ShellScriptInfo) (script string, err error) {
w := &PsWriter{
TemporaryPath: info.Build.FullProjectDir() + ".tmp",
}
if scriptType == common.ShellPrepareScript {
if len(info.Build.Hostname) != 0 {
w.Line("echo \"Running on $env:computername via " + psQuoteVariable(info.Build.Hostname) + "...\"")
} else {
w.Line("echo \"Running on $env:computername...\"")
}
}
err = b.writeScript(w, scriptType, info)
script = w.String()
return
}
func (b *PowerShell) IsDefault() bool {
return false
}
func init() {
common.RegisterShell(&PowerShell{})
}