Module: RunLoop::DotDir

Defined in:
lib/run_loop/dot_dir.rb

Overview

A module for managing the ~/.run-loop directory.

Class Method Summary collapse

Class Method Details

.directoryObject


4
5
6
7
8
9
10
11
# File 'lib/run_loop/dot_dir.rb', line 4

def self.directory
  home = RunLoop::Environment.user_home_directory
  dir = File.join(home, ".run-loop")
  if !File.exist?(dir)
    FileUtils.mkdir_p(dir)
  end
  dir
end

.make_results_dirObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/run_loop/dot_dir.rb', line 13

def self.make_results_dir
  if RunLoop::Environment.xtc?
    next_results_dir = Dir.mktmpdir("run_loop")
  else
    results_dir = File.join(self.directory, 'results')
    next_results_dir = self.next_timestamped_dirname(results_dir)
    FileUtils.mkdir_p(next_results_dir)

    current = File.join(self.directory, "results", "current")
    FileUtils.rm_rf(current)
    FileUtils.ln_s(next_results_dir, current)
  end

  next_results_dir
end

.rotate_result_directoriesObject


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/run_loop/dot_dir.rb', line 29

def self.rotate_result_directories
  return :xtc if RunLoop::Environment.xtc?

  start = Time.now

  glob = "#{self.directory}/results/*"

  RunLoop.log_debug("Searching for run-loop results with glob: #{glob}")

  directories = Dir.glob(glob).select do |path|
    File.directory?(path) && !File.symlink?(path)
  end

  oldest_first = directories.sort_by { |f| File.mtime(f) }

  RunLoop.log_debug("Found #{oldest_first.count} previous run-loop results")
  oldest_first.pop(5)

  RunLoop.log_debug("Will delete #{oldest_first.count} previous run-loop results")

  oldest_first.each do |path|
    FileUtils.rm_rf(path)
  end

  elapsed = Time.now - start

  RunLoop.log_debug("Deleted #{oldest_first.count} previous results in #{elapsed} seconds")
rescue StandardError => e
  RunLoop.log_error("While rotating previous results, encountered: #{e}")
end