Class: Kompo::WorkDir

Inherits:
Taski::Task
  • Object
show all
Defined in:
lib/kompo/tasks/work_dir.rb

Overview

Create a temporary working directory and change into it

Constant Summary collapse

MARKER_FILE =

Marker file to identify Kompo-created work directories

".kompo_work_dir_marker"

Instance Method Summary collapse

Instance Method Details

#cleanObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kompo/tasks/work_dir.rb', line 70

def clean
  return unless @path && Dir.exist?(@path)

  # Only remove if marker file exists (confirms this is a Kompo work directory)
  marker_path = File.join(@path, MARKER_FILE)
  unless File.exist?(marker_path)
    puts "Skipping cleanup: #{@path} is not a Kompo work directory"
    return
  end

  FileUtils.rm_rf(@path)
  puts "Cleaned up working directory: #{@path}"
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
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
59
60
61
62
63
64
65
66
67
68
# File 'lib/kompo/tasks/work_dir.rb', line 16

def run
  @original_dir = Dir.pwd

  # Check if Ruby cache exists and use its work_dir path for $LOAD_PATH compatibility
  ruby_version = Taski.args.fetch(:ruby_version, RUBY_VERSION)
  kompo_cache = Taski.args.fetch(:kompo_cache, File.expand_path("~/.kompo/cache"))
   = File.join(kompo_cache, ruby_version, "metadata.json")

  if File.exist?()
    begin
       = JSON.parse(File.read())
      cached_work_dir = ["work_dir"]

      if cached_work_dir
        # Check if the directory exists and belongs to us (has marker) or doesn't exist at all
        # In CI environments, the temp directory is cleaned between runs, so we recreate it
        if Dir.exist?(cached_work_dir)
          marker_path = File.join(cached_work_dir, MARKER_FILE)
          if File.exist?(marker_path)
            # Directory exists and has our marker - reuse it
            @path = cached_work_dir
            puts "Using cached work directory: #{@path}"
            return
          else
            # Directory exists but wasn't created by Kompo - don't use it
            warn "warn: #{cached_work_dir} exists but is not a Kompo work directory, creating new one"
          end
        else
          # Directory doesn't exist - recreate it (common in CI after cache restore)
          FileUtils.mkdir_p(cached_work_dir)
          File.write(File.join(cached_work_dir, MARKER_FILE), "kompo-work-dir")
          @path = cached_work_dir
          puts "Recreated cached work directory: #{@path}"
          return
        end
      end
    rescue JSON::ParserError
      # Fall through to create new work_dir
    end
  end

  # No valid cache, create new work_dir
  tmpdir = Dir.mktmpdir(SecureRandom.uuid)
  # Resolve symlinks to get the real path
  # On macOS, /var/folders is a symlink to /private/var/folders
  # If we don't resolve this, paths won't match at runtime
  @path = File.realpath(tmpdir)

  # Create marker file to identify this as a Kompo work directory
  File.write(File.join(@path, MARKER_FILE), "kompo-work-dir")

  puts "Working directory: #{@path}"
end