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