forked from jessehouwing/azure-pipelines-variable-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.ps1
163 lines (131 loc) · 4.41 KB
/
publish.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
[cmdletbinding()]
param(
[string[]] $Items = @(".\vsts-extension-packageandpublish"),
[switch] $Force = $false,
[switch] $Package = $false,
[switch] $PublishLocal = $false,
[switch] $PublishMarket = $false,
[switch] $Release = $false,
[string] $ServiceUrl = "https://jessehouwing.visualstudio.com/DefaultCollection"
)
if ((Get-Command "tfx" -ErrorAction SilentlyContinue) -eq $null)
{
[Environment]::SetEnvironmentVariable("Path", "$($env:Path);node_modules\.bin\", "Process")
}
$patchMarkerName = "task.json.lastpatched"
$uploadMarkerName = "task.json.lastuploaded"
$packagedMarkerName = ".lastpackaged"
$publisherId = "jessehouwing"
$extensionId = "jessehouwing-vsts-extension-tasks"
if (-not $Release.IsPresent)
{
$extensionId = "$extensionId-TEST"
}
cd -Path $PSScriptRoot
if ($Force)
{
Write-Warning "Force specified"
}
function Update-TaskVersion
{
param(
[Parameter(Mandatory=$true)]
[string] $TaskPath
)
$result = $false
$patch = $Force -or {
$item = Get-ChildItem $TaskPath\*.* -Recurse | ?{ -not ("$($_.Name)" -eq "$uploadMarkerName") } | Sort {$_.LastWriteTime} | select -last 1
return -not ("$($item.Name)" -eq "$patchMarkerName")
}
if ($patch)
{
$taskJson = ConvertFrom-Json (Get-Content "$TaskPath\task.json" -Raw)
$taskJson.Version.Patch = $taskJson.Version.Patch + 1
$taskjson | ConvertTo-JSON -Depth 255 | Out-File "$TaskPath\task.json" -Force -Encoding ascii
New-Item -Path $TaskPath -Name $patchMarkerName -ItemType File -Force | Out-Null
$result = $true
Write-Output "Updated version to $($taskJson.Version)"
}
return $result
}
function Publish-TaskLocally
{
param(
[Parameter(Mandatory=$true)]
[string] $TaskPath
)
$result = $false
$publish = $Force -or {
$item = Get-ChildItem $TaskPath -Recurse | Sort {$_.LastWriteTime} | select -last 1
return -not ("$($item.Name)" -eq "$uploadMarkerName")
}
if ($publish)
{
tfx build tasks upload --task-path $TaskPath --service-url $ServiceUrl
New-Item -Path $TaskPath -Name $uploadMarkerName -ItemType File -Force | Out-Null
$result = $true
Write-Output Published
}
return $result
}
function Publish-ExtensionToMarket
{
param(
)
$result = $false
$extensionJson = ConvertFrom-Json (Get-Content ".\vss-extension.json" -Raw)
$Version = [System.Version]::Parse($extensionJson.Version)
$versionString = $Version.ToString(3)
tfx extension publish --vsix "$publisherId.$extensionId$versionString.vsix" --service-url https://app.market.visualstudio.com
return $result
}
function Package-Extension
{
$result = $false
$patch = $Force -or {
$item = Get-ChildItem *.* -Recurse | ?{ -not ("$($_.Name)" -eq "$packagedMarkerName") } | Sort {$_.LastWriteTime} | select -last 1
return -not ("$($item.Name)" -eq "$packagedMarkerName")
}
if ($patch)
{
$extensionJson = ConvertFrom-Json (Get-Content ".\vss-extension.json" -Raw)
$Version = [System.Version]::Parse($extensionJson.Version)
$Version = New-Object System.Version -ArgumentList $Version.Major, $Version.Minor, ($Version.Build + 1)
$extensionJson.Version = $Version.ToString(3)
$extensionJson.Id = "$extensionId"
$extensionJson.Public = $Release.IsPresent
$extensionJson | ConvertTo-JSON -Depth 255 | Out-File ".\vss-extension.json" -Force -Encoding ascii
New-Item -Path . -Name $packagedMarkerName -ItemType File -Force | Out-Null
$result = $true
Write-Output "Updated version to $($extensionJson.Version)"
}
tfx extension create --root . --output-path . --manifest-globs vss-extension.json
return $result
}
$updated = $false
foreach ($Item in $Items)
{
if (Test-Path $Item)
{
Write-Output "Processing: $Item"
Copy-Item .\vsts-*-shared\*.psm1 .\$item -Force
$taskUpdated = Update-TaskVersion -TaskPath $item
$updated = ($updated -or $taskUpdated)
if ($taskUpdated -and $PublishLocal)
{
$published = Publish-TaskLocally -TaskPath $Item
}
}
else
{
Write-Error "Path not found: $Item"
}
}
if (($updated -or $Force) -and $Package)
{
$packaged = Package-Extension
if ($packaged -and $PublishMarket)
{
Publish-ExtensionToMarket
}
}