Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dangdungcntt/diagram-as-code


https://github.com/dangdungcntt/diagram-as-code

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# diagram-as-code

## Installation
```bash
composer require nddcoder/diagram-as-code
```

## Usage

Create a `demo.php` and return `Diagram` instance

```php
direction(Style::DIR_LR)
->append(
Cluster::create('kafka', 'Kafka Cluster')
->append($kafka = Kafka::create('kafka', 'Kafka'))
)
->append(
Cluster::create('event_driven', 'Event Driven')
->append($consumer = Ec2::create('consumer', 'Consumer'))
)
->append($database = Mysql::create('mysql', 'Mysql'))
->connect($kafka, $consumer, EdgeStyle::create()->style(Style::BOLD)->color(Color::BROWN))
->connect($consumer, $database, EdgeStyle::create()->style(Style::BOLD)->color(Color::DARKGREEN)->label('WRITE'));
```

Run `dac` command to build `dot` config

```bash
vendor/bin/dac demo.php
```

Dot config output

```dot
digraph G {
label="Demo Diagram As Code";
node [shape=box, style=rounded, fixedsize=true, width="1.4", height="1.4", labelloc=b, imagescale=true, fontname="Sans-Serif", fontsize="13", fontcolor="#2D3436"];
edge [color="#7B8894", fontcolor="#2D3436", fontname="Sans-Serif", fontsize="13"];
graph [pad="0.5", splines=ortho, nodesep="1.2", ranksep="1.2", fontname="Sans-Serif", fontsize="15", fontcolor="#2D3436"];
rankdir=LR;
subgraph cluster_kafka {
label="Kafka Cluster";
graph [pad="1", shape=box, style=rounded, labeljust=l, pencolor="#AEB6BE", fontname="Sans-Serif", fontsize="12", bgcolor="#E5F5FD"];
kafka [label=Kafka, shape=none, height="2", image="resources/onprem/queue/kafka.png"];
}
subgraph cluster_event_driven {
label="Event Driven";
graph [pad="1", shape=box, style=rounded, labeljust=l, pencolor="#AEB6BE", fontname="Sans-Serif", fontsize="12", bgcolor="#E5F5FD"];
consumer [label=Consumer, shape=none, height="2", image="resources/aws/compute/ec2.png"];
}
mysql [label=Mysql, shape=none, height="2", image="resources/onprem/database/mysql.png"];
kafka -> consumer [style=bold, color=brown];
consumer -> mysql [style=bold, color=darkgreen, xlabel=WRITE];
}
```

### Generate PNG file

Using Docker

```bash
vendor/bin/dac --root=/dot/vendor/nddcoder/diagram-as-code demo.php | docker run --rm -i -v $(pwd):/dot nshine/dot > demo.png
```

Use `dot` command from `graphviz`.

```bash
vendor/bin/dac demo.php | dot -Tpng > demo.png
```

### PNG Output

![Demo Diagram As Code](examples/output/00.png)

## Merge multiple files

If you want to merge multiple files, you can use `--all` option

Example for following directory struct

```bash
projects/
project-a/
diagram.php
project-b/
.diagram/
diagram.php
...
vendor
```

```bash
vendor/bin/dac --all .
```

This command will look up for `diagram.php` or `.diagram/diagram.php` file in all sub folders

If you want set name of output diagram, you can use `--name` option

```bash
vendor/bin/dac --all --name=demo .
```

## Sample

* [examples/00-readme.php](examples/00-readme.php): example from README
* [examples/01-more-complex.php](examples/01-more-complex.php): more complex example