Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
MarcoEidinger/SwiftPlantUML
SwiftPlantUML
Generate UML class diagrams from swift code with this Command Line Interface (CLI) and Swift Package.
Use one or more Swift files as input for a diagram to visualize class
, struct
, protocol
, enum
and extension
types
with their instance and static members as well as their inheritance and implementation relationships
Table of Contents (click to expand)
Usage
Command Line
Example to generate and render diagram, based on a single Swift file, in your browser:
swiftplantuml ./Tests/SwiftPlantUMLFrameworkTests/TestData/basics.txt
Run swiftplantuml
in the directory containing the Swift files to be considered for diagram generation. Directories
will be searched recursively.
$ swiftplantuml classdiagram --help
OVERVIEW: Generate PlantUML script and view it and diagram in browser
USAGE: swift-plant-uml classdiagram [--config <config>] [--exclude <exclude> ...] [--output <format>] [--sdk <sdk>] [--verbose] [<paths> ...]
ARGUMENTS:
<paths> List of paths to the files or directories containing
swift sources
OPTIONS:
--config <config> Path to custom configuration filed (otherwise will
search for `.swiftplantuml.yml` in current directory)
--exclude <exclude> paths to ignore source files. Takes precedence over
arguments
--output <format> Defines output format. Options: browser,
browserImageOnly, consoleOnly
--sdk <sdk> MacOSX SDK path used to handle type inference
resolution, usually `$(xcrun --show-sdk-path -sdk
macosx)`
--verbose Verbose
--version Show the version.
-h, --help Show help information.
As classdiagram
is the default subcommand you can omit it.
Note: unknown type in diagrams for variables declared with type inference (e.g. var hasBasket = false
) unless you specify sdk
argument
Type inference details (click to expand)
In the following example the property hasBasket
is of type Bool
but the type is inferred and not explicitly declared.
class Bicycle: Vehicle {
var hasBasket = false
}
SwiftPlantUML requires the macosx SDK in order to properly recognize the inferred type. If you have Xcode installed (which includes the macosx SDK) then use --sdk $(xcrun --show-sdk-path -sdk macosx)
.
No inferred type (default) | inferred type | |
---|---|---|
Generated PlantUML | ~hasBasket |
~hasBasket : Bool |
Command | swiftplantuml classdiagram Tests/SwiftPlantUMLFrameworkTests/TestData/demo.txt |
swiftplantuml classdiagram Tests/SwiftPlantUMLFrameworkTests/TestData/demo.txt --sdk $(xcrun --show-sdk-path -sdk macosx) |
Swift package
dependencies: [
.package(url: "https://github.com/MarcoEidinger/SwiftPlantUML.git", .upToNextMajor(from: "0.3.0"))
]
This project is still in initial development. Generally this anything may change at any time and the public API should not be considered stable). However, I'll try to reserve breaking changes for new minor versions. You might feel more comfortable to pin the version to .upToNextMinor
or even to an .exact
version.
Xcode source editor extension
See MarcoEidinger/SwiftPlantUML-Xcode-Extension for more details
Installation
Using Homebrew
brew install MarcoEidinger/formulae/swiftplantuml
Using Mint
$ mint install MarcoEidinger/SwiftPlantUML
Installing from source
You can also build and install from source by cloning this project and running
make install
(Xcode 12 or later).
Manually Run the following commands to build and install manually:
$ git clone https://github.com/MarcoEidinger/SwiftPlantUML.git
$ cd SwiftPlantUML
$ make install
Configuration
Configure SwiftPlantUML by adding a .swiftplantuml.yml
file from the directory you'll run SwiftPlantUML from. Note: the same configuration options can be set programmatically with Configuration
.
Options
You are able to
- include/exclude files (wildcards supported)
- include/exclude elements by name (wildcards supported)
- limit elements and members bases on their access level, e.g. show only
public
types - hide extensions
- hide member access level attribute
- configure styles, use skin parameters and even include external files for theming (e.g. PUML Themes)
- exclude inheritance relationships based on parent (wildcards supported), e.g. do not show inheritance to
Codable
Examples
Simple Example:
files:
exclude:
- "Tests/**/*.swift" # paths to ignore for diagram. Takes precedence over `included`.
elements:
havingAccessLevel:
- public
showMembersWithAccessLevel:
- public
showExtensions: false
skinparamCommands: # see https://plantuml.com/skinparam
- skinparam classBackgroundColor PaleGreen
- skinparam classArrowColor SeaGreen
- skinparam classBorderColor SpringGreen
- skinparam stereotypeCBackgroundColor YellowGreen
- skinparam stereotypeCBackgroundColor<< Foo >> DimGray
relationships:
inheritance:
label: "inherits from"
style:
lineStyle: dotted
lineColor: DarkViolet
textColor: DarkViolet
exclude:
- "Codable"
Rich example: here
Code Completion during Editing
To get code completiong during editing use Visual Studio Code, the YAML extension created by Red Hat and the JSON schema for SwiftPlantUML.
In Visual Studio Code: Code
-> Preferences
-> Settings
-> search for yaml

Click on Edit in settings.json
and add the respective entry:
"yaml.schemas": {"https://raw.githubusercontent.com/MarcoEidinger/SwiftPlantUML/main/Configuration/Schema/json-schema-swiftplantuml.json": "/.swiftplantuml.yml" }
Planned improvements
- being able to render associations between elements
- being able to merge extensions with their known type
Known limitations
- huge diagrams in browser
- PlantUML limits image width and height to 4096 with the option to override this limit when using PlantUML.jar. locally so your option is to use the
--textonly
option and adjust/use it with PlantUML tools directly
- PlantUML limits image width and height to 4096 with the option to override this limit when using PlantUML.jar. locally so your option is to use the
Acknowledgements
This project was inspired by https://github.com/palaniraja and its various predecessors. Out of personal preference I chose to start a new project. I wanted to provide a tool for Swift developers written in Swift! This will hopefully allow me and potential contributors to work on planned improvements faster and more efficient.
Last but not least a big shoutout to the great developers of PlantUML and the people who operate the related online servers / tools available on http://plantuml.com/ and https://www.planttext.com/