toys-core 0.10.2 → 0.11.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.
@@ -10,7 +10,7 @@ module Toys
10
10
  # @private
11
11
  #
12
12
  def initialize(parent, context_directory, source_type, source_path, source_proc,
13
- source_name, data_dir, lib_dir)
13
+ source_name, data_dir_name, lib_dir_name)
14
14
  @parent = parent
15
15
  @context_directory = context_directory
16
16
  @source_type = source_type
@@ -18,8 +18,10 @@ module Toys
18
18
  @source_path = source_path
19
19
  @source_proc = source_proc
20
20
  @source_name = source_name
21
- @data_dir = data_dir
22
- @lib_dir = lib_dir
21
+ @data_dir_name = data_dir_name
22
+ @lib_dir_name = lib_dir_name
23
+ @data_dir = find_special_dir(data_dir_name)
24
+ @lib_dir = find_special_dir(lib_dir_name)
23
25
  end
24
26
 
25
27
  ##
@@ -118,15 +120,13 @@ module Toys
118
120
  # Create a child SourceInfo relative to the parent path.
119
121
  # @private
120
122
  #
121
- def relative_child(filename, data_dir_name, lib_dir_name)
122
- raise "no parent path for relative_child" unless source_path
123
+ def relative_child(filename)
124
+ raise "relative_child is valid only on a directory source" unless source_type == :directory
123
125
  child_path = ::File.join(source_path, filename)
124
126
  child_path, type = SourceInfo.check_path(child_path, true)
125
127
  return nil unless child_path
126
- data_dir = SourceInfo.find_special_dir(type, child_path, data_dir_name)
127
- lib_dir = SourceInfo.find_special_dir(type, child_path, lib_dir_name)
128
- SourceInfo.new(self, context_directory, type, child_path, source_proc, child_path,
129
- data_dir, lib_dir)
128
+ SourceInfo.new(self, context_directory, type, child_path, nil, child_path,
129
+ @data_dir_name, @lib_dir_name)
130
130
  end
131
131
 
132
132
  ##
@@ -135,7 +135,8 @@ module Toys
135
135
  #
136
136
  def absolute_child(child_path)
137
137
  child_path, type = SourceInfo.check_path(child_path, false)
138
- SourceInfo.new(self, context_directory, type, child_path, source_proc, child_path, nil, nil)
138
+ SourceInfo.new(self, context_directory, type, child_path, nil, child_path,
139
+ @data_dir_name, @lib_dir_name)
139
140
  end
140
141
 
141
142
  ##
@@ -144,25 +145,26 @@ module Toys
144
145
  #
145
146
  def proc_child(child_proc, source_name = nil)
146
147
  source_name ||= self.source_name
147
- SourceInfo.new(self, context_directory, :proc, source_path, child_proc, source_name, nil, nil)
148
+ SourceInfo.new(self, context_directory, :proc, source_path, child_proc, source_name,
149
+ @data_dir_name, @lib_dir_name)
148
150
  end
149
151
 
150
152
  ##
151
153
  # Create a root source info for a file path.
152
154
  # @private
153
155
  #
154
- def self.create_path_root(source_path)
156
+ def self.create_path_root(source_path, data_dir_name, lib_dir_name)
155
157
  source_path, type = check_path(source_path, false)
156
158
  context_directory = ::File.dirname(source_path)
157
- new(nil, context_directory, type, source_path, nil, source_path, nil, nil)
159
+ new(nil, context_directory, type, source_path, nil, source_path, data_dir_name, lib_dir_name)
158
160
  end
159
161
 
160
162
  ##
161
163
  # Create a root source info for a proc.
162
164
  # @private
163
165
  #
164
- def self.create_proc_root(source_proc, source_name)
165
- new(nil, nil, :proc, nil, source_proc, source_name, nil, nil)
166
+ def self.create_proc_root(source_proc, source_name, data_dir_name, lib_dir_name)
167
+ new(nil, nil, :proc, nil, source_proc, source_name, data_dir_name, lib_dir_name)
166
168
  end
167
169
 
168
170
  ##
@@ -189,14 +191,11 @@ module Toys
189
191
  end
190
192
  end
191
193
 
