shell_on_ruby 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 28edd900a1181bc605bd995a730db3a6e851b4d394224ab3e6473c9f0cec5cba
4
+ data.tar.gz: 2060c870ed4a33afc888f070163d76458c24a9f22cc02709523b24aa81519734
5
+ SHA512:
6
+ metadata.gz: b3e69f05645fcb98926e0054510ba92247f61be78c2255235511da58fe23a3e9ae6249acd21136e8f65b484b86cf7231a7d34dcffb6b72aade7349a40f56ee0a
7
+ data.tar.gz: '03898d3adeb5ea7306e62002ce610d0db59d397247093f7b7e237d1dacdfdadef5f666dd0827de9bb244a3c20ae5835816ae9520bd5ca959d0104210f23d1347'
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [1.0.0] - 2023-08-16
2
+
3
+ - Initial release.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nnbnh@protonmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in shell_on_ruby.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rubocop', '~> 1.21'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 NNB
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,104 @@
1
+ <img width="100%" src="https://capsule-render.vercel.app/api?type=waving&section=header&color=DC2626&fontColor=FEF2F2&height=256&text=Shell%20on%20Ruby&desc=Convenient%20subprocess%20interface&fontAlignY=40" />
2
+
3
+ ## 💡 About
4
+
5
+ **Shell on Ruby** is a convenient subprocess interface that allows you to call any program as if it were a method.
6
+
7
+ ## 📥 Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```sh
12
+ bundle add shell_on_ruby
13
+ ```
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ ```sh
18
+ gem install shell_on_ruby
19
+ ```
20
+
21
+ <!--
22
+
23
+ ## ⌨️ Usage
24
+
25
+ In Ruby do:
26
+
27
+ ```rb
28
+ require 'shell_on_ruby'
29
+
30
+ # Or
31
+ require 'shell_on_ruby/sh' # To just require ShellOnRuby::Command and ShellOnRuby::Sh
32
+ require 'shell_on_ruby/pipe' # To just require ShellOnRuby::Command and ShellOnRuby::Pipe
33
+
34
+ # Kernel and string extensions
35
+ require 'shell_on_ruby/extensions'
36
+
37
+ # Or
38
+ require 'shell_on_ruby/sh/extensions' # To just require extensions using ShellOnRuby::Sh
39
+ require 'shell_on_ruby/pipe/extensions' # To just require extensions using ShellOnRuby::Pipe
40
+ ```
41
+
42
+ ### `Command`
43
+
44
+ TODO
45
+
46
+ ### `Sh`
47
+
48
+ TODO
49
+
50
+ ```rb
51
+ ShellOnRuby::Sh.echo('Hello, World!')
52
+ ```
53
+
54
+ ### `Pipe`
55
+
56
+ TODO
57
+
58
+ ```rb
59
+ ShellOnRuby::Pipe.echo('Hello, World!').figlet(f: 'slant').>
60
+
61
+ ShellOnRuby::Pipe.echo('fii').tr('i', 'o').figlet > $stderr
62
+
63
+ ShellOnRuby::Pipe.echo('bar').tr('r', 'z').figlet > "a/b/c/baz.txt"
64
+ ```
65
+
66
+ ### ➕ Ruby extensions
67
+
68
+ TODO
69
+
70
+ ```rb
71
+ require 'ruby_on_shell/extensions'
72
+
73
+ # TODO
74
+ figlet('Hello, World!', f: 'slant')
75
+
76
+ # TODO
77
+ Sh.ls(Dir.home)
78
+
79
+ # TODO
80
+ 'foo'.sh.figlet(f: 'slant')
81
+
82
+ # TODO
83
+ 'bar'.sh.tr('r', 'z').sh.figlet
84
+
85
+ # TODO
86
+ Pipe.yes.tr('y', 'n').>
87
+
88
+ # TODO
89
+ 'bor'.pipe.tr('o', 'a').figlet > $stderr
90
+ ```
91
+
92
+ -->
93
+
94
+ ## 💌 Credits
95
+
96
+ - [**Python Sh**](https://sh.readthedocs.io) by [Andrew](https://github.com/amoffat)
97
+
98
+ <a href="https://codeberg.org/NNB">
99
+ <img
100
+ width="100%"
101
+ src="https://capsule-render.vercel.app/api?type=waving&section=footer&color=0284C7&fontColor=F0F9FF&height=128&desc=Made%20with%20%26lt;3%20by%20NNB&descAlignY=80"
102
+ alt="Made with <3 by NNB"
103
+ />
104
+ </a>
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rubocop/rake_task'
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShellOnRuby
4
+ # A command construction, contains the command name and it's arguments / options.
5
+ class Command
6
+ attr_reader :name, :arguments, :options
7
+
8
+ def initialize(name, *args, **kargs)
9
+ @name = name.to_s
10
+ @arguments = []
11
+ @options = {}
12
+ command_args = args.flatten.map { |arg| argument_symbol_parse(arg) }
13
+ kargs.map do |key, value|
14
+ if [IO, Array].include?(key.class)
15
+ @options[key] = value
16
+ next
17
+ end
18
+
19
+ [argument_symbol_parse(key), value.to_s]
20
+ end.compact.flatten => command_kargs
21
+
22
+ @arguments = [*command_args, *command_kargs]
23
+ end
24
+
25
+ def self.method_missing(name, *args, **kargs)
26
+ new(name, *args, **kargs)
27
+ end
28
+
29
+ private
30
+
31
+ def argument_symbol_parse(symbol)
32
+ symbol.is_a?(Symbol) ? "-#{'-' if symbol.size > 1}#{symbol}" : symbol.to_s
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sh/extensions'
4
+ require_relative 'pipe/extensions'
5
+
6
+ include ShellOnRuby
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../pipe' unless defined? ShellOnRuby::Pipe
4
+
5
+ class String
6
+ # Returns a standard input of `self` for a pipeline.
7
+ def pipe
8
+ ShellOnRuby::Pipe::Input.new self
9
+ end
10
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ require_relative 'command' unless defined? ShellOnRuby::Command
6
+
7
+ module ShellOnRuby
8
+ # A shell pipeline.
9
+ class Pipe
10
+ # A standard input for a pipeline.
11
+ class Input
12
+ attr_reader :string
13
+
14
+ alias to_s string
15
+
16
+ def initialize(string)
17
+ @string = string.to_s
18
+ end
19
+
20
+ def method_missing(name, *args, **kargs)
21
+ pipe = ShellOnRuby::Pipe.new(ShellOnRuby::Command.new(name, *args, **kargs))
22
+ pipe.stdin = @string
23
+ pipe
24
+ end
25
+ end
26
+
27
+ attr_reader :commands
28
+ attr_accessor :stdin
29
+
30
+ def initialize(*commands)
31
+ commands.each do |command|
32
+ next if command.is_a?(ShellOnRuby::Command)
33
+
34
+ raise TypeError, "no implicit conversion of #{command.class} into ShellOnRuby::Command"
35
+ end
36
+
37
+ @commands = commands
38
+ end
39
+
40
+ def self.method_missing(name, *args, **kargs)
41
+ new(ShellOnRuby::Command.new(name, *args, **kargs))
42
+ end
43
+
44
+ def method_missing(name, *args, **kargs)
45
+ @commands.append(ShellOnRuby::Command.new(name, *args, **kargs))
46
+ self
47
+ end
48
+
49
+ # Execute the pipeline and redirect its output.
50
+ def >(io = nil, append: false, **options)
51
+ case io
52
+ in IO|Array|String
53
+ { out: io }
54
+ in Hash|NilClass
55
+ io || options
56
+ else
57
+ raise TypeError, "no implicit conversion of #{io.class} into IO"
58
+ end.then do |spawn_options|
59
+ spawn_pipeline(@commands, @stdin, spawn_options_parse(spawn_options, append:))
60
+ end
61
+ end
62
+
63
+ # Execute the pipeline and redirect its output (append).
64
+ def >>(io = nil, **options)
65
+ self.>(io, append: true, **options)
66
+ end
67
+
68
+ private
69
+
70
+ def spawn_options_parse(options, append: false)
71
+ options.map do |key, value|
72
+ next [key, value] unless %i[in out err].include?(key) || [Integer, IO, Array].include?(key.class)
73
+
74
+ file = [value].flatten(1)
75
+ next [key, value] unless file.first.is_a? String
76
+
77
+ file[0] = file.first.sub(/\A~\//, "#{Dir.home}/")
78
+ FileUtils.mkpath(File.dirname(file.first))
79
+ FileUtils.touch(file.first)
80
+
81
+ file[1] = append ? 'a' : 'w' unless file[1]
82
+
83
+ [key, file]
84
+ end.to_h
85
+ end
86
+
87
+ def spawn_pipeline(commands, stdin, options)
88
+ if stdin
89
+ input_read, input_write = IO.pipe
90
+ input_write.puts stdin
91
+ options[:in] = input_read
92
+ end
93
+
94
+ wait_threads = []
95
+ last_command_read = nil
96
+ commands.each.with_index do |command, index|
97
+ command_options = options.dup
98
+ command_options[:in] = last_command_read if last_command_read
99
+
100
+ if index < commands.size - 1
101
+ command_read, command_write = IO.pipe
102
+ command_options[:out] = command_write
103
+ end
104
+
105
+ pid = spawn(command.name, *command.arguments, command.options.merge(command_options))
106
+ wait_threads.append Process.detach(pid)
107
+
108
+ [last_command_read, command_write].each { |pipe| pipe&.close }
109
+ last_command_read = command_read
110
+ end
111
+
112
+ begin
113
+ wait_threads
114
+ ensure
115
+ input_write&.close
116
+ wait_threads.each(&:join)
117
+ end
118
+ rescue Interrupt => e
119
+ e
120
+ end
121
+ end
122
+
123
+ class Command
124
+ def to_pipe
125
+ ShellOnRuby::Pipe.new(self)
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../sh' unless defined? ShellOnRuby::Sh
4
+
5
+ def self.method_missing(name, *args, **kargs)
6
+ ShellOnRuby::Sh.new(ShellOnRuby::Command.new(name, *args, **kargs)).stdout
7
+ end
8
+
9
+ class String
10
+ # Returns a standard input of `self` for a shell command.
11
+ def sh
12
+ ShellOnRuby::Sh::Input.new self
13
+ end
14
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'command' unless defined? ShellOnRuby::Command
4
+
5
+ module ShellOnRuby
6
+ # A command shell.
7
+ class Sh
8
+ # A standard input for a shell command.
9
+ class Input
10
+ attr_reader :string
11
+
12
+ alias to_s string
13
+
14
+ def initialize(string)
15
+ @string = string.to_s
16
+ end
17
+
18
+ def method_missing(name, *args, **kargs)
19
+ Sh.new(ShellOnRuby::Command.new(name, *args, **kargs), stdin: @string).stdout
20
+ end
21
+ end
22
+
23
+ attr_reader :command, :stdin, :stdout
24
+
25
+ alias to_s stdout
26
+
27
+ def initialize(command, stdin: nil)
28
+ raise TypeError, "no implicit conversion of #{command.class} into ShellOnRuby::Command" unless command.is_a?(ShellOnRuby::Command)
29
+
30
+ @command = command
31
+ @stdin = stdin
32
+
33
+ IO.popen([@command.name, *@command.arguments, @command.options], 'r+') do |pipe|
34
+ pipe.write @stdin.to_s if @stdin
35
+ pipe.close_write
36
+
37
+ @stdout = pipe.read
38
+ end
39
+ end
40
+
41
+ def self.method_missing(name, *args, **kargs)
42
+ new(ShellOnRuby::Command.new(name, *args, **kargs)).stdout
43
+ end
44
+ end
45
+
46
+ class Command
47
+ def stdout
48
+ ShellOnRuby::Sh.new(self).stdout
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShellOnRuby
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ruby_on_shell/version'
4
+ require_relative 'ruby_on_shell/sh'
5
+ require_relative 'ruby_on_shell/pipe'
6
+
7
+ # Convenient subprocess interface.
8
+ module ShellOnRuby
9
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shell_on_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - NNB
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Shell on Ruby is a convenient subprocess interface that allows you to
14
+ call any program as if it were a method.
15
+ email:
16
+ - nnbnh@protonmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - README.md
21
+ - CHANGELOG.md
22
+ - LICENSE.txt
23
+ files:
24
+ - ".rubocop.yml"
25
+ - CHANGELOG.md
26
+ - CODE_OF_CONDUCT.md
27
+ - Gemfile
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - lib/ruby_on_shell.rb
32
+ - lib/ruby_on_shell/command.rb
33
+ - lib/ruby_on_shell/extensions.rb
34
+ - lib/ruby_on_shell/pipe.rb
35
+ - lib/ruby_on_shell/pipe/extensions.rb
36
+ - lib/ruby_on_shell/sh.rb
37
+ - lib/ruby_on_shell/sh/extensions.rb
38
+ - lib/ruby_on_shell/version.rb
39
+ homepage: https://codeberg.org/NNB/shell_on_ruby.rb
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://codeberg.org/NNB/shell_on_ruby.rb
44
+ source_code_uri: https://codeberg.org/NNB/shell_on_ruby.rb
45
+ changelog_uri: https://codeberg.org/NNB/shell_on_ruby.rb/src/branch/main/CHANGELOG.md
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.3.15
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Convenient subprocess interface.
65
+ test_files: []