Class: Bookbinder::LocalFilesystemAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/bookbinder/local_filesystem_accessor.rb

Instance Method Summary collapse

Instance Method Details

#copy(src, dest) ⇒ Object



44
45
46
47
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 44

def copy(src, dest)
  make_directory(dest)
  FileUtils.cp_r src, dest
end

#copy_and_rename(src, dest) ⇒ Object



49
50
51
52
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 49

def copy_and_rename(src, dest)
  make_directory(Pathname(dest).dirname)
  FileUtils.cp_r src, dest
end

#copy_contents(src, dest) ⇒ Object



54
55
56
57
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 54

def copy_contents(src, dest)
  raise Errors::ProgrammerMistake.new("The method copy_contents cannot copy the contents of the directory '#{src}' because it was not found.") unless Dir.exists?(src)
  copy "#{src}/.", dest
end

#copy_including_intermediate_dirs(file, root, dest) ⇒ Object



59
60
61
62
63
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 59

def copy_including_intermediate_dirs(file, root, dest)
  path_within_destination = relative_path_from(root, file)
  extended_dest = File.dirname(File.join dest, path_within_destination)
  copy file, extended_dest
end

#empty_directory(path) ⇒ Object



32
33
34
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 32

def empty_directory(path)
  FileUtils.rm_rf(File.join(path, '.'))
end

#file_exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 9

def file_exist?(path)
  File.exist?(path)
end

#find_files_extension_agnostically(pattern, directory = '.') ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 96

def find_files_extension_agnostically(pattern, directory='.')
  extensionless_pattern = pattern.to_s.split('.').first

  `find -L #{directory} -path '*/#{extensionless_pattern}.*'`.
    lines.
    map(&:chomp).
    map(&Pathname.method(:new))
end

#find_files_recursively(from) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 87

def find_files_recursively(from)
  `find -L #{from} -type f`.
    lines.
    map(&:chomp).
    map(&Pathname.method(:new)).
    reject {|p| p.to_s.match %r{/\.}}.
    reject(&:directory?)
end

#find_files_with_ext(ext, path) ⇒ Object



75
76
77
78
79
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 75

def find_files_with_ext(ext, path)
  all_files = find_files_recursively(path)
  matching_files = all_files.select {|p| p.to_s.match(/\.#{ext}/) }
  matching_files.map(&:to_s)
end


65
66
67
68
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 65

def link_creating_intermediate_dirs(src, dst)
  FileUtils.mkdir_p(File.dirname(dst))
  File.symlink(src, dst)
end

#make_directory(path) ⇒ Object



40
41
42
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 40

def make_directory(path)
  FileUtils.mkdir_p(path)
end

#overwrite(to: nil, text: nil) ⇒ Object



23
24
25
26
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 23

def overwrite(to: nil, text: nil)
  File.delete(to) if file_exist?(to)
  write(to: to, text: text)
end

#read(path) ⇒ Object



28
29
30
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 28

def read(path)
  File.read(path)
end

#relative_path_from(src, target) ⇒ Object



81
82
83
84
85
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 81

def relative_path_from(src, target)
  target_path = Pathname(File.absolute_path target)
  relative_path = target_path.relative_path_from(Pathname(File.absolute_path src))
  relative_path.to_s
end

#remove_directory(path) ⇒ Object



36
37
38
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 36

def remove_directory(path)
  FileUtils.rm_rf(path)
end

#rename_file(path, new_name) ⇒ Object



70
71
72
73
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 70

def rename_file(path, new_name)
  new_path = File.expand_path File.join path, '..', new_name
  File.rename(path, new_path)
end

#source_file_exists?(directory, path_to_file) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 105

def source_file_exists?(directory, path_to_file)
  path = Pathname(path_to_file.split('/').last)
  source_file_found = false

  Pathname(directory).ascend do |dir|
    source_file_found = true if dir.entries.any? { |entry| entry == path }
  end
  source_file_found
end

#write(to: nil, text: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/bookbinder/local_filesystem_accessor.rb', line 13

def write(to: nil, text: nil)
  make_directory(File.dirname to)

  File.open(to, 'a') do |f|
    f.write(text)
  end

  to
end