Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffmuc-mesh-vpn-wireguard: add shell minifier #120

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more dollars, more better

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"