Class: JetBlack::FileHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/jet_black/file_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(working_directory) ⇒ FileHelper

Returns a new instance of FileHelper.



6
7
8
# File 'lib/jet_black/file_helper.rb', line 6

def initialize(working_directory)
  @working_directory = working_directory
end

Instance Method Details

#append_to_file(file_path, append_content) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jet_black/file_helper.rb', line 25

def append_to_file(file_path, append_content)
  resolved_file_path = resolve_working_path(file_path)

  unless File.exist?(resolved_file_path)
    raise JetBlack::NonExistentFileError.new(file_path, resolved_file_path)
  end

  File.open(resolved_file_path, "a") do |file|
    file.write(append_content)
  end
end

#copy_fixture(source_path, destination_path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jet_black/file_helper.rb', line 37

def copy_fixture(source_path, destination_path)
  source_fixture_dir = JetBlack.configuration.fixture_directory
  resolved_source_path = File.expand_path(source_path, source_fixture_dir)

  resolved_destination_path = resolve_working_path(destination_path)
  resolved_destination_dir = File.dirname(resolved_destination_path)

  if source_fixture_dir.nil?
    raise Error.new("Please configure the fixture_directory")
  end

  FileUtils.mkdir_p(resolved_destination_dir)
  FileUtils.cp(resolved_source_path, resolved_destination_path)
end

#create_executable(file_path, file_content) ⇒ Object



18
19
20
21
22
23
# File 'lib/jet_black/file_helper.rb', line 18

def create_executable(file_path, file_content)
  resolved_file_path = resolve_working_path(file_path)

  create_file(file_path, file_content)
  FileUtils.chmod("+x", resolved_file_path)
end

#create_file(file_path, file_content) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/jet_black/file_helper.rb', line 10

def create_file(file_path, file_content)
  resolved_file_path = resolve_working_path(file_path)
  resolved_dir = File.dirname(resolved_file_path)

  FileUtils.mkdir_p(resolved_dir)
  File.write(resolved_file_path, file_content)
end