Skip to content

Instantly share code, notes, and snippets.

@williamd1k0
Last active April 26, 2023 23:50
Show Gist options
  • Save williamd1k0/3bd76bef32f7132faf55c3e1c34a7577 to your computer and use it in GitHub Desktop.
Save williamd1k0/3bd76bef32f7132faf55c3e1c34a7577 to your computer and use it in GitHub Desktop.
Simple dependency and ownership finder for Unity assets.
any posix shell
coreutils (basename, dirname, realpath, sort, cut)
ripgrep
moreutils (sponge): optional for cache script
fd-find: optional for cache script
make: optional for install script
INSTALL_DIR = ${HOME}/.local/bin
INSTALL_TARGETS = $(patsubst %,${INSTALL_DIR}/unity-%,guid projdir cache deps owns globals)
install: ${INSTALL_TARGETS}
${INSTALL_DIR}/%: ./%
install -Dm 764 $< $@
uninstall:
rm ${INSTALL_TARGETS}
.PHONY: install uninstall
#!/usr/bin/sh
PROJECT_BASE="$(unity-projdir "$1")"
[ -n "$PROJECT_BASE" ] || return 1
CACHE_FILE="$PROJECT_BASE/Temp/assets.cache"
mkdir -p "$(dirname "$CACHE_FILE")"
printf "" > "$CACHE_FILE"
fd -e meta . "$PROJECT_BASE/Assets" | while read -r META; do
ASSET="$(dirname "$META")/$(basename "$META" .meta)"
ASSET_REL="$(realpath --relative-to "$PROJECT_BASE" "$ASSET")"
GUID="$(unity-guid "$ASSET")"
printf "[cache] %s\n" "$ASSET_REL" 1>&2
printf "%s,%s\n" "$ASSET_REL" "$GUID" >> "$CACHE_FILE"
done
sort -u "$CACHE_FILE" | sponge "$CACHE_FILE"
#!/usr/bin/sh
TARGET_ASSET="$1"
PROJECT_BASE="$(unity-projdir "$TARGET_ASSET")"
PROJECT_ASSETS="$PROJECT_BASE"/Assets
ASSET_REFS=$(rg '^.*guid: (\w{32}).*$' -r '$1' "$TARGET_ASSET" | sort -u | rg -v "$(unity-guid "$TARGET_ASSET")")
if [ ! -f "$PROJECT_BASE/Temp/assets.cache" ]; then
printf "Cache not found, consider running \`unity-cache\` to generate it.\n" 1>&2
fi
printf "[%s]\n" "$TARGET_ASSET" 1>&2
for GUID in $ASSET_REFS; do
REF_PATH=""
if [ -f "$PROJECT_BASE/Temp/assets.cache" ]; then
REF_PATH="$(rg -F "$GUID" "$PROJECT_BASE/Temp/assets.cache" | cut -d, -f1)"
else
REF_PATH="$(rg "^guid: $GUID" "$PROJECT_ASSETS" -g "*.meta" -m1 -l | rg '^(.+).meta$' -r '$1')"
fi
printf "%s: " "$GUID" 1>&2
if [ -n "$REF_PATH" ]; then
printf "%s\n" "$(realpath --relative-to "$(pwd)" "$REF_PATH")"
else
printf "\n"
fi
done
#!/usr/bin/sh
PROJECT_BASE="$(unity-projdir "$(pwd)")"
PROJECT_ASSETS="$PROJECT_BASE"/Assets
GLOBALS_SCRIPTS=$(rg -l "DontDestroyOnLoad" "$PROJECT_ASSETS")
for SCRIPT in $GLOBALS_SCRIPTS; do
printf "%s\n" "$(realpath --relative-to "$(pwd)" "$SCRIPT")"
done
#!/usr/bin/sh
rg '^guid: (\w{32})$' -r '$1' "$1".meta
#!/usr/bin/sh
TARGET_ASSET="$1"
PROJECT_BASE="$(unity-projdir "$TARGET_ASSET")"
PROJECT_ASSETS="$PROJECT_BASE"/Assets
TARGET_GUID="$(unity-guid "$TARGET_ASSET")"
if [ -n "$TARGET_GUID" ]; then
printf "[%s]\n" "$TARGET_ASSET"
rg -g '!*.meta' -m1 -l "$TARGET_GUID" "$PROJECT_ASSETS" | while read -r OWNER; do
printf "%s: %s\n" "$(unity-guid "$OWNER")" "$(realpath --relative-to "$(pwd)" "$OWNER")"
done
fi
#!/usr/bin/sh
TEST_DIR="$(realpath "${1:-.}")"
if [ -f "$TEST_DIR" ]; then
TEST_DIR="$(dirname "$1")"
fi
while [ "$TEST_DIR" != "/" ]; do
if [ -f "$TEST_DIR/ProjectSettings/ProjectVersion.txt" ]; then
echo "$TEST_DIR"
return 0
fi
TEST_DIR=$(dirname "$TEST_DIR")
done
return 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment