Skip to content

Commit

Permalink
ffmuc-mesh-vpn-wireguard: add shell minifier
Browse files Browse the repository at this point in the history
  • Loading branch information
grische committed Jun 14, 2024
1 parent 961897f commit ed68c70
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ffmuc-mesh-vpn-wireguard-vxlan/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,30 @@ define Package/ffmuc-mesh-vpn-wireguard-vxlan
DEPENDS:=+gluon-mesh-vpn-core +micrond +kmod-wireguard +wireguard-tools +ip-full +lua-jsonc
endef

define ShSrcDiet
rm -rf $(2)
$(CP) $(1) $(2)
ifdef CONFIG_GLUON_MINIFY
set -e; $(FIND) $(2) -type f | while read src; do \
echo "Minifying $$$$src..."; \
./shell_minify.sh "$$$$src"; \
done
endif
endef

define Build/Compile
$(call Gluon/Build/Compile)
$(if $(wildcard ./shsrc/.),
$(call ShSrcDiet,shsrc,$(PKG_BUILD_DIR)/shdest/)
)
endef

define Package/$(PKG_NAME)/install
$(Gluon/Build/Install)

$(if $(wildcard ./shsrc/.),
$(CP) $(PKG_BUILD_DIR)/shdest/. $(1)/
)
endef

$(eval $(call BuildPackageGluon,ffmuc-mesh-vpn-wireguard-vxlan))
61 changes: 61 additions & 0 deletions ffmuc-mesh-vpn-wireguard-vxlan/shell_minify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
# SPDX-License-Identifier: MIT

set -eu

strip_comments() {
filename=$1

# strip comments from all but the first line
sed -i '2,$s/\s*#.*//' "${filename}"

# strip first line if it isn't a shebang
sed -i '1s/\s*#[^!].*//' "${filename}"
}

strip_duplicate_newlines() {
filename=$1

# strip empty lines
sed -i '/^\s*$/d' "${filename}"
}

strip_spaces_around_vertical_bar() {
filename=$1

# convert "true | false" to "true|false"
sed -i 's/\s\+|\s*/|/g' "${filename}"
# convert "true | false" to "true|false"
sed -i 's/\s*|\s\+/|/g' "${filename}"
}

strip_spaces_between_semicolon_and_then_or_do() {
filename=$1

# convert "if true; then" to "if true;then"
sed -i 's/;\s\+then/;then/g' "${filename}"

# convert "for xxx; do" to "for xxx;do" and "; done" to ";done"
sed -i 's/;\s\+do/;do/g' "${filename}"
}

strip_spaces_around_boolean_operators() {
filename=$1

# convert "true && false" to "true&&false"
sed -i 's/\s*||\s*/||/g' "${filename}"
sed -i 's/\s*&&\s*/&&/g' "${filename}"
}

strip_trailing_semicolon() {
filename=$1
# remove trailing ; excluding ";;" for switch/case clauses
sed -i 's/\([^;]\);$/\1/' "${filename}"
}

strip_comments "$1"
strip_trailing_semicolon "$1"
strip_duplicate_newlines "$1"
strip_spaces_around_vertical_bar "$1"
strip_spaces_around_boolean_operators "$1"
strip_spaces_between_semicolon_and_then_or_do "$1"

0 comments on commit ed68c70

Please sign in to comment.