|
29 | 29 | {{~ end ~}}
|
30 | 30 | public static partial class {{ $0.Name }}
|
31 | 31 | {
|
| 32 | + #if DEBUG |
| 33 | + static readonly string baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ""; |
| 34 | + #endif |
| 35 | + |
32 | 36 | {{~ if $0.IsText ~}}
|
33 | 37 | private static string text;
|
34 | 38 |
|
35 | 39 | /// <summary>
|
36 | 40 | /// Gets the resource as plain text.
|
37 | 41 | /// </summary>
|
38 |
| - public static string Text => |
39 |
| - text ??= EmbeddedResource.GetContent(@"{{ $0.Path }}"); |
| 42 | + public static string Text => text ??= GetContent(@"{{ $0.Path }}"); |
40 | 43 | {{~ end ~}}
|
41 | 44 |
|
42 | 45 | /// <summary>
|
43 | 46 | /// Gets the resource as a byte array.
|
44 | 47 | /// </summary>
|
45 |
| - public static byte[] GetBytes() => |
46 |
| - EmbeddedResource.GetBytes(@"{{ $0.Path }}"); |
| 48 | + public static byte[] GetBytes() => GetBytes(@"{{ $0.Path }}"); |
47 | 49 |
|
48 | 50 | /// <summary>
|
49 | 51 | /// Gets the resource as a stream.
|
50 | 52 | /// </summary>
|
51 |
| - public static Stream GetStream() => |
52 |
| - EmbeddedResource.GetStream(@"{{ $0.Path }}"); |
| 53 | + public static Stream GetStream() => GetStream(@"{{ $0.Path }}"); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets the content of the embedded resource at the specified relative path. |
| 57 | + /// </summary> |
| 58 | + static string GetContent(string relativePath) |
| 59 | + { |
| 60 | + using var stream = GetStream(relativePath); |
| 61 | + using var reader = new StreamReader(stream); |
| 62 | + return reader.ReadToEnd(); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Gets the bytes of the embedded resource at the specified relative path. |
| 67 | + /// </summary> |
| 68 | + static byte[] GetBytes(string relativePath) |
| 69 | + { |
| 70 | + using var stream = GetStream(relativePath); |
| 71 | + var bytes = new byte[stream.Length]; |
| 72 | + stream.Read(bytes, 0, bytes.Length); |
| 73 | + return bytes; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Gets the stream of the embedded resource at the specified relative path. |
| 78 | + /// </summary> |
| 79 | + /// <exception cref="InvalidOperationException"></exception> |
| 80 | + static Stream GetStream(string relativePath) |
| 81 | + { |
| 82 | + var baseName = Assembly.GetExecutingAssembly().GetName().Name; |
| 83 | + var resourceName = relativePath |
| 84 | + .TrimStart('.') |
| 85 | + .Replace('/', '.') |
| 86 | + .Replace('\\', '.'); |
| 87 | + |
| 88 | + var manifestResourceName = Assembly.GetExecutingAssembly() |
| 89 | + .GetManifestResourceNames().FirstOrDefault(x => x.EndsWith(resourceName, StringComparison.Ordinal)); |
| 90 | + |
| 91 | + if (string.IsNullOrEmpty(manifestResourceName)) |
| 92 | + throw new InvalidOperationException($"Did not find required resource ending in '{resourceName}' in assembly '{baseName}'."); |
| 93 | + |
| 94 | + return |
| 95 | + Assembly.GetExecutingAssembly().GetManifestResourceStream(manifestResourceName) ?? |
| 96 | + throw new InvalidOperationException($"Did not find required resource '{manifestResourceName}' in assembly '{baseName}'."); |
| 97 | + } |
53 | 98 | }
|
54 | 99 | {{ end }}
|
55 | 100 | {{ func render }}
|
|
70 | 115 |
|
71 | 116 | using System;
|
72 | 117 | using System.IO;
|
| 118 | +using System.Linq; |
| 119 | +using System.Reflection; |
73 | 120 | {{ if Namespace }}
|
74 | 121 | namespace {{ Namespace }};
|
75 | 122 | {{~ end ~}}
|
|
0 commit comments