Skip to content

Commit

Permalink
Turn examples into template, and add docker examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
realazthat committed Aug 22, 2024
1 parent c0c03c3 commit a7996ae
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- id: detect-private-key
- id: double-quote-string-fixer
- id: end-of-file-fixer
exclude: ^.*\.(md|svg)$
exclude: ^.*\.(md|svg|sh)$
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: name-tests-test
Expand Down
49 changes: 49 additions & 0 deletions examples/local-docker_example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
# WARNING: This file is auto-generated by snipinator. Do not edit directly.
# SOURCE: `examples/simple_example.sh.jinja2`.

#!/bin/bash
# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
set -e -x -v -u -o pipefail
set +v
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
PS4="${GREEN}$ ${NC}"

EXCALIDRAW_BRUTE_EXPORT_CLI_URL=${EXCALIDRAW_BRUTE_EXPORT_CLI_URL:-}
if [[ -z "${EXCALIDRAW_BRUTE_EXPORT_CLI_URL}" ]]; then
echo -e "${RED}EXCALIDRAW_BRUTE_EXPORT_CLI_URL is not set${NC}"
exit 1
fi



# Don't run this in act/GH actions because act doesn't play with with nested
# docker; the paths mess up.
if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
echo -e "${YELLOW}This script is not meant to be run in GitHub Actions.${NC}"
exit 0
fi

: ECHO_SNIPPET_START
# SNIPPET_START
docker build -t my-excalidraw-brute-export-cli-image .

# /data in the docker image is the working directory, so paths are simpler.
docker run --rm --tty \
-u "$(id -u):$(id -g)" \
-v "${PWD}:/data" \
my-excalidraw-brute-export-cli-image \
-i ./examples/simple.excalidraw \
--background 1 \
--embed-scene 1 \
--dark-mode 0 \
--scale 1 \
--format png \
-o "./examples/simple_example_output.png"

ls "./examples/simple_example_output.png"

# SNIPPET_END
: ECHO_SNIPPET_END
48 changes: 48 additions & 0 deletions examples/remote-docker_example-noautorun.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash
# WARNING: This file is auto-generated by snipinator. Do not edit directly.
# SOURCE: `examples/simple_example.sh.jinja2`.

#!/bin/bash
# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
set -e -x -v -u -o pipefail
set +v
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
PS4="${GREEN}$ ${NC}"

EXCALIDRAW_BRUTE_EXPORT_CLI_URL=${EXCALIDRAW_BRUTE_EXPORT_CLI_URL:-}
if [[ -z "${EXCALIDRAW_BRUTE_EXPORT_CLI_URL}" ]]; then
echo -e "${RED}EXCALIDRAW_BRUTE_EXPORT_CLI_URL is not set${NC}"
exit 1
fi



# Don't run this in act/GH actions because act doesn't play with with nested
# docker; the paths mess up.
if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
echo -e "${YELLOW}This script is not meant to be run in GitHub Actions.${NC}"
exit 0
fi

: ECHO_SNIPPET_START
# SNIPPET_START
# Use the published images at ghcr.io/realazthat/snipinator.
# /data in the docker image is the working directory, so paths are simpler.
docker run --rm --tty \
-u "$(id -u):$(id -g)" \
-v "${PWD}:/data" \
ghcr.io/realazthat/excalidraw-brute-export-cli:v0.3.2 \
-i ./examples/simple.excalidraw \
--background 1 \
--embed-scene 1 \
--dark-mode 0 \
--scale 1 \
--format png \
-o "./examples/simple_example_output.png"

ls "./examples/simple_example_output.png"

# SNIPPET_END
: ECHO_SNIPPET_END
16 changes: 0 additions & 16 deletions examples/simple.sh

This file was deleted.

34 changes: 34 additions & 0 deletions examples/simple_example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
# WARNING: This file is auto-generated by snipinator. Do not edit directly.
# SOURCE: `examples/simple_example.sh.jinja2`.

#!/bin/bash
# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
set -e -x -v -u -o pipefail
set +v
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
PS4="${GREEN}$ ${NC}"

