Skip to content

Commit

Permalink
feat: rewrite the binaries and the PlantUML library
Browse files Browse the repository at this point in the history
BREAKING CHANGE: full refactoring
  • Loading branch information
tmorin committed Oct 28, 2020
1 parent fb0a96a commit e5b889c
Show file tree
Hide file tree
Showing 45,137 changed files with 446,105 additions and 329,508 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.gdiag
.idea
build/
node_modules
test/**/*.png
tmp/
Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/.*
/test
/tmp
/build
/*.sh
/*.md
/*.tgz
70 changes: 52 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,44 @@
[![Integration](https://github.com/tmorin/plantuml-libs/workflows/Integration/badge.svg?branch=master)](https://github.com/tmorin/plantuml-libs/actions?query=workflow%3AIntegration+branch%3Amaster)
[![npm version](https://badge.fury.io/js/%40tmorin%2Fplantuml-libs.svg)](https://badge.fury.io/js/%40tmorin%2Fplantuml-libs)

Tested with [PlantUML compiled Jar (Version 1.2020.18.jar)](https://plantuml.com/download).
Tested with [PlantUML compiled Jar (Version 1.2020.9)](https://plantuml.com/download).

Set of libraries for [PlantUML](http://plantuml.com).
The version 1 of the library is available in the v1.x branch.

- [Cloud - with AWS, Azure and GCP](cloud/README.md)
- [C4 Model](c4model/README.md)
- [EventStorming](eventstorming/README.md)
## Presentation

# Usage of the NPM package
This repository hosts a [PlantUML] lib which provides several `packages`.
Each package focus to a particular technology/approach (Amazon Web Services (AWS), C4 Model or even EventStorming and more).

The repository hosts also two command lines utilities.
The first one (`glib`) helps to generate packages, the second one (`gdiag`) helps to use the packages within external projects in order to bootstrap a local and generate diagrams.

## PlantUML library

The resources can be browsed, from GitHub, with Markdown pages directly in the [dist](dist/README.md) directory.

The available packages:

- [aws-20200430](dist/aws-20200430/README.md)
- [aws-20200911](dist/aws-20200911/README.md)
- [azure-v2](dist/azure-v2/README.md)
- [c4model](dist/c4model/README.md)
- [eventstorming](dist/eventstorming/README.md)
- [homecloud](dist/homecloud/README.md)
- [material-4.0](dist/material-4.0/README.md)

[PlantUML]: https://plantuml.com

## Usage

The library can directly be used remotely hitting the GitHub repository.

However, the performances are not good enough for intensive usage.
In this case, the library should be installed locally and use directly from the file system.

To help the local installation, an NPM package is available.

## Usage of the NPM package

A npm package [@tmorin/plantuml-libs](https://www.npmjs.com/package/@tmorin/plantuml-libs) is available.
Its purpose is to provide locally the libraries as well as the command-line utility: `gdiag`.
Expand All @@ -27,23 +56,28 @@ Show the `gdiag` help output:
gdiag --help
```

```text
```
gdiag
Render PlantUML diagrams discovered (*.{puml,plantuml}) in the working
directory.
Options:
--version Show version number [boolean]
--working-directory, -w The directory where the PlantUML files will be
discovered and rendered. [string] [default: "."]
--technical-directory, -t The directory where technical resources will be
stored. [string] [default: ".gdiag"]
--library-directory, -l The directory where the library is located.
--version Show version number [boolean]
--work-directory, --wd The directory where the PlantUML files will be
discovered and rendered. [string] [default: "."]
--tech-directory, --td The directory where technical resources will be
stored. [string] [default: ".gdiag"]
--lib-directory, --ld The directory where the library is located.
[string] [default: "<Depend of the host>/node_modules/@tmorin/plantuml-libs/"]
--java-command, -j The command of the java binary.
--java-command, --jc The command of the java binary.
[string] [default: "java"]
--clean, -c Delete recursively the pictures located in the
working directory. [boolean] [default: false]
--help Show help [boolean]
```
--plantuml-version, --pv The version of PlantUML to use.
[string] [default: "1.2020.9"]
--log-level, --ll The level of the logs.
[string] [choices: "error", "warn", "info", "http", "verbose", "debug",
"silly"] [default: "info"]
-c, --clean Delete recursively the pictures located in the
working directory. [boolean] [default: false]
--help Show help [boolean]
```
167 changes: 4 additions & 163 deletions bin/gdiag.js
Original file line number Diff line number Diff line change
@@ -1,168 +1,9 @@
#!/usr/bin/env node

const F = require('fs');
const P = require('path');
const moment = require('moment');
const glob = require('glob');
const fetch = require('node-fetch');
const winston = require('winston');
const util = require('util');
const CP = require('child_process');
const {execute} = require('../lib/gdiag/cli');
const {log} = require('../lib/logger');

const exec = util.promisify(CP.exec);

const log = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.splat(),
winston.format.simple()
),
transports: [
new winston.transports.Console({})
]
});

const argv = require('yargs')
.scriptName('gdiag')
.option('working-directory', {
alias: 'w',
default: '.',
describe: 'The directory where the PlantUML files will be discovered and rendered.',
type: 'string'
})
.option('technical-directory', {
alias: 't',
default: '.gdiag',
describe: 'The directory where technical resources will be stored.',
type: 'string'
})
.option('library-directory', {
alias: 'l',
default: P.join(__dirname, '../'),
describe: 'The directory where the library is located.',
type: 'string'
})
.option('java-command', {
alias: 'j',
default: 'java',
describe: 'The command of the java binary.',
type: 'string'
})
.option('clean', {
alias: 'c',
default: false,
describe: 'Delete recursively the pictures located in the working directory.',
type: 'boolean'
})
.help()
.usage('$0', 'Render PlantUML diagrams discovered (*.{puml,plantuml}) in the working directory.')
.argv;

const techDir = argv.t;
const latestRunFile = P.join(argv.t, 'LATEST_RUN');
const workDir = argv.w;
const libDir = argv.l;
const doCleaning = argv.c;
const javaCmd = argv.j;

async function plantuml(file) {
await exec(`${javaCmd} -jar ${techDir}/plantuml.jar ${file}`, {
stdio: process.stdout
}).catch(error => log.warn(error.message));
}

async function download(url, destination) {
const directory = P.dirname(destination);
if (!F.existsSync(directory)) {
log.info('create destination directory [%s]', directory);
F.mkdirSync(directory, {recursive: true});
}
if (!F.existsSync(destination)) {
log.info('download [%s] to [%s]', url, destination);
const resource = await fetch(url);
const dest = F.createWriteStream(destination);
resource.body.pipe(dest);
return new Promise((resolve, reject) => {
resource.body.on('end', resolve);
resource.body.on('error', reject);
});
}
}

async function downloadPlantUML() {
// https://sourceforge.net/projects/plantuml/files
const url = `https://sourceforge.net/projects/plantuml/files/1.2020.18/plantuml.1.2020.18.jar/download`;
const destination = `${techDir}/plantuml.jar`;
await download(url, destination);
}

async function cleanDirectory(directory) {
log.info('clean directory [%s]', directory);
const pictures = glob.sync(`**/*.png`, {
cwd: directory
});
for (const picture of pictures) {
const picturePath = P.join(directory, picture);
log.info('delete [%s]', picturePath);
F.unlinkSync(picturePath);
}
}

async function getLatestRun() {
if (F.existsSync(latestRunFile)) {
const stat = F.statSync(latestRunFile);
log.info('ran previously on [%s]', stat.mtime);
return moment(stat.mtime);
}
log.info('never ran previously');
}

async function setLatestRun() {
F.writeFileSync(latestRunFile, '');
}

async function cleanLatestRun() {
if (F.existsSync(latestRunFile)) {
F.unlinkSync(latestRunFile);
}
}

async function parseDirectory(directory, latestRun) {
log.info('parse directory [%s]', directory);
const sources = glob.sync(`**/*.{puml,plantuml}`, {
cwd: directory
});
for (const source of sources) {
const file = P.join(directory, source);
const stat = F.statSync(file);
if (!latestRun || moment(stat.mtime).isAfter(latestRun)) {
log.info('process source [%s]', source);
await plantuml(file)
}
}
}

function linkPlantumlLibs() {
const src = libDir;
const dst = P.join(techDir, 'plantuml-libs');
log.info('link plantuml-libs from [%s] to [%s]', src, dst);
if (F.existsSync(dst)) {
F.unlinkSync(dst);
}
F.symlinkSync(src, dst, 'dir');
}

(async function execute() {
await downloadPlantUML();
if (doCleaning) {
await cleanLatestRun();
await cleanDirectory(workDir);
}
linkPlantumlLibs();
const latestRun = await getLatestRun();
await parseDirectory(workDir, latestRun);
await setLatestRun();
}()).catch(e => {
execute().catch(e => {
log.error('gdiag failed');
log.error(e);
console.error(e)
});
9 changes: 9 additions & 0 deletions bin/glib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node

const {execute} = require('../lib/glib/cli');
const {log} = require('../lib/logger');

execute().catch(e => {
log.error('glib failed');
console.error(e)
});
8 changes: 8 additions & 0 deletions build-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#rm -Rf build
set -ex
npm run build:aws-20200430 -- $@
npm run build:aws-20200911 -- $@
npm run build:azure-v2 -- $@
npm run build:c4model -- $@
npm run build:homecloud -- $@
npm run build:material-4.0 -- $@
Loading

0 comments on commit e5b889c

Please sign in to comment.