rubbish 0.2.200128 → 1.1.221208

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -51
  3. data/lib/rubbish.rb +33 -42
  4. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4cf8bce4e93ddb99df77ab62e0223ef4dd3c3d7a9397ecfee07a7630a13606d
4
- data.tar.gz: 6d5be379098c58eacac6b74ec5eedb5909f30be9bee03aeaf094f6726c04023a
3
+ metadata.gz: d6a9c2f7befeba1ee6244441d88f39abb7f3f955108913885c4a785d55b325dd
4
+ data.tar.gz: b7f136c136d7ab3711186afdc2334276432347010ddf899e09a4c85c9790a43c
5
5
  SHA512:
6
- metadata.gz: de35ecaa12da6ca6c3888afd9271344eaec0501baa669a58b0c90a4c1aa730868697b68c5b850cefe1ca62ab3780c255a87109868e63ff785228ec7733b33431
7
- data.tar.gz: 197de12725801e594fc1a5718f15e41e6998c57bd198c30d9f9d9fb44586c9ad3d00e34d763e5d7786a2fcf80811a1d919f5e4e164851706c84eae44c9066d03
6
+ metadata.gz: d8d6e4ce9dab28c64dde5a56f91178ad0dba87f2f20087722cad5c988733416f26633c95277bf5b14f6574c78d8537cd27e6fbc1609115bb9a370a9a0557d6e9
7
+ data.tar.gz: 1c9ba07b399f41aaff250b10fb09b151ce2122a65211b4cb8cc74fa8d9eee540ba06232b1ea0a7b9baeedb00e028df6991ff3420d5533c50f181c14d294dab34
data/README.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # rubbish
2
2
 
