Skip to content

Commit b7d3bc4

Browse files
committed
Support resources
1 parent 131677c commit b7d3bc4

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Collections.Immutable;
5+
using System.IO;
6+
using System.Resources;
7+
using Microsoft.CodeAnalysis;
8+
9+
namespace StyleCop.Analyzers.Status.Generator
10+
{
11+
/// <summary>
12+
/// Provides a method that creates binary resources in memory
13+
/// </summary>
14+
internal class ResourceReader
15+
{
16+
/// <summary>
17+
/// Creates a <see cref="ResourceDescription"/> from a resx file im memory.
18+
/// </summary>
19+
/// <param name="name">The name of the resource.</param>
20+
/// < param name="path">The path to the resource</param>
21+
/// <returns>A resource description for the resx file</returns>
22+
internal static ResourceDescription ReadResource(string name, string path)
23+
{
24+
return new ResourceDescription(name, () =>
25+
{
26+
using (ResXResourceReader reader = new ResXResourceReader(path))
27+
{
28+
IDictionaryEnumerator enumerator = reader.GetEnumerator();
29+
MemoryStream memStream = new MemoryStream();
30+
using (IResourceWriter writer = new ResourceWriter(memStream))
31+
{
32+
while (enumerator.MoveNext())
33+
{
34+
writer.AddResource(enumerator.Key.ToString(), enumerator.Value);
35+
}
36+
}
37+
return new MemoryStream(memStream.ToArray());
38+
}
39+
}, false);
40+
}
41+
42+
/// <summary>
43+
/// Gets a list that contains a <see cref="ResourceDescription"/> for each resx
44+
/// file in the directory and it's subdirectories.
45+
/// </summary>
46+
/// <param name="root">
47+
/// The path of the root directory.
48+
/// </param>
49+
/// <returns>
50+
/// A <see cref="ImmutableArray{ResourceDescription}"/> that contains a <see cref="ResourceDescription"/> for each resx
51+
/// file in the directory and it's subdirectories.
52+
/// </returns>
53+
internal static ImmutableArray<ResourceDescription> GetResourcesRecursive(string root)
54+
{
55+
root = Path.GetFullPath(root);
56+
List<ResourceDescription> descriptions = new List<ResourceDescription>();
57+
58+
foreach (var path in Directory.EnumerateFiles(root, "*.resx", SearchOption.AllDirectories))
59+
{
60+
string relativePath = Path.ChangeExtension(Path.GetFullPath(path)
61+
.Substring(root.Length)
62+
.Replace(Path.DirectorySeparatorChar, '.')
63+
.Replace(Path.AltDirectorySeparatorChar, '.'), "resources");
64+
65+
descriptions.Add(ReadResource(relativePath, path));
66+
}
67+
68+
return ImmutableArray.CreateRange(descriptions);
69+
}
70+
}
71+
}

StyleCop.Analyzers.Status.Generator/SolutionReader.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ private void Compile()
9393
{
9494
MemoryStream memStream = new MemoryStream();
9595

96-
var emitResult = this.compilation.Emit(memStream);
96+
string path = Path.Combine(Path.GetDirectoryName(this.SlnPath), this.ProjectName);
97+
98+
var emitResult = this.compilation.Emit(memStream, manifestResources: ResourceReader.GetResourcesRecursive(path));
9799

98100
if (!emitResult.Success)
99101
{

StyleCop.Analyzers.Status.Generator/StyleCop.Analyzers.Status.Generator.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<HintPath>..\packages\System.Reflection.Metadata.1.0.18-beta\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
9999
<Private>True</Private>
100100
</Reference>
101+
<Reference Include="System.Windows.Forms" />
101102
<Reference Include="System.Xml.Linq" />
102103
<Reference Include="System.Data.DataSetExtensions" />
103104
<Reference Include="Microsoft.CSharp" />
@@ -110,6 +111,7 @@
110111
<Compile Include="CompilationFailedException.cs" />
111112
<Compile Include="Program.cs" />
112113
<Compile Include="Properties\AssemblyInfo.cs" />
114+
<Compile Include="ResourceReader.cs" />
113115
<Compile Include="SolutionReader.cs" />
114116
<Compile Include="StyleCopDiagnostic.cs" />
115117
</ItemGroup>

0 commit comments

Comments
 (0)