1#!/bin/bash 2set -e 3 4stateDir="$1" 5moveArg="$2" 6 7scriptPath="$(cd $(dirname $0) && pwd)" 8supportRoot="$(cd $scriptPath/../../.. && pwd)" 9checkoutRoot="$(cd $supportRoot/../.. && pwd)" 10 11function usage() { 12 echo "usage: $0 <statePath>" 13 echo "Backs up build state into <statePath>" 14 exit 1 15} 16 17if [ "$stateDir" == "" ]; then 18 usage 19fi 20 21move=false 22if [ "$moveArg" == "--move" ]; then 23 move=true 24 shift 25fi 26 27if [ "$3" != "" ]; then 28 echo "Unrecognized argument $3" 29 usage 30fi 31 32rm -rf "$stateDir" 33mkdir -p "$stateDir" 34stateDir="$(cd $stateDir && pwd)" 35 36if [ "$OUT_DIR" == "" ]; then 37 OUT_DIR="$checkoutDir/out" 38else 39 if [ "$GRADLE_USER_HOME" == "" ]; then 40 GRADLE_USER_HOME="$OUT_DIR/.gradle" 41 fi 42fi 43 44if [ "$DIST_DIR" == "" ];then 45 DIST_DIR="$OUT_DIR/dist" 46fi 47 48if [ "$GRADLE_USER_HOME" == "" ]; then 49 GRADLE_USER_HOME="$(cd ~ && pwd)/.gradle" 50fi 51 52# makes the contents of $2 match the contents of $1 53function copy() { 54 from="$1" 55 to="$2" 56 rm -rf "$to" 57 if [ -e "$from" ]; then 58 mkdir -p "$(dirname $to)" 59 if [ "$move" == "true" ]; then 60 mv "$from" "$to" 61 else 62 cp --preserve=all -rT "$from" "$to" 63 fi 64 else 65 rm -rf "$to" 66 fi 67} 68 69function backupState() { 70 backupDir="$1" 71 echo "Saving state into $backupDir" 72 mkdir -p "$backupDir" 73 74 # back up DIST_DIR if not under OUT_DIR 75 if [ "$DIST_DIR" != "$OUT_DIR/dist" ]; then 76 copy "$DIST_DIR" "$backupDir/dist" 77 fi 78 # back up GRADLE_USER_HOME if not under OUT_DIR 79 if [ "$GRADLE_USER_HOME" != "$OUT_DIR/.gradle" ]; then 80 copy "$GRADLE_USER_HOME" "$backupDir/gradleUserHome" 81 fi 82 # back up out/ 83 copy "$OUT_DIR" "$backupDir/out" 84 85 # If DIST_DIR is under out/, then move it to where we will find it 86 if [ "$DIST_DIR" == "$OUT_DIR/dist" ]; then 87 mv "$backupDir/out/dist" "$backupDir/dist" 2>/dev/null || true 88 fi 89 90 # if $GRADLE_USER_HOME is under out/ , then move it to where we will find it 91 if [ "$GRADLE_USER_HOME" == "$OUT_DIR/.gradle" ]; then 92 mv "$backupDir/out/.gradle" "$backupDir/gradleUserHome" 2>/dev/null || true 93 fi 94 95 copy "$supportRoot/.gradle" "$backupDir/support/.gradle" 96 copy "$supportRoot/buildSrc/.gradle" "$backupDir/buildSrc/.gradle" 97 copy "$supportRoot/local.properties" "$backupDir/local.properties" 98} 99 100backupState $stateDir 101 102# If we move the state to another dir, we might move the Gradle Enterprise credentials too 103# This function attempts to restore them 104function tryToRestoreCredentials() { 105 stateDir="$1" 106 mkdir -p "$GRADLE_USER_HOME" 107 newCredentialDir="$stateDir/gradleUserHome/enterprise" 108 if [ -e "$newCredentialDir" ]; then 109 cp -r "$stateDir/gradleUserHome/enterprise" "$GRADLE_USER_HOME/" 110 fi 111} 112if [ "$move" == "true" ]; then 113 tryToRestoreCredentials $stateDir 114fi 115