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
# 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).expand_path
    short_source_path = MacSetup.shorten_path(source.to_s)

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

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

    MacSetup.log "Linking #{short_source_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)
      link(source, full_target)
    elsif target.to_s.end_with?("/")
      target.mkpath
      full_target = target.join(source.basename)
      link(source, full_target)
    else
      target.dirname.mkpath
      link(source, target)
    end
  end
end


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mac_setup/symlink_installer.rb', line 133

def self.link(source, target)
  if File.exist?(target)
    if File.symlink?(target)
      existing_link = File.readlink(target)

      if existing_link == source.to_s
        MacSetup.log "Already linked. Skipping."
      else
        print "Replacing existing symlink at #{MacSetup.shorten_path(target)}. "
        puts "Originally linked to #{MacSetup.shorten_path(existing_link)}..."
        FileUtils.ln_sf(source, target)
      end
    else
      MacSetup.log "WARNING: File already exists at #{MacSetup.shorten_path(target)}. Skipping."
    end
  else
    FileUtils.ln_s(source, target)
  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