dorian-eval 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e5c8633bae8b5db55cbe5bc38d09e06f04f87618c6665b3a781cb6d284b50cea
4
+ data.tar.gz: 91b5dce1c22902b23aa196b8dc5200ea7712c211b49e7b2fca47ac32c4ebece1
5
+ SHA512:
6
+ metadata.gz: 73e30d2c70ec85b556b0f901129edf3d60ba5922e1973c8fb3c618ecbc1ecba945227bbb5f98fe38cea1618f72b2ae8a5063b9cace5ba5c9a35a8ba519a137d1
7
+ data.tar.gz: cd8f9f463821b3aeb0de6efd8d05de5942bab9fb968b6df117e42ab462ab129a159d8657960d3c35cecea98df382a776f06438c0a5664202db54bee8220489bf
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/bin/eval ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "dorian/arguments"
5
+ require_relative "../lib/dorian/eval"
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
+ )
15
+
16
+ abort parsed.help if parsed.options.help
17
+
18
+ if parsed.options.version
19
+ abort File.read(File.expand_path("../../VERSION", __FILE__))
20
+ end
21
+
22
+ Dorian::Eval.eval(
23
+ ruby: parsed.arguments.join(" "),
24
+ it: parsed.options.it,
25
+ debug: parsed.options.debug,
26
+ stdout: parsed.options.stdout,
27
+ stderr: parsed.options.stderr,
28
+ colorize: parsed.options.colorize
29
+ )
@@ -0,0 +1,93 @@
1
+ class Dorian
2
+ class Eval
3
+ attr_reader :ruby, :it, :debug, :stdout, :stderr, :colorize
4
+
5
+ COLORS = {
6
+ red: "\e[31m",
7
+ green: "\e[32m",
8
+ reset: "\e[0m"
9
+ }
10
+
11
+ def initialize(
12
+ *args,
13
+ ruby: "",
14
+ it: "",
15
+ debug: false,
16
+ stdout: true,
17
+ stderr: true,
18
+ colorize: false
19
+ )
20
+ @ruby = ruby.empty? ? args.join(" ") : ruby
21
+ @it = it
22
+ @debug = !!debug
23
+ @stdout = !!stdout
24
+ @stderr = !!stderr
25
+ @colorize = !!colorize
26
+ end
27
+
28
+ def self.eval(...)
29
+ new(...).eval
30
+ end
31
+
32
+ def eval
33
+ read_out, write_out = IO.pipe
34
+ read_err, write_err = IO.pipe
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
+ )
45
+
46
+ write_out.close
47
+ write_err.close
48
+
49
+ out = ""
50
+ err = ""
51
+
52
+ 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
55
+ end
56
+
57
+ [out, err]
58
+ end
59
+
60
+ def debug?
61
+ !!debug
62
+ end
63
+
64
+ def stdout?
65
+ !!stdout
66
+ end
67
+
68
+ def stderr?
69
+ !!stderr
70
+ end
71
+
72
+ def colorize?
73
+ !!colorize
74
+ end
75
+
76
+ def prefix
77
+ debug? ? "[#{it}] " : ""
78
+ end
79
+
80
+ def gets(read, color: nil, print: true)
81
+ original_string = read.gets
82
+ return unless original_string
83
+ string = original_string.rstrip
84
+ string = colorize? ? colorize_string(string, color) : string
85
+ puts([prefix, string].join) if print
86
+ original_string
87
+ end
88
+
89
+ def colorize_string(string, color)
90
+ [COLORS.fetch(color), string, COLORS.fetch(:reset)]
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dorian/eval"
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dorian-eval
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dorian Marié
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: evaluates ruby
14
+ email: dorian@dorianmarie.com
15
+ executables:
16
+ - eval
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - VERSION
21
+ - bin/eval
22
+ - lib/dorian-eval.rb
23
+ - lib/dorian/eval.rb
24
+ homepage: https://github.com/dorianmariecom/dorian-eval
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ rubygems_mfa_required: 'true'
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.5.11
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: evaluates ruby
48
+ test_files: []