Repository: android/compose-samples
File: scripts/upgrade_samples.sh (Shell)

Code before:

# Copyright (C) 2020 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
#
#     https://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.

########################################################################
#
# Changes all samples to a new Compose version
#
# Example: To run 
#     ./scripts/upgrade_samples.sh
#
########################################################################

set -e

echo "Version to change Compose to (e.g 1.2.0-alpha06): ";
read compose_version;

echo "Snapshot ID: (Blank for none)";
read snapshot_version;

echo "Upgrading to $compose_version snapshot $snapshot_version"

# Change Dependencies.kt versions
for DEPENDENCIES_FILE in `find . -type f -iname "dependencies.kt"` ; do
    echo $DEPENDENCIES_FILE;
    COMPOSE_BLOCK=false;
    while IFS= read -r line; do
        if [[ $line == *"val version ="* ]] && $COMPOSE_BLOCK = true; then
            echo "$line" | sed -En 's/".*"/"'$compose_version'"/p'
        elif [[ $line == *"val snapshot ="* ]] && $COMPOSE_BLOCK = true; then
            echo "$line" | sed -En 's/".*"/"'$snapshot_version'"/p'
        else
            if [[ $line == *"object Compose {"* ]]; then
                COMPOSE_BLOCK=true;
            elif [[ $line == *"}"* ]]; then
                COMPOSE_BLOCK=false;
            fi
            echo "$line";
        fi

Review comment (suggestion):
I'm wondering if we should have a check to see if "git status" outputs "Changes not staged for commit" and offer a chance to continue or bail? If someone has made some local changes to test something, it might be good to call that out in case they want to revert/stash before doing the update.

Code after:

# 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
#
#     https://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.

########################################################################
#
# Changes all samples to a new Compose version
#
# Example: To run 
#     ./scripts/upgrade_samples.sh
#
########################################################################

set -e

# Check for clean git status
if [[ `git status --porcelain` ]]; then
    read -r -p "You have uncommited git changes. Are you sure you want to continue? [y/N] " response
    if [[ "$response" =~ ^([nN])$ ]]; then
        exit 0;
    fi
fi

echo "Version to change Compose to (e.g 1.2.0-alpha06): ";
read compose_version;

echo "Snapshot ID: (Blank for none)";
read snapshot_version;

if [ -z "$snapshot_version" ]; then
    echo "Changing Compose version to $compose_version"
else
    echo "Changing Compose version to $compose_version Snapshot $snapshot_version"
fi

# Change Dependencies.kt versions
for DEPENDENCIES_FILE in `find . -type f -iname "dependencies.kt"` ; do
    COMPOSE_BLOCK=false;
    MADE_CHANGE=false;
    TEMP_FILENAME="${DEPENDENCIES_FILE}_new";