Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions HISTORY.rdoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
= RELEASE HISTORY

== Unreleased (2.1.0)

This release takes in what was worth keeping from ProUtils, a later
reworking of Ratch's utilities, which is now retired.

Changes:

* Add Ratch::Path, a Pathname that keeps to its shell's mode. Shell#path,
#file, #dir, #work, #parent, #home and #root return one, so that
<tt>shell.path('tmp').rmtree</tt> removes nothing in noop mode, and is
reported when tracing. Before, these were plain Pathnames, which took
no notice of the mode.
* Add Shell#ruby, to run a command with the present Ruby.
* Add Shell#cmd, which runs any method called on it as a command:
<tt>shell.cmd.git 'status'</tt>.
* Add Ratch::Commands, which is optional, to call commands on the shell
itself: <tt>shell.git 'status'</tt>. A shell has it when made with the
+commands+ option (<tt>--commands</tt> for the +ratch+ command) or
when the module is included. Only a program found on the PATH is taken
as a command, so a misspelt method is still an error.
* The +ratch+ command takes options before the script's name as well as
after it.
* Array#to_list drops nil elements.
* Object#to_yamlfrag handles tagged documents and the end mark of a
plain value.

Fixes:

* FileTest.bin? raised if there was no PATH.


== 2.0.0 | 2026-11-25

Ratch is revived, for Ruby 3.1 and up. Neither 1.2.0 nor the unreleased
Expand Down
33 changes: 33 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,42 @@ The first three can also be given to a single command:

shell.rm('old.txt', :dryrun=>true)

The paths a shell gives out (+path+, +file+, +dir+, +work+, +parent+) are
Pathnames that keep to its mode too:

shell.path('tmp').rmtree # prints "rmtree /.../project/tmp", in a dry run

To build on it, subclass Ratch::Shell, or keep one and delegate to it.


== Commands

Besides +sh+, there is +ruby+, which runs the Ruby that is running now,
and +cmd+, which runs any method called on it as a command:

shell.ruby '-Ilib', 'script/build.rb'

shell.cmd.git 'status'
shell.cmd.rdoc 'lib', :op=>'doc', :q=>true # rdoc --op=doc -q lib
shell.cmd.ls '-l *.rb' # one string is a command line

If you would rather call commands on the shell itself, ask for it:

shell = Ratch::Shell.new('project', :commands=>true)

shell.git 'status'

This is optional because it changes what a mistake does. To keep that
small, only a program found on the PATH is taken as a command; any other
unknown method is still a NoMethodError. The shell's own methods, and
Ruby's (+test+, +sleep+, +exit+), come first. A Shell subclass gets the
same by including Ratch::Commands, and a script by <tt>--commands</tt>:

$ ratch publish.ratch --commands

All of these run from the shell's directory and follow its mode.


== Batches

A Ratch::Batch does the same over a list of files.
Expand Down
12 changes: 11 additions & 1 deletion bin/ratch
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#!/usr/bin/env ruby
require 'ratch/script'
Ratch::Script.execute(ARGV.shift)

# The script is the first argument that isn't an option, so options can
# be given before it or after it.
index = ARGV.index{ |arg| !arg.start_with?('-') }

unless index
$stderr.puts "Usage: ratch [OPTIONS] SCRIPT [ARGUMENTS]"
exit 1
end

Ratch::Script.execute(ARGV.delete_at(index))
72 changes: 72 additions & 0 deletions lib/ratch/commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module Ratch

# Commands lets system commands be called as if they were methods of
# a shell:
#
# shell.git 'status'
# shell.rdoc '--op', 'doc', 'lib'
#
# It is optional. A shell has it when made with the :commands option,
# or when the module is included in a Shell subclass or used to extend
# one shell:
#
# shell = Ratch::Shell.new('project', :commands=>true)
#
# A method the shell doesn't have is taken as a command only if there
# is a program of that name on the PATH. Anything else is the
# NoMethodError it would be without this, so a misspelt method is still
# caught.
#
# The command is run by Shell#sh, so it is run from the shell's
# directory and follows the shell's mode. See Shell#cmd for the
# arguments, and for a way to do this without the module.
module Commands

#
def method_missing(name, *args, &block)
return super if block || !command?(name)
run_command(name, *args)
end

#
def respond_to_missing?(name, include_private=false)
command?(name) || super
end

private

# Is there a program of this name on the PATH?
def command?(name)
name = name.to_s
return false unless name =~ /\A[A-Za-z0-9][\w.+-]*\z/
ENV['PATH'].to_s.split(File::PATH_SEPARATOR).any? do |dir|
file = File.join(dir, name)
File.file?(file) && File.executable?(file)
end
end

end

# What Shell#cmd gives: an object that runs any method called on it as
# a command of its shell.
class Commander < BasicObject

#
def initialize(shell)
@shell = shell
end

#
def method_missing(name, *args, &block)
return super if block
@shell.__send__(:run_command, name, *args)
end

