forked from stevencohn/OneMore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-releases.ps1
More file actions
234 lines (193 loc) · 7.77 KB
/
Copy pathget-releases.ps1
File metadata and controls
234 lines (193 loc) · 7.77 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
230
231
232
233
234
<#
Gets the release history of OneMore from GitHub, including prerelease versions.
By default, only the most recent 20 releases are fetched, but the -All switch can be used to
fetch all releases.
.PARAMETER All
Include all releases, including prerelease versions.
By default, only the most recent 20 releases are included.
.PARAMETER Released
Only include released versions, excluding prerelease versions.
.PARAMETER Monthly
Report estimated downloads per calendar month instead of per release. Implies -All since a
meaningful timeline needs the full release history. Each release's total downloads are divided
evenly across the days it was the current version (its Age), and those daily estimates are
summed into calendar months.
#>
using namespace System.Net.Http
using namespace System.Net.Http.Headers
[CmdletBinding()]
param(
[switch]$Released,
[switch]$All,
[switch]$Monthly
)
Begin
{
$releases = [System.Collections.Generic.List[Release]]::new()
class Release {
[string]$Tag
[string]$Name
[datetime]$Published
[int]$Age
[bool]$Prerelease
[int]$Downloads64
[int]$Downloads86
[int]$DownloadsArm
}
function GetReleases
{
param(
[System.Collections.Generic.List[Release]]$Releases,
[int]$Page,
[int]$PerPage
)
Write-Host "... fetching page $Page"
$url = "https://api.github.com/repos/stevencohn/OneMore/releases?page=$Page&per_page=$PerPage"
$count = 0
$client = [HttpClient]::new()
$client.DefaultRequestHeaders.UserAgent.Add(
[ProductInfoHeaderValue]::new("OneMore", "1.0")
)
$response = $client.GetAsync($url).GetAwaiter().GetResult()
if ($response.IsSuccessStatusCode)
{
$json = $response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
$dom = $json | ConvertFrom-Json
foreach ($rel in $dom)
{
if ($Released -and $rel.prerelease) { continue }
$published = [datetime]::Parse(
$rel.published_at,
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::RoundtripKind
).ToLocalTime()
$release = [Release]::new()
$release.Tag = $rel.tag_name
$release.Name = $rel.name
$release.Published = $published
$release.Prerelease = $rel.prerelease
foreach ($asset in $rel.assets)
{
$name = $asset.name
if ($name -match "ARM") { $release.DownloadsArm = $asset.download_count }
elseif ($name -match "64") { $release.Downloads64 = $asset.download_count }
else { $release.Downloads86 = $asset.download_count }
}
$Releases.Add($release)
$count++
}
}
else
{
Write-Warning "Status code: $($response.StatusCode)"
}
return $count -gt 0
}
function PatchAges
{
# prereleases age against whatever immediately superseded them
# stable releases age against the next stable release, skipping any prereleases
$today = Get-Date
for ($i = 0; $i -lt $releases.Count; $i++)
{
if ($releases[$i].Prerelease)
{
$releases[$i].Age =
if ($i -eq 0) { ($today - $releases[$i].Published).Days }
else { ($releases[$i - 1].Published - $releases[$i].Published).Days }
}
else
{
$nextStable = -1
for ($j = $i - 1; $j -ge 0; $j--)
{
if (-not $releases[$j].Prerelease) { $nextStable = $j; break }
}
$releases[$i].Age =
if ($nextStable -eq -1) { ($today - $releases[$i].Published).Days }
else { ($releases[$nextStable].Published - $releases[$i].Published).Days }
}
}
}
function Report
{
$tagW = [Math]::Max("Tag".Length, ($releases | foreach { $_.Tag.Length } | measure -Maximum).Maximum)
$nameW = [Math]::Max("Name".Length, ($releases | foreach { $_.Name.Length } | measure -Maximum).Maximum)
$pubW = [Math]::Max("Published".Length, ($releases | foreach { $_.Published.ToString().Length } | measure -Maximum).Maximum)
$ageW = [Math]::Max("Age".Length, ($releases | foreach { "$($_.Age)".Length } | measure -Maximum).Maximum)
$dl64W = [Math]::Max("Downloads64".Length, ($releases | foreach { "$($_.Downloads64)".Length } | measure -Maximum).Maximum)
$dl86W = [Math]::Max("Downloads86".Length, ($releases | foreach { "$($_.Downloads86)".Length } | measure -Maximum).Maximum)
$dlArmW = [Math]::Max("DownloadsArm".Length, ($releases | foreach { "$($_.DownloadsArm)".Length } | measure -Maximum).Maximum)
if ($nameW -gt 60) { $nameW = 60 }
$fmt = "{0,-$tagW} {1,-$nameW} {2,-$pubW} {3,$ageW} {4,$dl64W} {5,$dl86W} {6,$dlArmW}"
Write-Host
Write-Host ($fmt -f "Tag", "Name", "Published", "Age", "Downloads64", "Downloads86", "DownloadsArm") -fo Green
Write-Host ($fmt -f ("-" * $tagW), ("-" * $nameW), ("-" * $pubW), ("-" * $ageW), ("-" * $dl64W), ("-" * $dl86W), ("-" * $dlArmW))
foreach ($r in $releases)
{
$name = $r.Name
if ($name.Length -gt 60) { $name = $name.Substring(0, 57) + "..." }
$line = $fmt -f $r.Tag, $name, $r.Published.ToString(), $r.Age, $r.Downloads64, $r.Downloads86, $r.DownloadsArm
if ($r.Prerelease) {
Write-Host $line -ForegroundColor DarkGray
} else {
Write-Host $line
}
}
}
function MonthlyReport
{
# spread each release's total downloads evenly across the days it was the
# current version (its Age) and accumulate into calendar-month buckets
$months = [ordered]@{}
foreach ($r in $releases)
{
$total = $r.Downloads64 + $r.Downloads86 + $r.DownloadsArm
$days = [Math]::Max($r.Age, 1)
$rate = $total / $days
for ($d = 0; $d -lt $days; $d++)
{
$key = $r.Published.AddDays($d).ToString("yyyy-MM")
if ($months.Contains($key)) { $months[$key] += $rate }
else { $months[$key] = $rate }
}
}
$keys = $months.Keys | Sort-Object -Descending | Select-Object -First 12
$rows = $keys | foreach { [PSCustomObject]@{ Month = $_; Downloads = [Math]::Round($months[$_]) } }
$monW = [Math]::Max("Month".Length, ($rows | foreach { $_.Month.Length } | measure -Maximum).Maximum)
$dlW = [Math]::Max("EstDownloads".Length, ($rows | foreach { "$($_.Downloads)".Length } | measure -Maximum).Maximum)
$fmt = "{0,-$monW} {1,$dlW}"
Write-Host
Write-Host ($fmt -f "Month", "EstDownloads") -fo Green
Write-Host ($fmt -f ("-" * $monW), ("-" * $dlW))
foreach ($row in $rows)
{
Write-Host ($fmt -f $row.Month, $row.Downloads)
}
}
}
Process
{
if ($All -or $Monthly)
{
$page = 1
while (GetReleases $releases $page 100)
{
$page++
}
if ($All) { Write-Host "`nUse the Results panel Export menu to export to Excel" }
}
else
{
GetReleases $releases 1 20 | Out-Null
}
PatchAges
if ($Monthly)
{
MonthlyReport
}
else
{
Report
}
}