Skip to content

Instantly share code, notes, and snippets.

@auipga
Last active September 20, 2023 02:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auipga/8726110728ff6ccf75fe4671bf35115f to your computer and use it in GitHub Desktop.
Save auipga/8726110728ff6ccf75fe4671bf35115f to your computer and use it in GitHub Desktop.
This script circumvents the guaranteed loss of your loved local file history in PhpStorm (with hook for Archlinux)
[Trigger]
Operation = Upgrade
Operation = Install
Type = Package
Target = phpstorm
[Action]
Depends = rsync
Description = Keeping JetBrains Cache (including local file history)...
When = PostTransaction
Exec = /usr/bin/su your_user_name -c ~/.local/bin/phpstorm-upgrade.sh
#!/usr/bin/env bash
# choose: rsync, mv, cp, ln. cp is great for btrfs
METHOD=cp
PACKAGE=phpstorm # or: phpstorm-eap
PKGNAME=PhpStorm # what the folder name starts with
CACHE_LOCATION=~/.cache/JetBrains
# no changes needed below this line
VERSION=$(pacman -Qi "$PACKAGE" | grep -Po '^Version\s*: \K.+') # 2022.3.1-1
ARRAY=( ${VERSION//./ } ) # array ("2022", "3", "1-1") or array ("2023", "1-1")
NEW_FOLDER_NAME="${PKGNAME}${ARRAY[0]}.${ARRAY[1]%-*}" # PhpStorm2022.3
NEW_CACHE_FOLDER=$CACHE_LOCATION/$NEW_FOLDER_NAME
function e() { echo "${PACKAGE}-hook: $*"; }
if [[ $EUID -eq 0 ]]; then e "Do not run this as root!"; exit 1; fi
# TODO: fix this regex, which I think should already work but doesn't
#if [[ ! $VERSION =~ "^20\d\d\.\d\.\d+(\-\d+)?$" ]]; then
# e "Version \"${VERSION}\" looks wrong. Backup skipped."
# exit 1
#fi
if [ -d $NEW_CACHE_FOLDER ]; then
e "Recent cache folder is already present at $NEW_CACHE_FOLDER"
exit 0
fi
function handle() { # method, source, target
set +e
e "$OLD_FOLDER_NAME -> $NEW_FOLDER_NAME (${1})"
if [[ $1 == rsync ]]; then
rsync -avzq "$2" "$3"
elif [[ $1 == ln ]]; then
ln -srf "$2" "$3"
elif [[ $1 == cp ]]; then
cp -ra "$2" "$3"
elif [[ $1 == mv ]]; then
mv "$2" "$3"
else
e "Error: Method unsupported"
exit 1
fi
e "Success!"
exit 0
}
# find most recent existing folder
MAJOR=${ARRAY[0]}
MINOR=${ARRAY[1]%-*}
while [ $MAJOR -ge 2016 ]; do
while [ $MINOR -ge 1 ]; do
OLD_FOLDER_NAME="${PKGNAME}${MAJOR}.${MINOR}"
OLD_CACHE_FOLDER=$CACHE_LOCATION/$OLD_FOLDER_NAME
if [ -d $OLD_CACHE_FOLDER ]; then
handle "$METHOD" "$OLD_CACHE_FOLDER" "$NEW_CACHE_FOLDER"
#else
# echo "no hit: $OLD_CACHE_FOLDER"
fi
((MINOR-=1)) # check last release
done
((MAJOR-=1)) # check last year
MINOR=3 # reset to maximal possible minor
done
e "no recent cache folder found. This is ok if you never ran ${PKGNAME} before."
exit 1
@auipga
Copy link
Author

auipga commented Dec 29, 2022

Adjust your_user_name and the path of the script if you placed it somewhere else. Both here: hook#L11

@auipga
Copy link
Author

auipga commented Apr 5, 2023

Fixed a bug with 2023.1-1 (it has only major and minor but no patch version)

@auipga
Copy link
Author

auipga commented Sep 20, 2023

Fixed a bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment