summaryrefslogtreecommitdiff
path: root/include/pio/make_static.sh
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-06-10 23:20:37 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-06-10 23:20:37 +0300
commita6d8ba93056c1a4e243d56da447e241b2504fae2 (patch)
tree3ee5372ef4e4a2b8532bf8bc57d7151d77d96f71 /include/pio/make_static.sh
parentb0bf43e6a272d42a55158e657bd937cb82fc3d8d (diff)
move files again
Diffstat (limited to 'include/pio/make_static.sh')
-rwxr-xr-xinclude/pio/make_static.sh89
1 files changed, 89 insertions, 0 deletions
diff --git a/include/pio/make_static.sh b/include/pio/make_static.sh
new file mode 100755
index 0000000..d207e57
--- /dev/null
+++ b/include/pio/make_static.sh
@@ -0,0 +1,89 @@
+#!/bin/bash
+
+#set -x
+#set -e
+
+COMMON_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
+PROJECT_DIR="$(pwd)"
+
+fw_version="$(cat "$PROJECT_DIR/src/config.def.h" | grep "^#define FW_VERSION" | awk '{print $3}')"
+header="$PROJECT_DIR/src/static.h"
+source="$PROJECT_DIR/src/static.cpp"
+
+[ -f "$header" ] && rm "$header"
+[ -f "$source" ] && rm "$source"
+
+is_minifyable() {
+ local ext="$1"
+ [ "$ext" = "html" ] || [ "$ext" = "css" ] || [ "$ext" = "js" ]
+}
+
+minify() {
+ local ext="$1"
+ local bin="$(realpath "$COMMON_DIR"/../../tools/minify.js)"
+ "$bin" --type "$ext"
+}
+
+# .h header
+cat <<EOF >> "$header"
+/**
+ * This file is autogenerated with make_static.sh script
+ */
+
+#pragma once
+
+#include <stdlib.h>
+
+namespace homekit::files {
+
+typedef struct {
+ size_t size;
+ const uint8_t* content;
+} StaticFile;
+
+EOF
+
+cat <<EOF >> "$source"
+/**
+ * This file is autogenerated with make_static.sh script
+ */
+
+#include "static.h"
+
+namespace homekit::files {
+
+EOF
+
+# loop over files
+for ext in html js css ico; do
+ for f in "$COMMON_DIR"/static/*.$ext; do
+ filename="$(basename "$f")"
+ echo "processing ${filename}..."
+ filename="${filename/./_}"
+
+ # write .h
+ echo "extern const StaticFile $filename;" >> "$header"
+
+ # write .c
+ {
+ echo "static const uint8_t ${filename}_content[] PROGMEM = {"
+
+ cat "$f" |
+ ( [ "$ext" = "html" ] && sed "s/{version}/$fw_version/" || cat ) |
+ ( is_minifyable "$ext" && minify "$ext" || cat ) |
+ gzip |
+ xxd -ps -c 16 |
+ sed 's/.\{2\}/0x&, /g' |
+ sed 's/^/ /' |
+ sed 's/[ \t]*$//'
+
+ echo "};"
+ echo "const StaticFile $filename PROGMEM = {(sizeof(${filename}_content)/sizeof(${filename}_content[0])), ${filename}_content};"
+ echo ""
+ } >> "$source"
+ done
+done
+
+# end of homekit::files
+( echo ""; echo "}" ) >> "$header"
+echo "}" >> "$source"