Skip to content

Commit bf7bb2c

Browse files
author
Kapil Borle
committed
Add function to create strongly typed resource file
1 parent 6fd9274 commit bf7bb2c

File tree

2 files changed

+153
-9
lines changed

2 files changed

+153
-9
lines changed

New-StronglyTypedCsFileForResx.ps1

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
param(
2+
[ValidateSet("Engine","Rules")]
3+
[string] $project
4+
)
5+
6+
function Get-StronglyTypeCsFileForResx
7+
{
8+
param($xml, $ModuleName, $ClassName)
9+
10+
# Example
11+
#
12+
# $ClassName = Full.Name.Of.The.ClassFoo
13+
# $shortClassName = ClassFoo
14+
# $namespaceName = Full.Name.Of.The
15+
16+
$shortClassName = $ClassName
17+
$namespaceName = $null
18+
19+
$lastIndexOfDot = $className.LastIndexOf(".")
20+
if ($lastIndexOfDot -ne -1)
21+
{
22+
$namespaceName = $className.Substring(0, $lastIndexOfDot)
23+
$shortClassName = $className.Substring($lastIndexOfDot + 1)
24+
}
25+
26+
$banner = @'
27+
//------------------------------------------------------------------------------
28+
// <auto-generated>
29+
// This code was generated by a New-StronglyTypedCsFileForResx funciton.
30+
// To add or remove a member, edit your .ResX file then rerun Start-ResGen.
31+
//
32+
// Changes to this file may cause incorrect behavior and will be lost if
33+
// the code is regenerated.
34+
// </auto-generated>
35+
//------------------------------------------------------------------------------
36+
37+
{0}
38+
'@
39+
40+
$namespace = @'
41+
namespace {0} {{
42+
{1}
43+
}}
44+
'@
45+
46+
$body = @'
47+
using System;
48+
using System.Reflection;
49+
50+
/// <summary>
51+
/// A strongly-typed resource class, for looking up localized strings, etc.
52+
/// </summary>
53+
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
54+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
55+
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
56+
57+
internal class {0} {{
58+
59+
private static global::System.Resources.ResourceManager resourceMan;
60+
61+
private static global::System.Globalization.CultureInfo resourceCulture;
62+
63+
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
64+
internal {0}() {{
65+
}}
66+
67+
/// <summary>
68+
/// Returns the cached ResourceManager instance used by this class.
69+
/// </summary>
70+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
71+
internal static global::System.Resources.ResourceManager ResourceManager {{
72+
get {{
73+
if (object.ReferenceEquals(resourceMan, null)) {{
74+
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("{3}", typeof({0}).GetTypeInfo().Assembly);
75+
resourceMan = temp;
76+
}}
77+
return resourceMan;
78+
}}
79+
}}
80+
81+
/// <summary>
82+
/// Overrides the current thread's CurrentUICulture property for all
83+
/// resource lookups using this strongly typed resource class.
84+
/// </summary>
85+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
86+
internal static global::System.Globalization.CultureInfo Culture {{
87+
get {{
88+
return resourceCulture;
89+
}}
90+
set {{
91+
resourceCulture = value;
92+
}}
93+
}}
94+
{2}
95+
}}
96+
'@
97+
98+
$entry = @'
99+
100+
/// <summary>
101+
/// Looks up a localized string similar to {1}
102+
/// </summary>
103+
internal static string {0} {{
104+
get {{
105+
return ResourceManager.GetString("{0}", resourceCulture);
106+
}}
107+
}}
108+
'@
109+
$entries = $xml.root.data | % {
110+
if ($_) {
111+
$val = $_.value.Replace("`n", "`n ///")
112+
$name = $_.name.Replace(' ', '_')
113+
$entry -f $name,$val
114+
}
115+
} | Out-String
116+
117+
$bodyCode = $body -f $shortClassName,$ModuleName,$entries,$ClassName
118+
119+
if ($NamespaceName)
120+
{
121+
$bodyCode = $namespace -f $NamespaceName, $bodyCode
122+
}
123+
124+
$resultCode = $banner -f $bodyCode
125+
126+
return $resultCode -replace "`r`n?|`n","`r`n"
127+
}
128+
129+
$projectRoot = Split-Path $MyInvocation.InvocationName
130+
if (-not (Test-Path "$projectRoot/global.json"))
131+
{
132+
throw "Not in solution root"
133+
}
134+
$inputFilePath = Join-Path $projectRoot "$project/Strings.resx"
135+
$outputFilePath = Join-Path $projectRoot "$project/Strings.cs"
136+
$className = "Microsoft.Windows.PowerShell.ScriptAnalyzer"
137+
if ($project -eq "Rules")
138+
{
139+
$className += ".BuiltinRules"
140+
}
141+
$className += ".Strings"
142+
$xml = [xml](Get-Content -raw $inputFilePath)
143+
$genSource = Get-StronglyTypeCsFileForResx -xml $xml -ModuleName Foo -ClassName $className
144+
Set-Content -Encoding Ascii -Path $outputFilePath -Value $genSource

buildCoreClr.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
[switch]$install
55
)
66

7-
$solutionDir = "C:\Users\kborle\Source\Repos\PSScriptAnalyzer"
8-
7+
$solutionDir = "$HOME\Source\Repos\PSScriptAnalyzer"
98

109
$itemsToCopy = @("$solutionDir\Engine\bin\debug\netcoreapp1.0\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll",
1110
"$solutionDir\Rules\bin\debug\netcoreapp1.0\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll",
@@ -18,10 +17,12 @@ $destinationDir = "$solutionDir/out/coreclr/PSScriptAnalyzer"
1817

1918
if ($build)
2019
{
20+
.\New-StronglyTypedCsFileForResx.ps1 Engine
2121
Push-Location Engine\
2222
dotnet build
2323
Pop-Location
2424

25+
.\New-StronglyTypedCsFileForResx.ps1 Rules
2526
Push-Location Rules\
2627
dotnet build
2728
Pop-Location
@@ -35,28 +36,27 @@ if ($build)
3536
Remove-Item "$destinationDir\*" -Recurse
3637
}
3738

38-
foreach ($file in $itemsToCopy)
39+
foreach ($file in $itemsToCopy)
3940
{
4041
Copy-Item -Path $file -Destination (Join-Path $destinationDir (Split-Path $file -Leaf)) -Verbose
4142
}
42-
(Get-Content "$solutionDir\Engine\PSScriptAnalyzer.psd1") -replace "ModuleVersion = '1.6.0'","ModuleVersion = '0.0.1'" | Out-File "$solutionDir\Engine\PSScriptAnalyzer.psd1"
43+
(Get-Content "$destinationDir\PSScriptAnalyzer.psd1") -replace "ModuleVersion = '1.6.0'","ModuleVersion = '0.0.1'" | Out-File "$destinationDir\PSScriptAnalyzer.psd1" -Encoding ascii
4344
}
4445

45-
$modulePath = "C:\Users\kborle\Documents\WindowsPowerShell\Modules";
46+
$modulePath = "$HOME\Documents\WindowsPowerShell\Modules";
4647
$pssaModulePath = Join-Path $modulePath PSScriptAnalyzer
47-
48+
4849

4950
if ($uninstall)
5051
{
5152
if ((Test-Path $pssaModulePath))
5253
{
5354
Remove-Item -Recurse $pssaModulePath -Verbose
5455
}
55-
56+
5657
}
5758

5859
if ($install)
5960
{
6061
Copy-Item -Recurse -Path "$destinationDir" -Destination "$modulePath\." -Verbose -Force
61-
}
62-
62+
}

0 commit comments

Comments
 (0)