Skip to content

Instantly share code, notes, and snippets.

@Hilbrand
Last active April 28, 2019 14:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Hilbrand/a7127d0fb7afd36124a78a248453ff03 to your computer and use it in GitHub Desktop.
Script to migrate openhab2 bindings to the new bnd based build system
#/bin/bash
NEW_LOC="bundles/$1"
TEMPLATE_PROJECT="org.openhab.binding.airquality"
ADDON=binding
#TEMPLATE_PROJECT="org.openhab.io.imperihome"
#ADDON=io
if [ $# -ne 1 ] ; then
echo Pass the binding package name to convert. e.g. org.openhab.$ADDON.somebinding
exit 1
fi
if [ $((`grep $1 CODEOWNERS | wc -l`)) -lt 1 ] ; then
echo Invalid argument "$1". Pass the $ADDON to replace. e.g. org.openhab.$ADDON.somebinding
exit 1
fi
if [ -d "$NEW_LOC" ] ; then
echo New location "'$NEW_LOC'" already exists. Maybe left over from earlier migration run?
read -p "Do you want to remove this directory [y|n]?" REMOVE_BUNDLE
if [ $REMOVE_BUNDLE == "y" ] ; then
rm -rf "$NEW_LOC"
else
echo Manually remove "'$NEW_LOC'" before running this script again.
exit 1;
fi
fi
#Calculate bundle binding after which this binding should be placed in the files
BUNDLE_TO_MOVE_ATFER_TMP1=`sed "s|/addons/$ADDON/$1|/bundles/$1|" CODEOWNERS | sort | grep -B 1 "$1/" | grep -v "$1/"`
BUNDLE_TO_MOVE_ATFER_TMP=${BUNDLE_TO_MOVE_ATFER_TMP1%/ *}
BUNDLE_TO_MOVE_ATFER=${BUNDLE_TO_MOVE_ATFER_TMP#/bundles/}
echo Move after bundle $BUNDLE_TO_MOVE_ATFER
if [ ! $BUNDLE_TO_MOVE_ATFER ] ; then
echo Did you pass the correct $ADDON name on the command line? . e.g. exit 1
fi
echo Remove from.$ADDONs pom.xml
sed -i "/<module>$1/d" addons/$ADDON/pom.xml
echo Add to bundle pom.xml
BUNDLE_POM="bundles/pom.xml"
BUNDLE_MOVE_AFTER_INDEX=$((`grep -n "$BUNDLE_TO_MOVE_ATFER<" $BUNDLE_POM | cut -d':' -f 1` + 1))
MODULE="\ <module>$1</module>"
sed -i "${BUNDLE_MOVE_AFTER_INDEX}i$MODULE" $BUNDLE_POM
echo Update CODEOWNERS file
CODEOWNERS_MOVE_AFTER_INDX=`grep -n "$BUNDLE_TO_MOVE_ATFER/" CODEOWNERS | cut -d':' -f 1`
CURRENT_CODEOWNERS_INDEX=$((`grep -n "$1/" CODEOWNERS | cut -d':' -f 1` - 1))
sed -i "$CURRENT_CODEOWNERS_INDEX{n;h;d};$CODEOWNERS_MOVE_AFTER_INDX{G};s|/addons/$ADDON/$1|/bundles/$1|" CODEOWNERS
echo Update feature.xml
FEATURE_FILE="features/karaf/openhab-addons/src/main/feature/feature.xml"
sed -i "s|org.openhab.$ADDON/$1|org.openhab.addons.bundles/$1|" $FEATURE_FILE
echo Update bom pom
BOM_FILE="bom/openhab-addons/pom.xml"
BOM_LINE=$((`grep -n "$1" $BOM_FILE | cut -d':' -f 1` - 1))
sed -i "${BOM_LINE}b1;b;:1;s|org.openhab.${ADDON}|org.openhab.addons.bundles|" $BOM_FILE
echo Move $ADDON to new location
mv addons/$ADDON/$1 bundles/
if [ ! -e "${NEW_LOC}/src/main/resources" ] ; then
mkdir "${NEW_LOC}/src/main/resources"
fi
mv "${NEW_LOC}/ESH-INF" "${NEW_LOC}/src/main/resources/ESH-INF"
echo Remove obsolete files
rm -rf ${NEW_LOC}/META-INF
rm -f ${NEW_LOC}/build.properties
if [ -e "${NEW_LOC}/OSGI-INF/.gitignore" ] ; then
rm -rf "${NEW_LOC}/OSGI-INF"
else
mv "${NEW_LOC}/OSGI-INF" "${NEW_LOC}/src/main/resources/OSGI-INF"
fi
cd ${NEW_LOC}
echo Make changes to pom.xml
sed -i "s|<groupId>org.openhab.${ADDON}</groupId>|<groupId>org.openhab.addons.bundles</groupId>|" pom.xml
sed -i 's|<artifactId>pom</artifactId>|<artifactId>org.openhab.addons.reactor.bundles</artifactId>|' pom.xml
sed -i '/<packaging>eclipse-plugin<\/packaging>/d' pom.xml
sed -i "s|<name>\(.*\)|<name>openHAB Add-ons :: Bundles :: \1|" pom.xml
echo Replace .classpath with bnd version
cp ../${TEMPLATE_PROJECT}/.classpath .
echo Replace .project with bnd version and update te copied version
cp ../${TEMPLATE_PROJECT}/.project .
sed -i "s|${TEMPLATE_PROJECT}|$1|" .project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment