1+ <#
2+ This script will take a list of psm1 files that have Import-LocalizedData calls
3+ and attempt to extract the call and get the relevant localization data.
4+ #>
5+ [CmdletBinding ()]
6+ param (
7+ [string ]$ModuleFile ,
8+ [CultureInfo ]
9+ $UICulture
10+ )
11+
12+ $resolvedPath = Resolve-Path $ModuleFile
13+ if ($null -eq $resolvedPath.Path ) {
14+ Write-Warning " File not found: $ModuleFile "
15+ continue
16+ }
17+ $result = @ {}
18+ $file = $resolvedPath.Path
19+ $parentDirectory = Split-Path - Path $file - Parent
20+ # Get path variables
21+ if (Test-Path $file ) {
22+ Write-Verbose " Processing file: $file "
23+ $tokens = $null
24+ $errors = $null
25+ $scriptBlock = [System.Management.Automation.Language.Parser ]::ParseFile(
26+ $file ,
27+ [ref ]$tokens ,
28+ [ref ]$errors
29+ )
30+ # Find the Import-LocalizedData calls
31+ $importLocalizedDataCalls = $scriptBlock.FindAll (
32+ {
33+ param ($Ast )
34+ $Ast -is [System.Management.Automation.Language.CommandAst ] -and
35+ $Ast.GetCommandName () -eq ' Import-LocalizedData'
36+ },
37+ $true
38+ )
39+ if ($importLocalizedDataCalls.Count -eq 0 ) {
40+ Write-Warning " No Import-LocalizedData calls found in $file "
41+ continue
42+ }
43+
44+ try {
45+ Write-Verbose " Switching to $parentDirectory "
46+ Push-Location $parentDirectory
47+ foreach ($call in $importLocalizedDataCalls ) {
48+ # Here you can add logic to extract and process the localization data
49+ $splat = @ {
50+ BaseDirectory = $parentDirectory
51+ }
52+
53+ # We go over each command element and extract the value
54+ $parameterName = $null
55+ foreach ($element in $call.CommandElements [1 .. $call.CommandElements.Count ]) {
56+ Write-Verbose " Checking: $ ( $element.Extent.Text ) "
57+ switch ($element ) {
58+ { $_ -is [System.Management.Automation.Language.CommandParameterAst ] } {
59+ # This is the command
60+ $parameterName = $element.Extent.Text.Trim (' -' )
61+ continue
62+ }
63+ { $_ -is [System.Management.Automation.Language.CommandExpressionAst ] } {
64+ # This is an expression, we can ignore it for now
65+ continue
66+ }
67+ { $_ -is [System.Management.Automation.Language.ParameterAst ] } {
68+ # This is a parameter
69+ $parameterName = $element.Extent.Text.Trim (' -' )
70+ continue
71+ }
72+ { $_ -is [System.Management.Automation.Language.StringConstantExpressionAst ] } {
73+ # Handle string constants
74+ # Remove quotes
75+ $splat [$parameterName ] = $element.Extent.Text.Trim (' "' ).Trim(" '" )
76+ continue
77+ }
78+ { $_ -is [System.Management.Automation.Language.VariableExpressionAst ] } {
79+ # Handle variables
80+ # Look through script block for the last assignment that happens before this point
81+ $lastAssignment = $scriptBlock.FindAll (
82+ {
83+ param ($Ast )
84+ $Ast -is [System.Management.Automation.Language.AssignmentStatementAst ] -and
85+ $Ast.Left -is [System.Management.Automation.Language.VariableExpressionAst ] -and
86+ $Ast.Left.VariablePath.UserPath -eq $element.VariablePath.UserPath -and
87+ $Ast.Extent.StartLineNumber -le $element.Extent.StartLineNumber
88+ },
89+ $true
90+ ) | Select-Object - Last 1
91+ if ($lastAssignment ) {
92+ if ($element.Splatted ) {
93+ # Append all the items of the lastAssignment to the current splat
94+ foreach ($kv in $lastAssignment.Right.Expression.KeyValuePairs ) {
95+ $splat [$kv.Item1.Extent.Text ] = $kv.Item2.Extent.Text.Trim (' "' ).Trim(" '" )
96+ }
97+ } else {
98+ $splat [$parameterName ] = $lastAssignment.Right.Extent.Text.Trim (' "' ).Trim(" '" )
99+ }
100+ } else {
101+ $splat [$parameterName ] = $element.Extent.Text
102+ }
103+ continue
104+ }
105+ default {
106+ # Handle other cases
107+ throw " Unhandled case: $ ( $element.GetType ().Name) "
108+ }
109+ }
110+ }
111+ # Execute the Import-LocalizedData command with the extracted arguments
112+ if ($splat.BindingVariable ) {
113+ Write-Verbose " Binding variable found: $ ( $splat.BindingVariable ) "
114+ $bindingVariable = $splat.BindingVariable
115+ $splat.Remove (' BindingVariable' )
116+ }
117+ if ($null -ne $UICulture -and -not [String ]::IsNullOrEmpty($UICulture.Name )) {
118+ $splat [' UICulture' ] = $UICulture.Name
119+ }
120+ Write-Verbose " Running command with splat: $ ( $splat | ConvertTo-Json ) "
121+ $data = Import-LocalizedData @splat
122+ $result [$bindingVariable ] = $data
123+ }
124+ return $result | ConvertTo-Json
125+ } catch {
126+ Write-Error " Error processing ${file} : $_ "
127+ throw $_
128+ } finally {
129+ # Go back to original directory
130+ Write-Verbose " Returning to original directory"
131+ Pop-Location
132+ }
133+ } else {
134+ Write-Warning " File not found: $file "
135+ }
0 commit comments