ratch 2.0.0 → 2.1.0

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: 79b7a105b218cf3320ccaf308cf6fd164a389d012763a591cf5b69bfa8025786
4
- data.tar.gz: 17c463febd50d32012df3b54cb803ecd39ea68c269654c9396b8b83ac9ebe7cc
3
+ metadata.gz: 5b2d3147b902c8a2fcf4920ada1c5ad08e013ff28778c0182cd4c2dcc6b9b034
4
+ data.tar.gz: 51504a359e8ad63bdc69169d4b666d65524beb9c62ec7286c9490f0ce14dcdda
5
5
  SHA512:
6
- metadata.gz: fe619e0d3ce099896acf859ab58f61a4a5e7690a30ce2affc685624eee32d02877b654b18855c7c58cc23698d0b942a2266d20fee72be5d74f99525a2efc20af
7
- data.tar.gz: 4988d90ff2722d1214a6d542ef02def96502b177672bc52a2d0fcb0440b9338d9ee61bf3069a068c98f985956b7812a62657a68c940ac406aeb2211ae992a920
6
+ metadata.gz: ce2064f8d7c734b0d7a68aa211bea86d8e75ece794bd41610cc89481e7b4d5d1c16ffcadf5414db90a4952df96e22977516944fdb4f8fa6136a4ae9e18c532b4
7
+ data.tar.gz: 02a61f057af5618070bf9ffe38f3af40af2af6cb04c284afab46eb197010543713a2020f6c8b28eeb775cf171bc23b98772078363eaf4794e2a8601a19001574
data/HISTORY.rdoc CHANGED
@@ -1,5 +1,36 @@
1
1
  = RELEASE HISTORY
2
2
 
3
+ == 2.1.0 | 2026-11-25
4
+
5
+ This release takes in what was worth keeping from ProUtils, a later
6
+ reworking of Ratch's utilities, which is now retired.
7
+
8
+ Changes:
9
+
10
+ * Add Ratch::Path, a Pathname that keeps to its shell's mode. Shell#path,
11
+ #file, #dir, #work, #parent, #home and #root return one, so that
12
+ <tt>shell.path('tmp').rmtree</tt> removes nothing in noop mode, and is
13
+ reported when tracing. Before, these were plain Pathnames, which took
14
+ no notice of the mode.
15
+ * Add Shell#ruby, to run a command with the present Ruby.
16
+ * Add Shell#cmd, which runs any method called on it as a command:
17
+ <tt>shell.cmd.git 'status'</tt>.
18
+ * Add Ratch::Commands, which is optional, to call commands on the shell
19
+ itself: <tt>shell.git 'status'</tt>. A shell has it when made with the
20
+ +commands+ option (<tt>--commands</tt> for the +ratch+ command) or
21
+ when the module is included. Only a program found on the PATH is taken
22
+ as a command, so a misspelt method is still an error.
23
+ * The +ratch+ command takes options before the script's name as well as
24
+ after it.
25
+ * Array#to_list drops nil elements.
26
+ * Object#to_yamlfrag handles tagged documents and the end mark of a
27
+ plain value.
28
+
29
+ Fixes:
30
+
31
+ * FileTest.bin? raised if there was no PATH.
32
+
33
+
3
34
  == 2.0.0 | 2026-11-25
4
35
 
5
36
  Ratch is revived, for Ruby 3.1 and up. Neither 1.2.0 nor the unreleased
data/README.rdoc CHANGED
@@ -57,9 +57,42 @@ The first three can also be given to a single command:
57
57
 
58
58
  shell.rm('old.txt', :dryrun=>true)
59
59
 
60
+ The paths a shell gives out (+path+, +file+, +dir+, +work+, +parent+) are
61
+ Pathnames that keep to its mode too:
62
+
63
+ shell.path('tmp').rmtree # prints "rmtree /.../project/tmp", in a dry run
64
+
60
65
  To build on it, subclass Ratch::Shell, or keep one and delegate to it.
61
66
 
62
67
 
