Module: Spoom::Context::FileSystem

Extended by:
T::Helpers, T::Sig
Included in:
Spoom::Context
Defined in:
lib/spoom/context/file_system.rb

Overview

File System features for a context

Instance Method Summary collapse

Instance Method Details

#absolute_path_to(relative_path) ⇒ Object



15
16
17
# File 'lib/spoom/context/file_system.rb', line 15

def absolute_path_to(relative_path)
  File.join(absolute_path, relative_path)
end

#collect_files(allow_extensions: [], allow_mime_types: [], exclude_patterns: []) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/spoom/context/file_system.rb', line 53

def collect_files(allow_extensions: [], allow_mime_types: [], exclude_patterns: [])
  collector = FileCollector.new(
    allow_extensions: allow_extensions,
    allow_mime_types: allow_mime_types,
    exclude_patterns: exclude_patterns,
  )
  collector.visit_path(absolute_path)
  collector.files.map { |file| file.delete_prefix("#{absolute_path}/") }
end

#destroy!Object



105
106
107
# File 'lib/spoom/context/file_system.rb', line 105

def destroy!
  FileUtils.rm_rf(absolute_path)
end

#exist?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/spoom/context/file_system.rb', line 21

def exist?
  File.directory?(absolute_path)
end

#file?(relative_path) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/spoom/context/file_system.rb', line 65

def file?(relative_path)
  File.file?(absolute_path_to(relative_path))
end

#glob(pattern = "**/*") ⇒ Object



34
35
36
37
38
# File 'lib/spoom/context/file_system.rb', line 34

def glob(pattern = "**/*")
  Dir.glob(absolute_path_to(pattern)).map do |path|
    Pathname.new(path).relative_path_from(absolute_path).to_s
  end.sort
end

#listObject



42
43
44
# File 'lib/spoom/context/file_system.rb', line 42

def list
  glob("*")
end

#mkdir!Object



27
28
29
30
# File 'lib/spoom/context/file_system.rb', line 27

def mkdir!
  FileUtils.rm_rf(absolute_path)
  FileUtils.mkdir_p(absolute_path)
end

#move!(from_relative_path, to_relative_path) ⇒ Object



95
96
97
98
99
# File 'lib/spoom/context/file_system.rb', line 95

def move!(from_relative_path, to_relative_path)
  destination_path = absolute_path_to(to_relative_path)
  FileUtils.mkdir_p(File.dirname(destination_path))
  FileUtils.mv(absolute_path_to(from_relative_path), destination_path)
end

#read(relative_path) ⇒ Object



73
74
75
# File 'lib/spoom/context/file_system.rb', line 73

def read(relative_path)
  File.read(absolute_path_to(relative_path))
end

#remove!(relative_path) ⇒ Object



89
90
91
# File 'lib/spoom/context/file_system.rb', line 89

def remove!(relative_path)
  FileUtils.rm_rf(absolute_path_to(relative_path))
end

#write!(relative_path, contents = "", append: false) ⇒ Object



81
82
83
84
85
# File 'lib/spoom/context/file_system.rb', line 81

def write!(relative_path, contents = "", append: false)
  absolute_path = absolute_path_to(relative_path)
  FileUtils.mkdir_p(File.dirname(absolute_path))
  File.write(absolute_path, contents, mode: append ? "a" : "w")
end