-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-FHLatestRelease.ps1
36 lines (32 loc) · 1.6 KB
/
Get-FHLatestRelease.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
. "$PSScriptRoot\Get-FirstRegexGroupValue.ps1"
function Get-FHLatestRelease {
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$SearchTerm,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$Filter,
[switch]$DownloadUri
)
$FHWebUri = 'https://www.fosshub.com'
$FHApiUri = 'https://api.fosshub.com'
Invoke-RestMethod -UseBasicParsing -Method GET -Uri "$FHApiUri/search/?q=$SearchTerm" | Select-Object -ExpandProperty data | Where-Object {$_.name -eq $SearchTerm } | ForEach-Object {
$Response = Invoke-WebRequest -Uri "$FHWebUri/$($_.uri)"
$ReleaseID = $Response.Content | Select-String -Pattern ('r":"([0-9a-zA-Z]+)') | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Groups | Select-Object -ExpandProperty Value -Last 1
$FileName = $Response.Content | Select-String -Pattern ("n`":`"($Filter)`"") | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Groups | Select-Object -ExpandProperty Value -Last 1
if ($DownloadUri.IsPresent) {
$BodyData = @{
"projectId" = $_._id
"releaseId" = $ReleaseID
"projectUri" = $_.uri
"fileName" = $FileName
"isLatestVersion" = $true
}
Invoke-RestMethod -Uri "$FHApiUri/download" -Method POST -Body $BodyData | Select-Object -ExpandProperty data | Select-Object -ExpandProperty url
}
else {
($FileName | Get-FirstRegexGroupValue -Pattern '([0-9\.]{2,})').Trim('.')
}
}
}