Class: Daedalus::SourceFile

Inherits:
Path
  • Object
show all
Defined in:
lib/daedalus.rb

Direct Known Subclasses

InstructionSourceFile

Instance Attribute Summary

Attributes inherited from Path

#data, #path

Instance Method Summary collapse

Methods inherited from Path

#artifacts_path, #basename, #data_path, #save!

Constructor Details

#initialize(path) ⇒ SourceFile

Returns a new instance of SourceFile.



337
338
339
340
341
# File 'lib/daedalus.rb', line 337

def initialize(path)
  super
  @static_deps = []
  @autogen_builder = nil
end

Instance Method Details

#autogenerate(&builder) ⇒ Object



347
348
349
# File 'lib/daedalus.rb', line 347

def autogenerate(&builder)
  @autogen_builder = builder
end

#build(ctx) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/daedalus.rb', line 424

def build(ctx)
  ctx.log.inc!

  if @autogen_builder
    ctx.log.show "GN", @path
    @autogen_builder.call(ctx.log)
  end

  @data[:sha1] = sha1(ctx)
  ctx.compile path, object_path
  save!
end

#cleanObject



437
438
439
440
441
442
# File 'lib/daedalus.rb', line 437

def clean
  File.unlink object_path if File.exist?(object_path)
  File.unlink data_path if File.exist?(data_path)

  Dir.rmdir artifacts_path if Dir.entries(artifacts_path).empty?
end

#consider(ctx, tasks) ⇒ Object



420
421
422
# File 'lib/daedalus.rb', line 420

def consider(ctx, tasks)
  tasks << self if out_of_date?(ctx)
end

#dependencies(ctx) ⇒ Object



355
356
357
358
359
360
361
362
363
# File 'lib/daedalus.rb', line 355

def dependencies(ctx)
  deps = @data[:deps]

  if ctx.sha1(@path) != @data[:dep_sha1] or !deps
    deps = recalc_depedencies(ctx)
  end

  return deps + @static_deps
end

#depends_on(static_deps) ⇒ Object



343
344
345
# File 'lib/daedalus.rb', line 343

def depends_on(static_deps)
  @static_deps = static_deps
end

#describe(ctx) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/daedalus.rb', line 444

def describe(ctx)
  if !File.exist?(object_path)
    puts "#{@path}: unbuilt"
  else
    if @data[:sha1] != sha1(ctx)
      puts "#{@path}: out-of-date"
    end

    deps = newer_dependencies(ctx)

    unless deps.empty?
      puts "#{@path}: dependencies changed"
      deps.each do |d|
        puts "  - #{d}"
      end
    end
  end
end

#info(ctx) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
# File 'lib/daedalus.rb', line 463

def info(ctx)
  puts @path
  puts "  object: #{object_path}"
  puts "  last hash: #{@data[:sha1]}"
  puts "  curr hash: #{sha1(ctx)}"

  puts "  dependencies:"
  dependencies(ctx).each do |x|
    puts "    #{x}"
  end
end

#newer_dependencies(ctx) ⇒ Object



400
401
402
403
404
# File 'lib/daedalus.rb', line 400

def newer_dependencies(ctx)
  dependencies(ctx).find_all do |x|
    ctx.mtime(x) > ctx.mtime(object_path)
  end
end

#object_pathObject



351
352
353
# File 'lib/daedalus.rb', line 351

def object_path
  File.join artifacts_path, "#{basename}.o"
end

#out_of_date?(ctx) ⇒ Boolean

Returns:

  • (Boolean)


406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/daedalus.rb', line 406

def out_of_date?(ctx)
  unless File.exist?(@path)
    return true if @autogen_builder
    raise Errno::ENOENT, "Missing #{@path}"
  end

  return true unless File.exist?(object_path)

  return true if ctx.mtime_only and ctx.mtime(@path) > ctx.mtime(object_path)

  return true unless @data[:sha1] == sha1(ctx)
  return false
end

#recalc_depedencies(ctx) ⇒ Object



365
366
367
368
369
370
371
372
# File 'lib/daedalus.rb', line 365

def recalc_depedencies(ctx)
  deps = ctx.calculate_deps(@path)

  @data[:dep_sha1] = ctx.sha1(@path)
  @data[:deps] = deps

  return deps
end

#sha1(ctx) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/daedalus.rb', line 374

def sha1(ctx)
  sha1 = Digest::SHA1.new
  sha1 << ctx.sha1(@path)

  begin
    dependencies(ctx).each do |d|
      sha1 << ctx.sha1(d)
    end
  rescue StandardError
    recalc_depedencies(ctx)

    sha1 = Digest::SHA1.new
    sha1 << ctx.sha1(@path)

    dependencies(ctx).each do |d|
      begin
        sha1 << ctx.sha1(d)
      rescue StandardError
        raise "Unable to find dependency '#{d}' from #{@path}"
      end
    end
  end

  sha1.hexdigest
end