Browse Source

reading from embedded resource instead of file

pull/3324/head
Holger Schmidt 8 months ago
parent
commit
6f91b14601
  1. 56
      ICSharpCode.ILSpyX/MermaidDiagrammer/EmbeddedResource.cs
  2. 14
      ICSharpCode.ILSpyX/MermaidDiagrammer/Generator.Run.cs

56
ICSharpCode.ILSpyX/MermaidDiagrammer/EmbeddedResource.cs

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
// Copyright (c) 2024 Holger Schmidt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.IO;
namespace ICSharpCode.ILSpyX.MermaidDiagrammer
{
public partial class GenerateHtmlDiagrammer
{
/// <summary>A helper for loading resources embedded in the nested html folder.</summary>
private static class EmbeddedResource
{
internal static string ReadText(string resourceName)
{
Stream stream = GetStream(resourceName);
using StreamReader reader = new(stream);
return reader.ReadToEnd();
}
internal static void CopyTo(string outputFolder, string resourceName)
{
Stream resourceStream = GetStream(resourceName);
using FileStream output = new(Path.Combine(outputFolder, resourceName), FileMode.Create, FileAccess.Write);
resourceStream.CopyTo(output);
}
private static Stream GetStream(string resourceName)
{
var type = typeof(EmbeddedResource);
var assembly = type.Assembly;
var fullResourceName = $"{type.Namespace}.html.{resourceName}";
Stream? stream = assembly.GetManifestResourceStream(fullResourceName);
if (stream == null)
throw new FileNotFoundException("Resource not found.", fullResourceName);
return stream;
}
}
}
}

14
ICSharpCode.ILSpyX/MermaidDiagrammer/Generator.Run.cs

@ -90,14 +90,10 @@ namespace ICSharpCode.ILSpyX.MermaidDiagrammer @@ -90,14 +90,10 @@ namespace ICSharpCode.ILSpyX.MermaidDiagrammer
private void GenerateOutput(string assemblyPath, ClassDiagrammer model)
{
var htmlSourcePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html");
string modelJson = SerializeModel(model);
var outputFolder = OutputFolder ??
/* If no out folder is specified and export mode is JsonOnly,
* default to the HTML diagrammer source folder - that's where it's most likely used.
* Otherwise default to a "netAmermaid" folder next to the input assembly. */
(JsonOnly ? htmlSourcePath : Path.Combine(Path.GetDirectoryName(assemblyPath) ?? string.Empty, "netAmermaid"));
// If no out folder is specified, default to a "netAmermaid" folder next to the input assembly.
var outputFolder = OutputFolder ?? Path.Combine(Path.GetDirectoryName(assemblyPath) ?? string.Empty, "netAmermaid");
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);
@ -109,7 +105,7 @@ namespace ICSharpCode.ILSpyX.MermaidDiagrammer @@ -109,7 +105,7 @@ namespace ICSharpCode.ILSpyX.MermaidDiagrammer
}
else
{
var htmlTemplate = File.ReadAllText(Path.Combine(htmlSourcePath, "template.html"));
var htmlTemplate = EmbeddedResource.ReadText("template.html");
var html = htmlTemplate
.Replace("{{SourceAssemblyName}}", model.SourceAssemblyName)
@ -121,8 +117,8 @@ namespace ICSharpCode.ILSpyX.MermaidDiagrammer @@ -121,8 +117,8 @@ namespace ICSharpCode.ILSpyX.MermaidDiagrammer
File.WriteAllText(Path.Combine(outputFolder, "class-diagrammer.html"), html);
// copy required resources to output folder while flattening paths if required
foreach (var path in new[] { "styles.css", "netAmermaid.ico", "script.js" })
File.Copy(Path.Combine(htmlSourcePath, path), Path.Combine(outputFolder, Path.GetFileName(path)), overwrite: true);
foreach (var resource in new[] { "styles.css", "netAmermaid.ico", "script.js" })
EmbeddedResource.CopyTo(outputFolder, resource);
Console.WriteLine("Successfully generated HTML diagrammer.");
}

Loading…
Cancel
Save