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.
- checksums.yaml +4 -4
- data/lib/kurangu.rb +17 -5
- data/lib/trace.rb +27 -22
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1edac603765e16e67954a8413fb896f5eb29f3c1
|
4
|
+
data.tar.gz: 08ed42aa75aca7d75d52888bab152a1e4824dbba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
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
|
49
|
-
|
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
|
-
|
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
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
args
|
30
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|