Functions/StreamDeck/Get-StreamDeckPlugin.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
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
function Get-StreamDeckPlugin
{
    <#
    .Synopsis
        Gets Stream Deck Plugins
    .Description
        Gets plugins for StreamDeck.
    .Example
        Get-StreamDeckPlugin
    .Link
        New-StreamDeckAction
    #>

    [OutputType('StreamDeck.Plugin')]
    [CmdletBinding(DefaultParameterSetName='Plugin')]
    param(
    # The name of the plugin
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $Name,

    # The Plugin UUID
    [Parameter(ParameterSetName='Plugin',ValueFromPipelineByPropertyName)]
    [string]
    $UUID,

    # If set, will rebuild the cache of streamdeck plugins.
    [Parameter(ValueFromPipelineByPropertyName)]
    [switch]
    $Force,

    # The path to a plugin or a directory containing plugins.
    # If -Template is provided, will look for Plugin Templates beneath -PluginPath.
    [Parameter(ValueFromPipelineByPropertyName)]
    [Alias('Fullname')]
    [string]
    $PluginPath,

    # If set, will get plugin template scripts.
    # PluginTemplates are defined in *.StreamDeckPluginTemplate.ps1 files.
    [Parameter(ParameterSetName='PluginTemplate')]
    [Alias('PluginTemplate')]
    [switch]
    $Template
    )

    begin {
        filter manifest.json=>[StreamDeck.Plugin] {            
            $inFile = $_            
            try {
                $jsonObject = ConvertFrom-Json ([IO.File]::ReadAllText($inFile.Fullname))
                $jsonObject.pstypenames.clear()
                $jsonObject.pstypenames.add('StreamDeck.Plugin')
                $jsonObject.psobject.properties.add([PSNoteProperty]::new('PluginPath', $inFile.Fullname))
                $jsonObject
            } catch {
                Write-Error -ErrorRecord $_
            }            
        }
        ${?<PluginTemplate>} = 'StreamDeckPluginTemplate\.ps1$'
        filter .ps1=>[StreamDeck.PluginTemplate] {
            $inFile = $_
            $pluginTemplate = $ExecutionContext.SessionState.InvokeCommand.GetCommand($inFile.fullname,'ExternalScript')
            $pluginTemplate.pstypenames.clear()
            $pluginTemplate.pstypenames.add('StreamDeck.PluginTemplate')
            $pluginTemplate
        }
    }

    process {
        if ($PSCmdlet.ParameterSetName -eq 'PluginTemplate') {
            if ($Force -or -not $Script:CachedStreamDeckPluginTemplates) {
                $Script:CachedStreamDeckPluginTemplates = @($MyInvocation.MyCommand.Module |
                    Split-Path | 
                    Get-ChildItem -Filter *.ps1 |
                    Where-Object Name -Match ${?<PluginTemplate>} |
                    .ps1=>[StreamDeck.PluginTemplate])
            }

            $templateList = 
                if (-not $PluginPath) {
                    @() + $Script:CachedStreamDeckPluginTemplates
                } else {
                    @(if ($pluignPath -match ${?<PluginTemplate>}) {
                        Get-ChildItem -Path $pluignPath |
                                .ps1=>[StreamDeck.PluginTemplate]
                    } else {
                        Get-ChildItem -Path $PluginPath -Recurse -Filter *.ps1  |
                            Where-Object Name -Match ${?<PluginTemplate>} |                        
                            .ps1=>[StreamDeck.PluginTemplate]
                    }) 
                }

            if ($name) {
                $templateList | Where-Object { $_.Name -eq $Name -or $_.Name -like "$Name.ps1"}
            } else {
                $templateList
            }
            return
        }
        
        #region Cache StreamDeck Plugins
        if ($force -or -not $Script:CachedStreamDeckPlugins) {
            
            $Script:CachedStreamDeckPlugins = @(
                # On Windows, plugins can be in
                if ((-not $PSVersionTable.Platform) -or ($PSVersionTable.Platform -match 'Win')) {
                    $sdPluginPath = "$env:AppData\Elgato\StreamDeck\Plugins\"
                    Write-Verbose "Searching for Plugins beneath '$sdPluginPath'"
                    Get-ChildItem -Path $sdPluginPath -Directory | # appdata
                        Get-ChildItem -Filter manifest.json |
                        manifest.json=>[StreamDeck.Plugin]

                    Get-ChildItem -Path "$env:ProgramFiles\Elgato\StreamDeck\plugins" -Directory | # or programfiles
                        Get-ChildItem -Filter manifest.json |
                        manifest.json=>[StreamDeck.Plugin]
                } elseif ($PSVersionTable.Platform -eq 'Unix') {
                    if ($PSVersionTable.OS -like '*darwin*' -and -not $env:GITHUB_WORKSPACE) { # On Mac, plugins can be in
                        Get-ChildItem -Path "~/Library/Application Support/com.elgato.StreamDeck/Plugins" | # the library
                            Get-ChildItem -Filter manifest.json |
                            manifest.json=>[StreamDeck.Plugin]

                            
                        Get-ChildItem -Path "/Applications/Stream Deck.app/Contents/Plugins" -Directory | # or applications.
                            Get-ChildItem -Filter manifest.json |
                            manifest.json=>[StreamDeck.Plugin]
                    }
                }
            )
        }
        #endregion Cache StreamDeck Plugins
        $pluginList = 
            if (-not $PluginPath) {
                @() + $Script:CachedStreamDeckPlugins
            } else {
                @(if ($pluignPath -match '[\\/]manifest.json$') {
                    Get-ChildItem -Path $pluignPath |
                            manifest.json=>[StreamDeck.Plugin]
                } else {
                    Get-ChildItem -Path $PluginPath -Recurse -Filter manifest.json  |                        
                        manifest.json=>[StreamDeck.Plugin]
                }) 
            }
            


        if ($Name -or $UUID) {
            :nextPlugin foreach ($plugin in $pluginList) {
                if ($plugin.Name -like $name) { $plugin; continue }
                foreach ($act in $plugin.actions) {
                    if ($Name -and $act.Name -like $name) { $plugin; continue nextPlugin}
                    if ($UUID -and $act.uuid -like $UUID) { $plugin; continue nextPlugin}
                }
            }
        }
        else {
            $pluginList
        }
    }
}