-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSet-PublisherInfo.ps1
More file actions
229 lines (189 loc) · 8.7 KB
/
Copy pathSet-PublisherInfo.ps1
File metadata and controls
229 lines (189 loc) · 8.7 KB
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
<#
.SYNOPSIS
Updates the extension manifest id, name, build task display info and task id guids.
.DESCRIPTION
Azure DevOps extension developers have a couple hurdles to overcome.
Every build task that is uploaded must have a unique guid id.
To upload a test extension, all of the guid's must be replaced, and they must
be replaced reliably with the same values.
When doing co-development with multiple developers, they need to be able to
upload, install, and run their extensions side by side. Adding identifying info
to the extension name and build tasks names makes this possible.
For YAML builds, it's important that the task names are unique, otherwise you have to
add the full namespace of the task, instead of easy names just like "UseDotNet".
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path -Path $_ -PathType Leaf})]
[String] $ManifestPath,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path -Path $_ -PathType Container})]
[String] $StagingDirectory,
[Parameter(Mandatory=$false)]
[String] $PublishersDirectory,
[Parameter(Mandatory=$false)]
[String] $PublisherName
)
Write-Host 'Setting publisher info...'
Import-Module (Join-Path $PSScriptRoot 'ConvertTo-Hashtable.psm1')
Import-Module (Join-Path $PSScriptRoot 'Test-VersionString.psm1')
if ([String]::IsNullOrWhiteSpace($PublisherName))
{
$PublisherName = $env:USERNAME
}
if ([String]::IsNullOrWhiteSpace($PublishersDirectory))
{
$PublishersDirectory = Join-Path $PSScriptRoot '.publishers'
}
if (-not (Test-Path -Path $PublishersDirectory -PathType 'Container'))
{
throw [System.IO.DirectoryNotFoundException] "Could not find publishers directory: $PublishersDirectory"
}
Write-Host "Inputs:"
Write-Host " ManifestPath = $ManifestPath"
Write-Host " StagingDirectory = $StagingDirectory"
Write-Host " PublisherName = $PublisherName"
Write-Host " PublishersDirectory = $PublishersDirectory"
$updateExtensionVersionFilePath = Join-Path $PSScriptRoot 'Update-ExtensionVersion.ps1'
$updateBuildTaskVersionFilePath = Join-Path $PSScriptRoot 'Update-BuildTaskVersion.ps1'
Write-Host "Reading the manifest file: $ManifestPath"
$manifest = Get-Content -Path $ManifestPath | ConvertFrom-Json
$publisherFilePath = Join-Path $PublishersDirectory "$($PublisherName.ToLower())-publishers.json"
Write-Host "Publisher file: $publisherFilePath"
if (Test-Path -Path $publisherFilePath -PathType 'Leaf')
{
# Basically we have two invalid task.json files where we have a key name of "" (the empty string), so ConvertFrom-Json throws.
# We can't change them, because that will break the build tasks that are already deployed using that value. This appears to
# be an issue with ConvertFrom-Json and not the JSON itself.
$publisherInfoRaw = Get-Content -Path $publisherFilePath | Foreach-Object -Process { $_ -replace '""\s*:', '" ":'} | ConvertFrom-Json
$publisherInfo = ConvertTo-Hashtable $publisherInfoRaw
}
# Now that we have our PSCustomObject with our known publisher information, let's see if we have anything saved.
if ($publisherInfo)
{
if ($publisherInfo.version -eq $null)
{
$publisherInfo.version = $manifest.Version
$publisherInfo.count = 0
}
else
{
if ($publisherInfo.version -eq $manifest.Version)
{
$publisherInfo.count += 1
}
elseif (Test-VersionString -Version $publisherInfo.Version -LessThan $manifest.Version)
{
# Reset the version and count
$publisherInfo.version = $manifest.Version
$publisherInfo.count = 0
}
else
{
$publisherInfo.count += 1
Write-Warning 'Manifest version is less than the publishers.json version'
Write-Warning "Manifest Version = $($manifest.Version)"
Write-Warning "Publishers Version = $($publisherInfo.version)"
}
}
}
else # Create a new publisher
{
# We have 'publisher' as a separate field, in case the test Azure DevOps account name (the publisher) is
# different than the username.
$publisherInfo = @{
publisher = 'ms-secdevops-test'
publisherName = $PublisherName
extensionId = "$($manifest.id)-$($PublisherName)"
mapping = @{ }
count = 0
version = $manifest.Version
}
Write-Host "Saving new publisher file: $publisherFilePath"
$publisherInfo | ConvertTo-Json -Depth 10 | Out-File -Encoding utf8 -NoNewline -Force -FilePath $publisherFilePath
}
Write-Host 'Ensuring all tasks have entries in publishers.json'
$srcTaskNamesSearchPatern = Join-Path $PSScriptRoot '../'
$srcTaskNamesSearchPatern = Join-Path $srcTaskNamesSearchPatern 'src'
$srcTaskNamesSearchPatern = Join-Path $srcTaskNamesSearchPatern '*'
$taskNames = Get-ChildItem -Path $srcTaskNamesSearchPatern -Directory | Select -ExpandProperty Name
$newTaskIds = $false
foreach ($taskName in $taskNames)
{
if ($taskName -ne 'node_modules' -and -not $publisherInfo.mapping.$taskName)
{
Write-Host "Missing task '$taskName' for publisher '$PublisherName'"
$publisherInfo.mapping.$taskName = [Guid]::NewGuid().Guid
Write-Host " New task id = $($publisherInfo.mapping.$taskName)"
$newTaskIds = $true;
}
}
# Write the publisher map
Write-Host "Saving publisher file: $publisherFilePath"
$publisherInfo | ConvertTo-Json -Depth 10 | Out-File -Encoding utf8 -NoNewline -Force -FilePath $publisherFilePath
# Look for "id" : "<value>" with any amount of whitespace between the key ("id") and its value
# We're going to use a regex here, instead of loading the json and updating it, since we have the
# empty-string-name issue in some of our task.json files.
$idPattern = '"id"\s*:\s*"[^"]+"'
$namePattern = '"name"\s*:\s*"([^"]+)"'
$friendlyNamePattern = '"friendlyName"\s*:\s*"([^"]+)"'
$instanceNamePattern = '"instanceNameFormat"\s*:\s*"([^"]+)"'
Write-Host 'Processing staged task.json files'
$stagedTaskNamesSearchPattern = Join-Path $StagingDirectory '*'
$taskNames = Get-ChildItem -Path $stagedTaskNamesSearchPattern -Directory | Select -ExpandProperty Name
foreach ($taskName in $taskNames)
{
# Handle up to 1 level deep for versioned tasks
$taskFiles = Get-ChildItem -Path "$($StagingDirectory)\$($taskName)\" -Include @('task.json', 'task.loc.json') -Depth 1 | Select -ExpandProperty FullName
foreach ($taskFile in $taskFiles)
{
& $updateBuildTaskVersionFilePath -FilePath $taskFile -Patch -Count $publisherInfo.count
$contents = Get-Content -Path $taskFile
if ($publisherInfo.mapping.$taskName)
{
$replacement = '"id": "' + $publisherInfo.mapping.$taskName + '"'
}
else
{
Write-Warning "Extra build task '$($taskName)' found in staging folder, cleanup your build results."
$replacement = ($contents | Select-String $idPattern).Line.Trim()
}
$contents = $contents | Foreach-Object -Process { $_ -replace $idPattern, $replacement }
for ($idx = 0; $idx -lt $contents.Length; $idx++)
{
if ($contents[$idx] -match $namePattern)
{
$contents[$idx] = $contents[$idx].replace($Matches[1], "$PublisherName$($Matches[1])")
break
}
}
# Change the names of the tasks to have '[PublisherName]' at the end.
# There are multiple 'name' fields, we want to change the first one
for ($idx = 0; $idx -lt $contents.Length; $idx++)
{
if ($contents[$idx] -match $friendlyNamePattern)
{
$contents[$idx] = $contents[$idx].replace($Matches[1], "$($Matches[1]) [$($PublisherName)]")
break
}
}
for ($idx = 0; $idx -lt $contents.Length; $idx++)
{
if ($contents[$idx] -match $instanceNamePattern)
{
$contents[$idx] = $contents[$idx].replace($Matches[1], "$($Matches[1]) [$($PublisherName)]")
break
}
}
$contents | Out-File -Force -FilePath $taskFile -Encoding utf8
}
}
Write-Host 'All staged task.json files updated'
$manifest.id = $publisherInfo.extensionId
$manifest.name = "$PublisherName - $($manifest.name)"
$manifest.publisher = $publisherInfo.publisher
$manifest | ConvertTo-Json -Depth 99 | Out-File -Encoding utf8 -Force -FilePath $ManifestPath
Write-Host "Manifest file updated with info for publisher: $($publisherInfo.publisher)"
& $updateExtensionVersionFilePath -FilePath $ManifestPath -Rev -Count $publisherInfo.count