Public/Export/Export-ISEThemeFile.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function Export-ISEThemeFile {

    [cmdletbinding()]
    Param(
        [Parameter(ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
        [string]$ISETheme,

        [Parameter(ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
        [switch]$SaveToISE
    )

    Begin{}

    Process {
        # Determine whether exporting current theme or a saved theme
        If ($ISETheme) {
            $Theme = Get-ISETheme -ThemeName $ISETheme
            $XmlTheme = $Theme
        } Else {
            $XmlTheme = Get-CurrentISETheme
        }        

        # Set the File Name
        $FilePath = Get-SaveFile

        # Trim the extension if found
        If ($FilePath.Length -gt  26) {
            If ($FilePath.Substring($FilePath.Length - 26) -ieq '.StorableColorTheme.ps1xml') {
                $FilePath = $FilePath.Substring(0,$FilePath.Length - 26)
            }
        }

        # Set ThemeName for ISE import
        $ThemeName = $FilePath.Substring($FilePath.LastIndexOf('\') + 1)
        # Set file extension
        $FilePath = "$FilePath.StorableColorTheme.ps1xml"
 
        # Create The Document
        $XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,[Text.Encoding]::Unicode) 

        # Set The Formatting
        $xmlWriter.Formatting = 'Indented'
        $xmlWriter.Indentation = '4'
 
        # Write the XML Decleration
        $xmlWriter.WriteStartDocument()

        # Write Root Element
        $xmlWriter.WriteStartElement('StorableColorTheme')
        $XmlWriter.WriteAttributeString('xmlns:xsd','http://www.w3.org/2001/XMLSchema') 
        $XmlWriter.WriteAttributeString('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance')

        # Write Keys
        $xmlWriter.WriteStartElement('Keys')
        ForEach ($Attribute in $XmlTheme) {
            #Write Base Colors Keys
            If ($Attribute.Class -eq 'Base') {
                $xmlWriter.WriteElementString('string',$Attribute.Attribute)
            }

            # Write TokenColors Keys
            If ($Attribute.Class -eq 'TokenColors') {
                $xmlWriter.WriteElementString('string',"TokenColors\$($Attribute.Attribute)")
            }

            # Write ConsoleTokenColors Keys
            If ($Attribute.Class -eq 'ConsoleTokenColors') {
                $xmlWriter.WriteElementString('string',"ConsoleTokenColors\$($Attribute.Attribute)")
            }

            # Write XmlTokenColors Keys
            If ($Attribute.Class -eq 'XmlTokenColors') {
                $xmlWriter.WriteElementString('string',"XmlTokenColors\$($Attribute.Attribute)")
            }
        }
        $xmlWriter.WriteEndElement() # <-- Closing Keys

        # Write Values
        $xmlWriter.WriteStartElement('Values')

        ForEach ($Attribute in $XmlTheme) {
            # Write Base Colors Values
            If ($Attribute.Class -eq 'Base') {
                $xmlWriter.WriteStartElement('Color')
                $xmlWriter.WriteElementString('A',$Attribute.A)
                $xmlWriter.WriteElementString('R',$Attribute.R)
                $xmlWriter.WriteElementString('G',$Attribute.G)
                $xmlWriter.WriteElementString('B',$Attribute.B)
                $xmlWriter.WriteEndElement() # <-- Closing Color
            }

            # Write TokenColors Values
            If ($Attribute.Class -eq 'TokenColors') {
                $xmlWriter.WriteStartElement('Color')
                $xmlWriter.WriteElementString('A',$Attribute.A)
                $xmlWriter.WriteElementString('R',$Attribute.R)
                $xmlWriter.WriteElementString('G',$Attribute.G)
                $xmlWriter.WriteElementString('B',$Attribute.B)
                $xmlWriter.WriteEndElement() # <-- Closing Color
            }

            # Write ConsoleTokenColors Values
            If ($Attribute.Class -eq 'ConsoleTokenColors') {
                $xmlWriter.WriteStartElement('Color')
                $xmlWriter.WriteElementString('A',$Attribute.A)
                $xmlWriter.WriteElementString('R',$Attribute.R)
                $xmlWriter.WriteElementString('G',$Attribute.G)
                $xmlWriter.WriteElementString('B',$Attribute.B)
                $xmlWriter.WriteEndElement() # <-- Closing Color
            }

            # Write XmlTokenColors Values
            If ($Attribute.Class -eq 'XmlTokenColors') {
                $xmlWriter.WriteStartElement('Color')
                $xmlWriter.WriteElementString('A',$Attribute.A)
                $xmlWriter.WriteElementString('R',$Attribute.R)
                $xmlWriter.WriteElementString('G',$Attribute.G)
                $xmlWriter.WriteElementString('B',$Attribute.B)
                $xmlWriter.WriteEndElement() # <-- Closing Color
            } 
         }
         $xmlWriter.WriteEndElement() # <-- Closing Values

        #Write Name and Font Attribu
        If ($Attribute.Class -eq 'Other') {
            $xmlWriter.WriteElementString('Name',$ThemeName)
            $xmlWriter.WriteElementString('FontFamily',$Attribute.FontFamily)
            $xmlWriter.WriteElementString('FontSize',$Attribute.FontSize)
        }
 
        # Write Close Tag for Root Element
        $xmlWriter.WriteEndElement() # <-- Closing StorableColorTheme
 
        # End the XML Document
        $xmlWriter.WriteEndDocument()
 
        # Finish The Document
        $xmlWriter.Finalize
        $xmlWriter.Flush | out-null
        $xmlWriter.Close()

        If ($SaveToISE) {
            Import-ISEThemeFile $FilePath
        }
    }

    End{}
    <#
     .SYNOPSIS
      Exports an ISE theme to an xml file
 
  .DESCRIPTION
   Exports an ISE theme to an xml file and saves in the registry
            if SaveToISE is passed
 
  .PARAMETER RegistryTheme
   Optionally supply an ISE theme name to export that theme
 
   .PARAMETER SaveToISE
   After exporting, save the theme to the registry
 
        .EXAMPLE
   PS C:\> Export-ISEThemeFile -ISETheme "Monokai"
 
            Exports ISE theme to an xml file
 
        .EXAMPLE
   PS C:\> Export-ISEThemeFile -SaveToISE
 
            Exports the current ISE theme and saves it in the ISE for future reference
 
  .NOTES
            Part of the ISEColorThemeCmdlets.ps1 Script by Jeff Pollock
            http://Lifeinpowerhsell.blogspot.com
            http://gallery.technet.microsoft.com/ISE-Color-Theme-Cmdlets-24905f9e
 #>


}