hysh 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f13194edb86a917872e0c88c067ce33b7e18dcf
4
- data.tar.gz: 4e743290d7746ad6fabc62deeb058f8ecfe9effd
3
+ metadata.gz: d86830d2aaa21d1050bd816890f6c360a3d83030
4
+ data.tar.gz: bc9daeed25ab30da69434e3e4d403aa0a6326f9e
5
5
  SHA512:
6
- metadata.gz: 7380c1dd990f66b059b4969f0d5a9e29165a5a207a9281c584d97497b74613f704c736a4cc1117876a1ef8ff3f6a0248500fdd46ae17d099a6d2b859852cc02c
7
- data.tar.gz: 6186d9e71eb8148c0dc3d99755e2f89fb666818ef14c6e483cba93f73ce6f2808224ea5a3181c2769418acbcbac04459766b61b4804f825b881ce060d0d889bb
6
+ metadata.gz: b49c4784afa508b78886932e8bc804978d3f1995ea16964f89b3922ad1af193d3507add307b5dfb0b1519b3678c0d2ca83bfd255b10c97e59ffa9057ae3a2c8d
7
+ data.tar.gz: 5f36ac3189789678578268614b5ca6f3aba0536d80001deb2c7449e4fa21d60cf46b9ef8ca5b6d5464b43a0d93d535fdfc1f93d928a75fb98e1a7a41b6754140
@@ -9,7 +9,7 @@ Common Lisp: https://github.com/hying-caritas/hysh).
9
9
 
10
10
  == Example
11
11
 
12
- def dpkg_installed(package_names = nil)
12
+ def dpkg_installed1(package_names = nil)
13
13
  Hysh.out_lines ->{
14
14
  Hysh.pipe ['dpkg', '-l'],
15
15
  if package_names
@@ -20,9 +20,9 @@ Common Lisp: https://github.com/hying-caritas/hysh).
20
20
  }
21
21
  end
22
22
 
23
- or write the filter in Ruby.
23
+ or write the filter in Ruby,
24
24
 
