dsl_evaluator 0.2.5 → 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: 75e69e5dcedabbb5526f3904000165c50045a1f1629957c576132daec5c538ee
4
- data.tar.gz: 1ce18a76ee79dd815bf7284b3cf831097b48a0a863cefe1cd217b88e663d82fc
3
+ metadata.gz: '05298f76ff97b596a16a6914237d1fba17eebf8ad37f4e423ff2752074698486'
4
+ data.tar.gz: 8e4c020fafb16a52b8c029dbed7a097fd2c7532167484566f52657f04528dd6c
5
5
  SHA512:
6
- metadata.gz: d521c3290df0b2c6347a56233ae4ee4f24bc20e7c1eefe05c99cfe810ebd99fca9041ace34c5e43e2963e3a4fb52a2937310b72e5871d89d2ed1cf3459cb325f
7
- data.tar.gz: 8456cdad0388860b6011e9c849712281dc00640a562fa44f9b3d2a67cccb3a9b89ee0eb8ff52aa07aa1153dfdae001a7c9e763ba9621afb8e775a32b17405a56
6
+ metadata.gz: ac666f500e2fd198cf926d54e36151105ae4972625a8672d2b0714544106fd9d8b59ab299cc2c2f4a24a6279661ba4a2daf5b8e5ec1f760d335c116445db7cf1
7
+ data.tar.gz: 05f41d031627862d8911b626ddfe8bdc3454e35af044eb37dc4b41b10d24381df8b002fa551baba82facf42c54e5906932fb44f08ee03a29d4f2bbd830ffef78
data/CHANGELOG.md CHANGED
@@ -3,6 +3,9 @@
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
+
6
9
  ## [0.2.5] - 2022-03-06
7
10
  - [#6](https://github.com/tongueroo/dsl_evaluator/pull/6) DslEvaluator.print_code public method
8
11
 
data/README.md CHANGED
@@ -23,7 +23,20 @@ class Dsl
23
23
  end
24
24
  ```
25
25
 
26
- For other libraries where printing the code and context lines around the code is useful, you can use:
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)
27
40
 
28
41
  ```ruby
29
42
  path = "replace with path to file"
@@ -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
+
@@ -1,3 +1,3 @@
1
1
  module DslEvaluator
2
- VERSION = "0.2.5"
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
 
@@ -44,36 +45,5 @@ module DslEvaluator
44
45
  end
45
46
  memoize :config
46
47
 
47
- # So other libraries can use this method
48
- def print_code(path, line_number)
49
- check_line_number!(line_number)
50
- line_number = line_number.to_i
51
- contents = IO.read(path)
52
- content_lines = contents.split("\n")
53
- context = 5 # lines of context
54
- top, bottom = [line_number-context-1, 0].max, line_number+context-1
55
- lpad = content_lines.size.to_s.size
56
- content_lines[top..bottom].each_with_index do |line_content, index|
57
- current_line = top+index+1
58
- if current_line == line_number
59
- printf("%#{lpad}d %s\n".color(:red), current_line, line_content)
60
- else
61
- printf("%#{lpad}d %s\n", current_line, line_content)
62
- end
63
- end
64
-
65
- logger.info "Rerun with FULL_BACKTRACE=1 to see full backtrace" unless ENV['FULL_BACKTRACE']
66
- end
67
-
68
- def check_line_number!(line_number)
69
- return line_number unless line_number.is_a?(String)
70
- integer = line_number.to_i
71
- if integer == 0
72
- logger.error "ERROR: Think you accidentally passed in a String for the line_number: #{line_number}".color(:red)
73
- puts caller
74
- exit 1
75
- end
76
- end
77
-
78
48
  extend self
79
49
  end
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.5
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-03-06 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: