Module: Filander::Base

Included in:
Chown, Cmd, CopyDirectory, CopyFile, CreateFile, EmptyDirectory, InjectIntoFile, Inside, Template
Defined in:
lib/filander/actions/base.rb

Instance Method Summary collapse

Instance Method Details

#create_directory_for(destination = nil) ⇒ Object



15
16
17
18
19
20
# File 'lib/filander/actions/base.rb', line 15

def create_directory_for(destination = nil)
  dest_dir = File.dirname(join_destination(destination))

  return if Filander.behavior == :pretend
  FileUtils.mkdir_p dest_dir unless File.exists?(dest_dir)
end

#entries(dirname) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/filander/actions/base.rb', line 103

def entries(dirname)
  basename = Pathname.new(dirname)
  array = Dir[File.join(dirname, '**')]
  array.map! { |f| Pathname.new f }
  array.map! { |p| p.relative_path_from basename }
  array
end

#join_destination(filename = nil) ⇒ Object



9
10
11
12
13
# File 'lib/filander/actions/base.rb', line 9

def join_destination(filename = nil)
  join Filander.destination_root, filename
rescue
  raise 'Please specify destination_root'
end

#join_source(filename = nil) ⇒ Object



3
4
5
6
7
# File 'lib/filander/actions/base.rb', line 3

def join_source(filename = nil)
  join Filander.source_root, filename
rescue
  raise 'Please specify source_root'
end

#report(verb, filename) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/filander/actions/base.rb', line 68

def report(verb, filename)
  return if Filander.quiet

  # TODO: make this nice
  if Filander.destination_root
    begin
      name = Pathname.new(File.expand_path(join_destination(filename))).relative_path_from(Pathname.new(File.expand_path(Filander.destination_root_stack.first)))
    rescue
      name = filename
    end
  else
    name = filename
  end

  color = case verb
          when :create, :clone, :enable
            :green
          when :force, :skip
            :yellow
          when :conflict
            :red
          when :execute
            :magenta
          when :install, :installed
            :cyan
          else
            :blue
          end

  verb = verb.to_s.rjust(12)
  verb = $terminal.color(verb, :bold, color) if defined? $terminal

  puts '%s  %s' % [verb, name]
end

#with_report(destination, content = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/filander/actions/base.rb', line 22

def with_report(destination, content = nil)
  filename = join_destination(destination)

  skip = Filander.behavior == :skip
  if File.exists?(filename)
    if File.file?(filename) && content == File.read(filename)
      report :identical, destination
      skip = true
    elsif File.directory?(filename) && content == entries(filename)
      report :identical, destination
      skip = true
    else
      case Filander.behavior
      when :force, :pretend
        report :force, destination
      when :skip
        report :skip, destination
      else
        file = $terminal.color(filename, :bold) if defined? $terminal
        skip = nil
        puts 'Overwrite %s [Nyc]?' % file
        until !skip.nil?
          answer = STDIN.gets.chomp.downcase
          case answer
          when '', 'n', 'no'
            skip = true
          when 'y', 'yes'
            skip = false
          when 'c', 'cancel'
            exit
          else
            puts 'Please anwer either [Y]es, [N]o or [C]ancel'
            skip = nil
          end
        end
        report skip ? :skip : :force, destination
      end
    end
  else
    report :create, destination
  end

  yield unless skip || Filander.behavior == :pretend
  report nil, content.gsub(/(\n+)/, '\1' + "              ").strip if Filander.verbose
end