Skip to content

Instantly share code, notes, and snippets.

@JoseAlcerreca
Created June 17, 2022 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoseAlcerreca/db3934089312cf9ce348d64a0bcd0a77 to your computer and use it in GitHub Desktop.
Save JoseAlcerreca/db3934089312cf9ce348d64a0bcd0a77 to your computer and use it in GitHub Desktop.
Renames a whole template and data model
#!/bin/bash
#
# Copyright (C) 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
if [ "$#" -ne 2 ]; then
echo "Usage: bash customizer.sh my.new.package MyNewDataItem" >&2
exit 2
fi
PACKAGE=$1
DATAMODEL=$2
SUBDIR=${PACKAGE//.//} # Replaces . for /
# Create new directories
mkdir -p app/src/androidTest/java/$SUBDIR
mkdir -p app/src/main/java/$SUBDIR
mkdir -p app/src/test/java/$SUBDIR
# Move files to new directories
mv app/src/androidTest/java/templates/reactive/* app/src/androidTest/java/$SUBDIR
mv app/src/main/java/templates/reactive/* app/src/main/java/$SUBDIR
mv app/src/test/java/templates/reactive/* app/src/test/java/$SUBDIR
# Delete old directories
rm -rf mv app/src/androidTest/java/templates
rm -rf mv app/src/main/java/templates
rm -rf mv app/src/test/java/templates
# Rename package and imports
find ./ -type f -name "*.kt" -exec sed -i "s/package templates.reactive/package $PACKAGE/g" {} \;
find ./ -type f -name "*.kt" -exec sed -i "s/import templates.reactive/import $PACKAGE/g" {} \;
# Gradle files
find ./ -type f -name "*.kts" -exec sed -i "s/templates.reactive/$PACKAGE/g" {} \;
# Rename model
find ./ -type f -name "*.kt" -exec sed -i "s/DataItem/${DATAMODEL^}/g" {} \; # First upper case
find ./ -type f -name "*.kt" -exec sed -i "s/dataItem/${DATAMODEL,}/g" {} \; # First lower case
find ./ -type f -name "*.kt" -exec sed -i "s/dataitem/${DATAMODEL,,}/g" {} \; # All lowercase
# Rename files
find -name "*DataItem*.kt" | sed "p;s/DataItem/${DATAMODEL^}/" | xargs -d '\n' -n 2 mv
find -name "*dataitem*" -type d | sed "p;s/dataitem/${DATAMODEL,,}/" | xargs -d '\n' -n 2 mv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment