Add src/task.ps1
This commit is contained in:
57
src/task.ps1
Normal file
57
src/task.ps1
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user