68
+ == Commands
69
+
70
+ Besides +sh+, there is +ruby+, which runs the Ruby that is running now,
71
+ and +cmd+, which runs any method called on it as a command:
72
+
73
+ shell.ruby '-Ilib', 'script/build.rb'
74
+
75
+ shell.cmd.git 'status'
76
+ shell.cmd.rdoc 'lib', :op=>'doc', :q=>true # rdoc --op=doc -q lib
77
+ shell.cmd.ls '-l *.rb' # one string is a command line
78
+
79
+ If you would rather call commands on the shell itself, ask for it:
80
+
81
+ shell = Ratch::Shell.new('project', :commands=>true)
82
+
83
+ shell.git 'status'
84
+
85
+ This is optional because it changes what a mistake does. To keep that
86
+ small, only a program found on the PATH is taken as a command; any other
87
+ unknown method is still a NoMethodError. The shell's own methods, and
88
+ Ruby's (+test+, +sleep+, +exit+), come first. A Shell subclass gets the
89
+ same by including Ratch::Commands, and a script by <tt>--commands</tt>:
90
+
91
+ $ ratch publish.ratch --commands
92
+
93
+ All of these run from the shell's directory and follow its mode.
94
+
95
+
63
96
  == Batches
64
97
 
65
98
  A Ratch::Batch does the same over a list of files.
data/bin/ratch CHANGED
@@ -1,3 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'ratch/script'
3
- Ratch::Script.execute(ARGV.shift)
3
+
4
+ # The script is the first argument that isn't an option, so options can
5
+ # be given before it or after it.
6
+ index = ARGV.index{ |arg| !arg.start_with?('-') }
7
+
8
+ unless index
9
+ $stderr.puts "Usage: ratch [OPTIONS] SCRIPT [ARGUMENTS]"
10
+ exit 1
11
+ end
12
+
13
+ Ratch::Script.execute(ARGV.delete_at(index))
@@ -0,0 +1,72 @@
1
+ module Ratch
2
+
3
+ # Commands lets system commands be called as if they were methods of
4
+ # a shell:
5
+ #
6
+ # shell.git 'status'
7
+ # shell.rdoc '--op', 'doc', 'lib'
8
+ #
9
+ # It is optional. A shell has it when made with the :commands option,
10
+ # or when the module is included in a Shell subclass or used to extend
11
+ # one shell:
12
+ #
13
+ # shell = Ratch::Shell.new('project', :commands=>true)
14
+ #
15
+ # A method the shell doesn't have is taken as a command only if there
16
+ # is a program of that name on the PATH. Anything else is the
17
+ # NoMethodError it would be without this, so a misspelt method is still
18
+ # caught.
19
+ #
20
+ # The command is run by Shell#sh, so it is run from the shell's
21
+ # directory and follows the shell's mode. See Shell#cmd for the
22
+ # arguments, and for a way to do this without the module.
23
+ module Commands
24
+
25
+ #
26
+ def method_missing(name, *args, &block)
27
+ return super if block || !command?(name)
28
+ run_command(name, *args)
29
+ end
30
+
31
+ #
32
+ def respond_to_missing?(name, include_private=false)
33
+ command?(name) || super
34
+ end
35
+
36
+ private
37
+
38
+ # Is there a program of this name on the PATH?
39
+ def command?(name)
40
+ name = name.to_s
41
+ return false unless name =~ /\A[A-Za-z0-9][\w.+-]*\z/
42
+ ENV['PATH'].to_s.split(File::PATH_SEPARATOR).any? do |dir|
43
+ file = File.join(dir, name)
44
+ File.file?(file) && File.executable?(file)
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ # What Shell#cmd gives: an object that runs any method called on it as
51
+ # a command of its shell.
52
+ class Commander < BasicObject
53
+
54
+ #
55
+ def initialize(shell)
56
+ @shell = shell
57
+ end
58
+
59
+ #
60
+ def method_missing(name, *args, &block)
61
+ return super if block
62
+ @shell.__send__(:run_command, name, *args)
63
+ end
64
+
65
+ #
66
+ def respond_to_missing?(name, include_private=false)
67
+ true
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -1,29 +1,19 @@
1
1
  module FileTest
2
2
 
