|
1 | 1 | module Completely |
2 | 2 | class Installer |
3 | | - attr_reader :program, :script_path |
| 3 | + class << self |
| 4 | + def from_io(program:, io: nil) |
| 5 | + io ||= $stdin |
4 | 6 |
|
5 | | - def initialize(program:, script_path: nil) |
6 | | - if script_path == '-' |
7 | | - raise InstallError, 'Nothing is piped on stdin' if $stdin.tty? |
| 7 | + raise InstallError, "io must respond to #read" unless io.respond_to?(:read) |
| 8 | + raise InstallError, "io is closed" if io.respond_to?(:closed?) && io.closed? |
| 9 | + |
| 10 | + from_string program:, string: io.read |
| 11 | + end |
8 | 12 |
|
| 13 | + def from_string(program:, string:) |
| 14 | + tempfile = create_tempfile |
9 | 15 | script_path = tempfile.path |
10 | | - File.write script_path, $stdin.read |
| 16 | + begin |
| 17 | + File.write script_path, string |
| 18 | + ensure |
| 19 | + tempfile.close |
| 20 | + end |
| 21 | + |
| 22 | + new program:, script_path: |
| 23 | + end |
| 24 | + |
| 25 | + def create_tempfile |
| 26 | + tempfile = Tempfile.new ["completely-", '.bash'] |
| 27 | + tempfiles.push tempfile |
| 28 | + tempfile |
11 | 29 | end |
12 | 30 |
|
| 31 | + def tempfiles = @tempfiles ||= [] |
| 32 | + end |
| 33 | + |
| 34 | + attr_reader :program, :script_path |
| 35 | + |
| 36 | + def initialize(program:, script_path: nil) |
13 | 37 | @program = program |
14 | 38 | @script_path = script_path |
15 | 39 | end |
@@ -67,10 +91,6 @@ def uninstall |
67 | 91 |
|
68 | 92 | private |
69 | 93 |
|
70 | | - def tempfile |
71 | | - @tempfile ||= Tempfile.new('stdin-completely-') |
72 | | - end |
73 | | - |
74 | 94 | def target_exist? |
75 | 95 | File.exist? target_path |
76 | 96 | end |
|
0 commit comments