ru 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/bin/ru +2 -2
- data/doc/help.erb +33 -0
- data/gemfiles/activesupport_3.gemfile.lock +1 -1
- data/gemfiles/activesupport_4.gemfile.lock +1 -1
- data/lib/ru/options.rb +36 -0
- data/lib/ru/process.rb +8 -0
- data/lib/ru/version.rb +1 -1
- data/spec/lib/process_spec.rb +30 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a3f2a29229d4d84159e1fc9a1ec135a2cb00a93
|
4
|
+
data.tar.gz: a6559f3dc45dc3cae09776036adffb04ea48677a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca1b9747691d20c5cfe6af9167f8923f72dffe3e6cd79da88fb92c3a13f5eef8cd477e3eb9be34e700e39791a4ef9bdbd2f17da66d64030b1e6a7a51cd0b0ae7
|
7
|
+
data.tar.gz: 17dcc47340dfb7d84c5447f47816903d60901bf874e00d30a76658434e58c0d16267e0acd0a9bd74521aa3139509ca2fd77c03a59fabf0cc38aa9b2e99dede29
|
data/README.md
CHANGED
@@ -4,6 +4,8 @@ Ruby in your shell!
|
|
4
4
|
|
5
5
|
<img src="https://raw.github.com/tombenner/ru/master/doc/logo.png" />
|
6
6
|
|
7
|
+
[<img src="https://secure.travis-ci.org/tombenner/ru.png" />](http://travis-ci.org/tombenner/ru)
|
8
|
+
|
7
9
|
Overview
|
8
10
|
--------
|
9
11
|
|
@@ -33,7 +35,7 @@ ru 'map(:to_i).sum' myfile
|
|
33
35
|
awk '{s+=$1} END {print s}' myfile
|
34
36
|
```
|
35
37
|
|
36
|
-
Any method from Ruby Core and Active Support can be used. Ru also provides
|
38
|
+
Any method from Ruby Core and Active Support can be used. Ru also provides new methods to make transformations easier. Here are some variations on the above example:
|
37
39
|
|
38
40
|
```bash
|
39
41
|
ru 'map(:to_i, 10).sum' myfile
|
@@ -92,6 +94,8 @@ $ ru '! 2 + 3'
|
|
92
94
|
5
|
93
95
|
```
|
94
96
|
|
97
|
+
The code argument is run as if it has `$stdin.each_line.map(&:chomp).` prepended to it. The result is converted to a string and printed. So, if you run `ru 'map(&:to_i).sum'`, you can think of it as running `puts $stdin.each_line.map(&:chomp).map(&:to_i).sum`.
|
98
|
+
|
95
99
|
In addition to the methods provided by Ruby Core and Active Support, Ru provides other methods for performing transformations, like `each_line`, `files`, and `grep`. See [Methods](#methods) for more.
|
96
100
|
|
97
101
|
Examples
|
@@ -266,7 +270,7 @@ The [`files`](#files) method returns an enumerable of `Ru::File`s, which are sim
|
|
266
270
|
Testing
|
267
271
|
-------
|
268
272
|
|
269
|
-
|
273
|
+
Ru is tested against Active Support 3 and 4. If you'd like to submit a PR, please be sure to use [Appraisal](https://github.com/thoughtbot/appraisal) to test your changes in both contexts:
|
270
274
|
|
271
275
|
```bash
|
272
276
|
appraisal rspec
|
data/bin/ru
CHANGED
@@ -9,8 +9,8 @@ begin
|
|
9
9
|
# If the command starts with a '!', run it without stdin
|
10
10
|
if command.present? && command.start_with?('! ')
|
11
11
|
command = command[2..-1]
|
12
|
-
# If args is empty, read from stdin
|
13
|
-
elsif args.empty?
|
12
|
+
# If args is empty and an option (e.g. -h, --help, -v, --version) isn't present, read from stdin
|
13
|
+
elsif args.empty? && !command.try(:start_with?, '-')
|
14
14
|
stdin = STDIN.read
|
15
15
|
end
|
16
16
|
process = Ru::Process.new({
|
data/doc/help.erb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Ru (version <%= version %>)
|
2
|
+
Ruby in your shell!
|
3
|
+
|
4
|
+
Ru brings Ruby's expressiveness, cleanliness, and readability to the command line. It lets you avoid looking up pesky options in man pages and Googling how to write a transformation in bash that would take you approximately 1s to write in Ruby.
|
5
|
+
|
6
|
+
For example, to center a file's lines, use String#center:
|
7
|
+
ru 'map(:center, 80)' myfile
|
8
|
+
|
9
|
+
To sum the lines of a list of integers:
|
10
|
+
ru 'map(:to_i).sum' myfile
|
11
|
+
|
12
|
+
Ru reads from stdin:
|
13
|
+
$ echo "2\n3" | ru 'map(:to_i).sum'
|
14
|
+
5
|
15
|
+
$ cat myfile | ru 'map(:to_i).sum'
|
16
|
+
5
|
17
|
+
|
18
|
+
Or from file(s):
|
19
|
+
$ ru 'map(:to_i).sum' myfile
|
20
|
+
5
|
21
|
+
$ ru 'map(:to_i).sum' myfile myfile
|
22
|
+
10
|
23
|
+
|
24
|
+
You can also run Ruby code without any input by prepending a `! `:
|
25
|
+
$ ru '! 2 + 3'
|
26
|
+
5
|
27
|
+
|
28
|
+
The code argument is run as if it has `$stdin.each_line.map(&:chomp).` prepended to it. The result is converted to a string and printed. So, if you run `ru 'map(:to_i).sum'`, you can think of it as `puts $stdin.each_line.map(&:chomp).map(:to_i).sum`.
|
29
|
+
|
30
|
+
In addition to the methods provided by Ruby Core and Active Support, Ru provides other methods for performing transformations, like `each_line`, `files`, and `grep`.
|
31
|
+
|
32
|
+
To read more, see the README:
|
33
|
+
https://github.com/tombenner/ru
|
data/lib/ru/options.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
# TODO: If more options are added, we should use OptionParser
|
4
|
+
module Ru
|
5
|
+
class Options
|
6
|
+
def exists?(option_key)
|
7
|
+
options[option_key].present?
|
8
|
+
end
|
9
|
+
|
10
|
+
def run(option_key, option_value=nil)
|
11
|
+
send(options[option_key], *option_value)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def options
|
17
|
+
{
|
18
|
+
'-h' => :get_help,
|
19
|
+
'--help' => :get_help,
|
20
|
+
'-v' => :get_version,
|
21
|
+
'--version' => :get_version
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_help
|
26
|
+
namespace = OpenStruct.new(version: Ru::VERSION)
|
27
|
+
template_path = ::File.expand_path("../../../doc/help.erb", __FILE__)
|
28
|
+
template = ::File.open(template_path).read
|
29
|
+
ERB.new(template).result(namespace.instance_eval { binding })
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_version
|
33
|
+
Ru::VERSION
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/ru/process.rb
CHANGED
@@ -7,9 +7,17 @@ module Ru
|
|
7
7
|
if @command.kind_of?(String) && @command.start_with?('[')
|
8
8
|
@command = 'to_stdout' + @command
|
9
9
|
end
|
10
|
+
@options = Options.new
|
10
11
|
end
|
11
12
|
|
12
13
|
def run
|
14
|
+
if @command.nil?
|
15
|
+
STDERR.puts @options.run('--help')
|
16
|
+
exit 1
|
17
|
+
end
|
18
|
+
if @options.exists?(@command)
|
19
|
+
return @options.run(@command)
|
20
|
+
end
|
13
21
|
paths = @args
|
14
22
|
if @stdin.blank? && paths.present?
|
15
23
|
@stdin = paths.map { |path| ::File.open(path).read }.join("\n")
|
data/lib/ru/version.rb
CHANGED
data/spec/lib/process_spec.rb
CHANGED
@@ -63,5 +63,35 @@ describe Ru::Process do
|
|
63
63
|
expect { out = run(lines, 'foo') }.to raise_error(NoMethodError)
|
64
64
|
end
|
65
65
|
end
|
66
|
+
|
67
|
+
describe "options" do
|
68
|
+
context "-h" do
|
69
|
+
it "shows help" do
|
70
|
+
out = run('', '--help')
|
71
|
+
out.should include('Ruby in your shell!')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "--help" do
|
76
|
+
it "shows help" do
|
77
|
+
out = run('', '-h')
|
78
|
+
out.should include('Ruby in your shell!')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "-v" do
|
83
|
+
it "shows the version" do
|
84
|
+
out = run('', '--version')
|
85
|
+
out.should == Ru::VERSION
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "--version" do
|
90
|
+
it "shows the version" do
|
91
|
+
out = run('', '--version')
|
92
|
+
out.should == Ru::VERSION
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
66
96
|
end
|
67
97
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Benner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- README.md
|
69
69
|
- Rakefile
|
70
70
|
- bin/ru
|
71
|
+
- doc/help.erb
|
71
72
|
- doc/logo.png
|
72
73
|
- gemfiles/activesupport_3.gemfile
|
73
74
|
- gemfiles/activesupport_3.gemfile.lock
|
@@ -77,6 +78,7 @@ files:
|
|
77
78
|
- lib/ru/array.rb
|
78
79
|
- lib/ru/file.rb
|
79
80
|
- lib/ru/iterator.rb
|
81
|
+
- lib/ru/options.rb
|
80
82
|
- lib/ru/process.rb
|
81
83
|
- lib/ru/version.rb
|
82
84
|
- ru.gemspec
|