erbish 0.0.1

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
+ SHA1:
3
+ metadata.gz: aaf3b2dc4b2485799093e46354dabb437cddbc9f
4
+ data.tar.gz: 56f9fab3df52393b18753d02be0f38b245dd5f17
5
+ SHA512:
6
+ metadata.gz: 2489ef110a2a126f1ec8a7ed706cd76792ec2d485be9f458b670b5983493d4918da241ee737a0f190110b01665fb46d91bdc173e4ed02762e7322730094a3ade
7
+ data.tar.gz: 80f11d7e65f284faa2e6d3e9af1a2f488b5fb75dfd98f4c1abd0d56365283e79bdfe8b3261d7fd17cd57e96f9332c62524d810ad7ba0bf64ac4e9e57993a4eac
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in erbish.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 wanabe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Erbish
2
+
3
+ ERB In SHell
4
+
5
+ ## Installation
6
+
7
+ Install:
8
+
9
+ $ gem install erbish
10
+
11
+ ## Usage
12
+
13
+ Execute command with ERB notation.
14
+
15
+ $ erbish '<%= "data".succ.succ.succ.succ %>'
16
+ Thu Nov 13 21:09:10 JST 2014
17
+
18
+ Argument that start '%' is parsed as '<#{arg}%>'
19
+
20
+ $ erbish echo 5 + 4 = %=5+4
21
+ 5 + 4 = 9
22
+
23
+ '%'ed argument can be start/end with '-' to join before/after args.
24
+
25
+ $ erbish echo B -%=42/10- U
26
+ B4U
27
+
28
+ $ erbish ruby -e 'p ARGV' "%3.times do |i|-" %=i -%end- 3
29
+ ["0123"]
30
+
31
+ ';' is interpreted as separator.
32
+
33
+ $ erbish "% 3.times do |i|" echo %=i \; %end
34
+ 0
35
+ 1
36
+ 2
37
+
38
+ option '-d' is for debug.
39
+ option '-p' is for just parse args, without execute.
40
+
41
+ $ erbish -d echo 1\; echo %=4**5
42
+ raw argv : ["echo", "1;", "echo", "%=4**5"]
43
+ parsed argv : ["echo", "1;", "echo", "<%=4**5%>"]
44
+ ERBed argv : ["echo", "1;", "echo", "1024"]
45
+ result args : [["echo", "1"], ["echo", "1024"]]
46
+ system : ["echo", "1"]
47
+ 1
48
+ system : ["echo", "1024"]
49
+ 1024
50
+
51
+ $ erbish -p echo 1\; echo %=4**5
52
+ echo 1
53
+ echo 1024
54
+
55
+ Alias and function might be helpful.
56
+
57
+ $ cat ~/.bashrc
58
+ ...
59
+ alias es='erbish'
60
+ alias ee='erbish echo'
61
+ e () { while read f; do $f; done < <(erbish -p "$@"); }
62
+
63
+ $ ee '%= "".encoding'
64
+ ASCII-8BIT
65
+
66
+ $ e '%= "23".tr("0-9", "a-j")' /
67
+ $ pwd
68
+ /
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/[my-github-username]/erbish/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/erbish ADDED
@@ -0,0 +1,5 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "erbish"
4
+
5
+ Erbish.run(*ARGV)
data/erbish.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'erbish/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "erbish"
8
+ spec.version = Erbish::VERSION
9
+ spec.authors = ["wanabe"]
10
+ spec.email = ["s.wanabe@gmail.com"]
11
+ spec.summary = %q{ERB In SHell}
12
+ spec.description = %q{Command executor with Erb}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
data/lib/erbish.rb ADDED
@@ -0,0 +1,99 @@
1
+ require "erbish/version"
2
+ require "erb"
3
+ require "shellwords"
4
+
5
+ class Erbish
6
+ SEPARATOR = "\0\0".b
7
+ JOINT = "\xff\xff".b
8
+ DEBUG_TYPE_SIZE = 14
9
+
10
+ class << self
11
+ def run(*argv)
12
+ while case argv.first
13
+ when /^-d/
14
+ debug = true
15
+ when /^-p/
16
+ noexec = true
17
+ end
18
+ argv.shift
19
+ end
20
+ erbish = Erbish.new(debug, noexec)
21
+ if argv.empty?
22
+ while l = STDIN.gets
23
+ erbish.run(*Shellwords.split(l))
24
+ end
25
+ else
26
+ erbish.run(*argv)
27
+ end
28
+ end
29
+ end
30
+
31
+ def initialize(debug = false, noexec = false)
32
+ @binding = binding
33
+ @debug = !!debug
34
+ @noexec = noexec
35
+ end
36
+
37
+ def parse(*argv)
38
+ return if argv.empty?
39
+ argv = argv.map(&:b)
40
+ debug_p "raw argv", *argv
41
+ argv.each do |arg|
42
+ if /^([%\-])?%(.*?)(-)?\z/m =~ arg
43
+ joint = $3
44
+ case $1
45
+ when "%"
46
+ arg[0, 1] = ""
47
+ when "-"
48
+ arg.replace "#{JOINT}<%#{$2}%>"
49
+ when nil
50
+ arg.replace "<#{arg}%>"
51
+ joint = true
52
+ end
53
+ arg << JOINT if $3
54
+ end
55
+ end
56
+ debug_p "parsed argv", *argv
57
+ argbuf = ERB.new(argv.join(SEPARATOR).b, nil, "-").result(@binding)
58
+ [/#{JOINT}#{SEPARATOR}#{JOINT}/, # %arg1- -%arg2
59
+ /(?:\A|#{SEPARATOR})#{JOINT}|#{JOINT}(?:#{SEPARATOR}|\z)/ # erbish
60
+ ].each_with_index do |pat, i|
61
+ argbuf.gsub!(pat, "")
62
+ end
63
+ argbuf << SEPARATOR << JOINT
64
+ argv = argbuf.split(SEPARATOR)
65
+ argv.pop
66
+ debug_p "ERBed argv", *argv
67
+ new_line = true
68
+ args = []
69
+ argv.each do |arg|
70
+ args.push [] if new_line
71
+ new_line = arg.sub!(/;\z/, "") && arg !~ /;\z/
72
+ next if new_line && arg.empty?
73
+ args.last.push arg
74
+ end
75
+ debug_p("result args", *args)
76
+ args
77
+ end
78
+
79
+ def run(*argv)
80
+ if @noexec
81
+ line = parse(*argv).map do |argv|
82
+ argv.join(" ")
83
+ end.join("\n")
84
+ debug_p("result", *line)
85
+ puts line
86
+ else
87
+ parse(*argv).each do |argv|
88
+ debug_p "system", *argv
89
+ system(*argv)
90
+ end
91
+ end
92
+ end
93
+
94
+ private
95
+
96
+ def debug_p(type, *objs)
97
+ STDERR.printf "%-#{DEBUG_TYPE_SIZE}s: %s\n", type, objs.map(&:inspect).join(", ") if @debug
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ class Erbish
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erbish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - wanabe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-04 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: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ description: Command executor with Erb
42
+ email:
43
+ - s.wanabe@gmail.com
44
+ executables:
45
+ - erbish
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/erbish
55
+ - erbish.gemspec
56
+ - lib/erbish.rb
57
+ - lib/erbish/version.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: ERB In SHell
82
+ test_files: []