Functions/StreamDeck/Update-StreamDeckProfile.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
function Update-StreamDeckProfile
{
    <#
    .Synopsis
        Updates a StreamDeck profile
    .Description
        Updates a StreamDeck profile
    .Example
        $vsCodeProfile = Get-StreamDeckProfile -Name VSCode
        $defaultProfile = Get-streamDeckProfile -Name "Default Profile" |
            Where-Object DeviceName -eq StreamDeckXL

        Update-StreamDeckProfile -Name VSCode -Action @{
            "0,0" =
                New-StreamDeckAction -ProfileName $defaultProfile.Guid -DeviceUUID $defaultProfile.DeviceUUID -Image C:\Users\JamesBrundage\Pictures\Gif\CountryHome.gif

            "0,1" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "issues:github.focus"
                } -name "Execute Command" -Title ("GitHub", "Issues" -join [Environment]::newline)
            "0,2" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executeterminalcommand -Setting @{
                    command = "git pull"
                } -name "Execute Command" -Title ("git", "pull" -join [Environment]::newline)
            "0,3" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executeterminalcommand -Setting @{
                    command = "git push"
                } -name "Execute Command" -Title ("git", "push" -join [Environment]::newline)
            "0,4" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executeterminalcommand -Setting @{
                    command = "git status"
                } -name "Execute Command" -Title ("git", "status" -join [Environment]::newline)
                
            "0,6" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "workbench.debug.viewlet.action.removeAllBreakpoints"
                } -name "Execute Command" -Title ("Remove","All", "Breakpoints" -join [Environment]::newline)
            
            "0,7" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "workbench.action.closeAllEditors"
                } -name "Execute Command" -Title "Close All"


            "1,0" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "workbench.action.toggleSidebarVisibility"
                } -name "Execute Command" -Title ("toggle", "sidebar" -join [Environment]::newline)
            "1,7" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "workbench.files.action.focusFilesExplorer"
                } -name "Execute Command" -Title "Files"
            
            "2,0" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "workbench.action.toggleScreencastMode"
                } -name "Execute Command" -Title "ScreenCast"
                
            "2,7" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "workbench.files.action.collapseExplorerFolders"
                } -name "Execute Command" -Title ("collapse", "folders" -join [Environment]::newline)
            "3,0" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "workbench.action.toggleZenMode"
                } -name "Execute Command" -Title "zen"
            "3,1" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "workbench.action.toggleEditorVisibility"
                } -name "Execute Command" -Title ("toggle", "editor" -join [Environment]::newline)
            
            "3,7" =
                New-StreamDeckAction -UUID com.nicollasr.streamdeckvsc.executecommand -Setting @{
                    command = "editor.action.formatDocument"
                } -name "Execute Command" -Title "format"
                
            "7,3" = $null
        }
    .Link
        Get-StreamDeckProfile
    .Link
        Remove-StreamDeckProfile
    .Link
        Save-StreamDeckProfile
    .Link
        New-StreamDeckProfile
    #>

    [OutputType('StreamDeck.Profile')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification="Does not change state")]
    param(
    # The name of the profile
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $Name,

    # A collection of actions.
    [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
    [Collections.IDictionary]
    [ValidateScript({
        foreach ($k in $_.Keys) {
            if ($k -notmatch '\d+,\d+') {
                throw "Action keys must be in the form row, column (e.g. 0,2)."
            }
        }
        return $true
    })]
    $Action,
    
    # The profile UUID. If not provided, a GUID will be generated.
    [Parameter(ValueFromPipelineByPropertyName)]
    [Alias('guid')]
    [string]
    $ProfileUUID,

    [string]
    $DeviceType
    )    


    process {        
        $streamDeckProfiles = 
            if ($_ -and $_.pstypenames -contains 'StreamDeck.Profile') {
                $_
            } elseif ($ProfileUUID) {
                Get-StreamDeckProfile -Recurse | Where-Object Guid -eq $ProfileUUID
            } elseif ($name) {
                Get-StreamDeckProfile | Where-Object Name -eq $name
            }

        if ($streamDeckProfiles -is [Object[]] -and $DeviceType) {
            $streamDeckProfiles = $streamDeckProfiles | Where-Object DeviceType -eq $DeviceType
        }

        foreach ($streamDeckProfileObject in $streamDeckProfiles) {
            #region Map Actions
            foreach ($act in $Action.GetEnumerator()) {
                if (-not $act.Value) {
                    $streamDeckProfileObject.RemoveAction.Invoke(@($act.Key -split ','))                    
                } else {
                    $streamDeckProfileObject.AddAction.Invoke(@(
                        $act.Value
                        $act.Key -split ','
                    ))
                }                
            }            
            #endregion Map Actions

            $streamDeckProfileObject
            $streamDeckProfileObject.Save()
        }        
    }
}