Class: Hank::SymlinkManager

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/hank/symlink_manager.rb

Overview

Manages creation and removal of symlinks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hankfile) ⇒ SymlinkManager

Returns a new instance of SymlinkManager.



13
14
15
16
# File 'lib/hank/symlink_manager.rb', line 13

def initialize(hankfile)
  @hankfile = T.let(hankfile, Hankfile)
  @base_dir = T.let(Pathname.new(Dir.pwd), Pathname)
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



10
11
12
# File 'lib/hank/symlink_manager.rb', line 10

def base_dir
  @base_dir
end

Instance Method Details

#create_empty_target(source, target) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/hank/symlink_manager.rb', line 27

def create_empty_target(source, target)
  if source.directory?
    FileUtils.mkdir_p(target)
  else
    FileUtils.touch(target)
  end
end


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hank/symlink_manager.rb', line 36

def create_symlink(source_path, target_path, force: false)
  source = Pathname.new(source_path)
  target = @base_dir.join(target_path)

  # Ensure the source path directory exists
  FileUtils.mkdir_p(source.dirname) unless source.dirname.directory?

  # Ensure the target exists
  unless target.exist?
    if source.exist?
      if source.directory? || (!source.symlink? && source.file?)
        # Move the entire directory or file to the target location
        FileUtils.mv(source, target)
      else
        # Create empty file or directory as needed
        create_empty_target(source, target)
      end
    elsif source_path.end_with?('/')
      # Create empty file or directory based on the source path name
      # (If we can infer it's supposed to be a directory)
      FileUtils.mkdir_p(target)
    else
      FileUtils.touch(target)
    end
  end

  # Handle existing destination
  if source.exist?
    if source.symlink? && !force
      return false

      # Remove existing symlink if force is specified

      # Skip if symlink already exists and force is not specified

    end

    FileUtils.rm_rf(source)
  end

  # Create the symlink
  begin
    FileUtils.ln_s(target.to_s, source.to_s)
    puts "Created symlink: #{source} -> #{target}".green
    true
  rescue StandardError => e
    puts "⚠️ Failed to create symlink: #{source} -> #{target}: #{e.message}".red
    false
  end
end

#install_allObject



19
20
21
22
23
24
# File 'lib/hank/symlink_manager.rb', line 19

def install_all
  @hankfile.mappings.each do |source_path, target_path|
    create_symlink(source_path, target_path)
  end
  @hankfile.save
end

#remove_mapping(source_path) ⇒ Object



88
89
90
91
92
# File 'lib/hank/symlink_manager.rb', line 88

def remove_mapping(source_path)
  @hankfile.remove_mapping(source_path)
  @hankfile.save
  puts "Removed #{source_path} from Hankfile".green
end