Skip to content

Instantly share code, notes, and snippets.

@jswent
Last active July 18, 2024 19:21
Show Gist options
  • Save jswent/532d9aee556cb636262bcc523c38fe31 to your computer and use it in GitHub Desktop.
Save jswent/532d9aee556cb636262bcc523c38fe31 to your computer and use it in GitHub Desktop.
clone_crestron bash snippet
# Function to clone the jswent/hass-crestron-component repository and extract the component folder to be used
# in Home Assistant's `custom_components` folder. Optionally override default arguments to duplicate integrations
# with a new domain and name.
#
# Usage:
# clone_crestron [new_directory_name] [git_url] [subdirectory_path]
#
# Arguments:
# new_directory_name Optional. The name for the new HA component folder. Defaults to "crestron".
# git_url The URL of the git repository to clone. Defaults to my component.
# subdirectory_path The path to the subdirectory within the cloned repository to move.
# Defaults to "custom_components/crestron".
#
# If new_directory_name is "crestron", the const.py and manifest.json files will not be updated.
# If new_directory_name is provided and is not "crestron", the following updates will be made:
# - const.py: DOMAIN = "crestron" will be replaced with DOMAIN = "$new_directory_name".
# - manifest.json:
# - "domain": "crestron" will be replaced with "domain": "$new_directory_name".
# - "name": "Crestron XSIG Integration" will be replaced with "name": "Crestron XSIG Integration - $new_directory_name".
#
# Additionally, if the specified new_directory_name already exists in the current directory,
# it will be moved to a .old directory with a timestamp before proceeding.
clone_crestron() {
# Assign default values if no arguments are provided
NEW_DIR_NAME="${1:-crestron}"
GIT_URL="${2:-https://github.com/jswent/hass-crestron-component}"
SUBDIR_PATH="${3:-custom_components/crestron}"
# TODO: Add help menu and prompt for confirmation if executing with no arguments
# Ensure .old directory exists
if [ ! -d ".old" ]; then
mkdir .old
fi
# Check if the new directory already exists
if [ -d "$NEW_DIR_NAME" ]; then
BACKUP_NAME=".old/${NEW_DIR_NAME}_$(date +%Y%m%d_%H%M%S)"
echo "Directory $NEW_DIR_NAME already exists. Moving it to $BACKUP_NAME."
mv "$NEW_DIR_NAME" "$BACKUP_NAME"
fi
# Clone the repository into a temporary directory
TEMP_DIR=$(mktemp -d)
git clone "$GIT_URL" "$TEMP_DIR"
# Check if the clone was successful
if [ $? -ne 0 ]; then
echo "Failed to clone the repository"
rm -rf "$TEMP_DIR"
return 1
fi
# Move the specific subdirectory to the current directory with the new name
if [ -d "$TEMP_DIR/$SUBDIR_PATH" ]; then
mv "$TEMP_DIR/$SUBDIR_PATH" "$NEW_DIR_NAME"
else
echo "Subdirectory $SUBDIR_PATH does not exist in the repository."
rm -rf "$TEMP_DIR"
return 1
fi
# If NEW_DIR_NAME is not "crestron", update const.py and manifest.json
if [ "$NEW_DIR_NAME" != "crestron" ]; then
# Replace DOMAIN = "crestron" with DOMAIN = "$NEW_DIR_NAME" in const.py
CONST_FILE="$NEW_DIR_NAME/const.py"
if [ -f "$CONST_FILE" ]; then
sed -i "s/DOMAIN = \"crestron\"/DOMAIN = \"$NEW_DIR_NAME\"/" "$CONST_FILE"
if [ $? -ne 0 ]; then
echo "Failed to update DOMAIN in const.py"
return 1
fi
else
echo "const.py not found in $NEW_DIR_NAME"
fi
# Replace domain and name in manifest.json
MANIFEST_FILE="$NEW_DIR_NAME/manifest.json"
if [ -f "$MANIFEST_FILE" ]; then
sed -i "s/\"domain\": \"crestron\"/\"domain\": \"$NEW_DIR_NAME\"/" "$MANIFEST_FILE"
sed -i "s/\"name\": \"Crestron XSIG Integration\"/\"name\": \"Crestron XSIG Integration - $NEW_DIR_NAME\"/" "$MANIFEST_FILE"
if [ $? -ne 0 ]; then
echo "Failed to update manifest.json."
return 1
fi
else
echo "manifest.json not found in $NEW_DIR_PATH."
fi
fi
# Clean up the temporary directory
rm -rf "$TEMP_DIR"
echo "Cloned and moved crestron component to $NEW_DIR_NAME, domain/name updated if applicable."
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
clone_crestron "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment