diff --git a/src/C4Sharp/C4Sharp.csproj b/src/C4Sharp/C4Sharp.csproj index 6097739..7d15ecd 100644 --- a/src/C4Sharp/C4Sharp.csproj +++ b/src/C4Sharp/C4Sharp.csproj @@ -11,49 +11,28 @@ https://github.com/8T4/c4sharp git c4, diagrams - 1.1.0 - + 1.1.2 true true - snupkg + snupkg - + - + - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - + + + + + + - - - PreserveNewest - - diff --git a/src/C4Sharp/Extensions/ResourceMethods.cs b/src/C4Sharp/Extensions/ResourceMethods.cs new file mode 100644 index 0000000..906ed76 --- /dev/null +++ b/src/C4Sharp/Extensions/ResourceMethods.cs @@ -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); + } + } + } +} \ No newline at end of file diff --git a/src/C4Sharp/Extensions/StringMethods.cs b/src/C4Sharp/Extensions/StringMethods.cs index beecb76..307d808 100644 --- a/src/C4Sharp/Extensions/StringMethods.cs +++ b/src/C4Sharp/Extensions/StringMethods.cs @@ -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); diff --git a/src/C4Sharp/FileSystem/C4Directory.cs b/src/C4Sharp/FileSystem/C4Directory.cs new file mode 100644 index 0000000..9bcb5a5 --- /dev/null +++ b/src/C4Sharp/FileSystem/C4Directory.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/C4Sharp/FileSystem/C4FileException.cs b/src/C4Sharp/FileSystem/C4FileException.cs new file mode 100644 index 0000000..9404f29 --- /dev/null +++ b/src/C4Sharp/FileSystem/C4FileException.cs @@ -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) + { + + } + } +} \ No newline at end of file diff --git a/src/C4Sharp/Models/Diagrams/Diagram.cs b/src/C4Sharp/Models/Diagrams/Diagram.cs index 8c95484..29d2135 100644 --- a/src/C4Sharp/Models/Diagrams/Diagram.cs +++ b/src/C4Sharp/Models/Diagrams/Diagram.cs @@ -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 diff --git a/src/C4Sharp/Models/Plantuml/PlantumlDiagram.cs b/src/C4Sharp/Models/Plantuml/PlantumlDiagram.cs index 1b5fe5f..e1706f9 100644 --- a/src/C4Sharp/Models/Plantuml/PlantumlDiagram.cs +++ b/src/C4Sharp/Models/Plantuml/PlantumlDiagram.cs @@ -1,5 +1,6 @@ using System.IO; using System.Text; +using C4Sharp.FileSystem; using C4Sharp.Models.Diagrams; namespace C4Sharp.Models.Plantuml @@ -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()}"); diff --git a/src/C4Sharp/Models/Plantuml/PlantumlFile.cs b/src/C4Sharp/Models/Plantuml/PlantumlFile.cs index 2c18cf7..fb9356a 100644 --- a/src/C4Sharp/Models/Plantuml/PlantumlFile.cs +++ b/src/C4Sharp/Models/Plantuml/PlantumlFile.cs @@ -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 @@ -19,7 +20,7 @@ public static partial class PlantumlFile /// C4 Diagram public static void Save(Diagram diagram) { - Save(diagram, "c4"); + Save(diagram, C4Directory.DirectoryName); } /// @@ -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()); @@ -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); } @@ -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); } @@ -122,7 +124,7 @@ public static void Export(IEnumerable 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); diff --git a/src/C4Sharp/Models/Plantuml/PlantumlResource.cs b/src/C4Sharp/Models/Plantuml/PlantumlResource.cs index 7aa5da4..b8e38a9 100644 --- a/src/C4Sharp/Models/Plantuml/PlantumlResource.cs +++ b/src/C4Sharp/Models/Plantuml/PlantumlResource.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Reflection; +using C4Sharp.Extensions; using C4Sharp.Models.Diagrams; namespace C4Sharp.Models.Plantuml @@ -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)) { @@ -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); - } - } } } \ No newline at end of file diff --git a/src/C4Sharp/bin/LICENSE.txt b/src/C4Sharp/bin/LICENSE.txt deleted file mode 100644 index bb6fad5..0000000 --- a/src/C4Sharp/bin/LICENSE.txt +++ /dev/null @@ -1,18 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2016 jebbs - -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. diff --git a/src/C4Sharp/bin/logo.png b/src/C4Sharp/bin/logo.png deleted file mode 100644 index db0e251..0000000 Binary files a/src/C4Sharp/bin/logo.png and /dev/null differ