EXCALIDRAW_BRUTE_EXPORT_CLI_URL=${EXCALIDRAW_BRUTE_EXPORT_CLI_URL:-}
if [[ -z "${EXCALIDRAW_BRUTE_EXPORT_CLI_URL}" ]]; then
echo -e "${RED}EXCALIDRAW_BRUTE_EXPORT_CLI_URL is not set${NC}"
exit 1
fi

: ECHO_SNIPPET_START
# SNIPPET_START
npx excalidraw-brute-export-cli \
-i ./examples/simple.excalidraw \
--background 1 \
--embed-scene 1 \
--dark-mode 0 \
--scale 1 \
--format png \
-o "./examples/simple_example_output.png"

ls "./examples/simple_example_output.png"

# SNIPPET_END
: ECHO_SNIPPET_END
72 changes: 72 additions & 0 deletions examples/simple_example.sh.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{%- if True -%}
#!/bin/bash
# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
set -e -x -v -u -o pipefail
set +v
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
PS4="${GREEN}$ ${NC}"

EXCALIDRAW_BRUTE_EXPORT_CLI_URL=${EXCALIDRAW_BRUTE_EXPORT_CLI_URL:-}
if [[ -z "${EXCALIDRAW_BRUTE_EXPORT_CLI_URL}" ]]; then
echo -e "${RED}EXCALIDRAW_BRUTE_EXPORT_CLI_URL is not set${NC}"
exit 1
fi

{% endif -%}