192
- ##
193
- # Determine the data directory path, if any.
194
- # @private
195
- #
196
- def self.find_special_dir(type, source_path, dir_name)
197
- return nil if source_path.nil? || dir_name.nil?
198
- source_path = ::File.dirname(source_path) if type == :file
199
- dir = ::File.join(source_path, dir_name)
194
+ private
195
+
196
+ def find_special_dir(dir_name)
197
+ return nil if @source_type != :directory || dir_name.nil?
198
+ dir = ::File.join(@source_path, dir_name)
200
199
  dir if ::File.directory?(dir) && ::File.readable?(dir)
201
200
  end
202
201
  end
@@ -7,32 +7,55 @@ module Toys
7
7
  #
8
8
  # The following parameters can be passed when including this mixin:
9
9
  #
10
- # * `:groups` (Array<String>) The groups to include in setup
10
+ # * `:static` (Boolean) If `true`, installs the bundle immediately, when
11
+ # defining the tool. If `false` (the default), installs the bundle just
12
+ # before the tool runs.
11
13
  #
12
- # * `:search_dirs` (Array<String,Symbol>) Directories to search for a
13
- # Gemfile.
14
+ # * `:groups` (Array\<String\>) The groups to include in setup.
15
+ #
16
+ # * `:gemfile_path` (String) The path to the Gemfile to use. If `nil` or
17
+ # not given, the `:search_dirs` will be searched for a Gemfile.
18
+ #
19
+ # * `:search_dirs` (String,Symbol,Array\<String,Symbol\>) Directories to
20
+ # search for a Gemfile.
14
21
  #
15
22
  # You can pass full directory paths, and/or any of the following:
16
- # * `:context` - the current context directory
17
- # * `:current` - the current working directory
18
- # * `:toys` - the Toys directory containing the tool definition
23
+ # * `:context` - the current context directory.
24
+ # * `:current` - the current working directory.
25
+ # * `:toys` - the Toys directory containing the tool definition, and
26
+ # any of its parents within the Toys directory hierarchy.
19
27
  #
20
28
  # The default is to search `[:toys, :context, :current]` in that order.
29
+ # See {DEFAULT_SEARCH_DIRS}.
30
+ #
31
+ # For most directories, the bundler mixin will look for the files
32
+ # ".gems.rb", "gems.rb", and "Gemfile", in that order. In `:toys`
33
+ # directories, it will look only for ".gems.rb" and "Gemfile", in that
34
+ # order. These can be overridden by setting the `:gemfile_names` and/or
35
+ # `:toys_gemfile_names` arguments.
36
+ #
37
+ # * `:gemfile_names` (Array\<String\>) File names that are recognized as
38
+ # Gemfiles when searching in directories other than Toys directories.
39
+ # Defaults to {Toys::Utils::Gems::DEFAULT_GEMFILE_NAMES}.
40
+ #
41
+ # * `:toys_gemfile_names` (Array\<String\>) File names that are
42
+ # recognized as Gemfiles when wearching in Toys directories.
43
+ # Defaults to {DEFAULT_TOYS_GEMFILE_NAMES}.
21
44
  #
22
45
  # * `:on_missing` (Symbol) What to do if a needed gem is not installed.
23
46
  #
24
47
  # Supported values:
25
- # * `:confirm` - prompt the user on whether to install (default)
26
- # * `:error` - raise an exception
27
- # * `:install` - just install the gem
48
+ # * `:confirm` - prompt the user on whether to install (default).
49
+ # * `:error` - raise an exception.
50
+ # * `:install` - just install the gem.
28
51
  #
29
52
  # * `:on_conflict` (Symbol) What to do if bundler has already been run
30
53
  # with a different Gemfile.
31
54
  #
32
55
  # Supported values:
33
- # * `:error` - raise an exception (default)
34
- # * `:ignore` - just silently proceed without bundling again
35
- # * `:warn` - print a warning and proceed without bundling again
56
+ # * `:error` - raise an exception (default).
57
+ # * `:ignore` - just silently proceed without bundling again.
58
+ # * `:warn` - print a warning and proceed without bundling again.
36
59
  #
37
60
  # * `:terminal` (Toys::Utils::Terminal) Terminal to use (optional)
38
61
  # * `:input` (IO) Input IO (optional, defaults to STDIN)
@@ -41,48 +64,106 @@ module Toys
41
64
  module Bundler
42
65
  include Mixin
43
66
 
