Class: Kurangu

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

Instance Method Summary collapse

Instance Method Details

#apply_annotation(annotated_path, original_path) ⇒ Object



75
76
77
78
# File 'lib/kurangu.rb', line 75

def apply_annotation(annotated_path, original_path)
  puts "\napplying annotations for #{original_path}\n"
  FileUtils.mv(annotated_path, original_path)
end

#apply_annotations(paths_file) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/kurangu.rb', line 80

def apply_annotations(paths_file)
  File.open(paths_file, "r") do |f|
    f.each_line do |annotation_path|
      original_path = annotation_path.chomp('.annotations')
      annotated_path = "#{original_path}.annotated"
      apply_annotation(annotated_path, original_path)
    end
  end
end

#generate_annotated_files(paths_file) ⇒ Object



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
# File 'lib/kurangu.rb', line 29

def generate_annotated_files(paths_file)
  File.open(paths_file, "r") do |f|
    f.each_line do |annotation_path|
      original_path = annotation_path.chomp('.annotations')
      annotated_path = "#{original_path}.annotated"
      puts "\ngenerating annotated file #{annotated_path}\n"
      annotations = Hash.new { |h, k| h[k] = [] }
      File.open(annotation_path, "r") do |f|
        f.each_line do |line|
          split = line.split(" ", 2)
          index = split[0].to_i
          annotations[index].push split[1]
        end
      end
      lines = []
      has_types = false
      File.open(original_path, "r") do |f|
        f.each_line.with_index do |line, index|
          whitespace = line.chomp(line.lstrip)
          if annotations[index + 1].size > 0
            if lines.last and lines.last.start_with?('type')
              has_types = true
              lines.last = "#{whitespace}#{annotations[index + 1][0]}"
              annotations[index + 1].drop(1).each do |annotation|
                lines << "#{whitespace}#{annotation}"
              end
            else
              lines << "#{whitespace}extend RDL::Annotate\n"
              annotations[index + 1].each do |annotation|
                lines << "#{whitespace}#{annotation}"
              end
            end
            lines << "\n"
          end
          lines << line
        end
      end
      if !has_types
        lines.unshift "require 'types/core'\n\n"
        lines.unshift "require 'rdl'\n"
      end
      IO.write(annotated_path, lines.join())
    end
  end
end

#generate_annotations(input_file) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/kurangu.rb', line 6

def generate_annotations(input_file)
  ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
  trace_file = File.expand_path("lib/trace.rb")
  puts "\ngenerating annotations\n"
  Open3.popen2("#{ruby} -r #{trace_file} #{input_file}") {|stdin, stdout, wait_thr|
    stdin.puts(input_file)
    puts stdout.read
  }
end


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kurangu.rb', line 16

def print_annotations(paths_file)
  puts "\nthe annotations generated are:\n"
  File.open(paths_file, "r") do |f|
    f.each_line do |annotation_file|
      puts annotation_file
      File.open(annotation_file, "r") do |f|
        contents = f.read
        puts contents
      end
    end
  end
end

#run(input_file_path) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/kurangu.rb', line 96

def run(input_file_path)
  input_file = File.expand_path(input_file_path)
  paths_file = "#{File.dirname(input_file)}/annotations_paths.txt"
  self.generate_annotations(input_file)
  self.print_annotations(paths_file)
  self.generate_annotated_files(paths_file)
  self.apply_annotations(paths_file)
  self.run_rdl(input_file)
end

#run_rdl(input_file) ⇒ Object



90
91
92
93
94
# File 'lib/kurangu.rb', line 90

def run_rdl(input_file)
  puts "\nrunning rdl\n"
  ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
  system(ruby, input_file)
end