public
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/evanphx/rubinius.git
rubinius / lib / codearchive.rb
100644 83 lines (70 sloc) 1.499 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class CodeArchive
  def initialize(path)
    @path = path
    reload()
  end
  
  def reload
    if File.exists?(@path)
      @data = Archive.list_files(@path)
      @times = {}
      @data.each do |info|
        @times[info[0]] = info[1]
      end
    else
      @data = []
      @times = {}
    end
  end
  
  attr_accessor :data
  attr_accessor :times
  
  def files
    out = []
    @data.each { |t| out << t.at(0) }
    return out
  end
  
  def last_modified(file)
    if time = @times[file]
      return time
    elsif file.suffix?(".rb")
      return @times["#{file}c"]
    else
      return nil
    end
  end
  
  def out_of_date?(file)
    cur = last_modified(file)
    return true unless cur
    
    stat = File.stat(file)
    stat.mtime > cur
  end
  
  def find_out_of_date(dir)
    ood = []
    Dir["#{dir}/*.rb"].each do |file|
      ood << file if out_of_date?(file)
    end
    return ood
  end
  
  def add_file(name, method)
    # Save blah.rb as blah.rbc
    if name.suffix?(".rb")
      name = "#{name}c"
    end
    
    Archive.add_object @path, name, method
    reload()
    return true
  end
    
  def refresh_file(file)
    return false unless out_of_date?(file)
    
    meth = Compile.compile_file(file)
    add_file(file, meth)
  end
  
  def update_from_directory(dir)
    added = find_out_of_date(dir)
    added.each do |file|
      yield file if block_given?
      meth = Compile.compile_file(file)
      add_file(file, meth)
    end
    
    return added
  end
end