forked from microsoft/kiota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-versions.ps1
184 lines (173 loc) · 7.02 KB
/
update-versions.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
# Get the latest version of a package from NuGet
function Get-LatestNugetVersion {
param(
[string]$packageId
)
$url = "https://api.nuget.org/v3/registration5-gz-semver2/$($packageId.ToLowerInvariant())/index.json"
$response = Invoke-RestMethod -Uri $url -Method Get
$version = $response.items | Select-Object -ExpandProperty upper | ForEach-Object { [System.Management.Automation.SemanticVersion]$_ } | sort-object | Select-Object -Last 1
$version.ToString()
}
# Get the latest github release
function Get-LatestGithubRelease {
param(
[string]$packageId
)
$packageId = $packageId.Replace("github.com/", "")
$url = "https://api.github.com/repos/$packageId/releases/latest"
$response = Invoke-RestMethod -Uri $url -Method Get
$response.tag_name
}
# Get the latest version of a npm package
function Get-LatestNpmVersion {
param(
[string]$packageId
)
$url = "https://registry.npmjs.org/$($packageId.ToLowerInvariant())/latest"
$response = Invoke-RestMethod -Uri $url -Method Get
$response.version
}
# Get the latest version of a maven package
function Get-LatestMavenVersion {
param(
[string]$packageId
)
$url = "https://repo1.maven.org/maven2/$($packageId.Replace(":", "/").Replace(".", "/").Replace("|", "."))/maven-metadata.xml"
$response = Invoke-RestMethod -Uri $url -Method Get
$response.metadata.versioning.latest
}
# Get the latest version of a composer package
function Get-LatestComposerVersion {
param(
[string]$packageId
)
$url = "https://repo.packagist.org/p2/$($packageId.ToLowerInvariant()).json"
$response = Invoke-RestMethod -Uri $url -Method Get
$response.packages.$packageId[0].version
}
# Get the latest version of a pypi package
function Get-LatestPypiVersion {
param(
[string]$packageId
)
$url = "https://pypi.org/pypi/$($packageId.ToLowerInvariant())/json"
$response = Invoke-RestMethod -Uri $url -Method Get
$response.info.version
}
# Get the latest version of a rubygem package
function Get-LatestRubygemVersion {
param(
[string]$packageId
)
$url = "https://rubygems.org/api/v1/versions/$($packageId.ToLowerInvariant())/latest.json"
$response = Invoke-RestMethod -Uri $url -Method Get
$response.version
}
function Retry-Command {
[CmdletBinding()]
Param(
[Parameter(Position = 0, Mandatory = $true)]
[scriptblock]$ScriptBlock,
[Parameter(Position = 1, Mandatory = $false)]
[int]$Maximum = 5,
[Parameter(Position = 2, Mandatory = $false)]
[int]$Delay = 2000
)
Begin {
$cnt = 0
}
Process {
do {
$cnt++
try {
# If you want messages from the ScriptBlock
# Invoke-Command -Command $ScriptBlock
# Otherwise use this command which won't display underlying script messages
$ScriptBlock.Invoke()
return
}
catch {
Write-Error $_.Exception.InnerException.Message -ErrorAction Continue
$random = Get-Random -Minimum 1 -Maximum 10
Start-Sleep -Milliseconds ($Delay * $cnt * $random)
}
} while ($cnt -lt $Maximum)
# Throw an error after $Maximum unsuccessful invocations. Doesn't need
# a condition, since the function returns upon successful invocation.
throw 'Execution failed.'
}
}
# Read the appsettings.json file
$mainSettings = Join-Path -Path $PSScriptRoot -ChildPath "..\src\kiota\appsettings.json"
$appSettings = Get-Content -Path $mainSettings -Raw | ConvertFrom-Json
foreach ($languageName in ($appSettings.Languages | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name | Sort-Object)) {
$language = $appSettings.Languages.$languageName
if ($languageName -eq "CSharp" -or $languageName -eq "CLI") {
foreach ($dependency in $language.Dependencies) {
Retry-Command -ScriptBlock {
$latestVersion = Get-LatestNugetVersion -packageId $dependency.Name
Write-Information "Updating $($dependency.Name) from $($dependency.Version) to $latestVersion"
$dependency.Version = $latestVersion
}
}
}
elseif ($languageName -eq "Go") {
foreach ($dependency in $language.Dependencies) {
Retry-Command -ScriptBlock {
$latestVersion = Get-LatestGithubRelease -packageId $dependency.Name
Write-Information "Updating $($dependency.Name) from $($dependency.Version) to $latestVersion"
$dependency.Version = $latestVersion
}
}
}
elseif ($languageName -eq "TypeScript") {
foreach ($dependency in $language.Dependencies) {
Retry-Command -ScriptBlock {
$latestVersion = Get-LatestNpmVersion -packageId $dependency.Name
Write-Information "Updating $($dependency.Name) from $($dependency.Version) to $latestVersion"
$dependency.Version = $latestVersion
}
}
}
elseif ($languageName -eq "Java") {
foreach ($dependency in $language.Dependencies) {
Retry-Command -ScriptBlock {
$latestVersion = Get-LatestMavenVersion -packageId $dependency.Name.Replace("jakarta.annotation:jakarta.annotation-api", "jakarta/annotation.jakarta|annotation-api")
Write-Information "Updating $($dependency.Name) from $($dependency.Version) to $latestVersion"
$dependency.Version = $latestVersion
}
}
}
elseif ($languageName -eq "PHP") {
foreach ($dependency in $language.Dependencies) {
Retry-Command -ScriptBlock {
$latestVersion = Get-LatestComposerVersion -packageId $dependency.Name
Write-Information "Updating $($dependency.Name) from $($dependency.Version) to $latestVersion"
$dependency.Version = $latestVersion
}
}
}
elseif ($languageName -eq "Python") {
foreach ($dependency in $language.Dependencies) {
Retry-Command -ScriptBlock {
$latestVersion = Get-LatestPypiVersion -packageId $dependency.Name
Write-Information "Updating $($dependency.Name) from $($dependency.Version) to $latestVersion"
$dependency.Version = $latestVersion
}
}
}
elseif ($languageName -eq "Ruby") {
foreach ($dependency in $language.Dependencies) {
Retry-Command -ScriptBlock {
$latestVersion = Get-LatestRubygemVersion -packageId $dependency.Name
Write-Information "Updating $($dependency.Name) from $($dependency.Version) to $latestVersion"
$dependency.Version = $latestVersion
}
}
}
else {
Write-Warning "Unsupported language $languageName you need to update it manually"
}
}
# Write the updated appsettings.json file
$appSettings | ConvertTo-Json -Depth 100 | Set-Content -Path $mainSettings