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

Code before:
#!/bin/bash

# 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
    done < $DEPENDENCIES_FILE > "${DEPENDENCIES_FILE}_new"
    mv "${DEPENDENCIES_FILE}_new" $DEPENDENCIES_FILE
done

# Change build.gradle versions
for DEPENDENCIES_FILE in `find . -type f -iname "build.gradle"` ; do
    echo $DEPENDENCIES_FILE;
    
    while IFS= read -r line; do
        if [[ $line == *"ext.compose_version ="* ]]; then
            echo "$line" | sed -En "s/\'.*'/\'$compose_version\'/p"
        elif [[ $line == *"ext.compose_snapshot_version ="* ]]; then
            echo "$line" | sed -En "s/\'.*'/\'$snapshot_version\'/p"
        else
            echo "$line";
        fi
    done < $DEPENDENCIES_FILE > "${DEPENDENCIES_FILE}_new"
    mv "${DEPENDENCIES_FILE}_new" $DEPENDENCIES_FILE
done



Review comment (bug):
I don't want to go overboard with error/edge case handling since it's a quick script, but maybe in each for loop have a boolean to track if anything was changed and then set it to true if either sed line gets hit. Then the loop can write the file only if it was changed and then print that filename to the console. If I'm reading this right, it will currently write (and print the filename of) every dependencies.kt and build.gradle file, even when they're not changed, so it will be harder to spot any issues and cause a very minor amount of unnecessary io.

Code after:
    
done

# Change build.gradle versions
for DEPENDENCIES_FILE in `find . -type f -iname "build.gradle"` ; do
    MADE_CHANGE=false;
    TEMP_FILENAME="${DEPENDENCIES_FILE}_new";
    while IFS= read -r line; do
        if [[ $line == *"ext.compose_version ="* ]]; then
            echo "$line" | sed -En "s/\'.*'/\'$compose_version\'/p"
            MADE_CHANGE=true;
        elif [[ $line == *"ext.compose_snapshot_version ="* ]]; then
            echo "$line" | sed -En "s/\'.*'/\'$snapshot_version\'/p"
            MADE_CHANGE=true;
        else
            echo "$line";
        fi
    done < $DEPENDENCIES_FILE > $TEMP_FILENAME
    if $MADE_CHANGE = true; then
        echo $DEPENDENCIES_FILE;
        mv $TEMP_FILENAME $DEPENDENCIES_FILE;
    else
        rm $TEMP_FILENAME;
    fi
done