Skip to content

Commit

Permalink
Merge pull request #7 from 8T4/fix/load-c4-files
Browse files Browse the repository at this point in the history
Fix/load c4 files
  • Loading branch information
yanjustino authored Apr 12, 2021
2 parents 181d8f9 + 729b3d3 commit 3086a83
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 78 deletions.
41 changes: 10 additions & 31 deletions src/C4Sharp/C4Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,28 @@
<RepositoryUrl>https://github.com/8T4/c4sharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>c4, diagrams</PackageTags>
<PackageVersion>1.1.0</PackageVersion>

<PackageVersion>1.1.2</PackageVersion>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
</ItemGroup>

<ItemGroup>
<None Include="bin\icon.png" Pack="true" PackagePath="\" />
</ItemGroup>
</ItemGroup>

<!-- Embedded Resource For C4 Plunt UML and Plantuml -->
<ItemGroup>
<EmbeddedResource Include="bin\C4.puml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="bin\C4_Component.puml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="bin\C4_Container.puml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="bin\C4_Context.puml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="bin\C4_Deployment.puml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="bin\C4_Dynamic.puml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="bin\LICENSE.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="bin\C4.puml" />
<EmbeddedResource Include="bin\C4_Component.puml" />
<EmbeddedResource Include="bin\C4_Container.puml" />
<EmbeddedResource Include="bin\C4_Context.puml" />
<EmbeddedResource Include="bin\C4_Deployment.puml" />
<EmbeddedResource Include="bin\C4_Dynamic.puml" />
<EmbeddedResource Include="bin\plantuml.jar" />
</ItemGroup>
<ItemGroup>
<Content Include="bin\icon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
41 changes: 41 additions & 0 deletions src/C4Sharp/Extensions/ResourceMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.IO;
using System.Reflection;
using C4Sharp.Models.Plantuml;

