Skip to content

Commit

Permalink
Refactoring PlantumlFile.cs strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
yanjustino committed Apr 4, 2021
1 parent 16c9c07 commit cd9ab74
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 95 deletions.
17 changes: 17 additions & 0 deletions src/C4Sharp/Models/Plantuml/PlantumlException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace C4Sharp.Models.Plantuml
{
public class PlantumlException: Exception
{
public PlantumlException(string message):base(message)
{

}

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

}
}
}
162 changes: 110 additions & 52 deletions src/C4Sharp/Models/Plantuml/PlantumlFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -15,65 +17,65 @@ public static partial class PlantumlFile
/// Save puml file on c4/[file name].puml
/// </summary>
/// <param name="diagram">C4 Diagram</param>
public static string Save(Diagram diagram)
public static void Save(Diagram diagram)
{
Directory.CreateDirectory("c4");
return Save(diagram, "c4");
Save(diagram, "c4");
}

/// <summary>
/// Save puml file
/// Save puml file. It's creates path if non exists.
/// </summary>
/// <param name="diagram">C4 Diagram</param>
/// <param name="path">Output path</param>
public static string Save(Diagram diagram, string path)
public static void Save(Diagram diagram, string path)
{
var filePath = $"{path}/{diagram.Slug()}.puml";
File.WriteAllText(filePath, diagram.ToPumlString());
return filePath;
try
{
Directory.CreateDirectory(path);
var filePath = $"{path}/{diagram.Slug()}.puml";
File.WriteAllText(filePath, diagram.ToPumlString());
}
catch (Exception e)
{
throw new PlantumlException($"{nameof(PlantumlException)}: Could not save puml file.", e);
}
}

/// <summary>
/// It's creates a PUML file and exports the diagram to PNG File
/// </summary>
/// <param name="diagram">C4 Diagram</param>
public static PlantumlResult Export(Diagram diagram)
public static void Export(Diagram diagram)
{
using (var session = new PlantumlSession())
{
Save(diagram);
var dirPath = Directory.GetCurrentDirectory();
var umlPath = Path.Join(dirPath, "c4", $"{diagram.Slug()}.puml");
return Export(umlPath, session);
}
Save(diagram);
var dirPath = Directory.GetCurrentDirectory();
var umlPath = Path.Join(dirPath, "c4", $"{diagram.Slug()}.puml");
Export(umlPath, null);
}

/// <summary>
/// It's creates a PUML file and exports the diagram to PNG File
/// </summary>
/// <param name="diagram">C4 Diagram</param>
/// <param name="session">Plantuml Session</param>
public static PlantumlResult Export(Diagram diagram, PlantumlSession session)
public static void Export(Diagram diagram, PlantumlSession session)
{
Save(diagram);
var dirPath = Directory.GetCurrentDirectory();
var umlPath = Path.Join(dirPath, "c4", $"{diagram.Slug()}.puml");
return Export(umlPath, session);
Export(umlPath, session);
}

/// <summary>
/// It's creates a PUML file and exports the diagram to PNG File
/// </summary>
/// <param name="diagram"></param>
/// <param name="path">Output path</param>
public static PlantumlResult Export(Diagram diagram, string path)
public static void Export(Diagram diagram, string path)
{
using (var session = new PlantumlSession())
{
Save(diagram, path);
var umlPath = Path.Join(path, $"{diagram.Slug()}.puml");
return Export(umlPath, session);
}
Save(diagram, path);
var umlPath = Path.Join(path, $"{diagram.Slug()}.puml");
Export(umlPath, null);
}

/// <summary>
Expand All @@ -82,21 +84,59 @@ public static PlantumlResult Export(Diagram diagram, string path)
/// <param name="diagram">C4 Diagram</param>
/// <param name="path">Output path</param>
/// <param name="session">Plantuml Session</param>
public static PlantumlResult Export(Diagram diagram, string path, PlantumlSession session)
public static void Export(Diagram diagram, string path, PlantumlSession session)
{
Save(diagram, path);
var umlPath = Path.Join(path, $"{diagram.Slug()}.puml");
return Export(umlPath, session);
Export(umlPath, session);
}

/// <summary>
/// It's creates a PUML file and exports the diagram to PNG File
/// </summary>
/// <param name="diagrams">C4 Diagrams</param>
/// <param name="path">Output path</param>
/// <param name="session">Plantuml Session</param>
public static void Export(IEnumerable<Diagram> diagrams, string path, PlantumlSession session)
{
foreach (var diagram in diagrams)
{
Save(diagram, path);
}

session ??= new PlantumlSession();
session.Execute(path, true);
}

/// <summary>
/// It's creates a PUML file and exports the diagram to PNG File
/// </summary>
/// <param name="diagrams">C4 Diagrams</param>
/// <param name="path">Output path</param>
/// <param name="session">Plantuml Session</param>
public static void Export(IEnumerable<Diagram> diagrams, PlantumlSession session)
{
foreach (var diagram in diagrams)
{
Save(diagram);
}

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

session ??= new PlantumlSession();
session.Execute(path, true);
}

