libexec 0.1.3 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77b2818bcb0419a2a77206010ffd1b9c8fb968c94c8edc5eab9885874371f388
4
- data.tar.gz: 2179f593890be22ce4fead91f43074eec53230ef90443c869450393f4c2bcea7
3
+ metadata.gz: 8705278823a3fe7c5edd0913befd194389f692ed9833396138a0a97d75b6f72b
4
+ data.tar.gz: 0eccd36d324534284495454e5116890c4620a63657d8fa44dca2fec36f837b49
5
5
  SHA512:
6
- metadata.gz: 3f59c196e116b776292e56d1f32655221681416e64f9fdca4f5a84975eb37d3b80d37142422ebc65fb56b8ff92caceba23b7c0aa9d9dfd0708bff7f4706d10e2
7
- data.tar.gz: 835c4005bdfe3b68d481582c6b30a882779ceba0352b1d09e6126245025082a322417c087c7cf99f5990c6fca0909051dcb1ade42b8463f6272947e3a55a821e
6
+ metadata.gz: 8062c95b812bf5f3e5d7d491984bfdfd842a54a4e1446af536476832c720dd9f1d8807c9c4b70936163a8d14b282eb703f6cec072eabb33ba8e54fb836c62e91
7
+ data.tar.gz: 3bc5590a2d359db0d1efbea7ccef0f447ecb8ecbe74e9e20bebfce3ec049b7a33ef8ab5b994febac6fbfc276185e7b438ee6d7d71cc9814aa97a6c21e721bf7d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- libexec (0.1.3)
4
+ libexec (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/libexec/exec.rb CHANGED
@@ -2,38 +2,75 @@
2
2
 
3
3
  module Libexec
4
4
  module Exec
5
- def run(cmd)
6
- # https://docs.ruby-lang.org/en/master/IO.html#method-c-popen
7
- IO.popen(cmd) do |pipe|
5
+ # wrapper for Ruby IO.popen
6
+ # {https://docs.ruby-lang.org/en/master/IO.html#method-c-popen link} <br>
7
+ # cmd first grammar <br>
8
+ # conv argv by _ruby_style_cmd
9
+ #
10
+ # @return [nil]
11
+ def run(*argv, env: {}, mode: "r", **opts)
12
+ cmd = _ruby_style_cmd(argv)
13
+
14
+ IO.popen(env, cmd, mode, opts) do |pipe|
8
15
  print pipe.gets until pipe.eof?
9
16
  end
10
17
  nil
11
18
  end
12
19
 
13
- def each_line(cmd, &block)
14
- # https://docs.ruby-lang.org/en/master/IO.html#method-i-each_line
15
- IO.popen(cmd) do |pipe|
20
+ # Run code block in IO.popen <br>
21
+ # inspired from Ruby IO.each_line
22
+ # {https://docs.ruby-lang.org/en/master/IO.html#method-i-each_line link}
23
+ def each_line(*argv, env: {}, mode: "r", **opts, &block)
24
+ cmd = _ruby_style_cmd(argv)
25
+
26
+ IO.popen(env, cmd, mode, opts) do |pipe|
16
27
  pipe.each_line(&block)
17
28
  end
18
29
  end
19
30
 
20
- def by_ls_1(dir)
31
+ # wrapper for `ls -1` which will be often used
32
+ #
33
+ # @param [Array] argv
34
+ # @return [Array] line.chomp
35
+ def by_ls_1(*argv, **opts)
21
36
  arr = []
22
- each_line("ls -1 #{dir}") do |line|
37
+ each_line("ls", "-1", argv, opts: opts) do |line|
23
38
  arr.push line.chomp
24
39
  end
25
40
  arr
26
41
  end
27
42
 
28
- def each_ls_1(dir, &block)
29
- each_line("ls -1 #{dir}", &block)
43
+ # Operate each line result by yourself in `ls -1`
44
+ #
45
+ # @param [Array] argv
46
+ def each_ls_1(*argv, **opts, &block)
47
+ each_line("ls", "-1", argv, opts: opts, &block)
30
48
  end
31
49
 
32
- def code(cmd, *opt, **args)
33
- catch_error = !opt.empty? || args[:catch_error] || false
34
- code = opt[0] || args[:code] || 1
50
+ # wrapper for Process.spawn
51
+ # {https://docs.ruby-lang.org/en/master/Process.html#method-c-last_status link}
52
+ #
53
+ # @return [Integer]
54
+ def code(*argv, **opts)
55
+ catch_error = opts[:catch_error]
56
+ code = opts[:code]
57
+
58
+ if argv.size < 2
59
+ catch_error ||= false
60
+ code ||= 1
61
+
62
+ cmd = _js_style_cmd(argv)
63
+ else
64
+ is_integer = argv.last.instance_of?(Integer)
65
+
66
+ catch_error ||= is_integer
67
+ code ||= (argv.last if is_integer) || 1
68
+
69
+ args = argv.clone
70
+ args.pop
71
+ cmd = is_integer ? _js_style_cmd(args) : _js_style_cmd(argv)
72
+ end
35
73
 
36
- # https://docs.ruby-lang.org/en/master/Process.html#method-c-last_status
37
74
  Process.spawn(cmd)
38
75
  Process.wait
39
76
 
@@ -48,10 +85,68 @@ module Libexec
48
85
  127
49
86
  end
50
87
 
51
- def output(cmd)
52
- # https://docs.ruby-lang.org/en/master/Kernel.html#method-i-60
88
+ # wrapper for Kernel.`command`
89
+ # {https://docs.ruby-lang.org/en/master/Kernel.html#method-i-60 link}
90
+ #
91
+ # @note Only recommend to use when get single line output
92
+ # @return [String] output.chomp
93
+ def output(*argv)
94
+ cmd = _js_style_cmd(argv)
95
+
53
96
  output = `#{cmd}`
54
97
  output.chomp
55
98
  end
99
+
100
+ private
101
+
102
+ # cmd must be unix command String <br>
103
+ # no " ", not Array <br>
104
+ # not support cmd with opt like `ls -1`
105
+ #
106
+ # @param [Array] argv
107
+ # @return [String | Array]
108
+ #
109
+ # @since 0.2.1
110
+ def _ruby_style_cmd(argv)
111
+ cmd, *args = argv
112
+
113
+ unless args.empty?
114
+ arr = [cmd]
115
+ args.each do |arg|
116
+ # https://docs.ruby-lang.org/en/master/Kernel.html#method-i-class
117
+ if arg.instance_of?(Array)
118
+ arr.concat(arg)
119
+ elsif arg.instance_of?(String)
120
+ arr.push(arg)
121
+ end
122
+ end
123
+ cmd = arr
124
+ end
125
+ cmd
126
+ end
127
+
128
+ # in JS style, all arg connect with " " <br>
129
+ # so cmd like `ls -1` is OK
130
+ #
131
+ # @param [Array] argv
132
+ # @return [String | Array]
133
+ #
134
+ # @since 0.2.1
135
+ def _js_style_cmd(argv)
136
+ cmd, *args = argv
137
+
138
+ unless args.empty?
139
+ arr = [cmd]
140
+ args.each do |arg|
141
+ if arg.instance_of?(Array)
142
+ arr.push(%('#{arg.join(" ")}'))
143
+ elsif arg.instance_of?(String)
144
+ arr.push(arg)
145
+ end
146
+ end
147
+ cmd = arr.join(" ")
148
+ end
149
+ cmd
150
+ end
56
151
  end
57
152
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Libexec
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/libexec.rb CHANGED
@@ -4,8 +4,5 @@ require_relative "libexec/version"
4
4
  require_relative "libexec/exec"
5
5
 
6
6
  module Libexec
7
- class Error < StandardError; end
8
- # Your code goes here...
9
-
10
7
  extend Libexec::Exec
11
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libexec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - initdc
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-25 00:00:00.000000000 Z
11
+ date: 2022-12-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Wrapper for ruby exec shell command.
14
14
  email: