rexe 0.0.1

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: 89d0c2488db063c926dcbdcc71311538e0aa6a6cea5508174adbb251b1583cd0
4
+ data.tar.gz: 86f099c8ef308584df094492a5e64765e3b26d3d7fc872442ec05e7963f578b0
5
+ SHA512:
6
+ metadata.gz: 8fd605f513730ec939e57a6ccc0c3b21974af416f6c0c7c1bcb033a9ed421375eecb55a6a871017dac2cffa2edc6c150c018e3532f88725bbff31505efdd59dc
7
+ data.tar.gz: 2cd444bb1b7300ac7d41238335c5c62432584b13474f87f3134f3bcd06be6d5304c81a69bbe9b278a249283fd932fa07cc2d11d8bacc18d6044a03c6fdc39b6d
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 2.0.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## rexe -- Ruby Command Line Executor
2
+
3
+ ### v0.0.1
4
+
5
+ * Initial version.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rexe.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Keith Bennett
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,100 @@
1
+ # Rexe
2
+
3
+
4
+ ## Installation
5
+
6
+ Installation can be performed by either installing the gem or copying the single executable file to your system and ensuring that it is executable:
7
+
8
+ ```gem install rexe```
9
+
10
+ or
11
+
12
+ ```
13
+ curl https://raw.githubusercontent.com/keithrbennett/rexe/master/exe/rexe > rexe
14
+ chmod +x rexe
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```
20
+
21
+ rexe -- Ruby Command Line Filter -- v0.0.1 -- https://github.com/keithrbennett/rexe
22
+
23
+ Takes standard input and runs the specified code on it, sending the result to standard output.
24
+ Your Ruby code can operate on each line individually (-ms) (the default),
25
+ or operate on the enumerator of all lines (-me). If the latter, you will probably need to
26
+ call chomp on the lines yourself to remove the trailing newlines.
27
+
28
+ Options:
29
+
30
+ -h, --help Print help and exit
31
+ -m, --mode MODE Mode with which to handle input, (-ms for string (default), -me for enumerator)
32
+ -r, --require REQUIRES Gems and built-in libraries (e.g. shellwords, yaml) to require, comma separated
33
+ -v, --[no-]verbose Verbose mode, writes to stderr
34
+
35
+ If there is an .rexerc file in your home directory, it will be run as Ruby code before processing the input.
36
+
37
+ If there is an REXE_OPTIONS environment variable, its content will be prepended to the command line
38
+ so that you can specify options implicitly (e.g. `export REXE_OPTIONS="-r awesome_print,yaml"`)
39
+
40
+ ```
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+
45
+
46
+ ## Examples
47
+
48
+ ```
49
+ ➜ rexe git:(master) ✗  ls | exe/rexe "map(&:reverse).to_s"
50
+ ["\nelifmeG", "\ntxt.ESNECIL", "\ndm.EMDAER", "\nelifekaR", "\nnib", "\nexe", "\nbil", "\ncepsmeg.cbr", "\nceps"]
51
+
52
+ ➜ rexe git:(master) ✗  uptime | exe/rexe -l split.first
53
+ 20:51
54
+
55
+
56
+ ➜ rexe git:(master) ✗  ls | exe/rexe -r json "to_a.to_json"
57
+ ["Gemfile\n","LICENSE.txt\n","README.md\n","Rakefile\n","bin\n","exe\n","lib\n","rexe.gemspec\n","spec\n"]
58
+
59
+
60
+ ➜ rexe git:(master) ✗  ls | exe/rexe -r yaml "map(&:chomp).to_a.to_yaml"
61
+ ---
62
+ - Gemfile
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - bin
67
+ - exe
68
+ - lib
69
+ - rexe.gemspec
70
+ - spec
71
+
72
+
73
+ ➜ rexe git:(master) ✗  export REXE_OPTIONS="-r yaml,awesome_print"
74
+
75
+ ➜ rexe git:(master) ✗  ls | exe/rexe "map(&:chomp).to_a.to_yaml"
76
+ ---
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin
82
+ - exe
83
+ - lib
84
+ - rexe.gemspec
85
+ - spec
86
+
87
+ ➜ rexe git:(master) ✗  ls | exe/rexe "map(&:chomp).to_a.ai"
88
+ [
89
+ [0] "Gemfile",
90
+ [1] "LICENSE.txt",
91
+ [2] "README.md",
92
+ [3] "Rakefile",
93
+ [4] "bin",
94
+ [5] "exe",
95
+ [6] "lib",
96
+ [7] "rexe.gemspec",
97
+ [8] "spec"
98
+ ]
99
+
100
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rexe"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/rexe ADDED
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # rexe - Ruby Command Line Filter
4
+ #
5
+ # Inspired by https://github.com/thisredone/rb
6
+ #
7
+
8
+ require 'optparse'
9
+ require 'shellwords'
10
+
11
+
12
+ class Rexe < Struct.new(:line_mode, :requires, :verbose)
13
+
14
+ VERSION = '0.0.1'
15
+
16
+ def initialize
17
+ self.line_mode = :string
18
+ self.requires = []
19
+ self.verbose = false
20
+ end
21
+
22
+
23
+ def help_text
24
+ <<~HEREDOC
25
+
26
+ rexe -- Ruby Command Line Filter -- v#{VERSION} -- https://github.com/keithrbennett/rexe
27
+
28
+ Takes standard input and runs the specified code on it, sending the result to standard output.
29
+ Your Ruby code can operate on each line individually (-ms) (the default),
30
+ or operate on the enumerator of all lines (-me). If the latter, you will probably need to
31
+ call chomp on the lines yourself to remove the trailing newlines.
32
+
33
+ Options:
34
+
35
+ -h, --help Print help and exit
36
+ -m, --mode MODE Mode with which to handle input, (-ms for string (default), -me for enumerator)
37
+ -r, --require REQUIRES Gems and built-in libraries (e.g. shellwords, yaml) to require, comma separated
38
+ -v, --[no-]verbose Verbose mode, writes to stderr
39
+
40
+ If there is an .rexerc file in your home directory, it will be run as Ruby code before processing the input.
41
+
42
+ If there is an REXE_OPTIONS environment variable, its content will be prepended to the command line
43
+ so that you can specify options implicitly (e.g. `export REXE_OPTIONS="-r awesome_print,yaml"`)
44
+
45
+ HEREDOC
46
+ end
47
+
48
+
49
+ def prepend_environment_options
50
+ env_opt_string = ENV['REXE_OPTIONS']
51
+ if env_opt_string
52
+ args_to_prepend = Shellwords.shellsplit(env_opt_string)
53
+ ARGV.unshift(args_to_prepend).flatten!
54
+ end
55
+ end
56
+
57
+
58
+ def parse_command_line
59
+ prepend_environment_options
60
+
61
+ OptionParser.new do |parser|
62
+
63
+ parser.on("-h", "--help", "Show help") do |_help_requested|
64
+ puts help_text
65
+ exit
66
+ end
67
+
68
+ parser.on('-m', '--mode MODE',
69
+ 'Mode with which to handle input (-ms for string (default), -me for enumerator)') do |v|
70
+ self.line_mode = case v
71
+ when 's'
72
+ self.line_mode = :string
73
+ when 'e'
74
+ self.line_mode = :enumerator
75
+ else
76
+ puts "Mode must be either 's' for string or 'e' for enumerator"
77
+ exit -1
78
+ end
79
+ end
80
+
81
+ parser.on('-r', '--require REQUIRES', 'Gems and modules to require, comma separated') do |v|
82
+ v.split(',').map(&:strip).each { |r| self.requires << r }
83
+ end
84
+
85
+ parser.on('-v', '--[no-]verbose', 'Verbose mode') do |v|
86
+ self.verbose = v
87
+ end
88
+ end.parse!
89
+ end
90
+
91
+
92
+ def load_global_config_if_exists
93
+ filespec = File.join(Dir.home, '.rexerc')
94
+ exists = File.exists?(filespec)
95
+ load(filespec) if exists
96
+ exists ? filespec : nil
97
+ end
98
+
99
+
100
+ def execute(line_or_enumerator, code)
101
+ puts line_or_enumerator.instance_eval(&code)
102
+ rescue Errno::EPIPE
103
+ exit(-13)
104
+ end
105
+
106
+
107
+ def log_if_verbose(string)
108
+ STDERR.puts(string) if verbose
109
+ end
110
+
111
+
112
+ def call
113
+ parse_command_line
114
+
115
+ log_if_verbose("Requiring #{requires}") if requires.any?
116
+ requires.each { |r| require r }
117
+
118
+ filespec = load_global_config_if_exists
119
+ log_if_verbose("Loaded #{filespec}") if filespec
120
+
121
+ source_code = "Proc.new { #{ARGV.join(' ')} }"
122
+ log_if_verbose("Source code: #{source_code}")
123
+ code = eval(source_code)
124
+
125
+ (line_mode == :string) ? STDIN.each { |l| execute(l.chomp, code) } : execute(STDIN.each_line, code)
126
+ end
127
+ end
128
+
129
+ Rexe.new.call if $0 == __FILE__
130
+
@@ -0,0 +1,3 @@
1
+ module Rexe
2
+ VERSION = "0.1.0"
3
+ end
data/lib/rexe.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "rexe/version"
2
+
3
+ module Rexe
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
data/rexe.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ lib = File.expand_path("../exe", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ load File.expand_path(File.join(File.dirname(__FILE__), 'exe', 'rexe'))
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rexe"
7
+ spec.version = Rexe::VERSION
8
+ spec.authors = ["Keith Bennett"]
9
+ spec.email = ["keithrbennett@gmail.com"]
10
+
11
+ spec.summary = %q{Ruby Command Line Executor}
12
+ spec.description = %q{Ruby Command Line Executor}
13
+ spec.homepage = "https://github.com/keithrbennett/rexe"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/keithrbennett/rexe"
23
+ spec.metadata["changelog_uri"] = "https://github.com/keithrbennett/rexe/blob/master/README.md"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 2.0"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rexe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Keith Bennett
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Ruby Command Line Executor
56
+ email:
57
+ - keithrbennett@gmail.com
58
+ executables:
59
+ - rexe
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CHANGELOG.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - exe/rexe
74
+ - lib/rexe.rb
75
+ - lib/rexe/version.rb
76
+ - rexe.gemspec
77
+ homepage: https://github.com/keithrbennett/rexe
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ allowed_push_host: https://rubygems.org/
82
+ homepage_uri: https://github.com/keithrbennett/rexe
83
+ source_code_uri: https://github.com/keithrbennett/rexe
84
+ changelog_uri: https://github.com/keithrbennett/rexe/blob/master/README.md
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Ruby Command Line Executor
104
+ test_files: []