Class: TRuby::IncrementalCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/cache.rb

Overview

Incremental compilation support

Direct Known Subclasses

EnhancedIncrementalCompiler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compiler, cache: nil) ⇒ IncrementalCompiler

Returns a new instance of IncrementalCompiler.



308
309
310
311
312
313
314
# File 'lib/t_ruby/cache.rb', line 308

def initialize(compiler, cache: nil)
  @compiler = compiler
  @cache = cache || ParseCache.new
  @file_hashes = {}
  @dependencies = {}
  @compiled_files = {}
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



306
307
308
# File 'lib/t_ruby/cache.rb', line 306

def dependencies
  @dependencies
end

#file_hashesObject (readonly)

Returns the value of attribute file_hashes.



306
307
308
# File 'lib/t_ruby/cache.rb', line 306

def file_hashes
  @file_hashes
end

Instance Method Details

#add_dependency(file_path, depends_on) ⇒ Object

Register dependency between files



354
355
356
357
# File 'lib/t_ruby/cache.rb', line 354

def add_dependency(file_path, depends_on)
  @dependencies[file_path] ||= []
  @dependencies[file_path] << depends_on unless @dependencies[file_path].include?(depends_on)
end

#clearObject

Clear compilation cache



360
361
362
363
364
365
# File 'lib/t_ruby/cache.rb', line 360

def clear
  @file_hashes.clear
  @dependencies.clear
  @compiled_files.clear
  @cache.stats # Just accessing for potential cleanup
end

#compile(file_path) ⇒ Object

Compile file with caching



331
332
333
334
335
336
337
338
339
# File 'lib/t_ruby/cache.rb', line 331

def compile(file_path)
  return @compiled_files[file_path] unless needs_compile?(file_path)

  result = @compiler.compile(file_path)
  @file_hashes[file_path] = file_hash(file_path)
  @compiled_files[file_path] = result

  result
end

#compile_all(file_paths) ⇒ Object

Compile multiple files, skipping unchanged



342
343
344
345
346
347
348
349
350
351
# File 'lib/t_ruby/cache.rb', line 342

def compile_all(file_paths)
  results = {}
  to_compile = file_paths.select { |f| needs_compile?(f) }

  to_compile.each do |file_path|
    results[file_path] = compile(file_path)
  end

  results
end

#needs_compile?(file_path) ⇒ Boolean

Check if file needs recompilation

Returns:

  • (Boolean)


317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/t_ruby/cache.rb', line 317

def needs_compile?(file_path)
  return true unless File.exist?(file_path)

  current_hash = file_hash(file_path)
  stored_hash = @file_hashes[file_path]

  return true if stored_hash.nil? || stored_hash != current_hash

  # Check dependencies
  deps = @dependencies[file_path] || []
  deps.any? { |dep| needs_compile?(dep) }
end