Module: Cline::Utils::File

Defined in:
lib/cline/utils/file.rb

Overview

Some file helpers

Class Method Summary collapse

Class Method Details

.safe_json_read(file, max_retries: 3) ⇒ Object

Try to read a file and parse its JSON content with retries. Uses safe_read internally, and also retries on JSON parse errors (e.g. if the file was half-written when read).

Parameters
  • file (String): Path to read
  • max_retries (Integer): Number of retries for both file access and JSON parsing [default: 3]
Result
  • Object: The parsed JSON content


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cline/utils/file.rb', line 41

def self.safe_json_read(file, max_retries: 3)
  retries = 0
  begin
    content = safe_read(file, max_retries: max_retries)
    JSON.parse(content)
  rescue JSON::ParserError
    retries += 1
    raise if retries > max_retries

    sleep(0.05 * retries)
    retry
  end
end

.safe_read(file, max_retries: 3) ⇒ Object

Try to read a file with retries in case other processes are using it.

Parameters
  • file (String): Path to read
  • max_retries (Integer): Number of retries in case of concurrent access [default: 3]
Result
  • String: The file content


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cline/utils/file.rb', line 15

def self.safe_read(file, max_retries: 3)
  retries = 0
  file_content = nil
  begin
    file_content = ::File.read(file)
  rescue Errno::EACCES, Errno::EAGAIN
    # Could be that the file is being written at the same time.
    # Just try again.
    retries += 1
    raise if retries > max_retries

    sleep(0.05 * retries)
    retry
  end
  file_content
end

.with_temp_dir {|temp_dir| ... } ⇒ Object

Provide a temporary directory. Will clean up the directory after code execution unless debug mode is on.

Yields:

  • (temp_dir)

    Block called with the temp directory ready

Yield Parameters:

  • temp_dir (String)

    The temp directory



60
61
62
63
64
65
66
67
68
# File 'lib/cline/utils/file.rb', line 60

def self.with_temp_dir(&)
  if Cline.config.debug
    temp_dir = "#{Cline.config.temp_dir_root}/#{Time.now.utc.strftime('%Y-%m-%d-%H-%M-%S-%N')}"
    FileUtils.mkdir_p temp_dir
    yield temp_dir
  else
    Dir.mktmpdir(&)
  end
end