3
+ * [VERSION 1.1.221208](https://github.com/carlosjhr64/rubbish/releases)
4
+ * [github](https://www.github.com/carlosjhr64/rubbish)
5
+ * [rubygems](https://rubygems.org/gems/rubbish)
6
+
7
+ ## Deprecation notice:
8
+
9
+ This is my last `Rubbish` update.
10
+ I've merged this project with [Helpema](https://www.github.com/carlosjhr64/helpema).
11
+
3
12
  ## DESCRIPTION:
4
13
 
5
14
  A ruby-ish way to go to the shell.
@@ -10,64 +19,75 @@ Bash and Fish aware.
10
19
 
11
20
  Although it's easy enough to access the system's default shell from ruby,
12
21
  you may want to run a specific shell like fish for it's unique features:
13
-
14
- require 'rubbish'
15
-
16
- # Fish features the double splat...
17
- # reads output and returns it.
18
- ls_lib = Rubbish.fish('ls lib/**.rb').split #=> ["lib/good.rb", "lib/good/bad.rb", "lib/good/ugly.rb"]
19
-
20
- # Run a bash script, have it output to STDOUT(read: false)...
21
- Rubbish.bash <<-BASH, read: false
22
- echo "Date/Time now: "
23
- date
24
- BASH
25
-
26
- # Rubbish will accept :fish and :bash calls, but
27
- # you can add other shells... say... ruby?
28
- # sure, why not:
29
- Rubbish::SHELL_VERSION[:ruby] = '2.0' # Ensures it's ruby ~>2.0.
30
- answer = Rubbish.ruby <<-RUBY #=> "11"
31
- a = 5
32
- b = 6
33
- puts a+b
34
- RUBY
35
-
22
+ ```ruby
23
+ require 'rubbish'
24
+
25
+ # Fish features the double splat...
26
+ # reads output and returns it:
27
+ Rubbish.fish('ls lib/**.rb').split #=> ["lib/rubbish.rb"]
28
+ # The '?' method return weather or not the command succeeded.
29
+ # Fish's true and false commands:
30
+ Rubbish.fish?('true') #=> true
31
+ Rubbish.fish?('false') #=> false
32
+
33
+ # Run a bash script, have it output to STDOUT...
34
+ Rubbish.bash? <<-BASH
35
+ echo "Date/Time now: "
36
+ date
37
+ BASH
38
+ #=> true
39
+
40
+ # Rubbish will accept :fish and :bash calls, but
41
+ # you can add other shells... say... ruby?
42
+ # sure, why not:
43
+ Rubbish::SHELL_VERSION[:ruby] = '3.0' # Ensures it's ruby ~>3.0.
44
+ answer = Rubbish.ruby <<-RUBY
45
+ a = 5
46
+ b = 6
47
+ puts a+b
48
+ RUBY
49
+ #=> "11\n"
50
+ ```
36
51
  ## FEATURES:
37
52
 
38
- Read(read: true) by default:
39
-
40
- * Rubbish.shell('bash','echo "OK"') #=> OK
41
- * Rubbish.shell('fish'){|p| p.puts 'echo "OK"'} #=> OK
42
- * Rubbish.bash('echo "OK"') #=> OK
43
- * Rubbish.fish('true') #=> ''
44
-
45
- Exit status instead when not reading(read: false):
46
-
47
- * Rubbish.fish('true', read: false) #=> true
48
- * Rubbish.fish('false', read: false) #=> false
49
-
53
+ ```ruby
54
+ Rubbish.shell('echo "OK"') #=> "OK\n"
55
+ Rubbish.shell(shell:'fish'){|p| p.puts 'echo "OK"'} #=> "OK\n"
56
+ Rubbish.bash('echo "OK"') #=> "OK\n"
57
+ Rubbish.fish('true') #=> ""
58
+ ```
59
+ Exit status instead:
60
+ ```ruby
61
+ Rubbish.shell('true', shell:'fish', read:false) #=> true
62
+ Rubbish.shell('false', shell:'fish', read:false) #=> false
63
+ Rubbish.fish?('true') #=> true
64
+ Rubbish.fish?('false') #=> false
65
+ ```
50
66
  An edge case:
51
-
52
- require 'rubbish'
53
- b = nil
54
- a = Rubbish.bash do |p|
55
- p.puts "echo Good"
56
- p.puts "echo Day!"
57
- b = p.gets
58
- end
59
- puts b # Good
60
- puts a # Day!
61
-
67
+ ```ruby
68
+ b = nil
69
+ a = Rubbish.bash do |p|
70
+ p.puts "echo Good"
71
+ p.puts "echo Day!"
72
+ b = p.gets
73
+ end
74
+ b #=> "Good\n"
75
+ a #=> "Day!\n"
76
+ ```
77
+ Passing options to the "shell":
78
+ ```ruby
79
+ Rubbish::SHELL_VERSION[:date] = nil
80
+ Rubbish.date date:'2021-09-21', R:true #=> "Tue, 21 Sep 2021 00:00:00 -0700\n"
81
+ ```
62
82
  ## INSTALL:
63
-
64
- $ gem install rubbish
65
-
66
- == LICENSE:
83
+ ```console
84
+ $ gem install rubbish
85
+ ```
86
+ ## LICENSE:
67
87
 
68
88
  (The MIT License)
69
89
 
70
- Copyright (c) 2020 carlosjhr64
90
+ Copyright (c) 2022 CarlosJHR64
71
91
 
72
92
  Permission is hereby granted, free of charge, to any person obtaining
73
93
  a copy of this software and associated documentation files (the
data/lib/rubbish.rb CHANGED
@@ -1,28 +1,10 @@
1
1
  module Rubbish
2
- VERSION = '0.2.200128'
2
+ VERSION = '1.1.221208'
3
3
  SHELL_VERSION = {bash: nil, fish: nil}
4
+ # This is a contraction of Shellwords.escape function
5
+ SHELLWORDS_ESCAPE = lambda{|w|w.gsub(/[^\w\-.,:+\/@\n]/,'\\\\\\&').gsub(/\n/,"'\n'")}
4
6
 
5
- # Fine!
6
- # Let there be Rubbish.args!!!
7
- def self.args(*args)
8
- script, read = nil, true
9
- until args.empty?
10
- arg = args.shift
11
- case arg
12
- when String
13
- script = arg
14
- when TrueClass, FalseClass
15
- read = arg
16
- when Hash
17
- script = arg[:script] if arg.has_key? :script
18
- read = arg[:read] if arg.has_key? :read
19
- end
20
- end
21
- return script, read
22
- end
23
-
24
- def self.shell(shell, *args, &block)
25
- script, read = Rubbish.args(*args)
7
+ def self.shell(script=nil, shell:'bash', read:true, &block)
26
8
  IO.popen(shell, (read)? 'w+' : 'w') do |pipe|
27
9
  # No matter what, we know we're going to write first.
28
10
  writing = true
@@ -32,38 +14,47 @@ module Rubbish
32
14
  pipe.close_write
33
15
  writing = false
34
16
  end
35
- if block
36
- block.call(pipe)
37
- # close if we were writing
38
- pipe.close_write if writing
39
- end
17
+ block.call(pipe) if block
18
+ # close if we were writing
19
+ pipe.close_write if writing
40
20
  # Read if reading
41
21
  read = pipe.read if read
42
22
  end
43
23
  read || $?.to_i==0
44
24
  end
45
25
 
46
- def self.method_missing(shell, *args, &block)
47
- if args.length.between?(0,2) and SHELL_VERSION.has_key? shell
48
- if minimum = SHELL_VERSION[shell]
49
- minimum = Gem::Version.new minimum
50
- if version = `#{shell} --version`.scan(/\d+\.\d+\.\d+/).first
51
- version = Gem::Version.new version
52
- bumped = minimum.bump
53
- raise "Need #{shell} version ~> #{minimum}" unless version >= minimum and version < bumped
54
- # need to only check once
55
- SHELL_VERSION[shell] = nil
26
+ def self.method_missing(cmd, *args, **kw, &block)
27
+ cmd=cmd[0..-2].to_sym unless read=(cmd[-1]!='?')
28
+ return super unless SHELL_VERSION.has_key?(cmd) and args.length.between?(0,1)
29
+ if minimum = SHELL_VERSION[cmd]
30
+ minimum = Gem::Version.new minimum
31
+ if version = `#{cmd} --version`.match(/\d+\.\d+\.\d+/)&.match(0)
32
+ version = Gem::Version.new version
33
+ bumped = minimum.bump
34
+ unless version >= minimum and version < bumped
35
+ raise "Need #{cmd} version ~> #{minimum}"
36
+ end
37
+ # need to only check once
38
+ SHELL_VERSION[cmd] = nil
39
+ else
40
+ raise "Could not get the #{cmd} version"
41
+ end
42
+ end
43
+ command = [cmd.to_s]
44
+ if kw.length > 0
45
+ kw.each do |k,w|
46
+ next if not w
47
+ if w==true
48
+ command << "#{k.length>1? '--':'-'}#{k}"
56
49
  else
57
- raise "Could not get the #{shell} version"
50
+ command << "--#{k}=#{w}"
58
51
  end
59
52
  end
60
- script, read = Rubbish.args(*args)
61
- return Rubbish.shell(shell.to_s, script, read, &block)
62
53
  end
63
- super
54
+ script = args[0]
55
+ return Rubbish.shell(script, shell:command, read:read, &block)
64
56
  end
65
57
  end
66
-
67
58
  # Requires:
68
59
  #`ruby`
69
60
  #`bash`
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubbish
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.200128
4
+ version: 1.1.221208
5
5
  platform: ruby
6
6
  authors:
7
- - carlosjhr64
7
+ - CarlosJHR64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-28 00:00:00.000000000 Z
11
+ date: 2022-12-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  A ruby-ish way to go to the shell.
@@ -40,10 +40,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  requirements:
43
- - 'ruby: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]'
44
- - 'bash: GNU bash, version 5.0.11(1)-release (x86_64-redhat-linux-gnu)'
45
- - 'fish: fish, version 3.0.2'
46
- rubygems_version: 3.1.2
43
+ - 'ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]'
44
+ - 'bash: GNU bash, version 5.1.4(1)-release (aarch64-unknown-linux-gnu)'
45
+ - 'fish: fish, version 3.5.1'
46
+ rubygems_version: 3.3.7
47
47
  signing_key:
48
48
  specification_version: 4
49
49
  summary: A ruby-ish way to go to the shell.