44
- on_initialize do
45
- |groups: nil,
46
- search_dirs: nil,
47
- on_missing: nil,
48
- on_conflict: nil,
49
- terminal: nil,
50
- input: nil,
51
- output: nil|
67
+ on_initialize do |static: false, **kwargs|
68
+ unless static
69
+ context_directory = self[::Toys::Context::Key::CONTEXT_DIRECTORY]
70
+ source_info = self[::Toys::Context::Key::TOOL_SOURCE]
71
+ ::Toys::StandardMixins::Bundler.setup_bundle(context_directory, source_info, **kwargs)
72
+ end
73
+ end
74
+
75
+ on_include do |static: false, **kwargs|
76
+ if static
77
+ ::Toys::StandardMixins::Bundler.setup_bundle(context_directory, source_info, **kwargs)
78
+ end
79
+ end
80
+
81
+ ##
82
+ # Default search directories for Gemfiles.
83
+ # @return [Array<String,Symbol>]
84
+ #
85
+ DEFAULT_SEARCH_DIRS = [:toys, :context, :current].freeze
86
+
87
+ ##
88
+ # The gemfile names that are searched by default in Toys directories.
89
+ # @return [Array<String>]
90
+ #
91
+ DEFAULT_TOYS_GEMFILE_NAMES = [".gems.rb", "Gemfile"].freeze
92
+
93
+ # @private
94
+ def self.setup_bundle(context_directory,
95
+ source_info,
96
+ gemfile_path: nil,
97
+ search_dirs: nil,
98
+ gemfile_names: nil,
99
+ toys_gemfile_names: nil,
100
+ groups: nil,
101
+ on_missing: nil,
102
+ on_conflict: nil,
103
+ terminal: nil,
104
+ input: nil,
105
+ output: nil)
52
106
  require "toys/utils/gems"
53
- search_dirs = ::Toys::StandardMixins::Bundler.resolve_search_dirs(search_dirs, self)
107
+ gemfile_path ||= begin
108
+ gemfile_finder = GemfileFinder.new(context_directory, source_info,
109
+ gemfile_names, toys_gemfile_names)
110
+ gemfile_finder.search(search_dirs || DEFAULT_SEARCH_DIRS)
111
+ end
54
112
  gems = ::Toys::Utils::Gems.new(on_missing: on_missing, on_conflict: on_conflict,
55
113
  terminal: terminal, input: input, output: output)
56
- gems.bundle(groups: groups, search_dirs: search_dirs)
114
+ gems.bundle(groups: groups, gemfile_path: gemfile_path)
57
115
  end
58
116
 
59
- ## @private
60
- def self.resolve_search_dirs(search_dirs, context)
61
- search_dirs ||= [:toys, :context, :current]
62
- Array(search_dirs).flat_map do |dir|
63
- case dir
117
+ # @private
118
+ class GemfileFinder
119
+ # @private
120
+ def initialize(context_directory, source_info, gemfile_names, toys_gemfile_names)
121
+ @context_directory = context_directory
122
+ @source_info = source_info
123
+ @gemfile_names = gemfile_names
124
+ @toys_gemfile_names = toys_gemfile_names || DEFAULT_TOYS_GEMFILE_NAMES
125
+ end
126
+
127
+ # @private
128
+ def search(search_dir)
129
+ case search_dir
130
+ when ::Array
131
+ search_array(search_dir)
132
+ when ::String
133
+ ::Toys::Utils::Gems.find_gemfile(search_dir, gemfile_names: @gemfile_names)
64
134
  when :context
65
- context[::Toys::Context::Key::CONTEXT_DIRECTORY]
135
+ search(@context_directory)
66
136
  when :current
67
- ::Dir.getwd
137
+ search(::Dir.getwd)
68
138
  when :toys
69
- toys_dir_stack(context[::Toys::Context::Key::TOOL_SOURCE])
70
- when ::String
71
- dir
139
+ search_toys
72
140
  else
73
141
  raise ::ArgumentError, "Unrecognized search_dir: #{dir.inspect}"
74
142
  end
75
143
  end
76
- end
77
144
 
78
- ## @private
79
- def self.toys_dir_stack(source_info)
80
- dirs = []
81
- while source_info
82
- dirs << source_info.source_path if source_info.source_type == :directory
83
- source_info = source_info.parent
145
+ private
146
+
147
+ def search_array(search_dirs)
148
+ search_dirs.each do |search_dir|
149
+ result = search(search_dir)
150
+ return result if result
151
+ end
152
+ nil
153
+ end
154
+
155
+ def search_toys
156
+ source_info = @source_info
157
+ while source_info
158
+ if source_info.source_type == :directory
159
+ result = ::Toys::Utils::Gems.find_gemfile(source_info.source_path,
160
+ gemfile_names: @toys_gemfile_names)
161
+ return result if result
162
+ end
163
+ source_info = source_info.parent
164
+ end
165
+ nil
84
166
  end
85
- dirs
86
167
  end
87
168
  end
88
169
  end
@@ -728,7 +728,7 @@ module Toys
728
728
  def self._setup_clean_process(cmd)
729
729
  raise ::ArgumentError, "Toys process is unknown" unless ::Toys.executable_path
730
730
  cmd = ::Shellwords.split(cmd) if cmd.is_a?(::String)
731
- cmd = Array(::Toys.executable_path) + cmd
731
+ cmd = [::RbConfig.ruby, "--disable=gems", ::Toys.executable_path] + cmd
732
732
  if defined?(::Bundler)
733
733
  if ::Bundler.respond_to?(:with_unbundled_env)
734
734
  ::Bundler.with_unbundled_env { yield(cmd) }
@@ -547,7 +547,7 @@ module Toys
547
547
  ##
548
548
  # The process ID.
549
549
  #
550
- # Exactly one of `exception` and `pid` will be non-nil.
550
+ # Exactly one of {#exception} and {#pid} will be non-nil.
551
551
  #
552
552
  # @return [Integer] if the process start was successful
553
553
  # @return [nil] if the process could not be started.
@@ -557,7 +557,7 @@ module Toys
557
557
  ##
558
558
  # The exception raised when the process failed to start.
559
559
  #
560
- # Exactly one of `exception` and `pid` will be non-nil.
560
+ # Exactly one of {#exception} and {#pid} will be non-nil.
561
561
  #
562
562
  # @return [Exception] if the process failed to start.
563
563
  # @return [nil] if the process start was successful.
@@ -722,15 +722,20 @@ module Toys
722
722
  ##
723
723
  # Wait for the subcommand to complete, and return a result object.
724
724
  #
725
+ # Closes the control streams if present. The stdin stream is always
726
+ # closed, even if the call times out. The stdout and stderr streams are
727
+ # closed only after the command terminates.
728
+ #
725
729
  # @param timeout [Numeric,nil] The timeout in seconds, or `nil` to
726
730
  # wait indefinitely.
727
731
  # @return [Toys::Utils::Exec::Result] The result object
728
732
  # @return [nil] if a timeout occurred.
729
733
  #
730
734
  def result(timeout: nil)
735
+ close_streams(:in)
731
736
  return nil if @wait_thread && !@wait_thread.join(timeout)
732
737
  @result ||= begin
733
- close_streams
738
+ close_streams(:out)
734
739
  @join_threads.each(&:join)
735
740
  Result.new(name, @captures[:out], @captures[:err], @wait_thread&.value, @exception)
736
741
  .tap { |result| @result_callback&.call(result) }
@@ -738,13 +743,13 @@ module Toys
738
743
  end
739
744
 
740
745
  ##
741
- # Close all the controller's streams.
746
+ # Close the controller's streams.
742
747
  # @private
743
748
  #
744
- def close_streams
745
- @in.close if @in && !@in.closed?
746
- @out.close if @out && !@out.closed?
747
- @err.close if @err && !@err.closed?
749
+ def close_streams(which)
750
+ @in.close if which != :out && @in && !@in.closed?
751
+ @out.close if which != :in && @out && !@out.closed?
752
+ @err.close if which != :in && @err && !@err.closed?
748
753
  self
749
754
  end
750
755
 
@@ -773,7 +778,21 @@ module Toys
773
778
  end
774
779
 
775
780
  ##
776
- # The result returned from a subcommand execution.
781
+ # The result returned from a subcommand execution. This includes the
782
+ # identifying name of the execution (if any), the result status of the
783
+ # execution, and any captured stream output.
784
+ #
785
+ # Possible result statuses are:
786
+ #
787
+ # * The process failed to start. {Result#failed?} will return true, and
788
+ # {Result#exception} will return an exception describing the failure
789
+ # (often an errno).
790
+ # * The process executed and exited with a normal exit code. Either
791
+ # {Result#success?} or {Result#error?} will return true, and
792
+ # {Result.exit_code} will return the numeric exit code.
793
+ # * The process executed but was terminated by an uncaught signal.
794
+ # {Result#signaled?} will return true, and {Result#term_signal} will
795
+ # return the numeric signal code.
777
796
  #