{#- example_type: {docker-remote, docker-local, npm-module} -#}
{#- docker_image_name: str -#}
{#- module_name: str -#}
{#- cmd: str -#}

{%- macro abort(error) -%}
{{ None['[ERROR] ' ~ error][0] }}
{%- endmacro -%}

{%- if example_type in ['docker-remote', 'docker-local'] -%}
{% set cmd='docker run --rm --tty \\\n -u \"$(id -u):$(id -g)\" \\\n -v \"${PWD}:/data\" \\\n ' ~ docker_image_name %}
{%- elif example_type=='npm-module' -%}
{% set cmd='npx ' ~ module_name %}
{%- else -%}
{{ abort('example_type is unknown value: ' ~ example_type) }}
{%- endif -%}

{%- if example_type in ['docker-remote', 'docker-local'] %}

# Don't run this in act/GH actions because act doesn't play with with nested
# docker; the paths mess up.
if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
echo -e "${YELLOW}This script is not meant to be run in GitHub Actions.${NC}"
exit 0
fi

{% endif-%}

: ECHO_SNIPPET_START
# SNIPPET_START
{% if example_type in ['docker-local'] -%}
docker build -t {{docker_image_name|safe}} .

{% endif -%}

{% if example_type in ['docker-remote'] -%}
# Use the published images at ghcr.io/realazthat/snipinator.
{% endif -%}
{% if example_type in ['docker-local', 'docker-remote'] -%}
# /data in the docker image is the working directory, so paths are simpler.
{% endif -%}
{{cmd|safe}} \
-i ./examples/simple.excalidraw \
--background 1 \
--embed-scene 1 \
--dark-mode 0 \
--scale 1 \
--format png \
-o "./examples/simple_example_output.png"

ls "./examples/simple_example_output.png"

# SNIPPET_END
: ECHO_SNIPPET_END
Binary file added examples/simple_example_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions scripts/generate-examples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash
# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
set -e -x -v -u -o pipefail

SCRIPT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
source "${SCRIPT_DIR}/utilities/common.sh"

################################################################################
PYTHON_VERSION_PATH=${PWD}/scripts/.python-version \
VENV_PATH=${PWD}/.cache/scripts/.venv \
source "${PROJ_PATH}/scripts/utilities/ensure-venv.sh"
PYTHON_VERSION_PATH=${PWD}/scripts/.python-version \
TOML=${PROJ_PATH}/scripts/pyproject.toml EXTRA=dev \
DEV_VENV_PATH="${PWD}/.cache/scripts/.venv" \
TARGET_VENV_PATH="${PWD}/.cache/scripts/.venv" \
bash "${PROJ_PATH}/scripts/utilities/ensure-reqs.sh"
################################################################################


CUSTOM_WARNING=$(cat << 'EOF'
#!/bin/bash
# WARNING: This file is auto-generated by snipinator. Do not edit directly.
# SOURCE: `{template_file_name}`.
.
EOF
)

python -m snipinator.cli \
-t "${PROJ_PATH}/examples/simple_example.sh.jinja2" \
--args '{"example_type": "npm-module", "module_name": "excalidraw-brute-export-cli"}' \
--warning-header "${CUSTOM_WARNING%.}" \
--rm \
--force \
--create \
-o "${PROJ_PATH}/examples/simple_example.sh" \
--chmod-ro \
--skip-unchanged


python -m snipinator.cli \
-t "${PROJ_PATH}/examples/simple_example.sh.jinja2" \
--args '{"example_type": "docker-local", "docker_image_name": "my-excalidraw-brute-export-cli-image"}' \
--warning-header "${CUSTOM_WARNING%.}" \
--rm \
--force \
--create \
-o "${PROJ_PATH}/examples/local-docker_example.sh" \
--chmod-ro \
--skip-unchanged


LAST_VERSION=$(node -p "require('./package.json').version")
python -m snipinator.cli \
-t "${PROJ_PATH}/examples/simple_example.sh.jinja2" \
--args '{"example_type": "docker-remote", "docker_image_name": "ghcr.io/realazthat/excalidraw-brute-export-cli:v'"${LAST_VERSION}"'"}' \
--warning-header "${CUSTOM_WARNING%.}" \
--rm \
--force \
--create \
-o "${PROJ_PATH}/examples/remote-docker_example-noautorun.sh" \
--chmod-ro \
--skip-unchanged
1 change: 0 additions & 1 deletion scripts/generate-readme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ if [[ -z "${EXCALIDRAW_BRUTE_EXPORT_CLI_URL}" ]]; then
echo -e "${RED}EXCALIDRAW_BRUTE_EXPORT_CLI_URL is not set${NC}"
exit 1
fi
export EXCALIDRAW_BRUTE_EXPORT_CLI_URL
################################################################################
PYTHON_VERSION_PATH=${PWD}/scripts/.python-version \
VENV_PATH=${PWD}/.cache/scripts/.venv \
Expand Down
1 change: 1 addition & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fi

npm run genversion
bash scripts/format.sh
bash scripts/generate-examples.sh
bash scripts/run-all-examples.sh
bash scripts/generate-readme.sh
################################################################################
25 changes: 23 additions & 2 deletions scripts/run-all-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,33 @@ NODE_VERSION_PATH=${PWD}/.nvmrc \
# FORCE_COLOR and TERM are set, to produce consistent results across different
# systems.
#
# See generate-readme.sh for explanation.
# Explanation:
#
# @caporal/core is used for argument parsing.
#
# chalk/chalk, via @caporal/core, uses an internal copy of the supports-color
# library (https://github.com/chalk/supports-color) here:
# <https://github.com/chalk/chalk/blob/main/source/vendor/supports-color/index.js>.
#
# supports-color is terminal-emulator-dependent.
#
# But we want it to produce consistent results for the README output.
#
# To do this, we set the FORCE_COLOR environment variable to 3.
#
# However, this older version of supports-color does not always listen to
# FORCE_COLOR, so we also have to set TERM to 'dumb'.
export FORCE_COLOR=3
export TERM=dumb

# https://www.npmjs.com/package/cli-width, via @caporal/core, detects column
# width, and snipinator's rich_cols isn't cutting it.
export CLI_WIDTH=120
export LINES=40
export COLUMNS=120

# For each sh in examples
find examples -type f -name "*.sh" -print0 | while IFS= read -r -d '' EXAMPLE; do
find examples -type f -name "*_example.sh" -print0 | while IFS= read -r -d '' EXAMPLE; do
bash "${EXAMPLE}"
echo -e "${GREEN}${EXAMPLE} ran successfully${NC}"
done
Expand Down

0 comments on commit a7996ae

Please sign in to comment.