-
Notifications
You must be signed in to change notification settings - Fork 77
/
file_mover.rb
62 lines (50 loc) · 1.29 KB
/
file_mover.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# frozen_string_literal: true
module Jekyll
module Compose
class FileMover
attr_reader :force, :movement, :root
def initialize(movement, force = false, root = nil)
@movement = movement
@force = force
@root = root
end
def resource_type_from
"file"
end
def resource_type_to
"file"
end
def move
validate_source
validate_should_write!
ensure_directory_exists
move_file
end
def validate_source
raise ArgumentError, "There was no #{resource_type_from} found at '#{from}'." unless File.exist? from
end
def ensure_directory_exists
dir = File.dirname to
Dir.mkdir(dir) unless Dir.exist?(dir)
end
def validate_should_write!
raise ArgumentError, "A #{resource_type_to} already exists at #{to}" if File.exist?(to) && !force
end
def move_file
FileUtils.mv(from, to)
Jekyll.logger.info "#{resource_type_from.capitalize} #{from} was moved to #{to}"
end
private
def from
movement.from
end
def to
file_path(movement.to)
end
def file_path(path)
return path if root.nil? || root.empty?
File.join(root, path)
end
end
end
end