initial commit

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-12 14:32:40 +03:00
commit 2c1bf5444e
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 261 additions and 0 deletions

12
.gitignore vendored Normal file
View file

@ -0,0 +1,12 @@
# direnv
.envrc
.direnv
# test d2
*.d2
# generated by d2
*.svg
# built
result/

42
README.md Normal file
View file

@ -0,0 +1,42 @@
# *W*rapped _d2_
A wrapper over [d2] which allows to use \`layout\`, \`theme\`, \`pad\`
attributes from d2 file. All arguments will be passed to the d2 cli, but these
additional configs overwrite cli arguments with the same name.
[d2]: https://github.com/terrastruct/d2
# Install
## Nix Flake
This repo is also packaged via Nix flakes, the language server binary package is
available through the default flake output `github:pleshevskiy/wd2` with the
path `bin/wd2`.
You can enable flakes support in your nix configuration, and then run nix
profile install `github:pleshevskiy/wd2` to get `wd2` installed. You can also
use this repository as a flake input and add its output to your own
flake-managed systemwide or home configuration.
## Other
Copy `wd2` script to `/usr/local/bin` folder.
# Usage
Create a d2 file with additional configs at the top.
```d2
# layout: elk
# theme: 101
# pad: 5
x -> y -> z
```
Then run the `wd2` script
```sh
wd2 -w path/to/your/file.d2
```

90
flake.lock Normal file
View file

@ -0,0 +1,90 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1670827406,
"narHash": "sha256-nLNk7uiLbhbvb4TVz67XK7+Ezr1zcWYDWmNrWGmEUqA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ffca9ffaaafb38c8979068cee98b2644bd3f14cb",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1669261230,
"narHash": "sha256-AjddxRPd5y5jge77281P3O8+Cnafj842Xg59rwV4x+0=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8e8b5f3b1e899bf5d250279578c0283705b8cdb4",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"tools": "tools"
}
},
"tools": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1670842919,
"narHash": "sha256-4SpFhzzGGg7yppVCZvx0rDdVScXc/m6rZe4BYnNoOQ4=",
"ref": "refs/heads/main",
"rev": "79301872e58bf199118ba163d95f54e6fbefcd75",
"revCount": 11,
"type": "git",
"url": "https://git.pleshevski.ru/mynix/tools"
},
"original": {
"type": "git",
"url": "https://git.pleshevski.ru/mynix/tools"
}
}
},
"root": "root",
"version": 7
}

47
flake.nix Normal file
View file

@ -0,0 +1,47 @@
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
tools.url = "git+https://git.pleshevski.ru/mynix/tools";
};
outputs = { self, nixpkgs, flake-utils, tools, ... }:
let
inherit (builtins) readFile;
mkWd2 = { writeShellApplication, symlinkJoin, d2, ... }:
writeShellApplication {
name = "wd2";
runtimeInputs = [ d2 ];
text = (readFile ./wd2);
};
in
{
overlays = {
default = final: prev: {
wd2 = prev.callPackage mkWd2 { };
};
};
} //
flake-utils.lib.eachDefaultSystem (system:
let
inherit (builtins) mapAttrs;
pkgs = import nixpkgs { inherit system; };
wd2 = pkgs.callPackage mkWd2 {
d2 = tools.packages.${system}.d2;
};
mkApp = drv: flake-utils.lib.mkApp { inherit drv; };
packages = {
inherit wd2;
default = wd2;
};
in
{
check = packages;
inherit packages;
apps = mapAttrs (name: mkApp) packages;
});
}

1
result Symbolic link
View file

@ -0,0 +1 @@
/nix/store/paxh7dav8nq2s5lvijq192kz3p7jhgas-wd2

69
wd2 Executable file
View file

@ -0,0 +1,69 @@
#! /usr/bin/env bash
d2_args=( )
d2_input_file=
function print_help() {
cat <<EOF
Usage:
wd2 [FLAGS...] file.d2 [file.svg | file.png]
A wrapper over d2 which allows to use \`layout\`, \`theme\`, \`pad\`
attributes from d2 file. All arguments will be passed to the d2 cli,
but these additional configs overwrite cli arguments with the
same name.
See \`d2 --help\` or \`man d2\` for detailed docs.
See more docs and the source code at https://oss.terrastruct.com/d2
EOF
}
function parse_args() {
if [ "$#" == "0" ]; then
print_help
exit 1
fi
while (( "$#" )); do
case "$1" in
--help)
print_help
exit 0
;;
# extract input file arg
*.d2)
d2_input_file="$1"
d2_args+=( "$1" )
;;
# other args
*)
d2_args+=( "$1" )
;;
esac
shift
done
}
function get_attr_value() {
head -n3 "$d2_input_file" | grep "$1" | awk '{ print $3 }'
}
parse_args "$@"
if [ -z "$d2_input_file" ]; then
print_help
exit 1
fi
d2_layout=$(get_attr_value "layout")
d2_theme=$(get_attr_value "theme")
d2_pad=$(get_attr_value "pad")
d2 "${d2_args[@]}" \
${d2_layout:+--layout ${d2_layout}} \
${d2_theme:+--theme ${d2_theme}} \
${d2_pad:+--pad ${d2_pad}}