-
-
Save sffej/325d071431c8dcddf205066f7b66b3ea to your computer and use it in GitHub Desktop.
migrate bookmarks when ide update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# The user's home directory | |
USER_HOME=$HOME | |
# Define a function to display help information | |
show_help() { | |
echo "Usage information for the bookmark migration script" | |
echo "Usage: $0 [options]" | |
echo "" | |
echo "Options:" | |
echo " -h, --help Show this help message" | |
echo " -i IDE_NAME Specify the IDE name (e.g., AndroidStudio2023.3 or IntelliJIdea2023.2)" | |
echo " -o OUTPUT_PATH Specify the output path" | |
exit 0 | |
} | |
# Parse command-line arguments | |
while getopts "hi:o:" opt; do | |
case $opt in | |
h) | |
show_help | |
;; | |
i) | |
IDE_NAME="$OPTARG" | |
;; | |
o) | |
OUTPUT_PATH="$OPTARG" | |
;; | |
?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
*) | |
show_help | |
;; | |
esac | |
done | |
# Default values | |
# IDE_NAME_DEFAULT="AndroidStudio2023.3" | |
IDE_NAME_DEFAULT="AndroidStudioPreview2024.1" | |
OUTPUT_PATH_DEFAULT="$HOME/Desktop/Work/Other/Bookmarks" | |
# If IDE_NAME is not provided, use the default value | |
IDE_NAME=${IDE_NAME:-$IDE_NAME_DEFAULT} | |
OUTPUT_PATH=${OUTPUT_PATH:-$OUTPUT_PATH_DEFAULT/$IDE_NAME} | |
# Create the output directory | |
mkdir -p "$OUTPUT_PATH" | |
# Determine the vendor and bookmark directory based on IDE_NAME | |
get_bookmark_dir() { | |
if [[ "$IDE_NAME" == *"AndroidStudio"* ]]; then | |
echo "$USER_HOME/Library/Application Support/Google/$IDE_NAME/workspace" | |
elif [[ "$IDE_NAME" == *"IntelliJIdea"* ]]; then | |
echo "$USER_HOME/Library/Application Support/JetBrains/$IDE_NAME/workspace" | |
else | |
echo "Error: Unknown IDE name." >&2 | |
exit 1 | |
fi | |
} | |
# Get the bookmark directory | |
BOOKMARKS_DIR=$(get_bookmark_dir) | |
# Automatically obtain the current working directory as the project path | |
PROJECT_DIR=$(pwd) | |
# Path to .idea/workspace.xml | |
WORKSPACE_XML="$PROJECT_DIR/.idea/workspace.xml" | |
# Check if the workspace.xml file exists | |
if [ ! -f "$WORKSPACE_XML" ]; then | |
echo "Error: workspace.xml file not found in $PROJECT_DIR." | |
exit 1 | |
fi | |
# Use the xmllint tool to parse the XML file and extract the id value | |
PROJECT_ID=$(xmllint --xpath "//component[@name='ProjectId']/@id" "$WORKSPACE_XML" | cut -d '"' -f 2) | |
# Check if the id was successfully extracted | |
if [ -z "$PROJECT_ID" ]; then | |
echo "Error: Unable to extract the project ID from workspace.xml." | |
exit 1 | |
fi | |
# Full path to the bookmark file | |
BOOKMARK_FILE="$BOOKMARKS_DIR/$PROJECT_ID.xml" | |
# Check if the bookmark file exists | |
if [ ! -f "$BOOKMARK_FILE" ]; then | |
echo "Error: Bookmark file not found in $BOOKMARKS_DIR." | |
exit 1 | |
fi | |
# Copy the bookmark file to the specified output path | |
cp "$BOOKMARK_FILE" "$OUTPUT_PATH" | |
# Output the extracted id and save location | |
echo "Successfully extracted project ID: $PROJECT_ID" | |
echo "Bookmark file has been saved to: $OUTPUT_PATH/$PROJECT_ID.xml" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment