|
3 | 3 | <#
|
4 | 4 | .SYNOPSIS
|
5 | 5 | Parses a module that uses the PSFramework localization feature for strings and their value.
|
6 |
| - |
| 6 | +
|
7 | 7 | .DESCRIPTION
|
8 | 8 | Parses a module that uses the PSFramework localization feature for strings and their value.
|
9 | 9 | This command can be used to generate and update the language files used by the module.
|
10 | 10 | It is also used in automatic tests, ensuring no abandoned string has been left behind and no key is unused.
|
11 |
| - |
| 11 | +
|
12 | 12 | .PARAMETER ModuleRoot
|
13 | 13 | The root of the module to process.
|
14 | 14 | Must be the root folder where the psd1 file is stored in.
|
15 |
| - |
| 15 | +
|
16 | 16 | .EXAMPLE
|
17 | 17 | PS C:\> Export-PSMDString -ModuleRoot 'C:\Code\Github\MyModuleProject\MyModule'
|
18 |
| - |
| 18 | +
|
19 | 19 | Generates the strings data for the MyModule module.
|
20 | 20 | #>
|
21 | 21 | [CmdletBinding()]
|
|
25 | 25 | [string]
|
26 | 26 | $ModuleRoot
|
27 | 27 | )
|
28 |
| - |
| 28 | + |
29 | 29 | process
|
30 | 30 | {
|
31 | 31 | #region Find Language Files : $languageFiles
|
|
40 | 40 | }
|
41 | 41 | }
|
42 | 42 | #endregion Find Language Files : $languageFiles
|
43 |
| - |
| 43 | + |
44 | 44 | #region Find Keys : $foundKeys
|
45 | 45 | $foundKeys = foreach ($file in (Get-ChildItem -Path $ModuleRoot -Recurse | Where-Object Extension -match '^\.ps1$|^\.psm1$'))
|
46 | 46 | {
|
|
51 | 51 | if (-not ($args[0].CommandElements.ParameterName -match '^String$|^ActionString$')) { return $false }
|
52 | 52 | $true
|
53 | 53 | }, $true)
|
54 |
| - |
| 54 | + |
55 | 55 | foreach ($commandAst in $commandAsts)
|
56 | 56 | {
|
57 | 57 | $stringParam = $commandAst.CommandElements | Where-Object ParameterName -match '^String$|^ActionString$'
|
58 | 58 | $stringParamValue = $commandAst.CommandElements[($commandAst.CommandElements.IndexOf($stringParam) + 1)].Value
|
59 |
| - |
| 59 | + |
60 | 60 | $stringValueParam = $commandAst.CommandElements | Where-Object ParameterName -match '^StringValues$|^ActionStringValues$'
|
61 | 61 | if ($stringValueParam)
|
62 | 62 | {
|
|
72 | 72 | StringValues = $stringValueParamValue
|
73 | 73 | }
|
74 | 74 | }
|
75 |
| - |
| 75 | + |
76 | 76 | # Additional checks for splatted commands
|
77 | 77 | # find all splatted commands
|
78 | 78 | $splattedVariables = $ast.FindAll( {
|
|
109 | 109 | {
|
110 | 110 | # find any String or ActionString
|
111 | 111 | $splatParam = $splatAssignmentAst.Right.Expression.KeyValuePairs | Where-Object Item1 -match '^String$|^ActionString$'
|
112 |
| - # The kvp.item.extent.text returns nested quotes where as the commandast.extent.text doesn't so strip them off |
113 |
| - $splatParamValue = $splatParam.Item2.Extent.Text.Trim('"').Trim("'") |
114 |
| - # find any StringValue or ActionStringValue |
115 |
| - $splatValueParam = $splatAssignmentAst.Right.Expression.KeyValuePairs | Where-Object Item1 -match '^StringValues$|^ActionStringValues$' |
| 112 | + if ($splatParam) |
| 113 | + { |
| 114 | + # The kvp.item.extent.text returns nested quotes where as the commandast.extent.text doesn't so strip them off |
| 115 | + $splatParamValue = $splatParam.Item2.Extent.Text.Trim('"').Trim("'") |
| 116 | + # find any StringValue or ActionStringValue |
| 117 | + $splatValueParam = $splatAssignmentAst.Right.Expression.KeyValuePairs | Where-Object Item1 -match '^StringValues$|^ActionStringValues$' |
| 118 | + } |
116 | 119 | if ($splatValueParam)
|
117 | 120 | {
|
118 | 121 | # The kvp.item.extent.text returns nested quotes whereas the commandast.extent.text doesn't so strip them off
|
|
139 | 142 | if (-not ($args[0].NamedArguments.ArgumentName -eq 'ErrorString')) { return $false }
|
140 | 143 | $true
|
141 | 144 | }, $true)
|
142 |
| - |
| 145 | + |
143 | 146 | foreach ($validateAst in $validateAsts)
|
144 | 147 | {
|
145 | 148 | [PSCustomObject]@{
|
|
153 | 156 | }
|
154 | 157 | }
|
155 | 158 | #endregion Find Keys : $foundKeys
|
156 |
| - |
| 159 | + |
157 | 160 | #region Report Findings
|
158 | 161 | $totalResults = foreach ($languageFile in $languageFiles.Keys)
|
159 | 162 | {
|
|
166 | 169 | $results[$foundKey.String].Entries += $foundKey
|
167 | 170 | continue
|
168 | 171 | }
|
169 |
| - |
| 172 | + |
170 | 173 | $results[$foundKey.String] = [PSCustomObject] @{
|
171 | 174 | PSTypeName = 'PSmoduleDevelopment.String.LanguageFinding'
|
172 | 175 | Language = $languageFile
|
|
180 | 183 | }
|
181 | 184 | $results.Values
|
182 | 185 | #endregion Phase 1: Matching parsed strings to language file
|
183 |
| - |
| 186 | + |
184 | 187 | #region Phase 2: Finding unneeded strings
|
185 | 188 | foreach ($key in $languageFiles[$languageFile].Keys)
|
186 | 189 | {
|
|
0 commit comments