exec2 0.1.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: e762e84e7985adfc74fc99b9431aa9dcefb61aac0fecfcb630cb70c85b386591
4
+ data.tar.gz: 8e5840e8dec71aff66a3a50a551234927a8b28287886b14c0e9c9a66c125508e
5
+ SHA512:
6
+ metadata.gz: 9220572e94a2588772117cd3413c1270a145dc7f917db74d78e61c41c6d1b68a482052836bf4a12b519e6fede7279e098968b448267d66adb8c65f95c0801a92
7
+ data.tar.gz: 441a8ab422cdf66badf72ee5f83c9c8f169e2bdbb0b9e209b90c677fd54273f0177b9170a0e5100cdc4ae8078385a06767412336470fc071e1455aa81fb1ef78
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ Exclude:
5
+ - lib/slop/slop.rb
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ Enabled: true
13
+ EnforcedStyle: double_quotes
14
+
15
+ Layout/LineLength:
16
+ Max: 120
17
+
18
+ Layout/FirstHashElementIndentation:
19
+ EnforcedStyle: consistent
20
+
21
+ Layout/EndAlignment:
22
+ EnforcedStyleAlignWith: start_of_line
23
+
24
+ Layout/MultilineMethodCallIndentation:
25
+ EnforcedStyle: indented
26
+
27
+ Layout/CaseIndentation:
28
+ EnforcedStyle: end
29
+
30
+ Metrics/BlockLength:
31
+ Exclude:
32
+ - spec/**/*
33
+
34
+ Metrics/MethodLength:
35
+ Enabled: false
36
+
37
+ Metrics/AbcSize:
38
+ Enabled: false
39
+
40
+ Metrics/CyclomaticComplexity:
41
+ Enabled: false
42
+
43
+ Metrics/PerceivedComplexity:
44
+ Enabled: false
@@ -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 hey@vito.io. 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,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in exec2.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "simplecov"
15
+
16
+ gem "simplecov_json_formatter"
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ exec2 (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ docile (1.4.0)
12
+ json (2.6.3)
13
+ parallel (1.22.1)
14
+ parser (3.2.1.0)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.1.1)
17
+ rake (13.0.6)
18
+ regexp_parser (2.7.0)
19
+ rexml (3.2.5)
20
+ rspec (3.12.0)
21
+ rspec-core (~> 3.12.0)
22
+ rspec-expectations (~> 3.12.0)
23
+ rspec-mocks (~> 3.12.0)
24
+ rspec-core (3.12.1)
25
+ rspec-support (~> 3.12.0)
26
+ rspec-expectations (3.12.2)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-mocks (3.12.3)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-support (3.12.0)
33
+ rubocop (1.45.1)
34
+ json (~> 2.3)
35
+ parallel (~> 1.10)
36
+ parser (>= 3.2.0.0)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ regexp_parser (>= 1.8, < 3.0)
39
+ rexml (>= 3.2.5, < 4.0)
40
+ rubocop-ast (>= 1.24.1, < 2.0)
41
+ ruby-progressbar (~> 1.7)
42
+ unicode-display_width (>= 2.4.0, < 3.0)
43
+ rubocop-ast (1.24.1)
44
+ parser (>= 3.1.1.0)
45
+ ruby-progressbar (1.11.0)
46
+ simplecov (0.22.0)
47
+ docile (~> 1.1)
48
+ simplecov-html (~> 0.11)
49
+ simplecov_json_formatter (~> 0.1)
50
+ simplecov-html (0.12.3)
51
+ simplecov_json_formatter (0.1.4)
52
+ unicode-display_width (2.4.2)
53
+
54
+ PLATFORMS
55
+ arm64-darwin-22
56
+
57
+ DEPENDENCIES
58
+ exec2!
59
+ rake (~> 13.0)
60
+ rspec (~> 3.0)
61
+ rubocop (~> 1.21)
62
+ simplecov
63
+ simplecov_json_formatter
64
+
65
+ BUNDLED WITH
66
+ 2.4.2
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Exec2
2
+
3
+ Exec2 is a small wrapper around `Process::spawn`. This gem exists because I
4
+ usually follow this same pattern in multiple projects, and copy-paste is tedious.
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add exec2
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install exec2
15
+
16
+ ## Usage
17
+
18
+ Executing a command using `Exec2.exec2` returns its stdout and stderr contents:
19
+
20
+ ```ruby
21
+ Exec2.exec2("git rev-parse --show-toplevel")
22
+ # => ["/Users/victorgama/Developer/exec2\n", ""]
23
+ ```
24
+
25
+ `Exec2.exec` is an alias of `Exec2.exec2(...).first` in case you don't care
26
+ about what `stderr` may hold:
27
+
28
+ ```ruby
29
+ Exec2.exec2("git rev-parse --show-toplevel")
30
+ # => "/Users/victorgama/Developer/exec2\n"
31
+ ```
32
+
33
+ Commands exiting with a non-zero status raises an `Exec2::Error`:
34
+ ```ruby
35
+ Exec2.exec2("git bla")
36
+ # /Users/victorgama/Developer/exec2/lib/exec2.rb:71:in `exec2': Process `git bla' exited with status 1: git: 'bla' is not a git command. See 'git --help'. (Exec2::Error)
37
+ #
38
+ # The most similar command is
39
+ # blame
40
+ #
41
+ # from (irb):4:in `<main>'
42
+ # from bin/console:15:in `<main>'
43
+ ```
44
+
45
+ For convenience, `Exec2::Error` contains the following fields: `:stdout, :stderr, :status, :cmd, :env`
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/heyvito/exec2. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/heyvito/exec2/blob/master/CODE_OF_CONDUCT.md).
56
+
57
+ ## Code of Conduct
58
+
59
+ Everyone interacting in the Exec2 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/heyvito/exec2/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Exec2
4
+ VERSION = "0.1.0"
5
+ end
data/lib/exec2.rb ADDED
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "exec2/version"
4
+
5
+ # Exec2 provides the pretty much the same interface as Process.spawn, but with
6
+ # extra whistles.
7
+ class Exec2
8
+ # Error contains information about a process that exited with a non-zero
9
+ # status
10
+ class Error < StandardError
11
+ attr_reader :stdout, :stderr, :status, :cmd, :env
12
+
13
+ def initialize(status, stdout, stderr, cmd, env)
14
+ super("Process `#{cmd}' exited with status #{status}: #{stdout} #{stderr}")
15
+ @status = status
16
+ @stdout = stdout
17
+ @stderr = stderr
18
+ @cmd = cmd
19
+ @env = env
20
+ end
21
+ end
22
+
23
+ # exec2 executes a given cmd with a set of options, and returns its stdout
24
+ # and stderr output, or raises an error.
25
+ def self.exec2(cmd, **options)
26
+ out_reader, out_writer = IO.pipe
27
+ err_reader, err_writer = IO.pipe
28
+ in_reader, in_writer = IO.pipe
29
+
30
+ env = (options.delete(:env) || {}).to_h { |k, v| [k.to_s, v.to_s] }
31
+ options.delete(:chdir) if options.fetch(:chdir, nil).nil?
32
+
33
+ env["PATH"] = ENV.fetch("PATH", "") unless env.key? "PATH"
34
+
35
+ opts = {
36
+ unsetenv_others: options.delete(:isolate_env) || false,
37
+ out: out_writer.fileno,
38
+ err: err_writer.fileno,
39
+ in: in_reader.fileno
40
+ }.merge options
41
+
42
+ pid = if cmd.is_a? Array
43
+ Process.spawn(env, *cmd, **opts)
44
+ else
45
+ Process.spawn(env, cmd, **opts)
46
+ end
47
+
48
+ in_writer.close
49
+ mut = Mutex.new
50
+ cond = ConditionVariable.new
51
+
52
+ status = nil
53
+ Thread.new do
54
+ _pid, status = Process.wait2(pid)
55
+ mut.synchronize { cond.signal }
56
+ end
57
+
58
+ stdout = nil
59
+ stderr = nil
60
+ out_thread = Thread.new { stdout = out_reader.read }
61
+ err_thread = Thread.new { stderr = err_reader.read }
62
+ mut.synchronize { cond.wait(mut, 0.1) } while status.nil?
63
+
64
+ out_writer.close
65
+ out_thread.join
66
+
67
+ err_writer.close
68
+ err_thread.join
69
+
70
+ out_reader.close
71
+ err_reader.close
72
+
73
+ raise Error.new(status.exitstatus, stdout, stderr, cmd, env) unless status.success?
74
+
75
+ [stdout, stderr]
76
+ end
77
+
78
+ # exec behaves just like exec2, but only returns stdout
79
+ def self.exec(cmd, **options)
80
+ exec2(cmd, **options).first
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exec2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor Gama
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Small interface wrapping Process::spawn
14
+ email:
15
+ - hey@vito.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - README.md
26
+ - Rakefile
27
+ - lib/exec2.rb
28
+ - lib/exec2/version.rb
29
+ homepage: https://github.com/heyvito/exec2
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/heyvito/exec2
34
+ source_code_uri: https://github.com/heyvito/exec2
35
+ changelog_uri: https://github.com/heyvito/exec2
36
+ rubygems_mfa_required: 'true'
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 2.6.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.4.2
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Small interface wrapping Process::spawn
56
+ test_files: []