2017-10-04 23:11:23 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.CLI
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class EmbeddedFileHelper
|
|
|
|
|
|
{
|
2021-06-28 15:43:40 +02:00
|
|
|
|
private const string ResourcePath = "ElectronNET.CLI.{0}";
|
2021-06-28 16:08:00 +02:00
|
|
|
|
private const string ResourcePath2 = "ElectronNet.CLI.{0}";
|
2017-10-04 23:11:23 +02:00
|
|
|
|
|
2017-10-05 21:28:47 +02:00
|
|
|
|
private static Stream GetTestResourceFileStream(string folderAndFileInProjectPath)
|
2017-10-04 23:11:23 +02:00
|
|
|
|
{
|
|
|
|
|
|
var asm = Assembly.GetExecutingAssembly();
|
|
|
|
|
|
var resource = string.Format(ResourcePath, folderAndFileInProjectPath);
|
2021-06-28 16:08:00 +02:00
|
|
|
|
var resource2 = string.Format(ResourcePath2, folderAndFileInProjectPath);
|
|
|
|
|
|
|
|
|
|
|
|
var stream = asm.GetManifestResourceStream(resource) ?? asm.GetManifestResourceStream(resource2);
|
2017-10-04 23:11:23 +02:00
|
|
|
|
|
2021-06-28 15:43:40 +02:00
|
|
|
|
if(stream is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
PrintAllResources();
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Was missing resource: {0}", resource);
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return stream;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-28 15:43:55 +02:00
|
|
|
|
public static void PrintAllResources()
|
2021-06-28 15:43:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var asm = Assembly.GetExecutingAssembly();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var n in asm.GetManifestResourceNames())
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Found resource : {0}", n);
|
|
|
|
|
|
}
|
2017-10-04 23:11:23 +02:00
|
|
|
|
}
|
2017-10-15 22:43:35 +02:00
|
|
|
|
|
2017-10-07 01:32:19 +02:00
|
|
|
|
public static void DeployEmbeddedFile(string targetPath, string file, string namespacePath = "")
|
2017-10-05 21:28:47 +02:00
|
|
|
|
{
|
|
|
|
|
|
using (var fileStream = File.Create(Path.Combine(targetPath, file)))
|
|
|
|
|
|
{
|
2017-10-15 22:43:35 +02:00
|
|
|
|
var streamFromEmbeddedFile = GetTestResourceFileStream("ElectronHost." + namespacePath + file);
|
|
|
|
|
|
if (streamFromEmbeddedFile == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Error: Couldn't find embedded file: " + file);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-05 21:28:47 +02:00
|
|
|
|
streamFromEmbeddedFile.CopyTo(fileStream);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-11-30 23:30:15 +01:00
|
|
|
|
|
|
|
|
|
|
public static void DeployEmbeddedFileToTargetFile(string targetPath, string embeddedFile, string targetFile, string namespacePath = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var fileStream = File.Create(Path.Combine(targetPath, targetFile)))
|
|
|
|
|
|
{
|
|
|
|
|
|
var streamFromEmbeddedFile = GetTestResourceFileStream("ElectronHost." + namespacePath + embeddedFile);
|
|
|
|
|
|
if (streamFromEmbeddedFile == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Error: Couldn't find embedded file: " + embeddedFile);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
streamFromEmbeddedFile.CopyTo(fileStream);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-04 23:11:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|