ruby_snooper 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7f76024cd4ebf598fc71e2ff34cb6d9cda4e53fa2cad98444c9db29fe005437e
4
+ data.tar.gz: cdab886d2524496c6d27bc8006cabd820f02f7f2d9485260b9f74b9a57c20195
5
+ SHA512:
6
+ metadata.gz: 25b10369c1480cbe774ae9bd566a95a2c207d5f55a58578cb50f4ebf86e4f923d3a6c8d4ba669c582faf5735c761bf7638ff1b1a39574b85e52e0d4800de0c96
7
+ data.tar.gz: e12afbdb620569e1be113b45a37dd44ef038e3363ff25a15e1edb21c477c6c6c2d534c7b61b7acfaf52ddc3e9a54367ea7a36c25fd61732a837bd4be9013b591
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ # rspec
53
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at koyama.gakugai@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ruby_snooper.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby_snooper (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.3)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.17)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ ruby_snooper!
33
+
34
+ BUNDLED WITH
35
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 nanonanomachine
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # RubySnooper
2
+
3
+ RubySnooper is Poort man's debugger heavily inspired by [PySnooper](https://github.com/cool-RR/PySnooper)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby_snooper'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby_snooper
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class SampleClass
25
+ extend RubySnooper
26
+ snoop :sample_instance_method
27
+ snoop_class_methods :sample_class_method
28
+
29
+ def self.sample_class_method(arg1, arg2)
30
+ arg1 = arg1 + 1
31
+ arg3 = arg1 + arg2
32
+ arg3 * 2
33
+ end
34
+
35
+ def sample_instance_method(arg1, arg2)
36
+ arg1 = arg1 + 1
37
+ arg3 = arg1 + arg2
38
+ arg3 * 2
39
+ end
40
+ end
41
+
42
+ SampleClass.new.sample_instance_method(1, 2)
43
+ SampleClass.sample_class_method(1, 2)
44
+ ```
45
+
46
+ The output to stderr is:
47
+
48
+ ```
49
+ From /some/file/path.rb
50
+ Starting var arg1 = 1, arg2 = 2, arg3 = nil
51
+ 02:06:31,239 call 59 def sample_instance_method(arg1, arg2)
52
+ 02:06:31,239 line 60 arg1 = arg1 + 1
53
+ Modified var arg1 = 2
54
+ 02:06:31,239 line 61 arg3 = arg1 + arg2
55
+ Modified var arg3 = 4
56
+ 02:06:31,239 line 62 arg3 * 2
57
+ 02:06:31,239 return 63 end
58
+ Return value 8
59
+ From /some/file/path.rb
60
+ Starting var arg1 = 1, arg2 = 2, arg3 = nil
61
+ 02:06:31,240 call 53 def self.sample_class_method(arg1, arg2)
62
+ 02:06:31,240 line 54 arg1 = arg1 + 1
63
+ Modified var arg1 = 2
64
+ 02:06:31,240 line 55 arg3 = arg1 + arg2
65
+ Modified var arg3 = 4
66
+ 02:06:31,241 line 56 arg3 * 2
67
+ 02:06:31,241 return 57 end
68
+ Return value 8
69
+ ```
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_snooper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
84
+
85
+ ## Code of Conduct
86
+
87
+ Everyone interacting in the RubySnooper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ruby_snooper/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruby_snooper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,120 @@
1
+ module RubySnooper
2
+ class TraceWriter
3
+ def initialize(method_names, caller_path)
4
+ @method_names = method_names
5
+ @caller_path = caller_path
6
+ @source_cache = {}
7
+ @lines = []
8
+ @return = nil
9
+ end
10
+
11
+ def code_for(filename)
12
+ @source_cache[filename] ||= IO.readlines(filename, chomp: true)
13
+ end
14
+
15
+ def print
16
+ @lines.each(&:print)
17
+ @return.print
18
+ end
19
+
20
+ def trace_point
21
+ @trace_point ||= TracePoint.new(:call, :line, :return) do |tp|
22
+ next unless @method_names.include?(tp.method_id)
23
+ next if tp.path != @caller_path
24
+
25
+ local_variables = tp.binding.local_variables.map do |name|
26
+ [name, tp.binding.local_variable_get(name).inspect]
27
+ end.to_h
28
+
29
+ if @lines.count > 0
30
+ new_variables = local_variables.select { |key,value| !@lines.last.local_variables.has_key?(key) }
31
+ modified_variables = local_variables.select {|key, value| @lines.last.local_variables.has_key?(key) && value != @lines.last.local_variables[key] }
32
+ end
33
+
34
+ case tp.event
35
+ when :call, :line
36
+ @lines << Line.new(
37
+ tp.method_id,
38
+ tp.event,
39
+ tp.lineno,
40
+ Time.now,
41
+ code_for(tp.path)[tp.lineno - 1],
42
+ local_variables,
43
+ new_variables,
44
+ modified_variables,
45
+ tp.path,
46
+ )
47
+ when :return
48
+ @return = Return.new(
49
+ tp.method_id,
50
+ tp.event,
51
+ tp.lineno,
52
+ Time.now,
53
+ code_for(tp.path)[tp.lineno - 1],
54
+ tp.return_value,
55
+ tp.path,
56
+ )
57
+ end
58
+ end
59
+ end
60
+
61
+ class Line
62
+ attr_reader :method_id, :event, :number, :time, :code, :local_variables, :new_variables, :modified_variables
63
+
64
+ def initialize(method_id, event, number, time, code, local_variables, new_variables, modified_variables, file_path)
65
+ @method_id = method_id
66
+ @event = event
67
+ @number = number
68
+ @time = time
69
+ @code = code
70
+ @local_variables = local_variables
71
+ @new_variables = new_variables
72
+ @modified_variables = modified_variables
73
+ @file_path = file_path
74
+ end
75
+
76
+ def print
77
+ STDERR.puts "From #{@file_path}" if @event == :call
78
+ print_variables
79
+ STDERR.puts "#{Time.now.strftime("%T,%L")} #{@event} #{number} #{code}"
80
+ end
81
+
82
+ private
83
+
84
+ def print_variables
85
+ case @event
86
+ when :call
87
+ STDERR.puts "Starting var #{variables_str(@local_variables)}"
88
+ when :line
89
+ STDERR.puts "New var #{variables_str(@new_variables)}" if @new_variables.count > 0
90
+ STDERR.puts "Modified var #{variables_str(@modified_variables)}" if @modified_variables.count > 0
91
+ end
92
+ end
93
+
94
+ def variables_str(variables)
95
+ variables.map do |key, value|
96
+ "#{key} = #{value}"
97
+ end.join(', ')
98
+ end
99
+ end
100
+
101
+ class Return
102
+ attr_reader :method_id, :event, :number, :time, :code, :return_value
103
+
104
+ def initialize(method_id, event, number, time, code, return_value, file_path)
105
+ @method_id = method_id
106
+ @event = event
107
+ @number = number
108
+ @time = time
109
+ @code = code
110
+ @return_value = return_value
111
+ @file_path = file_path
112
+ end
113
+
114
+ def print
115
+ STDERR.puts "#{Time.now.strftime("%T,%L")} #{@event} #{@number} #{@code}"
116
+ STDERR.puts "Return value #{@return_value}"
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,3 @@
1
+ module RubySnooper
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ require "ruby_snooper/version"
2
+ require "ruby_snooper/trace_writer"
3
+
4
+ module RubySnooper
5
+ class Error < StandardError; end
6
+
7
+ CLASS_NAME_PATTERN = /\<class\:(\w+)\>/.freeze
8
+
9
+ def snoop(*method_names)
10
+ prepend to_prepend(method_names)
11
+ end
12
+
13
+ def snoop_class_methods(*method_names)
14
+ Kernel.const_get(caller_locations.first.label.match(CLASS_NAME_PATTERN)[1])
15
+ .singleton_class
16
+ .prepend(to_prepend(method_names))
17
+ end
18
+
19
+ private
20
+
21
+ def to_prepend(method_names)
22
+ Module.new do
23
+ method_names.each do |method_name|
24
+ define_method(method_name) do |*args, &block|
25
+ trace_writer = TraceWriter.new(
26
+ method_names,
27
+ caller_locations.first.path,
28
+ )
29
+ trace_writer.trace_point.enable
30
+ super(*args,&block).tap do
31
+ trace_writer.trace_point.disable
32
+ trace_writer.print
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ruby_snooper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_snooper"
8
+ spec.version = RubySnooper::VERSION
9
+ spec.authors = ["nanonanomachine"]
10
+ spec.email = ["koyama.gakugai@gmail.com"]
11
+
12
+ spec.summary = %q{Poort man's debugger heavily inspired by PySnooper}
13
+ spec.description = %q{Poort man's debugger heavily inspired by PySnooper}
14
+ spec.homepage = "https://github.com/nanonanomachine/ruby_snooper"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.17"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_snooper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nanonanomachine
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Poort man's debugger heavily inspired by PySnooper
56
+ email:
57
+ - koyama.gakugai@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/ruby_snooper.rb
73
+ - lib/ruby_snooper/trace_writer.rb
74
+ - lib/ruby_snooper/version.rb
75
+ - ruby_snooper.gemspec
76
+ homepage: https://github.com/nanonanomachine/ruby_snooper
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.0.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Poort man's debugger heavily inspired by PySnooper
99
+ test_files: []