Class: Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/dir.rb

Class Method Summary collapse

Class Method Details

.get_data_dirObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/raykit/dir.rb', line 39

def self.get_data_dir
  unless ENV["DATA_DIR"].nil?
    data_dir = ENV["DATA_DIR"]
    return data_dir if Dir.exist?(data_dir)
  end
  home_dir = Raykit::Environment.home_dir
  return home_dir if Dir.exist?(home_dir)

  Dir.tmpdir
end

.get_git_directories(dir) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/raykit/dir.rb', line 15

def self.get_git_directories(dir)
  git_dirs = []
  Dir.chdir(dir) do
    Dir.glob("**/.git/index") do |f|
      git_dir = File.dirname(File.dirname(f))
      git_dirs << git_dir
    end
  end
  git_dirs
end

.get_git_urls(dir) ⇒ Object



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

def self.get_git_urls(dir)
  urls = Set.new
  Dir.chdir(dir) do
    get_git_directories(dir).each do |git_dir|
      Dir.chdir(git_dir) do
        url = `git config --get remote.origin.url`.strip
        urls.add(url)
      end
    end
  end
  urls
end

.get_text(dir, relative_name) ⇒ Object



7
8
9
# File 'lib/raykit/dir.rb', line 7

def self.get_text(dir, relative_name)
  File.read("#{dir}/#{relative_name}")
end

.remove_empty_directories(dir) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/raykit/dir.rb', line 52

def self.remove_empty_directories(dir)
  # List all entries in the directory except for '.' and '..'
  Dir.entries(dir).each do |entry|
    next if entry == "." || entry == ".."  # Skip the current and parent directory entries

    path = File.join(dir, entry)  # Construct the full path

    if File.directory?(path)
      remove_empty_directories(path)  # Recursively call the method if the entry is a directory
      # Remove the directory if it's empty after processing its contents
      Dir.rmdir(path) if Dir.empty?(path)
    end
  end
rescue Errno::ENOENT
  # Handle the case where the directory doesn't exist or is removed before rmdir is called
  puts "Directory not found: #{dir}"
end

.set_text(dir, relative_name, text) ⇒ Object



11
12
13
# File 'lib/raykit/dir.rb', line 11

def self.set_text(dir, relative_name, text)
  File.open("#{dir}/#{relative_name}", "w") { |f| f.write text }
end