Skip to content

Instantly share code, notes, and snippets.

@basinilya
Last active February 6, 2024 16:18
Show Gist options
  • Save basinilya/4589f0b41cebbafbe4fd8bd59d7ac31a to your computer and use it in GitHub Desktop.
Save basinilya/4589f0b41cebbafbe4fd8bd59d7ac31a to your computer and use it in GitHub Desktop.
mxmoduleclean
#!/bin/bash
#
# delete *.Required* marker files belonging to a Mendix module and delete their
# *.jar files unused by other modules
#
# Usage:
# cd userlib
# # delete module:
# mxmoduleclean MODULE
# # delete everything except MODULE
# mxmoduleclean -MODULE
#
# # delete everything except MODULE in the provided .mpk file
# MXMODULECLEANMPK=../some.mpk mxmoduleclean -MODULE
#
set -e
if [ ! -z "$MXMODULECLEANMPK" ]; then
# thankfully realpath is part of git bash
MXMODULECLEANMPK=$(realpath "$MXMODULECLEANMPK")
echo "working on the .mpk file: $MXMODULECLEANMPK"
unzipdir=$(mktemp -d)
# unzip may expect Windows path so we have to copy :(
cp -v "$MXMODULECLEANMPK" "$unzipdir/copy.mpk"
echo "unzipping to: $unzipdir"
cd "$unzipdir"
>/dev/null unzip copy.mpk
cd userlib
fi
# expand non-matching pattern to nothing
shopt -s nullglob dotglob
s=${1:?}
module=${s#-}
removedfiles=()
fn_rm() {
removedfiles+=("userlib/${1:?}")
rm -v -f "${1:?}"
}
if [ x"$s" != x"$module" ]; then
negate=x
declare -A matchedfiles=()
fn_onmatch() {
matchedfiles["${1:?}"]=x
}
else
negate=
fn_onmatch() {
fn_rm "${1:?}"
}
fi
# ex: CommunityCommons.migration
fn_onmatch "${module:?}".migration
# iterate the markers matching by module name
# some legacy markers end with ".Required", not ".RequiredLib":
# - jaxb-runtime-2.3.2.jar.SAML20-module.Required
for r in *."${module:?}".Required*; do
# guess the jar name by removing the suffix
# Some legacy markers don't have the ".jar" part though:
# - commons-codec-1.15.JWT.RequiredLib
# - opencsv-4.1.OQLModule.RequiredLib
j=${r%.*.*}
# guess the base name by removing the optional ".jar" suffix
b=${j%.jar}
# make sure it ends with ".jar"
j=$b.jar
# no delete by default
nodel=n;
# iterate the other markers of the current library
for y in "$b".*.Required*; do
if [ r"$r" != r"$y" ]; then
if [ ! "$negate" ]; then
echo "not deleting the jar because other modules depend on it: $y"
nodel=y;
break;
fi
fi;
done;
if [ $nodel = n ]; then
if [ -e "$j" ]; then
fn_onmatch "$j"
fi
if [ -e "$b" ]; then
fn_onmatch "$b"
fi
fi;
fn_onmatch "$r";
done
# Example:
# - guava-32.0.1-JWT.RequiredLib
for r in *-"${module:?}".Required*; do
echo "Warning: ignoring non-standard marker file: $r"
done
if [ "$negate" ]; then
for r in *; do
if [ -z "${matchedfiles["$r"]}" ]; then
fn_rm "$r"
else
echo "keeping: $r"
fi
done
fi
if [ ! -z "$MXMODULECLEANMPK" ]; then
cd ..
if [ 0 != "${#removedfiles[@]}" ]; then
echo "updating zip..."
zip -d copy.mpk "${removedfiles[@]}"
cp -v "$unzipdir/copy.mpk" "$MXMODULECLEANMPK"
else
echo "Nothing to delete in zip"
fi
cd "$unzipdir"/..
rm -rf "$unzipdir"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment