kurangu 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/kurangu.rb +17 -5
  3. data/lib/trace.rb +27 -22
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c878be455c92153388c28789fa52dd75cf8af897
4
- data.tar.gz: 11199180b1f492e70c92e479c0435ad4e30c594f
3
+ metadata.gz: 1edac603765e16e67954a8413fb896f5eb29f3c1
4
+ data.tar.gz: 08ed42aa75aca7d75d52888bab152a1e4824dbba
5
5
  SHA512:
6
- metadata.gz: c965377a60f1e88885d69b9a5b092534441ab008c3ed3c439c2534e47f83735b53625c7e588254168c26439ef4a0850fe96045371a1440d9bb76fe75a6a70c32
7
- data.tar.gz: 860f00f76c6513fff33f7711a85355388339a69cace2a469664442891edd7ac2dc61e4695f844d85dad71031cf4944fcf880f81de3c1f6365435605f97efb2ce
6
+ metadata.gz: b9b0bb828692c4f30dfbd53569c97e6f441716e4bc49c89fa028da82e243b4383f87bc7203945632c06f1fcf47d064a951c6ed170ec714f49c1d91d9a0fb6f2a
7
+ data.tar.gz: 82c27311d8eb3ce022052d5ce04e07f0df6adf9c06efdb961716db9b8dd689bd128df7bfdf01468590e45ccc05722ae82d43dbed2d7fd148e33074d1b212cf7a
data/lib/kurangu.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  require 'rbconfig'
2
2
  require 'fileutils'
3
+ require 'open3'
3
4
 
4
5
  class Kurangu
5
6
  def generate_annotations(input_file)
6
7
  ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
7
8
  trace_file = File.expand_path("lib/trace.rb")
8
9
  puts "\ngenerating annotations\n"
9
- system(ruby, "-r", trace_file, input_file)
10
+ Open3.popen2("#{ruby} -r #{trace_file} #{input_file}") {|stdin, stdout, wait_thr|
11
+ stdin.puts(input_file)
12
+ }
10
13
  end
11
14
 
12
15
  def print_annotations(paths_file)
@@ -31,7 +34,6 @@ class Kurangu
31
34
  original_path = annotation_path.chomp('.annotations')
32
35
  annotated_path = "#{original_path}.annotated"
33
36
  puts "\ngenerating annotated file #{annotated_path}\n"
34
- lines = ["require 'rdl'\n", "require 'types/core'\n"]
35
37
  annotations = Hash.new
36
38
  File.open(annotation_path, "r") do |f|
37
39
  f.each_line do |line|
@@ -40,17 +42,27 @@ class Kurangu
40
42
  annotations[index] = "#{split[1]}\n"
41
43
  end
42
44
  end
43
- lines << "\n"
45
+ lines = []
46
+ has_types = false
44
47
  File.open(original_path, "r") do |f|
45
48
  f.each_line.with_index do |line, index|
46
49
  whitespace = line.chomp(line.lstrip)
47
50
  if annotations.key?(index + 1)
48
- lines << "#{whitespace}extend RDL::Annotate\n"
49
- lines << "#{whitespace}#{annotations[index + 1]}"
51
+ if lines.last and lines.last.start_with?('type')
52
+ has_types = true
53
+ lines.last = "#{whitespace}#{annotations[index + 1]}"
54
+ else
55
+ lines << "#{whitespace}extend RDL::Annotate\n"
56
+ lines << "#{whitespace}#{annotations[index + 1]}"
57
+ end
50
58
  end
51
59
  lines << line
52
60
  end
53
61
  end
62
+ if !has_types
63
+ lines.unshift "require 'types/core'\n\n"
64
+ lines.unshift "require 'rdl'\n"
65
+ end
54
66
  IO.write(annotated_path, lines.join())
55
67
  end
56
68
  end
data/lib/trace.rb CHANGED
@@ -1,16 +1,17 @@
1
1
  require 'set'
2
- require 'yaml'
2
+
3
+ INPUT_FILE = $stdin.readline.chomp
3
4
 
4
5
  stack = Hash.new Array.new
5
6
  parameter_types = Hash.new Hash.new Set.new
6
- return_types = Hash.new Array.new
7
+ return_types = Hash.new Set.new
7
8
  parameter_list = Hash.new Array.new
8
9
  signatures = Hash.new
9
10
  paths = Set.new
10
11
 
11
12
  def generate_signature(line, parameters, parameter_types, return_types)
12
13
  joined_parameters = parameters.map { |arg| parameter_types[arg].to_a.join(" or ") }
13
- "#{line} type '(#{joined_parameters.join(", ")}) -> #{return_types.join(" or ")}'"
14
+ "#{line} type '(#{joined_parameters.join(", ")}) -> #{return_types.to_a.join(" or ")}'"
14
15
  end
15
16
 
16
17
  def write_annotations_paths(dir, paths)
@@ -23,31 +24,35 @@ def write_annotations(path, signatures)
23
24
  end
24
25
 
25
26
  trace_return = TracePoint.new(:return) do |t|
26
- s = "#{t.defined_class}, :#{t.method_id}"
27
- args = stack[s].pop
28
- if args
29
- args.each do |arg, type|
30
- parameter_types[s][arg].add(type)
27
+ if File.dirname(t.path) == File.dirname(INPUT_FILE)
28
+ s = "#{t.defined_class}, :#{t.method_id}"
29
+ args = stack[s].pop
30
+ if args
31
+ args.each do |arg, type|
32
+ parameter_types[s][arg].add(type)
33
+ end
34
+ return_types[s].add(t.return_value.class)
35
+ parameter_list[s] = t.self.method(t.method_id).parameters.map { |a | a[1] }
36
+ line = t.self.method(t.method_id).source_location[1]
37
+ signatures[s] = generate_signature(line, parameter_list[s], parameter_types[s], return_types[s])
38
+ path = "#{t.path}.annotations"
39
+ dir = File.dirname(t.path)
40
+ write_annotations_paths(dir, paths.add(path))
41
+ write_annotations(path, signatures)
31
42
  end
32
- return_types[s] << t.return_value.class
33
- parameter_list[s] = t.self.method(t.method_id).parameters.map { |a | a[1] }
34
- line = t.self.method(t.method_id).source_location[1]
35
- signatures[s] = generate_signature(line, parameter_list[s], parameter_types[s], return_types[s])
36
- path = "#{t.path}.annotations"
37
- dir = File.dirname(t.path)
38
- write_annotations_paths(dir, paths.add(path))
39
- write_annotations(path, signatures)
40
43
  end
41
44
  end
42
45
 
43
46
  trace_call = TracePoint.new(:call) do |t|
44
- s = "#{t.defined_class}, :#{t.method_id}"
45
- args = t.binding.eval("local_variables").inject({}) do |vars, name|
46
- value = t.binding.eval name.to_s
47
- vars[name] = value.class
48
- vars
47
+ if File.dirname(t.path) == File.dirname(INPUT_FILE)
48
+ s = "#{t.defined_class}, :#{t.method_id}"
49
+ args = t.binding.eval("local_variables").inject({}) do |vars, name|
50
+ value = t.binding.eval name.to_s
51
+ vars[name] = value.class
52
+ vars
53
+ end
54
+ stack[s] << args
49
55
  end
50
- stack[s] << args
51
56
  end
52
57
 
53
58
  trace_return.enable
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kurangu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arpith Siromoney