dorian-eval 1.0.0 → 1.1.1

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bin/eval +34 -10
  4. data/lib/dorian/eval.rb +51 -22
  5. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5c8633bae8b5db55cbe5bc38d09e06f04f87618c6665b3a781cb6d284b50cea
4
- data.tar.gz: 91b5dce1c22902b23aa196b8dc5200ea7712c211b49e7b2fca47ac32c4ebece1
3
+ metadata.gz: bcfc5e74c01f95c84c211b93c2d0a581ed989a5985d4e8432350073a6811777f
4
+ data.tar.gz: b6e28dff4e13d842b53997cb3b00931ebbe2272b2f0bab9ebd7fd91fcb43a18a
5
5
  SHA512:
6
- metadata.gz: 73e30d2c70ec85b556b0f901129edf3d60ba5922e1973c8fb3c618ecbc1ecba945227bbb5f98fe38cea1618f72b2ae8a5063b9cace5ba5c9a35a8ba519a137d1
7
- data.tar.gz: cd8f9f463821b3aeb0de6efd8d05de5942bab9fb968b6df117e42ab462ab129a159d8657960d3c35cecea98df382a776f06438c0a5664202db54bee8220489bf
6
+ metadata.gz: 41b97220af4b806ba42f40b54262d947d075367737392cc49b685748080fe12d7d0030307466a122adf2eafc1375bdd4b631672d181c6344858c99a00ed6961c
7
+ data.tar.gz: 8b9728add481168d18ad811780560def8c2b26cc8001b65a6472d46acfa69d6edaed8b5985d117632fee71ad4581786427a89a95bf52c0c4bb2ab29abde45b1a
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.1
data/bin/eval CHANGED
@@ -4,19 +4,42 @@
4
4
  require "dorian/arguments"
5
5
  require_relative "../lib/dorian/eval"
6
6
 
7
- parsed = Dorian::Arguments.parse(
8
- it: { type: :string, alias: :i },
9
- debug: { alias: :d },
10
- stdout: { aliases: [:out, :o], default: true },
11
- stderr: { aliases: [:err, :e], default: true },
12
- colorize: { aliases: [:color, :c], default: true },
13
- version: { alias: :v }, help: { alias: :h }
14
- )
7
+ parsed =
8
+ Dorian::Arguments.parse(
9
+ it: {
10
+ type: :string,
11
+ alias: :i
12
+ },
13
+ debug: {
14
+ alias: :d
15
+ },
16
+ stdout: {
17
+ aliases: %i[out o],
18
+ default: true
19
+ },
20
+ stderr: {
21
+ aliases: %i[err e],
22
+ default: true
23
+ },
24
+ colorize: {
25
+ aliases: %i[color c],
26
+ default: true
27
+ },
28
+ rails: {
29
+ alias: :r
30
+ },
31
+ version: {
32
+ alias: :v
33
+ },
34
+ help: {
35
+ alias: :h
36
+ }
37
+ )
15
38
 
16
39
  abort parsed.help if parsed.options.help
17
40
 
18
41
  if parsed.options.version
19
- abort File.read(File.expand_path("../../VERSION", __FILE__))
42
+ abort File.read(File.expand_path("../VERSION", __dir__))
20
43
  end
21
44
 
22
45
  Dorian::Eval.eval(
@@ -25,5 +48,6 @@ Dorian::Eval.eval(
25
48
  debug: parsed.options.debug,
26
49
  stdout: parsed.options.stdout,
27
50
  stderr: parsed.options.stderr,
28
- colorize: parsed.options.colorize
51
+ colorize: parsed.options.colorize,
52
+ rails: parsed.options.rails
29
53
  )
data/lib/dorian/eval.rb CHANGED
@@ -1,12 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Dorian
2
4
  class Eval
3
- attr_reader :ruby, :it, :debug, :stdout, :stderr, :colorize
5
+ attr_reader :ruby, :it, :debug, :stdout, :stderr, :colorize, :rails
4
6
 
5
- COLORS = {
6
- red: "\e[31m",
7
- green: "\e[32m",
8
- reset: "\e[0m"
9
- }
7
+ COLORS = { red: "\e[31m", green: "\e[32m", reset: "\e[0m" }.freeze
10
8
 
11
9
  def initialize(
12
10
  *args,
@@ -15,7 +13,8 @@ class Dorian
15
13
  debug: false,
16
14
  stdout: true,
17
15
  stderr: true,
18
- colorize: false
16
+ colorize: false,
17
+ rails: false
19
18
  )
20
19
  @ruby = ruby.empty? ? args.join(" ") : ruby
21
20
  @it = it
@@ -23,6 +22,7 @@ class Dorian
23
22
  @stdout = !!stdout
24
23
  @stderr = !!stderr
25
24
  @colorize = !!colorize
25
+ @rails = !!rails
26
26
  end
27
27
 
28
28
  def self.eval(...)
@@ -33,15 +33,7 @@ class Dorian
33
33
  read_out, write_out = IO.pipe
34
34
  read_err, write_err = IO.pipe
35
35
 
36
- Process.spawn(
37
- RbConfig.ruby,
38
- "-e",
39
- "it = #{it.inspect}",
40
- "-e",
41
- ruby,
42
- out: write_out,
43
- err: write_err
44
- )
36
+ spawn(write_out:, write_err:)
45
37
 
46
38
  write_out.close
47
39
  write_err.close
@@ -50,8 +42,8 @@ class Dorian
50
42
  err = ""
51
43
 
52
44
  while !read_out.eof? || !read_err.eof?
53
- out << gets(read_out, color: :green, print: stdout?).to_s
54
- err << gets(read_err, color: :red, print: stderr?).to_s
45
+ out += gets(read_out, print: stdout?, method: :puts).to_s
46
+ err += gets(read_err, color: :red, print: stderr?, method: :warn).to_s
55
47
  end
56
48
 
57
49
  [out, err]
@@ -73,21 +65,58 @@ class Dorian
73
65
  !!colorize
74
66
  end
75
67
 
68
+ def rails?
69
+ !!rails
70
+ end
71
+
76
72
  def prefix
77
73
  debug? ? "[#{it}] " : ""
78
74
  end
79
75
 
80
- def gets(read, color: nil, print: true)
76
+ def full_ruby
77
+ full_ruby = <<~RUBY
78
+ it = #{it.inspect}
79
+ #{ruby}
80
+ RUBY
81
+
82
+ full_ruby = <<~RUBY if rails?
83
+ require "#{Dir.pwd}/config/environment"
84
+ #{full_ruby}
85
+ RUBY
86
+
87
+ full_ruby
88
+ end
89
+
90
+ def spawn(write_out:, write_err:)
91
+ Process.spawn(
92
+ RbConfig.ruby,
93
+ "-e",
94
+ full_ruby,
95
+ out: write_out,
96
+ err: write_err
97
+ )
98
+ end
99
+
100
+ def gets(read, color: nil, print: true, method: :puts)
81
101
  original_string = read.gets
82
102
  return unless original_string
103
+
83
104
  string = original_string.rstrip
84
- string = colorize? ? colorize_string(string, color) : string
85
- puts([prefix, string].join) if print
105
+ string = colorize_string(string, color) if colorize? && color
106
+
107
+ if method == :puts && print
108
+ puts [prefix, string].join
109
+ elsif method == :warn && print
110
+ warn [prefix, string].join
111
+ end
112
+
86
113
  original_string
87
114
  end
88
115
 
89
116
  def colorize_string(string, color)
90
- [COLORS.fetch(color), string, COLORS.fetch(:reset)]
117
+ return string unless color
118
+
119
+ [COLORS.fetch(color), string, COLORS.fetch(:reset)].join
91
120
  end
92
121
  end
93
122
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-eval
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-24 00:00:00.000000000 Z
11
+ date: 2024-08-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: evaluates ruby
14
14
  email: dorian@dorianmarie.com
@@ -32,9 +32,9 @@ require_paths:
32
32
  - lib
33
33
  required_ruby_version: !ruby/object:Gem::Requirement
34
34
  requirements:
35
- - - ">="
35
+ - - '='
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 3.3.4
38
38
  required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - ">="