namespace C4Sharp.Extensions
{
internal static class ResourceMethods
{
public static Stream GetPlantumlResource()
{
try
{
return Assembly
.GetExecutingAssembly()
.GetManifestResourceStream($"C4Sharp.bin.plantuml.jar");
}
catch (Exception e)
{
throw new PlantumlException($"{nameof(PlantumlException)}: Could not get resource.", e);
}
}

public static string GetResource(string name)
{
try
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = $"C4Sharp.bin.{name}";

using var stream = assembly.GetManifestResourceStream(resourceName);
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
catch (Exception e)
{
throw new PlantumlException($"{nameof(PlantumlException)}: Could not get resource.", e);
}
}
}
}
5 changes: 0 additions & 5 deletions src/C4Sharp/Extensions/StringMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ namespace C4Sharp.Extensions
{
internal static class StringMethods
{
internal static string AddSpaces(this string value, int size)
{
return value.PadLeft(size, ' ');
}

internal static string GenerateSlug(this string phrase)
{
var str = phrase.RemoveAccent().ToLower(CultureInfo.InvariantCulture);
Expand Down
38 changes: 38 additions & 0 deletions src/C4Sharp/FileSystem/C4Directory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using C4Sharp.Extensions;
using C4Sharp.Models.Diagrams;

namespace C4Sharp.FileSystem
{
internal static class C4Directory
{
public static string DirectoryName => "c4";
public static string ResourcesFolderName => "resources";
private static string ResourcesPath => Path.Join(DirectoryName, ResourcesFolderName);

public static void LoadResources(Diagram diagram)
{
LoadBaseResourceFile();
var path = Path.Join(ResourcesPath, $"{diagram.Name}.puml");

if (File.Exists(path))
return;

var stream = ResourceMethods.GetResource($"{diagram.Name}.puml");
Directory.CreateDirectory(ResourcesPath);
File.WriteAllText(path, stream);
}

private static void LoadBaseResourceFile()
{
var path = Path.Join(ResourcesPath, $"C4.puml");

if (File.Exists(path))
return;

var stream = ResourceMethods.GetResource($"C4.puml");
Directory.CreateDirectory(ResourcesPath);
File.WriteAllText(path, stream);
}
}
}
17 changes: 17 additions & 0 deletions src/C4Sharp/FileSystem/C4FileException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace C4Sharp.FileSystem
{
public class C4FileException: Exception
{
public C4FileException(string message):base(message)
{

}

public C4FileException(string message, Exception innerException):base(message, innerException)
{

}
}
}
5 changes: 1 addition & 4 deletions src/C4Sharp/Models/Diagrams/Diagram.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.IO;
using System.Text;
using C4Sharp.Extensions;
using C4Sharp.Models.Plantuml;
using C4Sharp.Extensions;
using C4Sharp.Models.Relationships;

namespace C4Sharp.Models.Diagrams
Expand Down
3 changes: 2 additions & 1 deletion src/C4Sharp/Models/Plantuml/PlantumlDiagram.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Text;
using C4Sharp.FileSystem;
using C4Sharp.Models.Diagrams;

namespace C4Sharp.Models.Plantuml
Expand All @@ -11,7 +12,7 @@ internal static class PlantumlDiagram
{
public static string ToPumlString(this Diagram diagram)
{
var path = Path.Join("..","bin", $"{diagram.Name}.puml");
var path = Path.Join(C4Directory.ResourcesFolderName, $"{diagram.Name}.puml");

var stream = new StringBuilder();
stream.AppendLine($"@startuml {diagram.Slug()}");
Expand Down
10 changes: 6 additions & 4 deletions src/C4Sharp/Models/Plantuml/PlantumlFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using C4Sharp.FileSystem;
using C4Sharp.Models.Diagrams;

namespace C4Sharp.Models.Plantuml
Expand All @@ -19,7 +20,7 @@ public static partial class PlantumlFile
/// <param name="diagram">C4 Diagram</param>
public static void Save(Diagram diagram)
{
Save(diagram, "c4");
Save(diagram, C4Directory.DirectoryName);
}

/// <summary>
Expand All @@ -31,6 +32,7 @@ public static void Save(Diagram diagram, string path)
{
try
{
C4Directory.LoadResources(diagram);
Directory.CreateDirectory(path);
var filePath = $"{path}/{diagram.Slug()}.puml";
File.WriteAllText(filePath, diagram.ToPumlString());
Expand All @@ -49,7 +51,7 @@ public static void Export(Diagram diagram)
{
Save(diagram);
var dirPath = Directory.GetCurrentDirectory();
var umlPath = Path.Join(dirPath, "c4", $"{diagram.Slug()}.puml");
var umlPath = Path.Join(dirPath, C4Directory.DirectoryName, $"{diagram.Slug()}.puml");
Export(umlPath, null);
}

Expand All @@ -62,7 +64,7 @@ public static void Export(Diagram diagram, PlantumlSession session)
{
Save(diagram);
var dirPath = Directory.GetCurrentDirectory();
var umlPath = Path.Join(dirPath, "c4", $"{diagram.Slug()}.puml");
var umlPath = Path.Join(dirPath, C4Directory.DirectoryName, $"{diagram.Slug()}.puml");
Export(umlPath, session);
}

Expand Down Expand Up @@ -122,7 +124,7 @@ public static void Export(IEnumerable<Diagram> diagrams, PlantumlSession session
}

var dirPath = Directory.GetCurrentDirectory();
var path = Path.Join(dirPath, "c4");
var path = Path.Join(dirPath, C4Directory.DirectoryName);

session ??= new PlantumlSession();
session.Execute(path, true);
Expand Down
17 changes: 2 additions & 15 deletions src/C4Sharp/Models/Plantuml/PlantumlResource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Reflection;
using C4Sharp.Extensions;
using C4Sharp.Models.Diagrams;

namespace C4Sharp.Models.Plantuml
Expand All @@ -16,7 +17,7 @@ public static string Load()
{
var fileName = Path.GetTempFileName();

using (var resource = GetResource())
using (var resource = ResourceMethods.GetPlantumlResource())
{
using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
Expand All @@ -43,19 +44,5 @@ public static void Clear(string file)
Console.WriteLine(e);
}
}

private static Stream GetResource()
{
try
{
return Assembly
.GetExecutingAssembly()
.GetManifestResourceStream($"C4Sharp.bin.plantuml.jar");
}
catch (Exception e)
{
throw new PlantumlException($"{nameof(PlantumlException)}: Could not get resource.", e);
}
}
}
}
18 changes: 0 additions & 18 deletions src/C4Sharp/bin/LICENSE.txt

This file was deleted.

Binary file removed src/C4Sharp/bin/logo.png
Binary file not shown.

0 comments on commit 3086a83

Please sign in to comment.