-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
vsix.ps1
388 lines (316 loc) · 15.1 KB
/
vsix.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# VSIX Module for AppVeyor by Mads Kristensen
[cmdletbinding()]
param()
$vsixUploadEndpoint = "https://www.vsixgallery.com/api/upload"
function Vsix-PushArtifacts {
[cmdletbinding()]
param (
[Parameter(Position=0, Mandatory=0,ValueFromPipeline=$true)]
[string]$path = "./*.vsix",
[switch]$publishToGallery
)
process {
foreach($filePath in $path) {
$fileNames = (Get-ChildItem $filePath -Recurse)
foreach($vsixFile in $fileNames)
{
if (Get-Command Update-AppveyorBuild -errorAction SilentlyContinue)
{
Write-Host ("Pushing artifact " + $vsixFile.Name + "...") -ForegroundColor Cyan -NoNewline
Push-AppveyorArtifact ($vsixFile.FullName) -FileName $vsixFile.Name -DeploymentName "Latest build"
Write-Host "OK" -ForegroundColor Green
}
if ($publishToGallery -and $vsixFile)
{
Vsix-PublishToGallery $vsixFile.FullName
}
}
}
}
}
function Vsix-GetRepoUrl{
[cmdletbinding()]
param ()
if ($env:APPVEYOR_REPO_PROVIDER -contains "github"){
$repoUrl = "https://github.com/" + $env:APPVEYOR_REPO_NAME + "/"
} elseif ($env:APPVEYOR_REPO_PROVIDER -contains "bitbucket"){
$repoUrl = "https://bitbucket.org/" + $env:APPVEYOR_REPO_NAME + "/"
} else {
$repoUrl = ""
}
return $repoUrl
}
function Vsix-PublishToGallery{
[cmdletbinding()]
param (
[Parameter(Position=0, Mandatory=0,ValueFromPipeline=$true)]
[string[]]$path = "./*.vsix"
)
foreach($filePath in $path){
if ($env:APPVEYOR_PULL_REQUEST_NUMBER){
return
}
$repo = ""
$issueTracker = ""
$repoUrl = Vsix-GetRepoUrl
if ($baseRepoUrl -ne "") {
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
$repo = [System.Web.HttpUtility]::UrlEncode($repoUrl)
$issueTracker = [System.Web.HttpUtility]::UrlEncode(($repoUrl + "issues/"))
}
'Publish to VSIX Gallery...' | Write-Host -ForegroundColor Cyan -NoNewline
$fileNames = (Get-ChildItem $filePath -Recurse)
foreach($vsixFile in $fileNames)
{
[string]$url = ($vsixUploadEndpoint + "?repo=" + $repo + "&issuetracker=" + $issueTracker)
[byte[]]$bytes = [System.IO.File]::ReadAllBytes($vsixFile)
try {
$webclient = New-Object System.Net.WebClient
$webclient.UploadFile($url, $vsixFile) | Out-Null
'OK' | Write-Host -ForegroundColor Green
}
catch{
'FAIL' | Write-Error
$_.Exception.Response.Headers["x-error"] | Write-Error
}
}
}
}
function Vsix-UpdateBuildVersion {
[cmdletbinding()]
param (
[Parameter(Position=0, Mandatory=1,ValueFromPipelineByPropertyName=$true)]
[Version[]]$version,
[Parameter(Position=1,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
$vsixFilePath,
[switch]$updateOnPullRequests
)
process{
if ($updateOnPullRequests -or !$env:APPVEYOR_PULL_REQUEST_NUMBER){
foreach($ver in $version) {
if (Get-Command Update-AppveyorBuild -errorAction SilentlyContinue)
{
Write-Host "Updating AppVeyor build version..." -ForegroundColor Cyan -NoNewline
Update-AppveyorBuild -Version $ver | Out-Null
$ver | Write-Host -ForegroundColor Green
}
}
}
$vsixFilePath
}
}
function Vsix-IncrementVsixVersion {
[cmdletbinding()]
param (
[Parameter(Position=0, Mandatory=0,ValueFromPipeline=$true)]
[string[]]$manifestFilePath = ".\source.extension.vsixmanifest",
[Parameter(Position=1, Mandatory=0)]
[int]$buildNumber = $env:APPVEYOR_BUILD_NUMBER,
[ValidateSet("build","revision")]
[Parameter(Position=2, Mandatory=0)]
[string]$versionType = "build",
[switch]$updateBuildVersion
)
process {
foreach($manifestFile in $manifestFilePath)
{
"Incrementing VSIX version..." | Write-Host -ForegroundColor Cyan -NoNewline
$matches = (Get-ChildItem $manifestFile -Recurse)
$vsixManifest = $matches[$matches.Count - 1] # Get the last one which matches the top most file in the recursive matches
[xml]$vsixXml = Get-Content $vsixManifest
$ns = New-Object System.Xml.XmlNamespaceManager $vsixXml.NameTable
$ns.AddNamespace("ns", $vsixXml.DocumentElement.NamespaceURI) | Out-Null
$attrVersion = ""
if ($vsixXml.SelectSingleNode("//ns:Identity", $ns)){ # VS2012 format
$attrVersion = $vsixXml.SelectSingleNode("//ns:Identity", $ns).Attributes["Version"]
}
elseif ($vsixXml.SelectSingleNode("//ns:Version", $ns)){ # VS2010 format
$attrVersion = $vsixXml.SelectSingleNode("//ns:Version", $ns)
}
[Version]$version = $attrVersion.Value
if (!$attrVersion.Value){
$version = $attrVersion.InnerText
}
if ($versionType -eq "build"){
$version = New-Object Version ([int]$version.Major),([int]$version.Minor),$buildNumber
}
elseif ($versionType -eq "revision"){
$version = New-Object Version ([int]$version.Major),([int]$version.Minor),([System.Math]::Max([int]$version.Build, 0)),$buildNumber
}
$attrVersion.InnerText = $version
$vsixXml.Save($vsixManifest) | Out-Null
$version.ToString() | Write-Host -ForegroundColor Green
if ($updateBuildVersion -and $env:APPVEYOR_BUILD_VERSION -ne $version.ToString())
{
Vsix-UpdateBuildVersion $version | Out-Null
}
# return the values to the pipeline
New-Object PSObject -Property @{
'vsixFilePath' = $vsixManifest
'Version' = $version
}
}
}
}
function Vsix-IncrementNuspecVersion {
[cmdletbinding()]
param (
[Parameter(Position=0, Mandatory=0)]
[string[]]$nuspecFilePath = ".\**\*.nuspec",
[Parameter(Position=1, Mandatory=0)]
[Version]$buildVersion = $env:APPVEYOR_BUILD_VERSION
)
process {
foreach($nuspecFile in $nuspecFilePath)
{
"Incrementing Nuspec version..." | Write-Host -ForegroundColor Cyan -NoNewline
$matches = (Get-ChildItem $nuspecFile -Recurse)
$nuspec = $matches[$matches.Count - 1] # Get the last one which matches the top most file in the recursive matches
[xml]$vsixXml = Get-Content $nuspec
$ns = New-Object System.Xml.XmlNamespaceManager $vsixXml.NameTable
$ns.AddNamespace("ns", $vsixXml.DocumentElement.NamespaceURI) | Out-Null
$elmVersion = $vsixXml.SelectSingleNode("//ns:version", $ns)
$elmVersion.InnerText = $buildVersion
$vsixXml.Save($nuspec) | Out-Null
$buildVersion.ToString() | Write-Host -ForegroundColor Green
# return the values to the pipeline
New-Object PSObject -Property @{
'vsixFilePath' = $nuspec
'Version' = $version
}
}
}
}
function Vsix-TokenReplacement {
[cmdletbinding()]
param (
[Parameter(Position=0, Mandatory=$true)]
[string]$FilePath,
[Parameter(Position=1, Mandatory=$true)]
[string]$searchString,
[Parameter(Position=2, Mandatory=$true)]
[string]$replacement
)
process {
$replacement = $replacement.Replace("{version}", $env:APPVEYOR_BUILD_VERSION)
"Replacing $searchString with $replacement..." | Write-Host -ForegroundColor Cyan -NoNewline
$content = [string]::join([environment]::newline, (get-content $FilePath))
$regex = New-Object System.Text.RegularExpressions.Regex $searchString
$regex.Replace($content, $replacement) | Out-File $FilePath
"OK" | Write-Host -ForegroundColor Green
}
}
function Vsix-CreateChocolatyPackage {
[cmdletbinding()]
param (
[Parameter(Position=0, Mandatory=0)]
[string[]]$manifestFilePath = ".\source.extension.vsixmanifest",
[Parameter(Position=1, Mandatory=1)]
[string]$packageId
)
process {
if ([String]::IsNullOrEmpty($pacakgeId)){
$error = New-Object System.ArgumentNullException "packageID is null or empty"
}
foreach($manifestFile in $manifestFilePath)
{
"Creating Cholocatey package..." | Write-Host -ForegroundColor Cyan -NoNewline
$matches = (Get-ChildItem $manifestFile -Recurse)
$vsixManifest = $matches[$matches.Count - 1] # Get the last one which matches the top most file in the recursive matches
$vsixManifestDirectory = Split-Path -Parent -Path $vsixManifest
$vsixFile = Get-ChildItem -Path $vsixManifestDirectory -Filter '*.vsix' -Recurse | Select-Object -First 1
$hash = $vsixFile | Get-FileHash -Algorithm SHA256 | Select-Object -ExpandProperty Hash
[xml]$vsixXml = Get-Content $vsixManifest
$ns = New-Object System.Xml.XmlNamespaceManager $vsixXml.NameTable
$ns.AddNamespace("ns", $vsixXml.DocumentElement.NamespaceURI) | Out-Null
$id = ""
$version = ""
$author = ""
$displayName = ""
$description = ""
$tags = ""
$icon = ""
$preview = ""
$repoUrl = Vsix-GetRepoUrl
if ($vsixXml.SelectSingleNode("//ns:Identity", $ns)){ # VS2012 format
$id = $vsixXml.SelectSingleNode("//ns:Identity", $ns).Attributes["Id"].Value
$version = $vsixXml.SelectSingleNode("//ns:Identity", $ns).Attributes["Version"].Value
$author = $vsixXml.SelectSingleNode("//ns:Identity", $ns).Attributes["Publisher"].Value
$displayName = $vsixXml.SelectSingleNode("//ns:DisplayName", $ns).InnerText
$description = $vsixXml.SelectSingleNode("//ns:Description", $ns).InnerText
$tags = $vsixXml.SelectSingleNode("//ns:Tags", $ns).InnerText
$Icon = $vsixXml.SelectSingleNode("//ns:Tags", $ns).InnerText
$PreviewImage = $vsixXml.SelectSingleNode("//ns:Tags", $ns).InnerText
}
elseif ($vsixXml.SelectSingleNode("//ns:Version", $ns)){ # VS2010 format
$id = $vsixXml.SelectSingleNode("//ns:Identity", $ns).Attributes["Id"].Value
$version = $vsixXml.SelectSingleNode("//ns:Version", $ns).InnerText
$author = $vsixXml.SelectSingleNode("//ns:Author", $ns).InnerText
$displayName = $vsixXml.SelectSingleNode("//ns:Name", $ns).InnerText
$description = $vsixXml.SelectSingleNode("//ns:Description", $ns).InnerText
$tags = $vsixXml.SelectSingleNode("//ns:Tags", $ns).InnerText
$Icon = $vsixXml.SelectSingleNode("//ns:Tags", $ns).InnerText
$PreviewImage = $vsixXml.SelectSingleNode("//ns:Tags", $ns).InnerText
}
[System.IO.DirectoryInfo]$folder = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), ".vsixbuild", "$id")
[System.IO.Directory]::CreateDirectory($folder.FullName) | Out-Null
$XmlWriter = New-Object System.XMl.XmlTextWriter(($folder.FullName + "\chocolatey.nuspec"), (New-Object System.Text.UTF8Encoding))
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement("package")
$XmlWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd")
$xmlWriter.WriteStartElement("metadata")
$XmlWriter.WriteElementString("id", $packageId)
$XmlWriter.WriteElementString("version", $version)
$XmlWriter.WriteElementString("title", $displayName)
$XmlWriter.WriteElementString("description", $description)
$XmlWriter.WriteElementString("authors", $author)
$XmlWriter.WriteElementString("owners", $author)
$XmlWriter.WriteElementString("tags", "visualstudio extension vsix")
$XmlWriter.WriteElementString("licenseUrl", "http://vsixgallery.com/extension/" + $id + "/")
$XmlWriter.WriteElementString("projectUrl", "http://vsixgallery.com/extension/" + $id + "/")
$XmlWriter.WriteElementString("iconUrl", "http://vsixgallery.com/extensions/" + $id + "/icon.png")
$XmlWriter.WriteElementString("packageSourceUrl", $repoUrl)
$XmlWriter.WriteStartElement("dependencies")
$XmlWriter.WriteStartElement("dependency")
$XmlWriter.WriteAttributeString("id", "chocolatey-visualstudio.extension")
$XmlWriter.WriteAttributeString("version", "1.6.0")
$XmlWriter.WriteEndElement() # dependency
$XmlWriter.WriteEndElement() # dependencies
$XmlWriter.WriteEndElement() # metadata
$XmlWriter.WriteStartElement("files")
$XmlWriter.WriteStartElement("file")
$XmlWriter.WriteAttributeString("src", "chocolateyInstall.ps1")
$XmlWriter.WriteAttributeString("target", "tools")
$XmlWriter.WriteEndElement() # file
$XmlWriter.WriteEndElement() # files
$xmlWriter.WriteEndElement() # package
$xmlWriter.WriteEndDocument()
$XmlWriter.Flush()
$XmlWriter.Dispose()
$sb = New-Object System.Text.StringBuilder
$sb.AppendLine("`$name = `'" + $displayName + "`'") | Out-Null
$sb.AppendLine("`$url = `'" + "https://vsixgallery.azurewebsites.net/extensions/" + $id + "/" + $displayName + ".vsix`'") | Out-Null
$sb.AppendLine("`$checksum = `'" + $hash + "`'") | Out-Null
$sb.AppendLine("`$checksumType = `'SHA256`'") | Out-Null
$sb.AppendLine("Install-VisualStudioVsixExtension `$name `$url -Checksum `$checksum -ChecksumType `$checksumType") | Out-Null
New-Item ($folder.FullName + "\chocolateyInstall.ps1") -type file -force -value $sb.ToString() | Out-Null
Push-Location $folder.FullName
try{
& choco pack | Out-Null
Write-Host "OK" -ForegroundColor Green
if (Get-Command Update-AppveyorBuild -errorAction SilentlyContinue)
{
$nupkg = Get-ChildItem $folder.FullName -Filter *.nupkg
Write-Host ("Pushing Chocolatey package " + $nupkg.Name + "...") -ForegroundColor Cyan -NoNewline
Push-AppveyorArtifact ($nupkg.FullName) -FileName $nupkg.Name -DeploymentName "Chocolatey package"
Write-Host "OK" -ForegroundColor Green
}
}
finally{
Pop-Location
}
}
}
}