Skip to content

Commit 4584a45

Browse files
committed
Added support for generating resource files in TS from .resx
1 parent 1bf21ab commit 4584a45

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace WebApiToTypeScript.Resources
2+
{
3+
public class ParameterTransform
4+
{
5+
public string Source { get; set; }
6+
public string Destination { get; set; }
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using WebApiToTypeScript.Block;
2+
3+
namespace WebApiToTypeScript.Resources
4+
{
5+
public class ResourceBlock
6+
{
7+
public TypeScriptBlock TypeScriptBlock { get; set; }
8+
public string Filename { get; set; }
9+
}
10+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Resources;
5+
using System.Text.RegularExpressions;
6+
using WebApiToTypeScript.Block;
7+
8+
namespace WebApiToTypeScript.Resources
9+
{
10+
public class ResourceService : ServiceAware
11+
{
12+
public Regex ParamRegex { get; }
13+
= new Regex(@"{(\w*)}");
14+
15+
public IEnumerable<ResourceBlock> GetBlocksForResources()
16+
{
17+
foreach (var resourceConfig in Config.ResourceConfigs)
18+
{
19+
var resourceFilename = Path.GetFileNameWithoutExtension(resourceConfig.SourcePath);
20+
var resourceBlock = CreateResourceBlock();
21+
22+
var block = resourceBlock
23+
.AddAndUseBlock($"export class {resourceFilename}");
24+
25+
var resourceReader = new ResXResourceReader(resourceConfig.SourcePath);
26+
var dictionary = resourceReader.GetEnumerator();
27+
28+
while (dictionary.MoveNext())
29+
{
30+
var parameters = new List<ParameterTransform>();
31+
32+
var matches = ParamRegex.Matches(dictionary.Value.ToString())
33+
.GetEnumerator();
34+
35+
while (matches.MoveNext())
36+
{
37+
var match = (Match)matches.Current;
38+
39+
var source = match.Groups[1].Value;
40+
41+
int integer;
42+
var isInteger = int.TryParse(source, out integer);
43+
44+
var destination = isInteger
45+
? $"slot{source}"
46+
: source;
47+
48+
parameters.Add(new ParameterTransform
49+
{
50+
Source = source,
51+
Destination = destination
52+
});
53+
}
54+
55+
var originalValue = dictionary.Value.ToString()
56+
.Replace("\"", "\\\"");
57+
58+
if (parameters.Any())
59+
{
60+
var paramsString = string.Join(", ", parameters.Select(p => $"{p.Destination}: string"));
61+
62+
var transformedValue = parameters
63+
.Aggregate(originalValue, (current, parameterTransform) => current.Replace(parameterTransform.Source, parameterTransform.Destination))
64+
.Replace("{", "${");
65+
66+
block
67+
.AddAndUseBlock($"static {dictionary.Key}({paramsString})")
68+
.AddStatement($"return `{transformedValue}`;");
69+
}
70+
else
71+
{
72+
block
73+
.AddAndUseBlock($"static get {dictionary.Key}()")
74+
.AddStatement($"return `{originalValue}`;");
75+
}
76+
}
77+
78+
yield return new ResourceBlock
79+
{
80+
TypeScriptBlock = resourceBlock,
81+
Filename = resourceConfig.OutputFilename
82+
};
83+
}
84+
}
85+
86+
private TypeScriptBlock CreateResourceBlock()
87+
{
88+
return new TypeScriptBlock($"{Config.NamespaceOrModuleName} {Config.ResourcesNamespace}");
89+
}
90+
}
91+
}

src/WebApiToTypeScript/WebApiToTypeScript.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using WebApiToTypeScript.Endpoints;
1010
using WebApiToTypeScript.Enums;
1111
using WebApiToTypeScript.Interfaces;
12+
using WebApiToTypeScript.Resources;
1213
using WebApiToTypeScript.Types;
1314
using WebApiToTypeScript.Views;
1415
using WebApiToTypeScript.WebApi;
@@ -31,6 +32,7 @@ public class WebApiToTypeScript : AppDomainIsolatedTask
3132
public static AngularEndpointsService AngularEndpointsService { get; private set; }
3233

3334
public static ViewsService ViewsService { get; private set; }
35+
public static ResourceService ResourceService { get; private set; }
3436

3537
[Required]
3638
public string ConfigFilePath { get; set; }
@@ -54,6 +56,16 @@ public override bool Execute()
5456
StopAnalysis();
5557
}
5658

59+
if (Config.GenerateResources)
60+
{
61+
StartAnalysis("resources");
62+
63+
foreach (var resourceBlock in ResourceService.GetBlocksForResources())
64+
CreateFileForBlock(resourceBlock.TypeScriptBlock, Config.ResourcesOutputDirectory, resourceBlock.Filename);
65+
66+
StopAnalysis();
67+
}
68+
5769
StartAnalysis("controllers and actions");
5870

5971
foreach (var apiController in apiControllers)
@@ -112,6 +124,7 @@ private void InitializeServices()
112124
AngularEndpointsService = new AngularEndpointsService();
113125

114126
ViewsService = new ViewsService();
127+
ResourceService = new ResourceService();
115128
}
116129

117130
private Config.Config GetConfig(string configFilePath)

src/WebApiToTypeScript/WebApiToTypeScript.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
</Reference>
5555
<Reference Include="System" />
5656
<Reference Include="System.Core" />
57+
<Reference Include="System.Windows.Forms" />
5758
<Reference Include="System.Xml.Linq" />
5859
<Reference Include="System.Data.DataSetExtensions" />
5960
<Reference Include="Microsoft.CSharp" />
@@ -64,9 +65,13 @@
6465
<ItemGroup>
6566
<Compile Include="Config\Config.cs" />
6667
<Compile Include="Config\InterfaceMatch.cs" />
68+
<Compile Include="Config\ResourceConfig.cs" />
6769
<Compile Include="Config\ViewConfig.cs" />
6870
<Compile Include="Endpoints\AngularEndpointsService.cs" />
6971
<Compile Include="Endpoints\EndpointsService.cs" />
72+
<Compile Include="Resources\ParameterTransform.cs" />
73+
<Compile Include="Resources\ResourceService.cs" />
74+
<Compile Include="Resources\ResourceBlock.cs" />
7075
<Compile Include="ServiceAware.cs" />
7176
<Compile Include="Types\ConstructorParameterMapping.cs" />
7277
<Compile Include="Types\CSharpType.cs" />

0 commit comments

Comments
 (0)