/// <summary>
/// Export Diagram to PNG File
/// </summary>
/// <param name="pumlPath">PUML file path</param>
/// <param name="session">Plantuml Session</param>
private static PlantumlResult Export(string pumlPath, PlantumlSession session)
private static void Export(string pumlPath, PlantumlSession session)
{
return session.Execute(pumlPath);
session ??= new PlantumlSession();
session.Execute(pumlPath, false);
}
}

Expand All @@ -111,11 +151,11 @@ public static partial class PlantumlFile
/// <param name="diagram">C4 Diagram</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<string> SaveAsync(Diagram diagram, CancellationToken cancellationToken)
public static Task SaveAsync(Diagram diagram, CancellationToken cancellationToken)
{
return cancellationToken.IsCancellationRequested
? Task.FromCanceled<string>(cancellationToken)
: Task.FromResult(Save(diagram));
: Task.CompletedTask;
}

/// <summary>
Expand All @@ -125,24 +165,28 @@ public static Task<string> SaveAsync(Diagram diagram, CancellationToken cancella
/// <param name="path">Output path</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<string> SaveAsync(Diagram diagram, string path, CancellationToken cancellationToken)
public static Task SaveAsync(Diagram diagram, string path, CancellationToken cancellationToken)
{
return cancellationToken.IsCancellationRequested
? Task.FromCanceled<string>(cancellationToken)
: Task.FromResult(Save(diagram, path));
: Task.CompletedTask;
}

/// <summary>
/// Export Diagram to PNG File
/// </summary>
/// <param name="diagram"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<PlantumlResult> ExportAsync(Diagram diagram, CancellationToken cancellationToken)
public static Task ExportAsync(Diagram diagram, CancellationToken cancellationToken)
{
return cancellationToken.IsCancellationRequested
? Task.FromCanceled<PlantumlResult>(cancellationToken)
: Task.FromResult(Export(diagram));
if (cancellationToken.IsCancellationRequested)
{
Task.FromCanceled(cancellationToken);
}

Export(diagram);
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -152,11 +196,16 @@ public static Task<PlantumlResult> ExportAsync(Diagram diagram, CancellationToke
/// <param name="session">Plantuml Session</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<PlantumlResult> ExportAsync(Diagram diagram, PlantumlSession session, CancellationToken cancellationToken)
public static Task ExportAsync(Diagram diagram, PlantumlSession session,
CancellationToken cancellationToken)
{
return cancellationToken.IsCancellationRequested
? Task.FromCanceled<PlantumlResult>(cancellationToken)
: Task.FromResult(Export(diagram, session));
if (cancellationToken.IsCancellationRequested)
{
Task.FromCanceled(cancellationToken);
}

Export(diagram, session);
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -166,11 +215,16 @@ public static Task<PlantumlResult> ExportAsync(Diagram diagram, PlantumlSession
/// <param name="path">Output path</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<PlantumlResult> ExportAsync(Diagram diagram, string path, CancellationToken cancellationToken)
public static Task ExportAsync(Diagram diagram, string path,
CancellationToken cancellationToken)
{
return cancellationToken.IsCancellationRequested
? Task.FromCanceled<PlantumlResult>(cancellationToken)
: Task.FromResult(Export(diagram, path));
if (cancellationToken.IsCancellationRequested)
{
Task.FromCanceled(cancellationToken);
}

Export(diagram, path);
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -181,12 +235,16 @@ public static Task<PlantumlResult> ExportAsync(Diagram diagram, string path, Can
/// <param name="session">Plantuml Session</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<PlantumlResult> ExportAsync(Diagram diagram, string path, PlantumlSession session,
public static Task ExportAsync(Diagram diagram, string path, PlantumlSession session,
CancellationToken cancellationToken)
{
return cancellationToken.IsCancellationRequested
? Task.FromCanceled<PlantumlResult>(cancellationToken)
: Task.FromResult(Export(diagram, path, session));
}
if (cancellationToken.IsCancellationRequested)
{
Task.FromCanceled(cancellationToken);
}

Export(diagram, path, session);
return Task.CompletedTask;
}
}
}
42 changes: 33 additions & 9 deletions src/C4Sharp/Models/Plantuml/PlantumlResource.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Reflection;
using C4Sharp.Models.Diagrams;
Expand All @@ -11,27 +12,50 @@ internal static class PlantumlResource
{
public static string Load()
{
var fileName = Path.GetTempFileName();

using (var resource = GetResource())
try
{
using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
var fileName = Path.GetTempFileName();

using (var resource = GetResource())
{
resource.CopyTo(file);
using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
resource.CopyTo(file);
}
}
}

return fileName;
return fileName;
}
catch (Exception e)
{
throw new PlantumlException($"{nameof(PlantumlException)}: Could not load plantuml engine.", e);
}
}

public static void Clear(string file)
{
File.Delete(file);
try
{
File.Delete(file);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

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

This file was deleted.

Loading

0 comments on commit cd9ab74

Please sign in to comment.