778
797
  class Result
779
798
  ## @private
@@ -809,11 +828,13 @@ module Toys
809
828
  attr_reader :captured_err
810
829
 
811
830
  ##
812
- # The status code object.
831
+ # The Ruby process status object, providing various information about
832
+ # the ending state of the process.
813
833
  #
814
- # Exactly one of `exception` and `status` will be non-nil.
834
+ # Exactly one of {#exception} and {#status} will be non-nil.
815
835
  #
816
- # @return [Process::Status] The status code.
836
+ # @return [Process::Status] The status, if the process was successfully
837
+ # spanwed and terminated.
817
838
  # @return [nil] if the process could not be started.
818
839
  #
819
840
  attr_reader :status
@@ -821,7 +842,7 @@ module Toys
821
842
  ##
822
843
  # The exception raised if a process couldn't be started.
823
844
  #
824
- # Exactly one of `exception` and `status` will be non-nil.
845
+ # Exactly one of {#exception} and {#status} will be non-nil.
825
846
  #
826
847
  # @return [Exception] The exception raised from process start.
827
848
  # @return [nil] if the process started successfully.
@@ -829,33 +850,76 @@ module Toys
829
850
  attr_reader :exception
830
851
 
831
852
  ##
832
- # The numeric status code.
853
+ # The numeric status code for a process that exited normally,
833
854
  #
834
- # This will be a nonzero integer if the process failed to start. That
835
- # is, `exit_code` will never be `nil`, even if `status` is `nil`.
855
+ # Exactly one of {#exception}, {#exit_code}, and {#term_signal} will be
856
+ # non-nil.
836
857
  #
837
- # @return [Integer]
858
+ # @return [Integer] the numeric status code, if the process started
859
+ # successfully and exited normally.
860
+ # @return [nil] if the process did not start successfully, or was
861
+ # terminated by an uncaught signal.
838
862
  #
839
863
  def exit_code
840
- status ? status.exitstatus : 127
864
+ status&.exitstatus
865
+ end
866
+
867
+ ##
868
+ # The numeric signal code that caused process termination.
869
+ #
870
+ # Exactly one of {#exception}, {#exit_code}, and {#term_signal} will be
871
+ # non-nil.
872
+ #
873
+ # @return [Integer] The signal that caused the process to terminate.
874
+ # @return [nil] if the process did not start successfully, or executed
875
+ # and exited with a normal exit code.
876
+ #
877
+ def term_signal
878
+ status&.termsig
879
+ end
880
+
881
+ ##
882
+ # Returns true if the subprocess failed to start, or false if the
883
+ # process was able to execute.
884
+ #
885
+ # @return [Boolean]
886
+ #
887
+ def failed?
888
+ status.nil?
889
+ end
890
+
891
+ ##
892
+ # Returns true if the subprocess terminated due to an unhandled signal,
893
+ # or false if the process failed to start or exited normally.
894
+ #
895
+ # @return [Boolean]
896
+ #
897
+ def signaled?
898
+ !term_signal.nil?
841
899
  end
842
900
 
843
901
  ##
844
- # Returns true if the subprocess terminated with a zero status.
902
+ # Returns true if the subprocess terminated with a zero status, or
903
+ # false if the process failed to start, terminated due to a signal, or
904
+ # returned a nonzero status.
845
905
  #
846
906
  # @return [Boolean]
847
907
  #
848
908
  def success?
849
- exit_code.zero?
909
+ code = exit_code
910
+ !code.nil? && code.zero?
850
911
  end
851
912
 
852
913
  ##
853
- # Returns true if the subprocess terminated with a nonzero status.
914
+ # Returns true if the subprocess terminated with a nonzero status, or
915
+ # false if the process failed to start, terminated due to a signal, or
916
+ # returned a zero status.
854
917
  #
855
918
  # @return [Boolean]
856
919
  #
857
920
  def error?
858
- !exit_code.zero?
921
+ code = exit_code
922
+ !code.nil? && !code.zero?
859
923
  end
860
924
  end
861
925
 
@@ -887,10 +951,10 @@ module Toys
887
951
  return controller if @config_opts[:background]
888
952
  begin
889
953
  @block&.call(controller)
954
+ controller.result
890
955
  ensure
891
- controller.close_streams
956
+ controller.close_streams(:both)
892
957
  end
893
- controller.result
894
958
  end
895
959
 
896
960
  private