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.



8
9
10
# File 'lib/jet_black/file_helper.rb', line 8

def initialize(working_directory)
  @working_directory = working_directory
end

Instance Method Details

#append_to_file(file_path, append_content) ⇒ Object



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

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



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

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



20
21
22
23
24
25
# File 'lib/jet_black/file_helper.rb', line 20

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



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

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