diff --git a/src/task.ps1 b/src/task.ps1 new file mode 100644 index 0000000..26b7766 --- /dev/null +++ b/src/task.ps1 @@ -0,0 +1,57 @@ +function Get-SamyTasks { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Url + ) + + try { + $json = Get-RemoteText -Url $Url + if ([string]::IsNullOrWhiteSpace($json)) { throw "Tasks JSON was empty." } + + $parsed = $json | ConvertFrom-Json -ErrorAction Stop + $tasks = @($parsed) + + if ($tasks.Count -eq 0) { throw "Tasks JSON parsed but contained no tasks." } + + foreach ($task in $tasks) { + + $labelRaw = $task.Label + if ($labelRaw -is [System.Collections.IEnumerable] -and -not ($labelRaw -is [string])) { + $labelRaw = @($labelRaw)[0] + } + $label = [string]$labelRaw + $task.Label = $label + + $tooltipRaw = $null + if ($task.PSObject.Properties.Name -contains 'Tooltip') { + $tooltipRaw = $task.Tooltip + } + + $tooltip = if ($tooltipRaw -is [string]) { + $tooltipRaw + } + elseif ($tooltipRaw -is [System.Collections.IEnumerable] -and -not ($tooltipRaw -is [string])) { + [string](@($tooltipRaw)[0]) + } + else { + [string]$tooltipRaw + } + + if ([string]::IsNullOrWhiteSpace($tooltip)) { $tooltip = $label } + + if ($task.PSObject.Properties.Name -contains 'Tooltip') { + $task.Tooltip = $tooltip + } + else { + $task | Add-Member -NotePropertyName Tooltip -NotePropertyValue $tooltip -Force + } + } + + return $tasks + } + catch { + Write-LogHybrid "Failed to load tasks from ${Url}: $($_.Exception.Message)" Error UI -LogToEvent + return $null + } +}