Class: Digipolitan::FileUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/digipolitan-apps-tools/file_utils.rb

Class Method Summary collapse

Class Method Details

.mkdir_p(path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/digipolitan-apps-tools/file_utils.rb', line 60

def self.mkdir_p(path)
  arr = path.split(File::SEPARATOR)
  count = arr.count
  i = 0
  f_path = nil
  while i < count
    f_name = arr[i]
    if f_path == nil && f_name.length != 0
      f_path = f_name
    else
      f_path = File.join(f_path != nil ? f_path : "", f_name)
    end
    if f_name.length != 0
      if !Dir.exist?(f_path)
        Dir.mkdir(f_path)
      end
    end
    i += 1
  end
end

.remove_dir(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/digipolitan-apps-tools/file_utils.rb', line 46

def self.remove_dir(path)
  if File.directory?(path)
      entries = Dir.entries(path)
      entries.each do |entry|
        if entry != "." && entry != ".."
          self.remove_dir(File.join(path, entry))
        end
      end
      Dir.delete(path)
  elsif File.exist?(path)
    File.delete(path)
  end
end

.rename_files(pattern, replacement, ignored_entries = nil, path = ".", recursive = true) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/digipolitan-apps-tools/file_utils.rb', line 5

def self.rename_files(pattern, replacement, ignored_entries = nil, path = ".", recursive = true)
  entries = Dir.entries(path)
  entries.each do |entry|
    replaced = entry
    replaced_path = File.join(path, entry)
    if replaced_path != __FILE__ && (ignored_entries == nil || ignored_entries.index(entry) == nil)
      if entry.include?(pattern)
        replaced = entry.gsub(pattern, replacement)
        replaced_path = File.join(path, replaced)
        File.rename(File.join(path, entry), replaced_path)
      end
      if recursive && File.directory?(replaced_path) && replaced != "." && replaced != ".."
        self.rename_files(pattern, replacement, ignored_entries, replaced_path, recursive)
      end
    end
  end
end

.replace_contents_of_files(pattern, replacement, ignored_entries = nil, path = ".", recursive = true) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/digipolitan-apps-tools/file_utils.rb', line 23

def self.replace_contents_of_files(pattern, replacement, ignored_entries = nil, path = ".", recursive = true)
  entries = Dir.entries(path)
  entries.each do |entry|
    file_path = File.join(path, entry)
    if file_path != __FILE__ && (ignored_entries == nil || ignored_entries.index(entry) == nil)
      if recursive && File.directory?(file_path) && entry != "." && entry != ".."
        self.replace_contents_of_files(pattern, replacement, ignored_entries, file_path, recursive)
      elsif File.file?(file_path)
        content = File.read(file_path)
        if content.include?(pattern)
          self.write_to_file(file_path, content.gsub(pattern, replacement))
        end
      end
    end
  end
end

.write_to_file(path, content = "") ⇒ Object



40
41
42
43
44
# File 'lib/digipolitan-apps-tools/file_utils.rb', line 40

def self.write_to_file(path, content = "")
  File.open(path, "w") { |file|
    file.puts(content)
  }
end