• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2#
3# Copyright 2021 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Disable expansion
8set -f
9
10script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
11readonly script_dir
12
13# Print usage string
14usage() {
15    echo "Usage: $(basename "$0") [options] <title> <proto> <output>"
16    echo
17    echo "Generate an SVG rendering of the protobuffer schema and embed"
18    echo "it in a static html page for documentation."
19    echo
20    echo "Arguments:"
21    echo "   title - Title for output HTML page"
22    echo "   proto - Path to protobuffer to generate SVG output for"
23    echo "  output - Output filename (relative to script directory)"
24    echo
25    echo "Options:"
26    echo "  --select/-s - Alternate select string to pass to protodot"
27}
28
29# Check that a binary with a given name exists.
30checkbin_exists() {
31    type "$1" &> /dev/null
32}
33
34# Convert a file into a data url
35make_data_url() {
36    mimetype=$(file -bN --mime-type "$1")
37    content=$(base64 -w0 < "$1")
38    echo "data:$mimetype;base64,$content"
39}
40
41if ! checkbin_exists "protodot"; then
42    echo "Protodot not found, please install and add to PATH"
43    exit 1
44fi
45
46# Parse options
47select=""
48while [[ $# -gt 0 ]]; do
49    case $1 in
50        --select|-s)
51            select=$2
52            shift 2
53            ;;
54        *)
55            break
56            ;;
57    esac
58done
59
60if [[ $# -lt 3 ]]; then
61    (
62        echo "Error, not enough arguments."
63        usage
64    ) >&2
65    exit 1
66fi
67
68# Get arguments
69title=$1
70proto=$2
71output=$3
72
73cd "${script_dir}" || exit 1
74
75# Generate SVG output
76cmd=(protodot -src "${proto}" -inc "$(realpath "${script_dir}/../proto")")
77if [[ -n "${select}" ]]; then
78    cmd+=(-select "${select}")
79fi
80
81# tee >(cat 1>&2) |
82file=$(${cmd[*]} | grep "creating file" | cut -d: -f2 | xargs echo)
83
84# Encode SVG to a data url and write it to a scratch file that we'll clean up.
85make_data_url "${file}.svg" > __data.url
86trap "rm -f __data.url" EXIT
87
88# Copy template to output location and replace template fields.
89cp "template/index.html" "${output}"
90sed -i "s/{{TITLE}}/${title}/g" "${output}"
91sed -i "s/{{DATE}}/$(date -u +%Y-%m-%d)/g" "${output}"
92
93python - <<EOF
94dataurl = open("__data.url").read().strip()
95lines = [line.replace("{{DATA_URL}}", dataurl)
96         for line in open("${output}").readlines()]
97
98open("${output}", "w").writelines(lines)
99EOF
100