Last active
July 11, 2022 23:54
-
-
Save DanielSmon/e4a11ed13f8347be50fd918ba7f0415d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Reflection; | |
namespace DAS.Utils | |
{ | |
public static class EmbeddedResource | |
{ | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="file">The path and file name of the resource to read. Path can be separated either by forward slash, backslash, or period (.). If in a folder, the folder is relative to the project.</param> | |
/// <returns></returns> | |
public static Stream GetStream(string file) | |
{ | |
if (string.IsNullOrEmpty(file)) throw new ArgumentException("File path/name not supplied.", nameof(file)); | |
var parsedFilePath = file.Replace('\\', '.').Replace('/', '.').TrimStart('.'); | |
var resourceName = $"{typeof(EmbeddedResource).Assembly.FullName.Split(',')[0]}.{parsedFilePath}"; | |
return GetStreamInternal(resourceName); | |
} | |
private static Stream GetStreamInternal(string resourceName) | |
{ | |
var assembly = typeof(EmbeddedResource).GetTypeInfo().Assembly; | |
var resourceInfo = assembly.GetManifestResourceInfo(resourceName); | |
if (resourceInfo == null) | |
throw new FileNotFoundException( | |
"Can't find Embedded Test Resource file. Have you set 'Build Action' to 'Embedded resource' in the file's properties?", | |
resourceName); | |
return assembly.GetManifestResourceStream(resourceName); | |
} | |
/// <summary> | |
/// Use this to figure out the data folder location by convention. The convention requires: | |
/// - That the namespace matches the data file path. | |
/// - That the data folder is the same name as the supplied <paramref name="testType"/> | |
/// but with the "Tests" prefix replaced with "Data". | |
/// E.g: Folder\ThingTests.cs -> Folder\ThingData\file.ext | |
/// </summary> | |
/// <param name="testType"></param> | |
/// <param name="fileNameWithExtension"></param> | |
/// <returns></returns> | |
public static string Read(Type testType, string fileNameWithExtension) | |
{ | |
if (testType is null) throw new ArgumentNullException(nameof(testType)); | |
if (string.IsNullOrEmpty(fileNameWithExtension)) throw new ArgumentException("Must be supplied", nameof(fileNameWithExtension)); | |
const string suffix = "Tests"; | |
var dataFolder = testType.FullName.EndsWith(suffix, StringComparison.Ordinal) | |
? testType.FullName.Substring(0, testType.FullName.Length - suffix.Length) | |
: testType.FullName; | |
var resourceName = $"{testType.FullName}.{dataFolder}Data.{fileNameWithExtension}"; | |
using (Stream stream = GetStreamInternal(resourceName)) | |
using (StreamReader reader = new StreamReader(stream)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
public static string Read(string file) | |
{ | |
using (Stream stream = GetStream(file)) | |
using (StreamReader reader = new StreamReader(stream)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment