dsl_evaluator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dsl_evaluator.gemspec +24 -0
- data/lib/dsl_evaluator/version.rb +3 -0
- data/lib/dsl_evaluator.rb +49 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6fee21bf08bd6a94ed3d044698e91023831ed75ca9e126cf9f676c13752e515e
|
4
|
+
data.tar.gz: 2c5c7a7889dc51f0dafeadc91f16f453028e87e1311e861bfa68a27ae7e3c4c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '0951a83f5c7ad782475f54dcf51ed459b07d5915b87429800572ac34ff9f102911ae8d09e4735a5ce94af025366e8a6cbcce138ea63b81c57ecdf2568e19c152'
|
7
|
+
data.tar.gz: 633b4714c4e8ffc16997f0c590f16385c79426424486a2f45c5019203e0d8bc62c151630d9141ff606c6db022cbc7ecc7ae492c20214d2bb421caf2aa7e4276f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Tung Nguyen
|
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,41 @@
|
|
1
|
+
# DslEvaluator
|
2
|
+
|
3
|
+
Small module to help with DSL evaluation. Notably, it produces a human-friendly backtrace error showing the original user-provided source code if there's a syntax error.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require "dsl_evaluator"
|
9
|
+
DslEvaluator.backtrace_reject = "lib/my_gem" # optional
|
10
|
+
|
11
|
+
class DslBuilder
|
12
|
+
def build
|
13
|
+
path = "/path/to/user/provided/dsl/file.rb"
|
14
|
+
evaluate_file(path)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'dsl_evaluator'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle install
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install dsl_evaluator
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tongueroo/dsl_evaluator.
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dsl_evaluator"
|
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,24 @@
|
|
1
|
+
require_relative 'lib/dsl_evaluator/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "dsl_evaluator"
|
5
|
+
spec.version = DslEvaluator::VERSION
|
6
|
+
spec.authors = ["Tung Nguyen"]
|
7
|
+
spec.email = ["tongueroo@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Small module to help with DSL evaluation. It produces a human-friendly backtrace error"
|
10
|
+
spec.homepage = "https://github.com/tongueroo/dsl_evaluator"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require "dsl_evaluator/version"
|
2
|
+
|
3
|
+
module DslEvaluator
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
def evaluate_file(path)
|
7
|
+
return unless path && File.file?(path)
|
8
|
+
instance_eval(IO.read(path), path)
|
9
|
+
rescue Exception => e
|
10
|
+
evaluation_error(e)
|
11
|
+
puts "\nFull error:"
|
12
|
+
raise
|
13
|
+
end
|
14
|
+
|
15
|
+
@@backtrace_reject = nil
|
16
|
+
def backtrace_reject
|
17
|
+
@@backtrace_reject
|
18
|
+
end
|
19
|
+
|
20
|
+
def backtrace_reject=(v)
|
21
|
+
@@backtrace_reject = v
|
22
|
+
end
|
23
|
+
|
24
|
+
# Prints out a user friendly task_definition error message
|
25
|
+
def evaluation_error(e)
|
26
|
+
lines = e.backtrace
|
27
|
+
lines = lines.reject { |l| l.include?(backtrace_reject) } if backtrace_reject
|
28
|
+
error_info = lines.first
|
29
|
+
path, line_no, _ = error_info.split(':')
|
30
|
+
line_no = line_no.to_i
|
31
|
+
puts "Error evaluating #{path}:".color(:red)
|
32
|
+
puts e.message
|
33
|
+
puts "Here's the line in #{path} with the error:\n\n"
|
34
|
+
|
35
|
+
contents = IO.read(path)
|
36
|
+
content_lines = contents.split("\n")
|
37
|
+
context = 5 # lines of context
|
38
|
+
top, bottom = [line_no-context-1, 0].max, line_no+context-1
|
39
|
+
spacing = content_lines.size.to_s.size
|
40
|
+
content_lines[top..bottom].each_with_index do |line_content, index|
|
41
|
+
line_number = top+index+1
|
42
|
+
if line_number == line_no
|
43
|
+
printf("%#{spacing}d %s\n".color(:red), line_number, line_content)
|
44
|
+
else
|
45
|
+
printf("%#{spacing}d %s\n", line_number, line_content)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dsl_evaluator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tung Nguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- tongueroo@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- dsl_evaluator.gemspec
|
30
|
+
- lib/dsl_evaluator.rb
|
31
|
+
- lib/dsl_evaluator/version.rb
|
32
|
+
homepage: https://github.com/tongueroo/dsl_evaluator
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata:
|
36
|
+
homepage_uri: https://github.com/tongueroo/dsl_evaluator
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.3.0
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.1.2
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Small module to help with DSL evaluation. It produces a human-friendly backtrace
|
56
|
+
error
|
57
|
+
test_files: []
|