symlink fixed !

This commit is contained in:
kumar vaibhav 2025-12-19 20:13:03 +05:30
parent 8128e92311
commit 359c250101
2 changed files with 61 additions and 29 deletions

68
forge
View file

@ -70,9 +70,11 @@ last_switch = null
# Format: "target_path" = "source_path"
# target_path: where the symlink should be created (relative to home directory)
# source_path: where the actual file is stored (relative to forge directory)
# Note: source_path automatically prefixed with "dotfiles/" if not present
# Examples:
# ".bashrc" = "dotfiles/.bashrc"
# ".config/vimrc" = "dotfiles/vimrc"
# ".bashrc" = ".bashrc"
# ".config/vimrc" = "vimrc"
# ".config/alacritty/alacritty.yml" = "alacritty.yml"
[options]
# Additional options
@ -218,6 +220,7 @@ deploy_dotfiles() {
log_info "Deploying dotfiles..."
local dotfiles_deployed=false
local dotfiles_dir="$FORGE_DIR/dotfiles"
while IFS='=' read -r entry; do
local section="${entry%%:*}"
@ -228,34 +231,61 @@ deploy_dotfiles() {
if [[ "$section" == "dotfiles" ]]; then
dotfiles_deployed=true
# Normalize source path - if it doesn't start with dotfiles/, add it
if [[ ! "$source" =~ ^dotfiles/ ]]; then
source="dotfiles/$source"
fi
local target_path="$HOME/$target"
local source_path="$FORGE_DIR/$source"
# Create source directory if it doesn't exist
local source_dir=$(dirname "$source_path")
mkdir -p "$source_dir"
# Verify source file exists
if [[ ! -f "$source_path" && ! -d "$source_path" ]]; then
log_error "Source file not found: $source_path"
continue
fi
# Create target directory if it doesn't exist
local target_dir=$(dirname "$target_path")
mkdir -p "$target_dir"
# Backup existing file if it exists and is not a symlink
if [[ -f "$target_path" && ! -L "$target_path" ]]; then
local backup_dir="$FORGE_DIR/backup"
mkdir -p "$backup_dir"
local backup_path="$backup_dir/$(basename "$target").$(date +%Y%m%d_%H%M%S).bak"
cp "$target_path" "$backup_path"
log_info "Backed up $target_path to $backup_path"
# Handle existing file/symlink
if [[ -e "$target_path" || -L "$target_path" ]]; then
# Check if it's already a correct symlink
if [[ -L "$target_path" ]]; then
local current_target=$(readlink "$target_path")
if [[ "$current_target" == "$source_path" ]]; then
log_info "Symlink already correct: $target_path"
continue
fi
fi
# Backup existing file if it exists and is not a symlink
if [[ -e "$target_path" && ! -L "$target_path" ]]; then
local backup_dir="$FORGE_DIR/backup"
mkdir -p "$backup_dir"
local backup_path="$backup_dir/$(basename "$target").$(date +%Y%m%d_%H%M%S).bak"
cp -r "$target_path" "$backup_path"
log_info "Backed up $target_path to $backup_path"
fi
# Remove existing file/symlink
rm -rf "$target_path"
fi
# Remove existing file/symlink
rm -f "$target_path"
# Create symlink
if ln -s "$source_path" "$target_path"; then
log_success "Created symlink: $target_path -> $source_path"
# Create relative symlink for better portability
local relative_source
if [[ -d "$source_path" ]]; then
relative_source=$(realpath --relative-to="$target_dir" "$source_path")
else
log_error "Failed to create symlink: $target_path -> $source_path"
relative_source=$(realpath --relative-to="$target_dir" "$source_path")
fi
# Create symlink using relative path
if ln -s "$relative_source" "$target_path"; then
log_success "Created symlink: $target_path -> $relative_source"
else
log_error "Failed to create symlink: $target_path -> $relative_source"
fi
fi
done < <(parse_toml)