3
- # Return a cached list of the PATH environment variable.
4
- # This is a support method used by #bin?
3
+ module_function
4
+
5
+ # The directories of the PATH. Empty if there is none.
5
6
  def command_paths
6
- @command_paths ||= ENV['PATH'].split(/[:;]/)
7
+ ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
7
8
  end
8
9
 
9
- # Is a file a bin/ executable?
10
- #
11
- # TODO: Make more robust. Probably needs to be fixed for Windows.
10
+ # Is there a file of this name in a directory of the PATH? Returns
11
+ # the name if so, and false if not.
12
12
  def bin?(fname)
13
- is_bin = command_paths.any? do |f|
14
- FileTest.exist?(File.join(f, fname))
13
+ is_bin = command_paths.any? do |dir|
14
+ FileTest.exist?(File.join(dir, fname.to_s))
15
15
  end
16
- #is_bin ? File.basename(fname) : false
17
16
  is_bin ? fname : false
18
17
  end
19
18
 
20
- ## Is a file a task?
21
- #
22
- #def task?(path)
23
- # task = File.dirname($0) + "/#{path}"
24
- # task.chomp!('!')
25
- # task if FileTest.file?(task) && FileTest.executable?(task)
26
- #end
27
-
28
19
  end
29
-
@@ -1,7 +1,7 @@
1
1
  class Array
2
2
 
3
3
  def to_list
4
- self
4
+ compact
5
5
  end
6
6
 
7
7
  end
@@ -1,9 +1,17 @@
1
+ require 'yaml'
2
+
1
3
  class Object
2
4
 
3
- #
5
+ # Same as #to_yaml but without the initial `---` line, or the `...`
6
+ # line that ends a document holding a plain value.
4
7
  def to_yamlfrag
5
- to_yaml.sub("---",'').rstrip
8
+ str = to_yaml
9
+ if /\A--- !/ =~ str
10
+ str = str.sub(/\A--- !.*?$/, '')
11
+ else
12
+ str = str.sub(/\A---\s*/, '')
13
+ end
14
+ str.chomp("\n...\n").rstrip
6
15
  end
7
16
 
8
17
  end
9
-
@@ -4,19 +4,16 @@ class String
4
4
 
5
5
  #
6
6
  def unfold_paragraphs
7
- blank = false
8
- text = ''
7
+ text = String.new
9
8
  split(/\n/).each do |line|
10
9
  if /\S/ !~ line
11
10
  text << "\n\n"
12
- blank = true
13
11
  else
14
12
  if /^(\s+|[*])/ =~ line
15
13
  text << (line.rstrip + "\n")
16
14
  else
17
15
  text << (line.rstrip + " ")
18
16
  end
19
- blank = false
20
17
  end
21
18
  end
22
19
  text = text.gsub("\n\n\n","\n\n")
