dsl_evaluator 0.2.3 → 0.3.0

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: b8435bd7d87d467ba317984fd74bd31c8af789524228a5ae8895d50856efb1d2
4
- data.tar.gz: d8297543d1c5e02f2c3a2333c5f45417318f34bb32d6d10175a3b09d2dbada53
3
+ metadata.gz: '05298f76ff97b596a16a6914237d1fba17eebf8ad37f4e423ff2752074698486'
4
+ data.tar.gz: 8e4c020fafb16a52b8c029dbed7a097fd2c7532167484566f52657f04528dd6c
5
5
  SHA512:
6
- metadata.gz: ac18d9e60a195db5cb84befd2ebb49eee7891f6de1b8faed08c686cceb9149d047322fa90eaebccfe11e9a66eb23c026c34eaa0ec988eadfcf30a8925e0a7822
7
- data.tar.gz: 0c06aaffebf2bd326e86ddd5f45bc1dbd89a5d330e43e70b3371df3e93cb3f202b7b9b370a8ae3d35698e745b70dfe418a949d12fc4bebbf34bde9d25070225f
6
+ metadata.gz: ac666f500e2fd198cf926d54e36151105ae4972625a8672d2b0714544106fd9d8b59ab299cc2c2f4a24a6279661ba4a2daf5b8e5ec1f760d335c116445db7cf1
7
+ data.tar.gz: 05f41d031627862d8911b626ddfe8bdc3454e35af044eb37dc4b41b10d24381df8b002fa551baba82facf42c54e5906932fb44f08ee03a29d4f2bbd830ffef78
data/CHANGELOG.md CHANGED
@@ -3,6 +3,15 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## [0.3.0] - 2022-03-14
7
+ - [#7](https://github.com/tongueroo/dsl_evaluator/pull/7) Print code improvements
8
+
9
+ ## [0.2.5] - 2022-03-06
10
+ - [#6](https://github.com/tongueroo/dsl_evaluator/pull/6) DslEvaluator.print_code public method
11
+
12
+ ## [0.2.4] - 2022-03-03
13
+ - print error message as part of trace
14
+
6
15
  ## [0.2.3] - 2022-02-27
7
16
  - [#5](https://github.com/tongueroo/dsl_evaluator/pull/5) add zeitwerk dependency
8
17
 
data/README.md CHANGED
@@ -14,7 +14,6 @@ DslEvaluator.configure do |config|
14
14
  config.root = Lono.root
15
15
  end
16
16
 
17
-
18
17
  class Dsl
19
18
  include DslEvaluator
20
19
  def build
@@ -24,6 +23,27 @@ class Dsl
24
23
  end
25
24
  ```
26
25
 
26
+ ## Print Code Helper
27
+
28
+ For other libraries where printing the code and context lines around the code is useful, you can use `DslEvaluator.print_code`.
29
+
30
+ The `print_code` method understands "polymorphic" arguments.
31
+
32
+ 1. If the caller line info is part of a standard ruby backtrace line. Example of this is in ufo [helpers/ecr.rb](https://github.com/boltops-tools/ufo/blob/master/lib/ufo/task_definition/helpers/ecr.rb)
33
+
34
+ ```ruby
35
+ call_line = ufo_config_call_line
36
+ DslEvaluator.print_code(call_line)
37
+ ```
38
+
39
+ 2. If the caller line info is custom. Example of this is in ufo [erb/yaml.rb](https://github.com/boltops-tools/ufo/blob/9247b77c6ad2a3a6307155a2a130308a24668333/lib/ufo/task_definition/erb/yaml.rb#L16)
40
+
41
+ ```ruby
42
+ path = "replace with path to file"
43
+ line_number = "replace with line number. usually can get the exception.message"
44
+ DslEvaluator.print_code(path, line_number)
45
+ ```
46
+
27
47
  ## Installation
28
48
 
29
49
  Add this line to your application's Gemfile:
@@ -0,0 +1,65 @@
1
+ class DslEvaluator::Printer
2
+ module Concern
3
+ # So other libraries can use this method
4
+ # The `print_code` method understands "polymorphic" arguments.
5
+ # See README.md
6
+ def print_code(*args)
7
+ if args.size == 2 # print_code(path, line_number)
8
+ path, line_number = args
9
+ else # print_code(caller_line)
10
+ # IE: .ufo/config/web/dev.rb:10:in `block in evaluate_file'
11
+ # User passed in a "standard" ruby backtrace call line
12
+ # windows: "C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/terraspace-1.1.1/lib/terraspace/builder.rb:34:in `build'"
13
+ # linux: "/home/ec2-user/.rvm/gems/ruby-3.0.3/gems/terraspace-1.1.1/lib/terraspace/compiler/dsl/syntax/mod.rb:4:in `<module:Mod>'"
14
+ caller_line = args[0]
15
+ parts = caller_line.split(':')
16
+ is_windows = caller_line.match(/^[a-zA-Z]:/) # windows vs linux
17
+ calling_file = is_windows ? parts[1] : parts[0]
18
+ line_number = is_windows ? parts[2] : parts[1]
19
+ path = calling_file
20
+ end
21
+
22
+ check_line_number!(line_number)
23
+ line_number = line_number.to_i
24
+
25
+ logger.info "Here's the original caller line from:"
26
+ logger.info pretty_path(path).color(:green)
27
+
28
+ contents = IO.read(path)
29
+ content_lines = contents.split("\n")
30
+ context = 5 # lines of context
31
+ top, bottom = [line_number-context-1, 0].max, line_number+context-1
32
+ lpad = content_lines.size.to_s.size
33
+ content_lines[top..bottom].each_with_index do |line_content, index|
34
+ current_line = top+index+1
35
+ if current_line == line_number
36
+ printf("%#{lpad}d %s\n".color(:red), current_line, line_content)
37
+ else
38
+ printf("%#{lpad}d %s\n", current_line, line_content)
39
+ end
40
+ end
41
+
42
+ logger.info "Rerun with FULL_BACKTRACE=1 to see full backtrace" unless ENV['FULL_BACKTRACE']
43
+ end
44
+
45
+ def check_line_number!(line_number)
46
+ return line_number unless line_number.is_a?(String)
47
+ integer = line_number.to_i
48
+ if integer == 0
49
+ logger.error "ERROR: Think you accidentally passed in a String for the line_number: #{line_number}".color(:red)
50
+ puts caller
51
+ exit 1
52
+ end
53
+ end
54
+
55
+ def pretty_path(path)
56
+ path.sub("#{Dir.pwd}/",'').sub(/^\.\//,'')
57
+ end
58
+
59
+ # Replace HOME with ~ - different from the main pretty_path
60
+ def pretty_home(path)
61
+ path.sub(ENV['HOME'], '~')
62
+ end
63
+ end
64
+ end
65
+
@@ -9,25 +9,14 @@ module DslEvaluator
9
9
  info = error_info
10
10
  path = info[:path]
11
11
  line_number = info[:line_number].to_i
12
-
12
+ logger.error "ERROR: #{@error.message}".color(:red)
13
13
  logger.error "Error evaluating #{pretty_path(path)}".color(:red)
14
14
  logger.error "Here's the line with the error:\n\n"
15
+ print_code(path, line_number)
16
+ end
15
17
 
16
- contents = IO.read(path)
17
- content_lines = contents.split("\n")
18
- context = 5 # lines of context
19
- top, bottom = [line_number-context-1, 0].max, line_number+context-1
20
- lpad = content_lines.size.to_s.size
21
- content_lines[top..bottom].each_with_index do |line_content, index|
22
- current_line = top+index+1
23
- if current_line == line_number
24
- printf("%#{lpad}d %s\n".color(:red), current_line, line_content)
25
- else
26
- printf("%#{lpad}d %s\n", current_line, line_content)
27
- end
28
- end
29
-
30
- logger.info "Rerun with FULL_BACKTRACE=1 to see full backtrace" unless ENV['FULL_BACKTRACE']
18
+ def print_code(path, line_number)
19
+ DslEvaluator.print_code(path, line_number)
31
20
  end
32
21
 
33
22
  def error_info
@@ -1,3 +1,3 @@
1
1
  module DslEvaluator
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/dsl_evaluator.rb CHANGED
@@ -11,6 +11,7 @@ DslEvaluator::Autoloader.setup
11
11
 
12
12
  module DslEvaluator
13
13
  extend Memoist
14
+ include Printer::Concern
14
15
 
15
16
  class Error < StandardError; end
16
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsl_evaluator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-27 00:00:00.000000000 Z
11
+ date: 2022-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -89,6 +89,7 @@ files:
89
89
  - lib/dsl_evaluator/autoloader.rb
90
90
  - lib/dsl_evaluator/logger.rb
91
91
  - lib/dsl_evaluator/printer.rb
92
+ - lib/dsl_evaluator/printer/concern.rb
92
93
  - lib/dsl_evaluator/version.rb
93
94
  homepage: https://github.com/tongueroo/dsl_evaluator
94
95
  licenses: