feat: Create new dotfiles format

This commit is contained in:
Nathan Woodburn 2025-03-28 16:49:18 +11:00
parent 645b49d313
commit 310d032a46
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -1,85 +1,52 @@
#!/bin/bash #!/bin/bash
cd ~/.dotfiles # Check if ~/.dotfiles exists
if [ ! -d "$HOME/.dotfiles" ]; then
echo "Error: ~/.dotfiles directory does not exist."
echo "Please clone your dotfiles repository to ~/.dotfiles first."
exit 1
fi
cd ~/.dotfiles || exit 1
git pull git pull
echo "Creating symbolic links for dotfiles..." echo "Creating symbolic links for dotfiles..."
# Convert paths to array of directory names to special handle ignore_files=("dotfiles.sh" "README.md" ".git" ".gitignore")
dirs_to_sym=(".config/alacritty/themes" ".zsh_functions") backup_dir="$HOME/.dotfiles_backup/$(date +%Y%m%d_%H%M%S)"
sub_modules=(".config/alacritty/themes")
ignore_files=("README.md" ".git" ".gitmodules")
# Function to create symlinks recursively # For each file/dir in the dotfiles directory
create_symlinks() { for file in $(ls -A); do
local src_dir="$1" # If the file is not in the ignore list
local target_dir="$2" if [[ ! " ${ignore_files[@]} " =~ " ${file} " ]]; then
local is_root="$3" # Flag to indicate if this is the root call target="$HOME/$file"
source="$PWD/$file"
# Create the target directory if it doesn't exist # Skip if it's already a correct symlink
mkdir -p "$target_dir" if [ -L "$target" ] && [ "$(readlink "$target")" = "$source" ]; then
echo "✓ Symlink for $file already exists and is correct."
# Process all files, including hidden ones
local files=("$src_dir"/* "$src_dir"/.[!.]*)
for file in "${files[@]}"; do
# Skip if the file doesn't exist (can happen if no hidden files)
[ -e "$file" ] || continue
# Get the file name
local file_name=$(basename "$file")
local rel_path=${file#$HOME/.dotfiles/} # Get relative path
# Check if file should be ignored
if [[ " ${ignore_files[@]} " =~ " $file_name " ]]; then
echo "Ignoring: $file_name"
continue continue
fi fi
# If the file is a directory # Backup existing file/directory if it exists
if [ -d "$file" ]; then if [ -e "$target" ]; then
# Handle directory symlinking based on whether it's in the special directories list mkdir -p "$backup_dir"
if [[ "$is_root" == "true" && "$file_name" == ".config" ]]; then echo "⚠ Backing up existing $file to $backup_dir/"
# For .config directory, we want to create it and process its contents differently mv "$target" "$backup_dir/$file"
mkdir -p "$target_dir/.config"
create_symlinks "$file" "$target_dir/.config" "false"
elif [[ " ${dirs_to_sym[@]} " =~ " $rel_path " ]]; then
# Special directory that needs special handling
echo "Special directory: $rel_path"
mkdir -p "$target_dir/$file_name"
# Handle submodule initialization if needed
if [[ " ${sub_modules[@]} " =~ " $rel_path " ]]; then
echo "Initializing submodule: $rel_path"
(cd "$file" && git submodule update --init --recursive)
fi fi
# Process the contents # Create a symbolic link
create_symlinks "$file" "$target_dir/$file_name" "false" if [ -d "$source" ]; then
echo "Creating symbolic link for directory: $file"
else else
# For regular directories in our dotfiles, we want to symlink them directly echo "Creating symbolic link for file: $file"
if [ ! -L "$target_dir/$file_name" ]; then
if [ -e "$target_dir/$file_name" ]; then
echo "Removing existing: $target_dir/$file_name"
rm -rf "$target_dir/$file_name"
fi
echo "Linking directory: $file -> $target_dir/$file_name"
ln -s "$file" "$target_dir/$file_name"
fi
fi fi
if ln -s "$source" "$target"; then
echo "✓ Successfully linked $file"
else else
# For regular files, always create symlinks echo "✗ Failed to create symlink for $file"
if [ ! -L "$target_dir/$file_name" ]; then
if [ -e "$target_dir/$file_name" ]; then
echo "Removing existing: $target_dir/$file_name"
rm -rf "$target_dir/$file_name"
fi
echo "Linking file: $file -> $target_dir/$file_name"
ln -s "$file" "$target_dir/$file_name"
fi fi
fi fi
done done
}
# Call the function to create symlinks from dotfiles to home echo "Dotfiles setup complete."
create_symlinks "$HOME/.dotfiles" "$HOME" "true"
echo "Dotfiles setup complete!"