data/lib/ratch/path.rb ADDED
@@ -0,0 +1,99 @@
1
+ require 'pathname'
2
+
3
+ module Ratch
4
+
5
+ # A Path is a Pathname that belongs to a shell and keeps to its mode:
6
+ # when the shell is in noop mode, the methods that change the file
7
+ # system do nothing, and when it is tracing, they are reported to
8
+ # standard error first.
9
+ #
10
+ # shell = Ratch::Shell.new('project', :dryrun=>true)
11
+ # shell.path('tmp').rmtree # prints "rmtree /.../project/tmp" and removes nothing
12
+ #
13
+ # Paths derived from one (by #+, #parent, #children and so on) belong
14
+ # to the same shell. In every other way it is a Pathname, and equal to
15
+ # the Pathname of the same path. A Path made without a shell behaves as
16
+ # a Pathname.
17
+ #
18
+ # Files opened for writing with #open are not covered.
19
+ class Path < ::Pathname
20
+
21
+ #
22
+ def initialize(path, shell=nil)
23
+ super(path.to_s)
24
+ @shell = shell
25
+ end
26
+
27
+ # The shell the path belongs to, if any.
28
+ attr_reader :shell
29
+
30
+ # Is the path's shell in noop mode?
31
+ def noop?
32
+ @shell ? !!@shell.noop? : false
33
+ end
34
+
35
+ # Is the path's shell tracing?
36
+ def trace?
37
+ @shell ? !!@shell.trace? : false
38
+ end
39
+
40
+ # Pathname methods that change the file system.
41
+ WRITE_METHODS = [
42
+ :chmod, :lchmod, :chown, :lchown, :make_link, :make_symlink,
43
+ :rename, :truncate, :utime, :mkdir, :mkpath, :rmdir, :rmtree,
44
+ :unlink, :delete, :write, :binwrite
45
+ ]
46
+
47
+ WRITE_METHODS.each do |name|
48
+ next unless ::Pathname.method_defined?(name)
49
+ define_method(name) do |*args, **opts, &block|
50
+ if trace?
51
+ shown = [:write, :binwrite].include?(name) ? [] : args
52
+ $stderr.puts [name, self, *shown].join(' ')
53
+ end
54
+ super(*args, **opts, &block) unless noop?
55
+ end
56
+ end
57
+
58
+ # Append text to the file.
59
+ def append(text)
60
+ $stderr.puts "append #{self}" if trace?
61
+ File.open(to_s, 'a'){ |f| f << text } unless noop?
62
+ end
63
+
64
+ # Pathname methods that give other paths.
65
+ DERIVING_METHODS = [
66
+ :+, :/, :join, :parent, :dirname, :basename, :children, :entries,
67
+ :expand_path, :realpath, :realdirpath, :cleanpath, :sub, :sub_ext,
68
+ :relative_path_from, :readlink, :glob
69
+ ]
70
+
71
+ DERIVING_METHODS.each do |name|
72
+ next unless ::Pathname.method_defined?(name)
73
+ define_method(name) do |*args, **opts, &block|
74
+ adopt(super(*args, **opts, &block))
75
+ end
76
+ end
77
+
78
+ #
79
+ def each_child(with_directory=true, &block)
80
+ children(with_directory).each(&block)
81
+ end
82
+
83
+ private
84
+
85
+ # Give a derived Pathname, or a list of them, to this path's shell.
86
+ def adopt(result)
87
+ case result
88
+ when ::Pathname
89
+ self.class.new(result.to_s, @shell)
90
+ when Array
91
+ result.map{ |r| adopt(r) }
92
+ else
93
+ result
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ end
data/lib/ratch/shell.rb CHANGED
@@ -2,6 +2,8 @@ require 'pathname'
2
2
  require 'fileutils'
3
3
  require 'ratch/core_ext'
4
4
  require 'ratch/batch'
5
+ require 'ratch/path'
6
+ require 'ratch/commands'
5
7
 
6
8
  module Ratch
7
9
 
@@ -55,6 +57,8 @@ module Ratch
55
57
  # :trace report each step (also :verbose)
56
58
  # :dryrun both :noop and :trace
57
59
  #
60
+ # And :commands, to have system commands as methods (see Commands).
61
+ #
58
62
  def initialize(*path)
59
63
  opts = (Hash === path.last ? path.pop : {})
60
64
  opts = opts.each_with_object({}){ |(k, v), h| h[k.to_sym] = v }
@@ -66,6 +70,8 @@ module Ratch
66
70
  raise FileNotFound, "#{path}" unless ::File.directory?(path)
67
71
 
68
72
  @_work = Pathname.new(path).expand_path
73
+
74
+ extend Commands if opts[:commands]
69
75
  end
70
76
 
71
77
  private
@@ -79,7 +85,7 @@ module Ratch
79
85
 
80
86
  # The options a new shell needs to be in the same mode as this one.
81
87
  def mode_options
82
- {:quiet=>quiet?, :noop=>noop?, :trace=>trace?}
88
+ {:quiet=>quiet?, :noop=>noop?, :trace=>trace?, :commands=>Commands === self}
83
89
  end
84
90
 
85
91
  public
@@ -141,7 +147,7 @@ module Ratch
141
147
 
142
148
  # Current working path.
143
149
  def work(*parts)
144
- return @_work if parts.empty?
150
+ return Path.new(@_work, self) if parts.empty?
145
151
  dir(File.join(*parts.map{ |part| part.to_s }))
146
152
  end
147
153
 
