Module: FilesHelper

Included in:
ProjectUtility
Defined in:
lib/joe_utils/helpers/files_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_to_open_zip(zip, file) ⇒ Object



62
63
64
# File 'lib/joe_utils/helpers/files_helper.rb', line 62

def add_to_open_zip(zip, file)
  zip.add(File.basename(file), file)
end

#catch_errno_enoentObject



110
111
112
113
114
# File 'lib/joe_utils/helpers/files_helper.rb', line 110

def catch_errno_enoent
  yield
rescue Errno::ENOENT => e
  # Nothing bad happened
end

#create_dir(location, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/joe_utils/helpers/files_helper.rb', line 29

def create_dir(location, options = {})
  if dir_exists?(location)
    if options[:overwrite]
      remove_dir(location)
    else
      puts "Directory #{location} already exists."
      return
    end
  end
  FileUtils.mkdir_p(location)
  puts "Directory #{location} created."
end

#create_file(path, text) ⇒ Object



81
82
83
84
# File 'lib/joe_utils/helpers/files_helper.rb', line 81

def create_file(path, text)
  File.open(path, 'w+') { |f| f.write(text) }
  puts "File #{path} created."
end

#create_file_by_template(template_path, path) {|text| ... } ⇒ Object

Yields:

  • (text)


71
72
73
74
75
# File 'lib/joe_utils/helpers/files_helper.rb', line 71

def create_file_by_template(template_path, path)
  text = load_file_text(template_path)
  yield text if block_given?
  create_file(path, text)
end

#dir_exists?(location) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/joe_utils/helpers/files_helper.rb', line 16

def dir_exists?(location)
  File.directory?(location) ? true : puts("Directory #{location} doesn't exist.")
end

#edit_file(address) ⇒ Object



66
67
68
69
# File 'lib/joe_utils/helpers/files_helper.rb', line 66

def edit_file(address)
  new_file_content = yield File.open(address, 'r') { |f| f.read }
  File.open(address, 'w+') { |f| f.write(new_file_content) }
end

#file_exists?(location) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/joe_utils/helpers/files_helper.rb', line 20

def file_exists?(location)
  File.exist?(location) ? true : puts("The file #{location} doesn't exist.")
end

#find_in_all(text, location) ⇒ Object

Puts all file addresses which contain the text

Parameters:

  • text (String)

    searched text

  • location (String)

    folder address searched

Returns:

  • nil



104
105
106
107
108
# File 'lib/joe_utils/helpers/files_helper.rb', line 104

def find_in_all(text, location)
  Dir[File.join(location, '*')].each do |address|
    File.open(address, 'r') { |f| puts "Found in file: #{f}" if f.read =~ /#{text}/ }
  end
end

#for_all_in_dir(location, options = {}) ⇒ Object



42
43
44
# File 'lib/joe_utils/helpers/files_helper.rb', line 42

def for_all_in_dir(location, options = {})
  Dir.glob(File.join(File.join(location, options[:recursive] && '**'), options[:matching] || '*')) { |f| yield f }
end

#get_current_addressObject



8
9
10
# File 'lib/joe_utils/helpers/files_helper.rb', line 8

def get_current_address
  Dir.pwd
end

#get_current_folderObject



12
13
14
# File 'lib/joe_utils/helpers/files_helper.rb', line 12

def get_current_folder
  File.basename(Dir.pwd)
end

#load_file_text(path) ⇒ Object



77
78
79
# File 'lib/joe_utils/helpers/files_helper.rb', line 77

def load_file_text(path)
  File.open(path, 'r') { |f| f.read } if file_exists?(path)
end

#remove_dir(location) ⇒ Object



24
25
26
27
# File 'lib/joe_utils/helpers/files_helper.rb', line 24

def remove_dir(location)
  FileUtils.rm_rf(location)
  puts "Directory #{location} removed."
end

#replace_in_all(text, folder_address) ⇒ Object

Replaces text in all files in the folder address

Parameters:

  • text (String)

    text to be replaced

  • folder_address (String)

    folder address to search in

Returns:

  • nil



90
91
92
93
94
95
96
97
98
# File 'lib/joe_utils/helpers/files_helper.rb', line 90

def replace_in_all(text, folder_address)
  Dir[File.join(folder_address, '*')].each do |address|
    content = File.open(address, 'r') { |f| f.read }
    if content.gsub!(text) { |match| yield match }
      File.open(address, 'w+') { |f| f.write(content) }
      puts "Replaced all in: #{address}."
    end
  end
end

#unzip(zip_file, location) ⇒ Object



46
47
48
49
# File 'lib/joe_utils/helpers/files_helper.rb', line 46

def unzip(zip_file, location)
  create_dir(location)
  Zip::File.open(zip_file) { |content| content.each { |f| content.extract(f, File.join(location, f.name)) } }
end

#zip(zip_file) ⇒ Object



51
52
53
# File 'lib/joe_utils/helpers/files_helper.rb', line 51

def zip(zip_file)
  Zip::File.open(zip_file, Zip::File::CREATE) { |zip| yield zip }
end

#zip_folder(zip_file, to_zip, options = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/joe_utils/helpers/files_helper.rb', line 55

def zip_folder(zip_file, to_zip, options = {})
  zip(zip_file) do |zip|
    for_all_in_dir(to_zip, matching: options[:matching], recursive: true) { |file| zip.add(File.basename(file), file) }
    yield zip if block_given?
  end
end