|
| 1 | +#!/usr/bin/env bash |
| 2 | +# The REAL CppBuild.java (and friends) live in THIS folder's src/java/. |
| 3 | +# This script creates symlinks INSIDE your processing4 checkout (at |
| 4 | +# java/src/processing/mode/cpp/) pointing back at these real files, so |
| 5 | +# that running Gradle from processing4 compiles the same source you're |
| 6 | +# editing here -- no need to keep two copies in sync. |
| 7 | +# |
| 8 | +# Handles: |
| 9 | +# - destination directory missing entirely (creates it) |
| 10 | +# - individual source files missing (skips just that one, warns) |
| 11 | +# - a target that's already a regular file (backs it up, then links) |
| 12 | +# - a target that's already a CORRECT symlink (skips, no-op) |
| 13 | +# - a target that's a symlink pointing somewhere ELSE / dangling |
| 14 | +# (replaces it with the correct link) |
| 15 | +# - files in the destination that aren't part of our known set |
| 16 | +# (left completely alone) |
| 17 | +# |
| 18 | +# Usage: |
| 19 | +# ./setup-dev-symlinks.sh /path/to/your/processing4 |
| 20 | + |
| 21 | +set -e |
| 22 | + |
| 23 | +if [ -z "$1" ]; then |
| 24 | + echo "Usage: $0 /path/to/your/processing4" |
| 25 | + echo "Example: $0 ~/Projects/processing4" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +if [ ! -d "$1" ]; then |
| 30 | + echo "ERROR: '$1' is not a directory." |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +PROCESSING4_DIR="$(cd "$1" && pwd)" |
| 35 | +DEST_DIR="$PROCESSING4_DIR/java/src/processing/mode/cpp" |
| 36 | + |
| 37 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 38 | +SRC_DIR="$SCRIPT_DIR/src/java" |
| 39 | + |
| 40 | +if [ ! -d "$SRC_DIR" ]; then |
| 41 | + echo "ERROR: $SRC_DIR does not exist." |
| 42 | + echo "Expected the real .java source files to live there." |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +mkdir -p "$DEST_DIR" |
| 47 | + |
| 48 | +echo "real source: $SRC_DIR" |
| 49 | +echo "linking into: $DEST_DIR" |
| 50 | +echo |
| 51 | + |
| 52 | +shopt -s nullglob |
| 53 | +SOURCE_FILES=("$SRC_DIR"/*.java) |
| 54 | +shopt -u nullglob |
| 55 | + |
| 56 | +if [ ${#SOURCE_FILES[@]} -eq 0 ]; then |
| 57 | + echo "WARNING: no .java files found in $SRC_DIR -- nothing to link." |
| 58 | + exit 0 |
| 59 | +fi |
| 60 | + |
| 61 | +linked=0 |
| 62 | +skipped=0 |
| 63 | +warned=0 |
| 64 | + |
| 65 | +for f in "${SOURCE_FILES[@]}"; do |
| 66 | + name="$(basename "$f")" |
| 67 | + target="$DEST_DIR/$name" |
| 68 | + |
| 69 | + if [ ! -e "$f" ]; then |
| 70 | + # Shouldn't normally happen (glob already matched it), but guards |
| 71 | + # against a file being deleted mid-run. |
| 72 | + echo "WARNING: $name disappeared from source -- skipping." |
| 73 | + warned=$((warned+1)) |
| 74 | + continue |
| 75 | + fi |
| 76 | + |
| 77 | + if [ -L "$target" ]; then |
| 78 | + # Already a symlink -- check if it already points at the right file. |
| 79 | + current_link="$(readlink "$target")" |
| 80 | + if [ "$current_link" == "$f" ]; then |
| 81 | + skipped=$((skipped+1)) |
| 82 | + continue |
| 83 | + fi |
| 84 | + echo "Replacing stale/incorrect symlink: $name (was -> $current_link)" |
| 85 | + rm "$target" |
| 86 | + elif [ -e "$target" ]; then |
| 87 | + # A REAL file/directory sits at the target -- back it up rather than |
| 88 | + # silently destroying whatever's there (could be a contributor's own |
| 89 | + # local edits made before running this script). |
| 90 | + backup="$target.bak" |
| 91 | + n=1 |
| 92 | + while [ -e "$backup" ]; do |
| 93 | + backup="$target.bak.$n" |
| 94 | + n=$((n+1)) |
| 95 | + done |
| 96 | + echo "Backing up existing $name -> $(basename "$backup")" |
| 97 | + mv "$target" "$backup" |
| 98 | + fi |
| 99 | + |
| 100 | + ln -s "$f" "$target" |
| 101 | + echo "linked $name" |
| 102 | + linked=$((linked+1)) |
| 103 | +done |
| 104 | + |
| 105 | +echo |
| 106 | +echo "Done: $linked linked, $skipped already correct, $warned warnings." |
| 107 | +echo "$DEST_DIR now points at the real source in $SRC_DIR." |
| 108 | +echo "Build the jar by running ./gradlew from $PROCESSING4_DIR." |
0 commit comments