@@ -165,24 +171,25 @@ module Ratch
165
171
  Batch.all(work, *patterns, mode_options)
166
172
  end
167
173
 
168
- # A Pathname for a path, which need not exist.
174
+ # A Path for a path, which need not exist. A Path is a Pathname that
175
+ # keeps to the shell's mode.
169
176
  def path(path)
170
- Pathname.new(localize(path))
177
+ Path.new(localize(path), self)
171
178
  end
172
179
  alias_method :pathname, :path
173
180
 
174
- # A Pathname for a file, which must exist.
181
+ # A Path for a file, which must exist.
175
182
  def file(path)
176
183
  path = localize(path)
177
184
  raise FileNotFound, path unless File.file?(path)
178
- Pathname.new(path)
185
+ Path.new(path, self)
179
186
  end
180
187
 
181
- # A Pathname for a directory, which must exist.
188
+ # A Path for a directory, which must exist.
182
189
  def dir(path)
183
190
  path = localize(path)
184
191
  raise FileNotFound, path unless File.directory?(path)
185
- Pathname.new(path)
192
+ Path.new(path, self)
186
193
  end
187
194
 
188
195
  # Lists all entries, as Dir.entries does, but as Pathnames.
@@ -256,6 +263,34 @@ module Ratch
256
263
  end
257
264
  end
258
265
 
266
+ # Run a command with the Ruby that is running now, as #sh would.
267
+ #
268
+ # shell.ruby '-Ilib', 'script/build.rb'
269
+ # shell.ruby '-Ilib script/build.rb'
270
+ #
271
+ def ruby(*args)
272
+ require 'rbconfig'
273
+ run_command(RbConfig.ruby, *args)
274
+ end
275
+
276
+ # An object that runs any method called on it as a command, from the
277
+ # working directory and in the shell's mode, as #sh would.
278
+ #
279
+ # shell.cmd.git 'status'
280
+ # shell.cmd.rdoc 'lib', :op=>'doc', :q=>true
281
+ #
282
+ # Several arguments are passed to the command as they are, and a
283
+ # final Hash becomes options (--op=doc -q). A single String is taken
284
+ # as the rest of a command line, and goes through the system shell:
285
+ #
286
+ # shell.cmd.ls '-l *.rb'
287
+ #
288
+ # Include the Commands module, or make the shell with the :commands
289
+ # option, to call commands on the shell itself.
290
+ def cmd
291
+ Commander.new(self)
292
+ end
293
+
259
294
  # Change working directory.
260
295
  #
261
296
  # With a block, the shell stands at +path+ for the length of the block
@@ -536,6 +571,17 @@ module Ratch
536
571
  end
537
572
  end
538
573
 
574
+ # Run a program with arguments given as they are to a method: see #cmd.
575
+ def run_command(name, *args)
576
+ name = name.to_s
577
+ if args.size == 1 && String === args.first
578
+ require 'shellwords'
579
+ sh("#{Shellwords.escape(name)} #{args.first}")
580
+ else
581
+ sh(name, *args.to_argv)
582
+ end
583
+ end
584
+
539
585
  # Run a block with the process's working directory set to the shell's.
540
586
  # Nothing in Shell itself needs this; it is for extensions that call
541
587
  # code which only knows the process's directory.
data/lib/ratch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ratch
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ratch
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trans
@@ -129,6 +129,7 @@ files:
129
129
  - bin/ratch
130
130
  - lib/ratch.rb
131
131
  - lib/ratch/batch.rb
132
+ - lib/ratch/commands.rb
132
133
  - lib/ratch/core_ext.rb
133
134
  - lib/ratch/core_ext/facets.rb
134
135
  - lib/ratch/core_ext/filetest.rb
@@ -138,6 +139,7 @@ files:
138
139
  - lib/ratch/core_ext/to_yamlfrag.rb
139
140
  - lib/ratch/core_ext/unfold_paragraphs.rb
140
141
  - lib/ratch/file_list.rb
142
+ - lib/ratch/path.rb
141
143
  - lib/ratch/script.rb
142
144
  - lib/ratch/script/help.rb
143
145
  - lib/ratch/shell.rb