debugvisualizer 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: 7a8a3162f364560e6455c89c36a29b7d82fd5aa437928b342d6317eb457c0d1b
4
+ data.tar.gz: c642e62625d02e20ef8d2691bbc737eb365619db984964be01d596b40e60c070
5
+ SHA512:
6
+ metadata.gz: e1ed30c914a88b7f86b08c77df974bb0e61471428565ad9f73e7a67d9bf3c05503b694ec5f873d7cb1c8908e025e86a70a9f8193f43b5886af903eef4741d300
7
+ data.tar.gz: 847b26a202f2bf10210635958d5b5ecc460c09a6baf037147c5b900f20f24fa19acb23e1b4e95633197518c0fce480ef476d0993c62ee5ed9a3f066ec47fa8f4
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in debugvisualizer.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "test-unit", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Naoto Ono
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,42 @@
1
+ # DebugVisualizer
2
+
3
+ ## Installation
4
+
5
+ Install the gem and add to the application's Gemfile by executing:
6
+
7
+ $ bundle add debugvisualizer
8
+
9
+ If bundler is not being used to manage dependencies, install the gem by executing:
10
+
11
+ $ gem install debugvisualizer
12
+
13
+ ## Usage
14
+
15
+ ### Example
16
+
17
+ ```ruby
18
+ require 'debugvisualizer'
19
+
20
+ DebugVisualizer.register do |data|
21
+ if data.is_a?(Hash)
22
+ {
23
+ id: "hash_visualizer",
24
+ name: "Hash",
25
+ data: {
26
+ kind: { table: true },
27
+ rows: [data]
28
+ }
29
+ }
30
+ end
31
+ end
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test-unit` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/debugvisualizer.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/debugvisualizer/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "debugvisualizer"
7
+ spec.version = DebugVisualizer::VERSION
8
+ spec.authors = ["Naoto Ono"]
9
+ spec.email = ["onoto1998@gmail.com"]
10
+
11
+ spec.summary = "debugvisualizer generates JSON based on VS Code Debug Visualizer protocol"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/ono-max/debugvisualizer"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,24 @@
1
+
2
+ module DebugVisualizer
3
+ DebugVisualizer.register do |data|
4
+ if (defined?(::ActiveRecord::Base) && data.kind_of?(::ActiveRecord::Base)) ||
5
+ (defined?(::ActiveRecord::Relation) && data.kind_of?(::ActiveRecord::Relation))
6
+ rows = nil
7
+ if data.respond_to? :to_a
8
+ ary = data.to_a
9
+ rows = ary.map{|elem| elem.attributes}
10
+ else
11
+ rows = [data.attributes]
12
+ end
13
+
14
+ {
15
+ id: "active_record_visualizer",
16
+ name: "ActiveRecord As Table",
17
+ data: {
18
+ kind: { table: true },
19
+ rows: rows
20
+ }
21
+ }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ module DebugVisualizer
2
+ DebugVisualizer.register do |data|
3
+ if data.is_a?(Array)
4
+ new_data = nil
5
+ name = ""
6
+ if data.all? {|v| v.is_a?(Integer) || v.is_a?(Float) }
7
+ new_data = {
8
+ "kind":{ "plotly": true },
9
+ "data":[
10
+ { "y": data },
11
+ ]
12
+ }
13
+ name = "Array As Plot"
14
+ else
15
+ columns = []
16
+ data.each{|elem|
17
+ columns << {content: elem.to_s, tag: elem.to_s}
18
+ }
19
+ new_data = {
20
+ "kind": { "grid": true },
21
+ "text": "test",
22
+ "columnLabels": [
23
+ {
24
+ "label": "test"
25
+ }
26
+ ],
27
+ "rows": [
28
+ {
29
+ "label": "foo",
30
+ "columns": columns
31
+ }
32
+ ]
33
+ }
34
+ name = "Array As Grid"
35
+ end
36
+ {
37
+ id: "array_visualizer",
38
+ name: name,
39
+ priority: 30,
40
+ data: new_data
41
+ }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,14 @@
1
+ module DebugVisualizer
2
+ DebugVisualizer.register do |data|
3
+ if data.is_a?(Hash)
4
+ {
5
+ id: "hash_visualizer",
6
+ name: "Hash As Table",
7
+ data: {
8
+ kind: { table: true },
9
+ rows: [data]
10
+ }
11
+ }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ module DebugVisualizer
2
+ DebugVisualizer.register do |data|
3
+ if data.is_a?(Array) && data.size == 2 && data.all? {|elem| elem.is_a?(String)}
4
+ {
5
+ id: "string_diff",
6
+ name: "String diff",
7
+ priority: 50,
8
+ data: {
9
+ kind: { text: true },
10
+ text: data[0],
11
+ otherText: data[1]
12
+ }
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module DebugVisualizer
2
+ DebugVisualizer.register do |data|
3
+ {
4
+ id: "to_string",
5
+ name: "To String",
6
+ priority: 1,
7
+ data: {
8
+ kind: { text: true },
9
+ text: inspect(data),
10
+ }
11
+ }
12
+ end
13
+
14
+ def self.inspect obj
15
+ obj.inspect
16
+ rescue Exception => e
17
+ "failed to inspect: #{e.message}"
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DebugVisualizer
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+ require 'json'
5
+
6
+ module DebugVisualizer
7
+ def register &block
8
+ raise unless block_given?
9
+
10
+ unless defined? GENERATOR
11
+ DebugVisualizer.const_set(:GENERATOR, JSONGenerator.new)
12
+ end
13
+ GENERATOR.register_block block
14
+ end
15
+
16
+ def to_debug_visualizer_protocol_json preferred_id, data
17
+ GENERATOR.generate preferred_id, data
18
+ end
19
+
20
+ module_function :register, :to_debug_visualizer_protocol_json
21
+
22
+ class JSONGenerator
23
+ attr_accessor :default_registered
24
+
25
+ def initialize
26
+ @blocks = []
27
+ end
28
+
29
+ def register_block block
30
+ @blocks << block
31
+ end
32
+
33
+ def generate preferred_id, data
34
+ extractors = []
35
+ @blocks.each{|block|
36
+ result = block.call(data)
37
+ if result && result[:id] && result[:data]
38
+ extractors << get_extractor(result)
39
+ end
40
+ }
41
+
42
+ return JSON.generate({ kind: "NoExtractors" }) if extractors.empty?
43
+
44
+ sorted = extractors.sort_by{|ex| -ex[:priority]}
45
+ used_ex = sorted[0]
46
+
47
+ unless preferred_id.empty?
48
+ sorted.each{|ex|
49
+ if ex[:id] == preferred_id
50
+ used_ex = ex
51
+ break
52
+ end
53
+ }
54
+ end
55
+
56
+
57
+ data = used_ex.delete(:data)
58
+
59
+ sorted.each{|ex| ex.delete :data}
60
+
61
+ JSON.generate({
62
+ kind: "Data",
63
+ extractionResult: {
64
+ data: data,
65
+ usedExtractor: used_ex,
66
+ availableExtractors: sorted,
67
+ },
68
+ })
69
+ rescue Exception => e
70
+ JSON.generate({
71
+ kind: "Error",
72
+ message: e.message
73
+ })
74
+ end
75
+
76
+ private
77
+
78
+ def get_extractor result
79
+ {
80
+ id: result[:id],
81
+ name: result[:name] || result[:id],
82
+ priority: result[:priority] || 100,
83
+ data: result[:data]
84
+ }
85
+ end
86
+ end
87
+ end
88
+
89
+ require_relative 'debugvisualizer/active_record'
90
+ require_relative 'debugvisualizer/array'
91
+ require_relative 'debugvisualizer/hash'
92
+ require_relative 'debugvisualizer/to_string'
93
+ require_relative 'debugvisualizer/string_diff'
@@ -0,0 +1,4 @@
1
+ module DebugVisualizer
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: debugvisualizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Naoto Ono
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-11-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: debugvisualizer generates JSON based on VS Code Debug Visualizer protocol
14
+ email:
15
+ - onoto1998@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - debugvisualizer.gemspec
25
+ - lib/debugvisualizer.rb
26
+ - lib/debugvisualizer/active_record.rb
27
+ - lib/debugvisualizer/array.rb
28
+ - lib/debugvisualizer/hash.rb
29
+ - lib/debugvisualizer/string_diff.rb
30
+ - lib/debugvisualizer/to_string.rb
31
+ - lib/debugvisualizer/version.rb
32
+ - sig/debugvisualizer.rbs
33
+ homepage: https://github.com/ono-max/debugvisualizer
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/ono-max/debugvisualizer
38
+ source_code_uri: https://github.com/ono-max/debugvisualizer
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.6.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.2.32
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: debugvisualizer generates JSON based on VS Code Debug Visualizer protocol
58
+ test_files: []