ariadne 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c5b693660af05c17570b71a98e6ed6899932f9b2571fd6f8e4fd7d66d61e4fc
4
- data.tar.gz: 8a3697425b580d3f40568b8b5a33d80e6d149197b0ede9fa2d35042b5ad90c8a
3
+ metadata.gz: 5d15963f0e72fcd20333f6227ced3e4dba5e3d5a7c2cc295440dcc98c9bb863a
4
+ data.tar.gz: bd90b66bad183442d7661fd72566ef11223d45f705817bcae2b2026efc928a3a
5
5
  SHA512:
6
- metadata.gz: 9d41649519721ea428249d049823a82ecaf4530dc436b27b4bd643e815e12f70eec38895e752b46ca57154ed7e9b5c9133bc799903b69479876c6d9c3577732f
7
- data.tar.gz: 70555aa75a6656cf16e199abb1b3b29ce960f806b3b68846edbf378072d7a4129c3268cfd2d8e06d44dde667f2682b9cee8766d63f9e98138df6f190b8a1ea7f
6
+ metadata.gz: 42c4370c8b426696d7b336fc42e40c60509569f60e1260a37aa952c1ee013b8a0ef94784032907574d256eb53071e5d8563ec5a9539b555e5abb08d4165b4bff
7
+ data.tar.gz: 30d1d3b263dee8c331d9f8d23b8bf8b9e422fd66b7f24a9488f2b4c3b8dc464a461201a0820475231628904a9a071d3ee496e8cd5cb27b0803818ecf2fce4bab
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ariadne
4
+ module Helpers
5
+ module Text
6
+ class Text < String
7
+ COLORS = %w[
8
+ black
9
+ red
10
+ green
11
+ yellow
12
+ blue
13
+ magenta
14
+ cyan
15
+ gray
16
+ ].freeze
17
+
18
+ COLORS.each.with_index do |color, index|
19
+ code = index + 30
20
+ define_method(color) do
21
+ "\e[#{code}m#{bleach}\e[0m"
22
+ end
23
+ bg_code = index + 40
24
+ define_method("bg_#{color}") do
25
+ "\e[#{bg_code}m#{bleach}\e[0m"
26
+ end
27
+ end
28
+
29
+ def initialize(str)
30
+ super(str.to_s)
31
+ end
32
+
33
+ def truncate(size)
34
+ if self.size > size
35
+ Text.new("#{self[..size]}...")
36
+ else
37
+ self
38
+ end
39
+ end
40
+
41
+ def bleach
42
+ gsub(/\e\[\d{1,2}m/, "")
43
+ end
44
+ end
45
+
46
+ def Text(str) # rubocop:disable Naming/MethodName
47
+ Text.new(str)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ariadne
4
+ module Helpers
5
+ module ValueFormat
6
+ RUBY_LITERALS = [
7
+ NilClass,
8
+ String,
9
+ Integer,
10
+ Float,
11
+ Proc,
12
+ TrueClass,
13
+ FalseClass,
14
+ Symbol,
15
+ Array,
16
+ Range,
17
+ Regexp,
18
+ Hash
19
+ ].freeze
20
+
21
+ def type(value)
22
+ return if value.is_a?(Class) || RUBY_LITERALS.include?(value.class)
23
+
24
+ value.class.name
25
+ end
26
+
27
+ def cast(value)
28
+ case value
29
+ when NilClass
30
+ "nil"
31
+ when Proc
32
+ "#<Proc>"
33
+ when Class
34
+ value.name
35
+ when String
36
+ "\"#{value}\""
37
+ else
38
+ value.to_s
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/ariadne/log.rb CHANGED
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "open3"
4
- require "ariadne/text"
4
+ require "ariadne/helpers/text"
5
+ require "ariadne/helpers/value_format"
5
6
 
6
7
  module Ariadne
7
8
  class Log
9
+ include Helpers::Text
10
+ include Helpers::ValueFormat
11
+
8
12
  def initialize(seam)
9
13
  @seam = seam
10
14
  end
@@ -22,50 +26,54 @@ module Ariadne
22
26
 
23
27
  def text
24
28
  @text ||=
25
- Text.new(
26
- [rank, " ", offset, " ", offset_dashes, class_name, prefix, method_name]
27
- .tap { add_parameters(_1) }
28
- .join
29
- ).truncate(200)
29
+ Text(
30
+ [
31
+ rank,
32
+ " ",
33
+ depth_dashes,
34
+ class_name,
35
+ prefix,
36
+ method_name,
37
+ parameters,
38
+ return_value
39
+ ].join
40
+ )
30
41
  end
31
42
 
32
43
  def rank
33
- Text.new(@seam.rank.to_s.rjust(4)).gray
34
- end
35
-
36
- def offset
37
- Text.new("(#{@seam.offset})").gray
44
+ Text(@seam.rank.to_s.rjust(4)).gray
38
45
  end
39
46
 
40
- def offset_dashes
41
- Text.new("-" * @seam.offset).gray.tap { _1 << " " if @seam.offset.positive? }
47
+ def depth_dashes
48
+ Text("-" * @seam.depth).gray.tap { _1 << " " if @seam.depth.positive? }
42
49
  end
43
50
 
44
51
  def class_name
45
- Text.new(@seam.class_name).green
52
+ Text(@seam.klass.name).green
46
53
  end
47
54
 
48
55
  def prefix
49
- Text.new(@seam.prefix).gray
56
+ Text(@seam.prefix).gray
50
57
  end
51
58
 
52
59
  def method_name
53
- Text.new(@seam.method_name).cyan
60
+ Text(@seam.method_name).cyan
54
61
  end
55
62
 
56
- def add_parameters(str)
57
- return if parameters == ""
58
-
59
- str << Text.new("(#{parameters})").magenta
63
+ def return_value
64
+ value = Text(cast(@seam.return_value)).truncate(50)
65
+ value = type(@seam.return_value) ? "<#{type(@seam.return_value)}> #{value}" : value
66
+ Text(" -> #{value}").yellow
60
67
  end
61
68
 
62
69
  def parameters
63
- @parameters ||=
70
+ str =
64
71
  @seam.parameters.map do |parameter|
65
- arg = Text.new(parameter.stringified_arg).truncate(50)
66
- arg = parameter.type ? "<#{parameter.type}> #{arg}" : arg
72
+ arg = Text(cast(parameter.arg)).truncate(50)
73
+ arg = type(parameter.arg) ? "<#{type(parameter.arg)}> #{arg}" : arg
67
74
  "#{parameter.param}: #{arg}"
68
75
  end.join(", ")
76
+ Text(str.empty? ? "" : "(#{str})").magenta
69
77
  end
70
78
  end
71
79
  end
@@ -1,25 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "open3"
4
- require "ariadne/text"
5
4
 
6
5
  module Ariadne
7
6
  class Parameter < SimpleDelegator
8
- RUBY_LITERALS = [
9
- NilClass,
10
- String,
11
- Integer,
12
- Float,
13
- Proc,
14
- TrueClass,
15
- FalseClass,
16
- Symbol,
17
- Array,
18
- Range,
19
- Regexp,
20
- Hash
21
- ].freeze
22
-
23
7
  def initialize(parameter, binding:)
24
8
  super(parameter)
25
9
  @binding = binding
@@ -34,26 +18,5 @@ module Ariadne
34
18
  rescue NameError
35
19
  "?"
36
20
  end
37
-
38
- def type
39
- return if arg.is_a?(Class) || RUBY_LITERALS.include?(arg.class)
40
-
41
- arg.class.name
42
- end
43
-
44
- def stringified_arg
45
- case arg
46
- when NilClass
47
- "nil"
48
- when Proc
49
- "#<Proc>"
50
- when Class
51
- arg.name
52
- when String
53
- "\"#{arg}\""
54
- else
55
- arg.to_s
56
- end
57
- end
58
21
  end
59
22
  end
data/lib/ariadne/seam.rb CHANGED
@@ -4,37 +4,63 @@ require "open3"
4
4
  require "ariadne/parameter"
5
5
 
6
6
  module Ariadne
7
- class Seam < SimpleDelegator
8
- attr_reader :rank, :offset
7
+ class Seam
8
+ attr_reader :rank, :depth, :klass, :method_name, :prefix, :parameters, :return_value, :binding, :path
9
9
 
10
- def initialize(tracepoint, rank:, offset:)
11
- super(tracepoint)
12
- @rank = rank
13
- @offset = offset
14
- end
10
+ class << self
11
+ # We cannot make public methods because accessing the TracePoint
12
+ # after it has been disabled would raise a RunTimeError
13
+ # with message "access from outside".
14
+ # We need to set the values while the TracePoint is enabled.
15
+ def build(tracepoint, rank:, depth:)
16
+ raise "tracepoint is disabled" unless tracepoint.enabled?
15
17
 
16
- # Seam.new(@rank, class_name(tp), prefix(tp), tp.method_id, parameters, @seams.size)
17
- def class_name
18
- if __getobj__.self.is_a?(Module)
19
- __getobj__.self.name
20
- else
21
- __getobj__.self.class.name
18
+ new(rank: rank, depth: depth).tap do |seam|
19
+ seam.instance_variable_set(:@klass, klass(tracepoint))
20
+ seam.instance_variable_set(:@prefix, prefix(tracepoint))
21
+ seam.instance_variable_set(:@method_name, method_name(tracepoint))
22
+ seam.instance_variable_set(:@parameters, parameters(tracepoint))
23
+ seam.instance_variable_set(:@path, path(tracepoint))
24
+ end
22
25
  end
23
- end
24
26
 
25
- def method_name
26
- method_id
27
- end
27
+ private
28
28
 
29
- def prefix
30
- __getobj__.self.is_a?(Module) ? "." : "#"
31
- end
29
+ def klass(tracepoint)
30
+ if tracepoint.self.is_a?(Module)
31
+ tracepoint.self
32
+ else
33
+ tracepoint.self.class
34
+ end
35
+ end
36
+
37
+ def method_name(tracepoint)
38
+ tracepoint.method_id
39
+ end
32
40
 
33
- def parameters
34
- method = __getobj__.self.method(method_name)
35
- method.parameters.map do |parameter|
36
- Parameter.new(parameter, binding: binding)
41
+ def prefix(tracepoint)
42
+ tracepoint.self.is_a?(Module) ? "." : "#"
37
43
  end
44
+
45
+ def parameters(tracepoint)
46
+ method = tracepoint.self.method(tracepoint.method_id)
47
+ method.parameters.map do |parameter|
48
+ Parameter.new(parameter, binding: tracepoint.binding)
49
+ end
50
+ rescue NoMethodError
51
+ []
52
+ end
53
+
54
+ def path(tracepoint)
55
+ tracepoint.path
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def initialize(rank:, depth:)
62
+ @rank = rank
63
+ @depth = depth
38
64
  end
39
65
  end
40
66
  end
@@ -6,44 +6,86 @@ require "ariadne/log"
6
6
 
7
7
  module Ariadne
8
8
  class Thread
9
+ attr_reader :seams
10
+
9
11
  LOG_FILE = "thread.log"
10
12
  DEFAULT_INCLUDE_PATHS = Open3.capture3("pwd").first.strip
11
13
  DEFAULT_EXCLUDE_PATHS = [].freeze
12
14
 
13
15
  def initialize(include_paths: nil, exclude_paths: nil)
14
- @include_paths = include_paths || DEFAULT_INCLUDE_PATHS
15
- @exclude_paths = exclude_paths || DEFAULT_EXCLUDE_PATHS
16
+ @include_paths = Array(include_paths) || DEFAULT_INCLUDE_PATHS
17
+ @exclude_paths = Array(exclude_paths) || DEFAULT_EXCLUDE_PATHS
18
+ @seams = []
16
19
  end
17
20
 
18
21
  def call
19
- @rank = 0
20
- @seams = []
21
- file = File.open(LOG_FILE, "w")
22
- trace = build_trace
22
+ prepare
23
+ open_file
23
24
  trace.enable
24
25
  yield
25
26
  ensure
26
27
  trace.disable
27
- file.close
28
+ log
29
+ close_file
28
30
  end
29
31
 
30
32
  private
31
33
 
32
- def build_trace
33
- TracePoint.new(:call, :return) do |tp|
34
- next if @include_paths.none? { tp.path.include?(_1) }
35
- next if @exclude_paths.any? { tp.path.include?(_1) }
36
-
37
- case tp.event
38
- when :call
39
- seam = Seam.new(tp, rank: @rank, offset: @seams.size)
40
- @seams << seam
41
- @rank += 1
42
- Log.new(seam).call
43
- when :return
44
- @seams.pop
34
+ def prepare
35
+ @rank = 0
36
+ @depth = 0
37
+ @seams = []
38
+ end
39
+
40
+ def open_file
41
+ @file = File.open(LOG_FILE, "w")
42
+ end
43
+
44
+ def close_file
45
+ @file.close
46
+ end
47
+
48
+ def trace
49
+ @trace ||=
50
+ TracePoint.new(:call, :return) do |tracepoint|
51
+ next if tracepoint.self.is_a?(TracePoint)
52
+
53
+ case tracepoint.event
54
+ when :call
55
+ on_call(tracepoint)
56
+ when :return
57
+ on_return(tracepoint)
58
+ end
45
59
  end
46
- end
60
+ end
61
+
62
+ def on_call(tracepoint)
63
+ return unless selected?(tracepoint)
64
+
65
+ seam = Seam.build(tracepoint, rank: @rank, depth: @depth)
66
+ @seams << seam
67
+
68
+ @rank += 1
69
+ @depth += 1
70
+ end
71
+
72
+ def on_return(tracepoint)
73
+ return unless selected?(tracepoint)
74
+
75
+ @depth -= 1
76
+
77
+ seam = @seams.reverse.find { _1.depth == @depth }
78
+ seam&.instance_variable_set(:@return_value, tracepoint.return_value)
79
+ seam&.instance_variable_set(:@binding, tracepoint.binding)
80
+ end
81
+
82
+ def selected?(tracepoint)
83
+ @include_paths.any? { tracepoint.path.include?(_1) } &&
84
+ @exclude_paths.none? { tracepoint.path.include?(_1) }
85
+ end
86
+
87
+ def log
88
+ @seams.each { Log.new(_1).call }
47
89
  end
48
90
  end
49
91
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ariadne
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ariadne
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edouard Piron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-23 00:00:00.000000000 Z
11
+ date: 2023-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: simplecov
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: yard
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -116,10 +102,11 @@ extensions: []
116
102
  extra_rdoc_files: []
117
103
  files:
118
104
  - lib/ariadne.rb
105
+ - lib/ariadne/helpers/text.rb
106
+ - lib/ariadne/helpers/value_format.rb
119
107
  - lib/ariadne/log.rb
120
108
  - lib/ariadne/parameter.rb
121
109
  - lib/ariadne/seam.rb
122
- - lib/ariadne/text.rb
123
110
  - lib/ariadne/thread.rb
124
111
  - lib/ariadne/version.rb
125
112
  homepage: https://github.com/BigBigDoudou/ariadne
data/lib/ariadne/text.rb DELETED
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Ariadne
4
- class Text < String
5
- COLORS = %w[
6
- black
7
- red
8
- green
9
- yellow
10
- blue
11
- magenta
12
- cyan
13
- gray
14
- ].freeze
15
-
16
- COLORS.each.with_index do |color, index|
17
- code = index + 30
18
- define_method(color) do
19
- "\e[#{code}m#{bleach}\e[0m"
20
- end
21
- bg_code = index + 40
22
- define_method("bg_#{color}") do
23
- "\e[#{bg_code}m#{bleach}\e[0m"
24
- end
25
- end
26
-
27
- def initialize(str)
28
- super(str.to_s)
29
- end
30
-
31
- def truncate(size)
32
- if self.size > size
33
- Text.new("#{self[..size]}...")
34
- else
35
- self
36
- end
37
- end
38
-
39
- def bleach
40
- gsub(/\e\[\d{1,2}m/, "")
41
- end
42
- end
43
- end