1#!/bin/bash
2# Helper script to kick-off a playground project setup.
3# This is intended to be used when we create a new Playground project or update existing ones
4# if we do structural changes in Playground's setup.
5
6function relativize() {
7    python3 -c "import os.path; print(os.path.relpath('$1', '$2'))"
8}
9
10PLAYGROUND_REL_PATH=$(dirname $0)
11WORKING_DIR=$(pwd)
12
13# helper symlink function that will also normalize the paths.
14function symlink() {
15    SRC=$1
16    TARGET=$2
17    TARGET_PARENT_DIR=$(dirname $TARGET)
18    REL_PATH_TO_TARGET_PARENT=$(relativize $SRC $WORKING_DIR/$TARGET_PARENT_DIR)
19    rm -rf $TARGET
20    ln -s $REL_PATH_TO_TARGET_PARENT $TARGET
21}
22
23# symlink to the gradle folder in playground-common
24symlink "${PLAYGROUND_REL_PATH}/gradle" gradle
25symlink "${PLAYGROUND_REL_PATH}/gradlew" gradlew
26symlink "${PLAYGROUND_REL_PATH}/gradlew.bat" gradlew.bat
27# symlink to the properties file that is shared w/ androidx main
28symlink "${PLAYGROUND_REL_PATH}/androidx-shared.properties" gradle.properties
29
30ANDROIDX_IDEA_DIR="${PLAYGROUND_REL_PATH}/../.idea"
31
32# cleanup .idea, we'll re-create it
33rm -rf .idea
34mkdir .idea
35
36# create idea directories first .idea config directories that are tracked in git
37git ls-tree -d -r HEAD --name-only --full-name $ANDROIDX_IDEA_DIR|xargs mkdir -p
38
39# get a list of all .idea files that are in git tree
40# we excluse vcs as it is used for multiple repo setup which we don't need in playground
41TRACKED_IDEA_FILES=( $(git ls-tree -r HEAD --name-only --full-name $ANDROIDX_IDEA_DIR| grep -v vcs| grep -v Compose) )
42
43# create a symlink for each one of them
44for IDEA_CONFIG_FILE in "${TRACKED_IDEA_FILES[@]}"
45do
46    # path to the actual .idea config file
47    ORIGINAL_FILE="$PLAYGROUND_REL_PATH/../$IDEA_CONFIG_FILE"
48    symlink $ORIGINAL_FILE $IDEA_CONFIG_FILE
49    # force add the file to git
50    git add -f $IDEA_CONFIG_FILE
51done
52