25
- def dpkg_installed(package_names = nil)
25
+ def dpkg_installed2(package_names = nil)
26
26
  Hysh.out_lines ->{
27
27
  Hysh.pipe ['dpkg', '-l'] {
28
28
  proc_line = if package_names
@@ -41,6 +41,24 @@ or write the filter in Ruby.
41
41
  }
42
42
  end
43
43
 
44
+ or use the hysh_script,
45
+
46
+ def dpkg_installed3(package_names = nil)
47
+ hysh_script {
48
+ if package_names
49
+ pipe ['dpkg', '-l'] {
50
+ filter_line { |l|
51
+ package_names.any? { |pkg|
52
+ l.index pkg
53
+ } && l
54
+ }
55
+ }
56
+ else
57
+ dpkg '-l'
58
+ end
59
+ }
60
+ end
61
+
44
62
  Compared with Kernel.system, HYSH provides different coding style for
45
63
  IO redirection, Environment manipulating, Setting current directory,
46
64
  pipe line support without shell, and writing pipeline filter in Ruby.
@@ -23,6 +23,8 @@ module Hysh
23
23
  @@on_command_error = IGNORE
24
24
  # :startdoc:
25
25
 
26
+ module_function
27
+
26
28
  # :section: Common Utilities
27
29
 
28
30
  # :call-seq:
@@ -32,7 +34,7 @@ module Hysh
32
34
  # +val+, then run the block, return the return value of the block.
33
35
  # Restore the original value of the variable upon returning.
34
36
  # Multiple pairs of +var_name+ and +val+ can be specified.
35
- def self.with_set_globals(*var_vals)
37
+ def with_set_globals(*var_vals)
36
38
  orig_var_vals = var_vals.each_slice(2).map { |var, val|
37
39
  svar = var.to_s
38
40
  unless svar.start_with? '$'
@@ -64,7 +66,7 @@ module Hysh
64
66
  # programs, then run the block, return the return value of the
65
67
  # block. Restore the original value of the variable and cancel the
66
68
  # arrangement to external program redirections upon returning.
67
- def self.with_redirect_to(fd, var, io, &b)
69
+ def with_redirect_to(fd, var, io, &b)
68
70
  @@redirections.push([fd, io]) if fd
69
71
  if var
70
72
  with_set_globals(var, io, &b)
@@ -83,7 +85,7 @@ module Hysh
83
85
  # then run the block, return the return value of the block. Restore
84
86
  # the original value of the $stdin and cancel the arrangement to
85
87
  # external program redirections upon returning.
86
- def self.with_redirect_stdin_to(io, &b)
88
+ def with_redirect_stdin_to(io, &b)
87
89
  with_redirect_to(0, :$stdin, io, &b)
88
90
  end
89
91
 
@@ -95,7 +97,7 @@ module Hysh
95
97
  # then run the block, return the return value of the block. Restore
96
98
  # the original value of the $stdout and cancel the arrangement to
97
99
  # external program redirections upon returning.
98
- def self.with_redirect_stdout_to(io, &b)
100
+ def with_redirect_stdout_to(io, &b)
99
101
  with_redirect_to(1, :$stdout, io, &b)
100
102
  end
101
103
 
@@ -107,7 +109,7 @@ module Hysh
107
109
  # then run the block, return the return value of the block. Restore
108
110
  # the original value of the $stderr and cancel the arrangement to
109
111
  # external program redirections upon returning.
110
- def self.with_redirect_stderr_to(io, &b)
112
+ def with_redirect_stderr_to(io, &b)
111
113
  with_redirect_to(2, :$stderr, io, &b)
112
114
  end
113
115
 
@@ -121,7 +123,7 @@ module Hysh
121
123
  # block, return the return value of the block. Restore the original
122
124
  # value of the $stdin and cancel the arrangement to external program
123
125
  # redirections upon returning.
124
- def self.with_redirect_stdin_to_file(*args, &b)
126
+ def with_redirect_stdin_to_file(*args, &b)
125
127
  File.open(*args) { |f|
126
128
  with_redirect_stdin_to f, &b
127
129
  }
@@ -137,7 +139,7 @@ module Hysh
137
139
  # block, return the return value of the block. Restore the original
138
140
  # value of the $stdout and cancel the arrangement to external
139
141
  # program redirections upon returning.
140
- def self.with_redirect_stdout_to_file(*args, &b)
142
+ def with_redirect_stdout_to_file(*args, &b)
141
143
  if args.size == 1
142
144
  args.push "w"
143
145
  end
@@ -156,7 +158,7 @@ module Hysh
156
158
  # block, return the return value of the block. Restore the original
157
159
  # value of the $stderr and cancel the arrangement to external
158
160
  # program redirections upon returning.
159
- def self.with_redirect_stderr_to_file(*args, &b)
161
+ def with_redirect_stderr_to_file(*args, &b)
160
162
  if args.size == 1
161
163
  args.push "w"
162
164
  end
@@ -165,7 +167,7 @@ module Hysh
165
167
  }
166
168
  end
167
169
 
168
- def self.__out_io(args, options, proc_arg) # :nodoc:
170
+ def __out_io(args, options, proc_arg) # :nodoc:
169
171
  Tempfile.open(TEMP_BASE) { |tempf|
170
172
  tempf.unlink
171
173
  ret = nil
@@ -190,7 +192,7 @@ module Hysh
190
192
  # collected output string and the return value of the block or the
191
193
  # function or exit success status of the external program as a two
192
194
  # element array. Restore stdout redirection upon returning.
193
- def self.out_s(*args, &blk)
195
+ def out_s(*args, &blk)
194
196
  __out_io(*__parse_args(args, blk)) { |tempf|
195
197
  tempf.read
196
198
  }
@@ -203,7 +205,7 @@ module Hysh
203
205
  #
204
206
  # Same as out_s, except the collected output string are right
205
207
  # stripped before return.
206
- def self.out_ss(*args_in, &blk)
208
+ def out_ss(*args_in, &blk)
207
209
  s, ret = out_s(*args_in, &blk)
208
210
  [s.rstrip, ret]
209
211
  end
@@ -229,7 +231,7 @@ module Hysh
229
231
  # +Process.spawn+. Feed each line of output to the block as +line+.
230
232
  # Return the exit success status of the forked sub-process or the
231
233
  # external program. Restore stdout redirection upon returning.
232
- def self.out_lines(*args_in, &blk)
234
+ def out_lines(*args_in, &blk)
233
235
  args, options, proc_arg = __parse_args args_in
234
236
  if block_given?
235
237
  __popen(nil, true, nil, args, options, proc_arg) { |pid, stdin, stdout, stderr|
@@ -250,7 +252,7 @@ module Hysh
250
252
  # out_err_s(command...[, options]) -> [string, true or false]
251
253
  #
252
254
  # Same as out_s, except collect output of stderr too.
253
- def self.out_err_s(*args_in, &blk)
255
+ def out_err_s(*args_in, &blk)
254
256
  args, options, proc_arg = __parse_args args_in, blk
255
257
  Tempfile.open(TEMP_BASE) { |tempf|
256
258
  tempf.unlink
@@ -273,12 +275,12 @@ module Hysh
273
275
  #
274
276
  # Same as out_err_s, except the collected output string are right
275
277
  # stripped before return.
276
- def self.out_err_ss(*args_in, &blk)
278
+ def out_err_ss(*args_in, &blk)
277
279
  s, ret = out_err_s(*args_in, &blk)
278
280
  [s.rstrip. ret]
279
281
  end
280
282
 
281
- def self.__in_io(args, options, proc_arg) # :nodoc:
283
+ def __in_io(args, options, proc_arg) # :nodoc:
282
284
  Tempfile.open(TEMP_BASE) { |tempf|
283
285
  tempf.unlink
284
286
  yield tempf
@@ -301,7 +303,7 @@ module Hysh
301
303
  # +Process.spawn+. Return the return value of the block or the
302
304
  # function or the exit success status of the external program.
303
305
  # Restore stdin redirection upon returning.
304
- def self.in_s(s, *args_in, &blk)
306
+ def in_s(s, *args_in, &blk)
305
307
  args, options, proc_arg = __parse_args args_in, blk
306
308
  __in_io(args, options, proc_arg) { |tempf|
307
309
  tempf.write s
@@ -315,7 +317,7 @@ module Hysh
315
317
  #
316
318
  # Same as +in_s+, except input string are specified via +lines+
317
319
  # (Array of String).
318
- def self.in_lines(lines, *args_in, &blk)
320
+ def in_lines(lines, *args_in, &blk)
319
321
  args, options, proc_arg = __parse_args args_in, blk
320
322
  __in_io(args, options, proc_arg) { |tempf|
321
323
  lines.each { |line| tempf.write line }
@@ -329,7 +331,7 @@ module Hysh
329
331
  #
330
332
  # Redirect the stdin and stdout like that of +in_s+ and +out_s+,
331
333
  # return value is same of +out_s+.
332
- def self.io_s(s, *args_in, &blk)
334
+ def io_s(s, *args_in, &blk)
333
335
  in_s(s) {
334
336
  out_s {
335
337
  run *args_in, &blk
@@ -344,7 +346,7 @@ module Hysh
344
346
  #
345
347
  # Same as +io_s+, except the output string is right stripped before
346
348
  # returning.
347
- def self.io_ss(s, *args_in, &blk)
349
+ def io_ss(s, *args_in, &blk)
348
350
  s = io_s(s, *args_in, &blk)
349
351
  s.rstrip
350
352
  end
@@ -357,7 +359,7 @@ module Hysh
357
359
  # When running the block, the non-zero exit status of running
358
360
  # external program are ignored. The original behavior is restored
359
361
  # upon returning.
360
- def self.ignore_on_command_error(&b)
362
+ def ignore_on_command_error(&b)
361
363
  with_set_globals(:@@on_command_error, IGNORE, &b)
362
364
  end
363
365
 
@@ -367,7 +369,7 @@ module Hysh
367
369
  # When running the block, the warning message will be print to
368
370
  # $stderr when the external program exited with non-zero status.
369
371
  # The original behavior is restored upon returning.
370
- def self.warn_on_command_error(&b)
372
+ def warn_on_command_error(&b)
371
373
  with_set_globals(:@@on_command_error, WARN, &b)
372
374
  end
373
375
 
@@ -377,11 +379,11 @@ module Hysh
377
379
  # When running the block, an +Hysh::CommandError+ exception will be
378
380
  # raised when the external program exited with non-zero status. The
379
381
  # original behavior is restored upon returning.
380
- def self.raise_on_command_error(&b)
382
+ def raise_on_command_error(&b)
381
383
  with_set_globals(:@@on_command_error, RAISE, &b)
382
384
  end
383
385
 
384
- def self.__parse_args(args, blk = nil) # :nodoc:
386
+ def __parse_args(args, blk = nil) # :nodoc:
385
387
  args = [args] unless args.is_a? Array
386
388
  if args.last.is_a?(Hash)
387
389
  options = args.pop
@@ -410,7 +412,7 @@ module Hysh
410
412
  # nil, the envioronment variable will be removed. Multiple pairs of
411
413
  # the environment variable names and values can be specified. The
412
414
  # changes to the environment are restored upon returning.
413
- def self.with_change_env(*var_vals)
415
+ def with_change_env(*var_vals)
414
416
  orig_var_vals = var_vals.each_slice(2).map { |var, val|
415
417
  orig_val = ENV[var]
416
418
  [var, orig_val]
@@ -431,11 +433,11 @@ module Hysh
431
433
  # chdir(dir) { ... }
432
434
  #
433
435
  # Same as +Dir.chdir+.
434
- def self.chdir(dir, &b)
436
+ def chdir(dir, &b)
435
437
  Dir.chdir(dir, &b)
436
438
  end
437
439
 
438
- def self.__spawn(args, options_in, proc_arg) # :nodoc:
440
+ def __spawn(args, options_in, proc_arg) # :nodoc:
439
441
  if proc_arg
440
442
  Process.fork {
441
443
  fclose = options_in[:close] || []
@@ -473,7 +475,7 @@ module Hysh
473
475
  # sub-process, or run external program specified via +command+ and
474
476
  # +options+, +command+ and +options+ are same as that of
475
477
  # Process.spawn. Return the +pid+.
476
- def self.spawn(*args_in, &blk)
478
+ def spawn(*args_in, &blk)
477
479
  __spawn *__parse_args(args_in, blk)
478
480
  end
479
481
 
@@ -504,7 +506,7 @@ module Hysh
504
506
  attr_reader :status
505
507
  end
506
508
 
507
- def self.__check_command_status(cmd) # :nodoc:
509
+ def __check_command_status(cmd) # :nodoc:
508
510
  unless $?.success?
509
511
  if @@on_command_error != IGNORE
510
512
  err = CommandError.new(cmd, $?)
@@ -521,7 +523,7 @@ module Hysh
521
523
  end
522
524
  end
523
525
 
524
- def self.__run(args, options, proc_arg) #:nodoc:
526
+ def __run(args, options, proc_arg) #:nodoc:
525
527
  if proc_arg
526
528
  args.first.()
527
529
  else
@@ -543,11 +545,11 @@ module Hysh
543
545
  # 0. All IO redirections, environment change, current directory
544
546
  # change, etc. will take effect when running the block, the function
545
547
  # and the external program.
546
- def self.run(*args_in, &blk)
548
+ def run(*args_in, &blk)
547
549
  __run *__parse_args(args_in, blk)
548
550
  end
549
551
 
550
- def self.__check_close(*ios) # :nodoc:
552
+ def __check_close(*ios) # :nodoc:
551
553
  ios.each { |io|
552
554
  if io && !io.closed?
553
555
  io.close
@@ -555,7 +557,7 @@ module Hysh
555
557
  }
556
558
  end
557
559
 
558
- def self.__popen(stdin, stdout, stderr, args, options, proc_arg) # :nodoc:
560
+ def __popen(stdin, stdout, stderr, args, options, proc_arg) # :nodoc:
559
561
  options[:close] = [] if proc_arg
560
562
 
561
563
  stdin_in = stdin_out = nil
@@ -624,7 +626,7 @@ module Hysh
624
626
  #
625
627
  # If no block is given, the pid and stdin, stdout and stderr pipe
626
628
  # will be returned.
627
- def self.popen(stdin, stdout, stderr, *args_in, &blk)
629
+ def popen(stdin, stdout, stderr, *args_in, &blk)
628
630
  args, options, proc_arg = __parse_args args_in
629
631
  options[:close] = [] if proc_arg
630
632
 
@@ -655,7 +657,7 @@ module Hysh
655
657
  # command # command without argument
656
658
  # [function] # function in an array
657
659
  # function # function
658
- def self.pipe(*cmds, &blk)
660
+ def pipe(*cmds, &blk)
659
661
  if block_given?
660
662
  cmds.push [blk]
661
663
  end
@@ -734,7 +736,7 @@ module Hysh
734
736
  # +command_line+, and the block if given, from left to right.
735
737
  # +command_line+ is same as that of +pipe+. Return the return value
736
738
  # or exit success status of the last function or external command.
737
- def self.run_seq(*cmds, &blk)
739
+ def run_seq(*cmds, &blk)
738
740
  if block_given?
739
741
  cmds.push blk
740
742
  end
@@ -757,7 +759,7 @@ module Hysh
757
759
  # program and return the value. If all failed, false or nil will be
758
760
  # returned. If no function, external program, or block is given,
759
761
  # return false.
760
- def self.run_or(*cmds, &blk)
762
+ def run_or(*cmds, &blk)
761
763
  if block_given?
762
764
  cmds.push blk
763
765
  end
@@ -787,7 +789,7 @@ module Hysh
787
789
  # last function, the block or the exit success status of the
788
790
  # external program. If no function, external program, or block is
789
791
  # provided, return true.
790
- def self.run_and(*cmds, &blk)
792
+ def run_and(*cmds, &blk)
791
793
  if block_given?
792
794
  cmds.push blk
793
795
  end
@@ -811,7 +813,7 @@ module Hysh
811
813
  #
812
814
  # Feed each line from $stdin to the block, if non-nil/false
813
815
  # returned, write the return value to the $stdout.
814
- def self.filter_line
816
+ def filter_line
815
817
  $stdin.each_line { |line|
816
818
  if ret_line = yield(line)
817
819
  $stdout.write ret_line
@@ -825,7 +827,7 @@ module Hysh
825
827
  #
826
828
  # Feed each character from $stdin to the block, if non-nil/false
827
829
  # returned, write the return value to the $stdout.
828
- def self.filter_char
830
+ def filter_char
829
831
  $stdin.each_char { |ch|
830
832
  if ret_ch = yield(ch)
831
833
  $stdout.write ret_ch
@@ -833,4 +835,51 @@ module Hysh
833
835
  }
834
836
  true
835
837
  end
838
+
839
+ def __with_add_hysh_to_self(s, &blk) # :nodoc:
840
+ sclass = s.singleton_class
841
+ mm_defined = sclass.instance_methods(false).include? :method_missing
842
+ mm_defined ||= sclass.private_instance_methods(false).include? :method_missing
843
+ class << s
844
+ alias __hysh_origin_method_missing__ method_missing
845
+ def method_missing(m, *args, &blk)
846
+ __hysh_origin_method_missing__ m, args, &blk
847
+ rescue NoMethodError
848
+ if Hysh.singleton_class.public_method_defined? m
849
+ Hysh.send m, *args, &blk
850
+ else
851
+ Hysh.run m.to_s, *args.map { |a| a.to_s }, &blk
852
+ end
853
+ end
854
+ end
855
+ s.instance_eval &blk
856
+ ensure
857
+ class << s
858
+ remove_method :method_missing
859
+ end
860
+ if mm_defined
861
+ class << s
862
+ alias method_missing __hysh_origin_method_missing__
863
+ end
864
+ end
865
+ class << s
866
+ remove_method :__hysh_origin_method_missing__
867
+ end
868
+ end
869
+ end
870
+
871
+ # :call-seq:
872
+ # hysh_script { ... }
873
+ #
874
+ # Call the block, in addition to the methods, variables reference of
875
+ # current self object, the public methods in Hysh module can be called
876
+ # directly too, and all other method callings are regarded as external
877
+ # program running. That is, if there is no ls method defined for
878
+ # self, you can run it via
879
+ #
880
+ # hysh_script {
881
+ # ls
882
+ # }
883
+ def hysh_script(&blk)
884
+ Hysh.__with_add_hysh_to_self(self, &blk)
836
885
  end
@@ -37,3 +37,32 @@ describe Hysh do
37
37
  end
38
38
  end
39
39
  end
40
+
41
+ class TestScript
42
+ def test
43
+ @x = 4
44
+ hysh_script {
45
+ pipe ['echo', '1', '2'], ['wc', '-w']
46
+ }
47
+ end
48
+ end
49
+
50
+ def test_script
51
+ t = TestScript.new
52
+ class << t
53
+ def method_missing(m, *args)
54
+ raise NoMethodError
55
+ end
56
+ end
57
+ t
58
+ end
59
+
60
+ describe "hysh_script" do
61
+ it "run Hysh methods directly" do
62
+ expect(hysh_script { out_ss { run "echo", "12" } }).to eql(["12", true])
63
+ end
64
+ it "run commands as function" do
65
+ expect(hysh_script { out_ss { echo 12 } }).to eql(["12", true])
66
+ expect(Hysh.out_ss { test_script.test }).to eql(["2", true])
67
+ end
68
+ end
@@ -31,3 +31,19 @@ def dpkg_installed2(package_names = nil)
31
31
  }
32
32
  }
33
33
  end
34
+
35
+ def dpkg_installed3(package_names = nil)
36
+ hysh_script {
37
+ if package_names
38
+ pipe ['dpkg', '-l'] {
39
+ filter_line { |l|
40
+ package_names.any? { |pkg|
41
+ l.index pkg
42
+ } && l
43
+ }
44
+ }
45
+ else
46
+ dpkg '-l'
47
+ end
48
+ }
49
+ end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hysh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Huang, Ying
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-12 00:00:00.000000000 Z
11
+ date: 2015-10-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: IO redirection and command glue in Ruby
13
+ description: Shell scripting in Ruby, including IO redirection and command glue, etc.
14
14
  email: huang.ying.caritas@gmail.com
15
15
  executables: []
16
16
  extensions: []