class MacSetup::Symlink
Public Class Methods
new(options)
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 5 def initialize(options) @source_path = options[:source_path] @file_name = options[:name] @target_path = sanitize_target(options[:target_path]) end
Public Instance Methods
link()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 11 def link return if Secrets.encrypted?(source_path) short_source_path = MacSetup.shorten_path(source_path) short_target_path = MacSetup.shorten_path(target_path) MacSetup.log "Linking #{short_source_path} to #{short_target_path}..." return unless source_exists target_exists? ? replace : FileUtils.ln_s(source_path, target_path) end
Private Instance Methods
children()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 85 def children Dir.entries(source_path).reject { |entry| entry.start_with?(".") } end
file_name()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 46 def file_name @file_name ||= File.basename(source_path) end
link_children()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 60 def link_children MacSetup.log "Linking children..." children.each do |child| child_source = Symlink.new( source_path: File.join(source_path, child), target_path: File.join(target_path, child) ) child_source.link end end
replace()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 50 def replace if File.symlink?(target_path) replace_symlink elsif File.directory?(source_path) link_children else MacSetup.log "WARNING: File already exists at #{MacSetup.shorten_path(target_path)}. Skipping." end end
replace_symlink()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 73 def replace_symlink existing_link = File.readlink(target_path) if existing_link == source_path MacSetup.log "Already linked. Skipping." else print "Replacing existing symlink at #{MacSetup.shorten_path(target_path)}. " puts "Originally linked to #{MacSetup.shorten_path(existing_link)}..." FileUtils.ln_sf(source_path, target_path) end end
sanitize_target(file)
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 89 def sanitize_target(file) Secrets.strip_extension(file) end
source_exists()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 25 def source_exists File.exist?(source_path).tap do |exists| unless exists short_source_path = MacSetup.shorten_path(source_path) MacSetup.log "WARNING: Source doesn’t exist at #{short_source_path}. Skipping." end end end
source_path()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 38 def source_path @source_path ||= File.expand_path(file_name, DOTFILES_PATH) end
target_exists?()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 34 def target_exists? File.exist?(target_path) end
target_path()
click to toggle source
# File lib/mac_setup/symlink_installer.rb, line 42 def target_path @target_path ||= File.join(ENV["HOME"], ".#{sanitize_target(file_name)}") end