Module: Homesick::Actions::FileActions

Included in:
CLI
Defined in:
lib/homesick/actions/file_actions.rb

Overview

File-related helper methods for Homesick

Instance Method Summary collapse

Instance Method Details



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/homesick/actions/file_actions.rb', line 62

def handle_symlink_action(action, source, destination)
  case action
  when :identical
    say_status :identical, destination.expand_path, :blue
  when :symlink_conflict, :conflict
    if action == :conflict
      say_status :conflict, "#{destination} exists", :red
    else
      say_status :conflict,
                "#{destination} exists and points to #{destination.readlink}",
                :red
    end
    if collision_accepted?(destination, source)
      FileUtils.rm_r destination, force: true unless options[:pretend]
      FileUtils.ln_s source, destination, force: true unless options[:pretend]
    end
  else
    say_status :symlink,
               "#{source.expand_path} to #{destination.expand_path}",
               :green
    FileUtils.ln_s source, destination unless options[:pretend]
  end
end

#ln_s(source, destination) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/homesick/actions/file_actions.rb', line 44

def ln_s(source, destination)
  source = Pathname.new(source).realpath        
  destination = Pathname.new(destination)
  FileUtils.mkdir_p destination.dirname

  action = if destination.symlink? && destination.readlink == source
             :identical
           elsif destination.symlink?
             :symlink_conflict
           elsif destination.exist?
             :conflict
           else
             :success
           end

  handle_symlink_action action, source, destination
end

#mv(source, destination) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/homesick/actions/file_actions.rb', line 6

def mv(source, destination)
  source = Pathname.new(source)
  destination = Pathname.new(destination + source.basename)
  case
  when destination.exist? && (options[:force] || shell.file_collision(destination) { source })
    say_status :conflict, "#{destination} exists", :red
    FileUtils.mv source, destination unless options[:pretend]
  else
    FileUtils.mv source, destination unless options[:pretend]
  end
end

#rm(file) ⇒ Object



34
35
36
37
# File 'lib/homesick/actions/file_actions.rb', line 34

def rm(file)
  say_status "rm #{file}", '', :green
  FileUtils.rm file, force: true
end


23
24
25
26
27
28
29
30
31
32
# File 'lib/homesick/actions/file_actions.rb', line 23

def rm_link(target)
  target = Pathname.new(target)

  if target.symlink?
    say_status :unlink, "#{target.expand_path}", :green
    FileUtils.rm_rf target
  else
    say_status :conflict, "#{target} is not a symlink", :red
  end
end

#rm_r(dir) ⇒ Object



39
40
41
42
# File 'lib/homesick/actions/file_actions.rb', line 39

def rm_r(dir)
  say_status "rm -r #{dir}", '', :green
  FileUtils.rm_r dir
end

#rm_rf(dir) ⇒ Object



18
19
20
21
# File 'lib/homesick/actions/file_actions.rb', line 18

def rm_rf(dir)
  say_status "rm -rf #{dir}", '', :green
  FileUtils.rm_r dir, force: true
end