#
def respond_to_missing?(name, include_private=false)
true
end

end

end
26 changes: 8 additions & 18 deletions lib/ratch/core_ext/filetest.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
module FileTest

# Return a cached list of the PATH environment variable.
# This is a support method used by #bin?
module_function

# The directories of the PATH. Empty if there is none.
def command_paths
@command_paths ||= ENV['PATH'].split(/[:;]/)
ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
end

# Is a file a bin/ executable?
#
# TODO: Make more robust. Probably needs to be fixed for Windows.
# Is there a file of this name in a directory of the PATH? Returns
# the name if so, and false if not.
def bin?(fname)
is_bin = command_paths.any? do |f|
FileTest.exist?(File.join(f, fname))
is_bin = command_paths.any? do |dir|
FileTest.exist?(File.join(dir, fname.to_s))
end
#is_bin ? File.basename(fname) : false
is_bin ? fname : false
end

## Is a file a task?
#
#def task?(path)
# task = File.dirname($0) + "/#{path}"
# task.chomp!('!')
# task if FileTest.file?(task) && FileTest.executable?(task)
#end

end

2 changes: 1 addition & 1 deletion lib/ratch/core_ext/to_list.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Array

def to_list
self
compact
end

end
Expand Down
14 changes: 11 additions & 3 deletions lib/ratch/core_ext/to_yamlfrag.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
require 'yaml'

class Object

#
# Same as #to_yaml but without the initial `---` line, or the `...`
# line that ends a document holding a plain value.
def to_yamlfrag
to_yaml.sub("---",'').rstrip
str = to_yaml
if /\A--- !/ =~ str
str = str.sub(/\A--- !.*?$/, '')
else
str = str.sub(/\A---\s*/, '')
end
str.chomp("\n...\n").rstrip
end

end

5 changes: 1 addition & 4 deletions lib/ratch/core_ext/unfold_paragraphs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ class String

#
def unfold_paragraphs
blank = false
text = ''
text = String.new
split(/\n/).each do |line|
if /\S/ !~ line
text << "\n\n"
blank = true
else
if /^(\s+|[*])/ =~ line
text << (line.rstrip + "\n")
else
text << (line.rstrip + " ")
end
blank = false
end
end
text = text.gsub("\n\n\n","\n\n")
Expand Down
99 changes: 99 additions & 0 deletions lib/ratch/path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'pathname'

module Ratch

# A Path is a Pathname that belongs to a shell and keeps to its mode:
# when the shell is in noop mode, the methods that change the file
# system do nothing, and when it is tracing, they are reported to
# standard error first.
#
# shell = Ratch::Shell.new('project', :dryrun=>true)
# shell.path('tmp').rmtree # prints "rmtree /.../project/tmp" and removes nothing
#
# Paths derived from one (by #+, #parent, #children and so on) belong
# to the same shell. In every other way it is a Pathname, and equal to
# the Pathname of the same path. A Path made without a shell behaves as
# a Pathname.
#
# Files opened for writing with #open are not covered.
class Path < ::Pathname

#
def initialize(path, shell=nil)
super(path.to_s)
@shell = shell
end

# The shell the path belongs to, if any.
attr_reader :shell

# Is the path's shell in noop mode?
def noop?
@shell ? !!@shell.noop? : false
end

# Is the path's shell tracing?
def trace?
@shell ? !!@shell.trace? : false
end

# Pathname methods that change the file system.
WRITE_METHODS = [
:chmod, :lchmod, :chown, :lchown, :make_link, :make_symlink,
:rename, :truncate, :utime, :mkdir, :mkpath, :rmdir, :rmtree,
:unlink, :delete, :write, :binwrite
]

WRITE_METHODS.each do |name|
next unless ::Pathname.method_defined?(name)
define_method(name) do |*args, **opts, &block|
if trace?
shown = [:write, :binwrite].include?(name) ? [] : args
$stderr.puts [name, self, *shown].join(' ')
end
super(*args, **opts, &block) unless noop?
end
end

# Append text to the file.
def append(text)
$stderr.puts "append #{self}" if trace?
File.open(to_s, 'a'){ |f| f << text } unless noop?
end

# Pathname methods that give other paths.
DERIVING_METHODS = [
:+, :/, :join, :parent, :dirname, :basename, :children, :entries,
:expand_path, :realpath, :realdirpath, :cleanpath, :sub, :sub_ext,
:relative_path_from, :readlink, :glob
]

DERIVING_METHODS.each do |name|
next unless ::Pathname.method_defined?(name)
define_method(name) do |*args, **opts, &block|
adopt(super(*args, **opts, &block))
end
end

#
def each_child(with_directory=true, &block)
children(with_directory).each(&block)
end

private

# Give a derived Pathname, or a list of them, to this path's shell.
def adopt(result)
case result
when ::Pathname
self.class.new(result.to_s, @shell)
when Array
result.map{ |r| adopt(r) }
else
result
end
end

end

end
Loading
Loading