Skip to content

Instantly share code, notes, and snippets.

@drewbrokke
Created February 28, 2018 17:14
Show Gist options
  • Save drewbrokke/27cfe06b7624470afbe50abcd578bf9b to your computer and use it in GitHub Desktop.
Save drewbrokke/27cfe06b7624470afbe50abcd578bf9b to your computer and use it in GitHub Desktop.
Move language keys from one directory to another

moveLang

Usage

moveLang \
  -k "key-one key-two key-three" \
  -s modules/apps/foundation/contacts/contacts-web/src/main/resources/content \
  -t portal-impl/src/content

-k A string of space-separated language keys to move

-s The directory containing the source Language.properties files

-t The target directory (does not have to contain Language.properties files)

#!/bin/bash
error() {
echo "$1" && exit 1
}
while getopts "k:s:t:" FLAGS ; do
case $FLAGS in
k) KEYS="${OPTARG}" ;;
s) SOURCE="${OPTARG}" ;;
t) TARGET="${OPTARG}" ;;
esac
done
[ -n "${KEYS}" ] || error "Add a string of language keys with '-k'"
[ -n "${SOURCE}" ] || error "Add a source directory with '-s'"
[ -n "${TARGET}" ] || error "Add a target directory with '-t'"
[ -d "${SOURCE}" ] || error "Source directory does not exist: ${SOURCE}"
[ -d "${TARGET}" ] || error "Target directory does not exist: ${TARGET}"
KEYS_PATTERN="^${KEYS// /|}="
SOURCE_FILES=(${SOURCE}/Language*.properties)
for SOURCE_FILE in "${SOURCE_FILES[@]}" ; do
LINES="$(grep -E "${KEYS_PATTERN}" "${SOURCE_FILE}")"
if [ -z "${LINES}" ] ; then
continue
fi
TARGET_FILE="${TARGET}/${SOURCE_FILE##*/}"
echo "${TARGET_FILE}"
if [ -s "${TARGET_FILE}" ]; then
echo >> "${TARGET_FILE}"
fi
echo -n "${LINES}" >> "${TARGET_FILE}"
gsed -i -r "/${KEYS_PATTERN}/d" "${SOURCE_FILE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment