Class: MacSetup::SymlinkInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/mac_setup/symlink_installer.rb

Class Method Summary collapse

Class Method Details



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mac_setup/symlink_installer.rb', line 99

def self.install_symlinks(config)
  config.symlinks.each do |source_path, target_path|
    # source = Symlink.new(source_path: File.expand_path(source_path))
    # source.link
    source = Pathname.new(source_path)
    short_source_path = source.to_s
    # MacSetup.shorten_path(source_path)

    unless source.exist?
      MacSetup.log "#{short_source_path} doesn't exist. Skipping."
      next
    end

    target = Pathname.new(target_path)
    short_target_path = target.to_s
    source = source.expand_path
    target = target.expand_path
    # MacSetup.shorten_path(target_path)

    MacSetup.log "Linking #{short_sorce_path} to #{short_target_path}..."

    home = Pathname.new(ENV.fetch("HOME"))

    if target.directory?
      filename = target == home ? ".#{source.basename}" : source.basename
      full_target = target.join(filename)
      File.symlink(source, full_target)
    elsif target.to_s.end_with?("/")
      target.mkpath
      full_target = target.join(source.basename)
      File.symlink(source, full_target)
    else
      target.dirname.mkpath
      File.symlink(source, target)
    end
  end
end

.run(config, _status) ⇒ Object



95
96
97
# File 'lib/mac_setup/symlink_installer.rb', line 95

def self.run(config, _status)
  install_symlinks(config)
end