|
| 1 | +function Get-MendeleyReferenceManager { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Get the current version and download URL for Mendeley Reference Manager. |
| 5 | + #> |
| 6 | + [OutputType([System.Management.Automation.PSObject])] |
| 7 | + [CmdletBinding(SupportsShouldProcess = $false)] |
| 8 | + param ( |
| 9 | + [Parameter(Mandatory = $false, Position = 0)] |
| 10 | + [ValidateNotNull()] |
| 11 | + [System.Management.Automation.PSObject] |
| 12 | + $res = (Get-FunctionResource -AppName ("$($MyInvocation.MyCommand)".Split("-"))[1]) |
| 13 | + ) |
| 14 | + |
| 15 | + # Get the latest update info |
| 16 | + $UpdateYaml = Invoke-EvergreenRestMethod -Uri $res.Get.Update.Uri |
| 17 | + |
| 18 | + # Build a PSCustomObject from the YAML content |
| 19 | + if ($null -ne $UpdateYaml) { |
| 20 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Converting YAML content to object." |
| 21 | + |
| 22 | + $lines = $UpdateYaml -split "`r?`n" |
| 23 | + $result = @{ |
| 24 | + Version = $null |
| 25 | + ReleaseDate = $null |
| 26 | + Downloads = @() |
| 27 | + } |
| 28 | + $currentDownload = $null |
| 29 | + $inDownloadsSection = $false |
| 30 | + foreach ($line in $lines) { |
| 31 | + # Skip empty lines and comments |
| 32 | + if ([System.String]::IsNullOrWhiteSpace($line) -or $line.TrimStart() -match '^#') { |
| 33 | + continue |
| 34 | + } |
| 35 | + |
| 36 | + # Extract version (various common formats) |
| 37 | + if ($line -match '^\s*version\s*:\s*[''"]?([^''"]+)[''"]?\s*$') { |
| 38 | + $result.Version = $matches[1].Trim() |
| 39 | + } |
| 40 | + |
| 41 | + # Extract release date (various common formats) |
| 42 | + if ($line -match '^\s*(?:release_?date|releaseDate|date)\s*:\s*[''"]?([^''"]+)[''"]?\s*$') { |
| 43 | + $result.ReleaseDate = $matches[1].Trim() |
| 44 | + } |
| 45 | + |
| 46 | + # Detect downloads section |
| 47 | + if ($line -match '^\s*files\s*:\s*$') { |
| 48 | + $inDownloadsSection = $true |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + if ($inDownloadsSection) { |
| 53 | + # New download entry (starts with dash or hyphen at specific indentation) |
| 54 | + if ($line -match '^\s*-\s*(?:url\s*:\s*(.+)|$)') { |
| 55 | + # Save previous download if exists |
| 56 | + if ($currentDownload -and ($currentDownload.url -or $currentDownload.sha512 -or $currentDownload.size)) { |
| 57 | + $result.Downloads += [PSCustomObject]$currentDownload |
| 58 | + } |
| 59 | + |
| 60 | + # Start new download |
| 61 | + $currentDownload = @{ |
| 62 | + url = $null |
| 63 | + sha512 = $null |
| 64 | + size = $null |
| 65 | + } |
| 66 | + |
| 67 | + # Check if url is on the same line |
| 68 | + if ($matches[1]) { |
| 69 | + $currentDownload.url = $matches[1].Trim(' ''"`t') |
| 70 | + } |
| 71 | + } |
| 72 | + # URL property |
| 73 | + elseif ($line -match '^\s+url\s*:\s*(.+)$') { |
| 74 | + if ($currentDownload) { |
| 75 | + $currentDownload.url = $matches[1].Trim(' ''"`t') |
| 76 | + } |
| 77 | + } |
| 78 | + # SHA512 property |
| 79 | + elseif ($line -match '^\s+sha512\s*:\s*(.+)$') { |
| 80 | + if ($currentDownload) { |
| 81 | + $currentDownload.sha512 = $matches[1].Trim(' ''"`t') |
| 82 | + } |
| 83 | + } |
| 84 | + # Size property |
| 85 | + elseif ($line -match '^\s+size\s*:\s*(.+)$') { |
| 86 | + if ($currentDownload) { |
| 87 | + $sizeValue = $matches[1].Trim(' ''"`t') |
| 88 | + # Try to convert to number if possible |
| 89 | + if ($sizeValue -match '^\d+$') { |
| 90 | + $currentDownload.size = [int64]$sizeValue |
| 91 | + } |
| 92 | + else { |
| 93 | + $currentDownload.size = $sizeValue |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + # End of downloads section (non-indented key or end of indented section) |
| 98 | + elseif ($line -match '^\S' -and $line -notmatch '^\s*-') { |
| 99 | + # Save the last download entry |
| 100 | + if ($currentDownload -and ($currentDownload.url -or $currentDownload.sha512 -or $currentDownload.size)) { |
| 101 | + $result.Downloads += [PSCustomObject]$currentDownload |
| 102 | + } |
| 103 | + $inDownloadsSection = $false |
| 104 | + $currentDownload = $null |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + # Save the last download entry if still in downloads section |
| 110 | + if ($inDownloadsSection -and $currentDownload -and ($currentDownload.url -or $currentDownload.sha512 -or $currentDownload.size)) { |
| 111 | + $result.Downloads += [PSCustomObject]$currentDownload |
| 112 | + } |
| 113 | + |
| 114 | + # Return the final object that we can use to build the output |
| 115 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Converted YAML to PSCustomObject." |
| 116 | + $YamlObject = [PSCustomObject]$result |
| 117 | + |
| 118 | + # Build output objects, filtering the .Downloads to unique entries |
| 119 | + foreach ($File in ($YamlObject.Downloads | Select-Object -Unique -Property "size", "sha512", "url")) { |
| 120 | + [PSCustomObject]@{ |
| 121 | + Version = $YamlObject.Version |
| 122 | + Date = ConvertTo-DateTime -DateTime $YamlObject.ReleaseDate -Pattern $res.Get.Update.DateTimeFormat |
| 123 | + Size = $File.size |
| 124 | + Sha512 = $File.sha512 |
| 125 | + Architecture = Get-Architecture -String $File.url |
| 126 | + Type = Get-FileType -File $File.url |
| 127 | + URI = $res.Get.Download.Uri -replace "#filename", $File.url |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments