Skip to content

Commit

Permalink
refactor: diagram script generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Sep 11, 2022
1 parent 0c11a58 commit d3fe63d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,4 @@
uuid = "728FEC82-9176-4D70-8E30-44BE9117AB39"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "139024BC-705D-4356-9F0A-E9B6F1D4F745"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftPlantUMLFramework/PlantUMLScript.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "45"
endingLineNumber = "45"
landmarkName = "init(items:configuration:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
84 changes: 51 additions & 33 deletions PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,61 @@ public func getRootViewController() -> UIViewController? {
getFirstWindow()?.rootViewController
}

fileprivate var plantUMLSymbols:[[String]] = [
struct Symbol : Identifiable, CustomStringConvertible {
var description: String {
return id
}

var id:String
private var _value:String?

var value: String {
get { _value ?? id }
}

init( _ id:String, _ value:String? = nil) {
self.id = id
self._value = value
}
}

fileprivate var plantUMLSymbols:[[Symbol]] = [
[
"title",
"header",
"footer",
"autonumber"
Symbol("title", "title my title"),
Symbol("header", "header my header"),
Symbol("footer", "footer my footer"),
Symbol("autonumber")
],
[
"participant",
"actor",
"boundary",
"control",
"entity",
"database",
"collections",
"queue"
Symbol("participant","participant \"my participant\" as P1"),
Symbol("actor", "actor \"my actor\" as A1"),
Symbol("boundary", "boundary \"my boundary\" as B1"),
Symbol("control", "control \"my control\" as C1"),
Symbol("entity", "entity \"my entity\" as E1"),
Symbol("database", "database \"my database\" as DB1"),
Symbol("collections","collections \"my collections\" as CC1" ),
Symbol("queue", "queue \"my queue\" as Q1")
],

[
"->x",
"->",
"->>",
"-\\\\",
"\\\\-",
"//--",
"->o",
"o\\\\--",
"<->",
"<->o"
Symbol("->x"),
Symbol("->"),
Symbol("->>"),
Symbol("-\\\\"),
Symbol("\\\\-"),
Symbol("//--"),
Symbol("->o"),
Symbol("o\\\\--"),
Symbol("<->"),
Symbol("<->o"),
],

[
"[#red]",
"note",
"end note"
Symbol("[#red]"),
Symbol("note", "note"),
Symbol("end note"),
]

]

fileprivate var plantUMLImages:[[UIImage?]] = {
Expand Down Expand Up @@ -144,11 +162,11 @@ public struct PlantUMLKeyboardView: View {

VStack(spacing: 15){

ForEach( Array(plantUMLSymbols.enumerated()), id: \.element) { rowIndex, i in
ForEach( Array(plantUMLSymbols.enumerated()), id: \.offset) { rowIndex, i in

HStack(spacing: 10) {

ForEach( Array(i.enumerated()), id: \.element ) { cellIndex, symbol in
ForEach( Array(i.enumerated()), id: \.offset ) { cellIndex, symbol in

Button {

Expand Down Expand Up @@ -183,7 +201,7 @@ public struct PlantUMLKeyboardView: View {
}

//
func replaceSymbolAtCursorPosition( _ symbol: String) {
func replaceSymbolAtCursorPosition( _ symbol: Symbol) {
guard let handleToYourTextView = getFirstTextFieldResponder() else {
return
}
Expand All @@ -193,7 +211,7 @@ public struct PlantUMLKeyboardView: View {
// [How to programmatically enter text in UITextView at the current cursor position](https://stackoverflow.com/a/35888634/521197)
if let range = handleToYourTextView.selectedTextRange {
// From your question I assume that you do not want to replace a selection, only insert some text where the cursor is.
handleToYourTextView.replace(range, withText: symbol)
handleToYourTextView.replace(range, withText: symbol.value )
}

}
Expand All @@ -210,12 +228,12 @@ struct KeyButtonStyle: ButtonStyle {

extension PlantUMLKeyboardView {

func ButtonLabel( rowIndex: Int, cellIndex: Int, symbol: String ) -> some View {
func ButtonLabel( rowIndex: Int, cellIndex: Int, symbol: Symbol ) -> some View {

Group {
if plantUMLImages[rowIndex].isEmpty || plantUMLImages[rowIndex].isEmpty || plantUMLImages[rowIndex][cellIndex]==nil
{
Text(symbol)
Text(symbol.description)
.font(.system(size: 16).bold())

}
Expand Down
11 changes: 10 additions & 1 deletion SwiftPlantUMLFramework/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ public enum AccessLevel: String, Codable {
/// Configuration options to influence the generation and visual representation of the class diagram
public struct Configuration: Codable {
/// memberwise initializer
public init(files: FileOptions = FileOptions(), elements: ElementOptions = ElementOptions(), hideShowCommands: [String]? = ["hide empty members"], skinparamCommands: [String]? = ["skinparam shadowing false"], includeRemoteURL: String? = nil, relationships: RelationshipOptions = RelationshipOptions(), stereotypes: Stereotypes = Stereotypes.default, relationshipExclude _: [String]? = nil) {
public init(
files: FileOptions = FileOptions(),
elements: ElementOptions = ElementOptions(),
hideShowCommands: [String]? = ["hide empty members"],
skinparamCommands: [String]? = ["skinparam shadowing false"],
includeRemoteURL: String? = nil,
relationships: RelationshipOptions = RelationshipOptions(),
stereotypes: Stereotypes = Stereotypes.default,
relationshipExclude _: [String]? = nil)
{
self.files = files
self.elements = elements
self.hideShowCommands = hideShowCommands
Expand Down
30 changes: 16 additions & 14 deletions SwiftPlantUMLFramework/PlantUMLScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public struct PlantUMLScript {

let methodStart = Date()

let STR2REPLACE = "STR2REPLACE"

// let plantumlTemplate = """
// @startuml
// ' STYLE START
Expand All @@ -32,14 +30,6 @@ public struct PlantUMLScript {
// @enduml
// """

var plantumlTemplate = "@startuml"
if let includeRemoteURL = configuration.includeRemoteURL {
plantumlTemplate.appendAsNewLine("!include \(includeRemoteURL)")
}
plantumlTemplate.appendAsNewLine(defaultStyling)
plantumlTemplate.appendAsNewLine("STR2REPLACE")
plantumlTemplate.appendAsNewLine("@enduml")

var replacingText = "\n"

for (index, element) in items.enumerated() {
Expand All @@ -48,9 +38,21 @@ public struct PlantUMLScript {
}
}

let neep = replacingText + "\n" + context.connections.joined(separator: "\n") + "\n" + context.extnConnections.joined(separator: "\n")
var includeURL = ""
if let includeRemoteURL = configuration.includeRemoteURL {
includeURL = "!include \(includeRemoteURL)"
}

text = plantumlTemplate.replacingOccurrences(of: STR2REPLACE, with: neep)
text =
"""
@startuml
\(includeURL)
\(defaultStyling)
\(replacingText)
\(context.connections.joined(separator: "\n"))
\(context.extnConnections.joined(separator: "\n"))
@enduml
"""

Logger.shared.debug("PlantUML script created in \(Date().timeIntervalSince(methodStart)) seconds")
}
Expand All @@ -77,10 +79,10 @@ public struct PlantUMLScript {
return ""
} else {
return """
' STYLE START
'' STYLE START
\(hideShowCommands.joined(separator: "\n"))
\(skinparamCommands.joined(separator: "\n"))
' STYLE END
'' STYLE END
"""
}
}
Expand Down

0 comments on commit d3fe63d

Please sign in to comment.