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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Tests
run: bundle exec rake test
run: bundle exec rake test demo
7 changes: 7 additions & 0 deletions HISTORY.rdoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
= RELEASE HISTORY

== Unreleased

* Bring back the QED demonstrations of Shell, Script and Batch, which
were dropped in 2.0.0. They are in <tt>demo/</tt>, brought up to date,
and run in CI. The Shell one gains sections on standing at another
directory, noop mode and system calls.

== 2.1.0 | 2026-11-25

This release takes in what was worth keeping from ProUtils, a later
Expand Down
9 changes: 9 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ the directory it is in.
$ ludo script/test --verbose # runs project/script/test


== Demonstrations

The +demo+ directory has walkthroughs of Shell, Script and Batch. They
are QED[https://github.com/rubyworks/qed] documents, so each example in
them is run and checked:

$ bundle exec rake demo


== Installation

$ gem install ratch
Expand Down
7 changes: 6 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ task 'test' do
sh 'rubytest -Ilib -r spectrum/testcase -r ./test/helper test/case_*.rb'
end

task 'default' => 'test'
desc 'run the QED demonstrations'
task 'demo' do
sh 'qed -Ilib demo'
end

task 'default' => ['test', 'demo']
240 changes: 240 additions & 0 deletions demo/01_shell.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
= Ratch::Shell

The Shell class mimics a basic shell command line prompt.

require 'ratch/shell'

The demonstration sets up sample directory consisting of the following
entries in a temporary directory:

foo.txt
zoo/
zoo/bar.txt

== Initial Path

When no path argument is provided to the initializer, the present working path
is used. The path is stored as a Pathname object.

shell = Ratch::Shell.new
shell.work.assert = Pathname.new(Dir.pwd).expand_path

Otherwise the path given is used.

shell = Ratch::Shell.new('.')
shell.work.assert = Pathname.new('.').expand_path

shell = Ratch::Shell.new('..')
shell.work.assert = Pathname.new('..').expand_path

An error is raised if the path does not exist.

expect Ratch::FileNotFound do
Ratch::Shell.new('not_a_path')
end

== Processing Options

The +initialize+ method accepts a few options. The +quiet+ option suppresses
all output to stdout.

shell = Ratch::Shell.new(:quiet=>true)
shell.assert.quiet?

The +noop+ option prevents any actual writing to disk --the process will
simply pretend that it has done so.

shell = Ratch::Shell.new(:noop=>true)
shell.assert.noop?

The +trace+ option provides step by step feedback on what is taking place.

shell = Ratch::Shell.new(:trace=>true)
shell.assert.trace?

The +dryrun+ option is simply a combination of +noop+ and +trace+.

shell = Ratch::Shell.new(:dryrun=>true)
shell.assert.noop?
shell.assert.trace?
shell.assert.dryrun?

== Equality

For two Shell objects to be considered equal, via #==, they must both
be instances of Ratch::Shell (or subclass) and have the same working
path.

shell1 = Ratch::Shell.new('.')
shell2 = Ratch::Shell.new('.')
shell1.assert = shell2

shell1 = Ratch::Shell.new('.')
shell2 = Ratch::Shell.new('..')
shell1.refute = shell2

The more strict #eql? method also ensures that the +noop+ option is the same.

shell1 = Ratch::Shell.new('.', :noop=>true)
shell2 = Ratch::Shell.new('.', :noop=>true)
shell1.assert.eql? shell2

shell1 = Ratch::Shell.new('.', :noop=>true)
shell2 = Ratch::Shell.new('.', :noop=>false)
shell1.refute.eql? shell2

== File System Locations

The Shell class provides a few convenient methods for accessing common
locations in the file system, namely #work, #home, #parent and #root.

shell = Ratch::Shell.new

shell.work.assert == Pathname.new('.').expand_path
shell.parent.assert == Pathname.new('..').expand_path

The current user's home directory is accessible via the #home method.

shell.home.assert == Pathname.new('~').expand_path

The system's root folder can be accessed via #root method.

shell.root.assert == Pathname.new('/').expand_path

In addition the Shell class provides methods for accessing pathnames
relative to the current shell directory. The #path (or #pathname) method
returns a Pathname object with the given path.

shell.path('foo')

This will work regardless if the path actually exists or not. On the other hand,
the #file and #dir methods will do the same, but will raise an error if the path
given is not an existing file or a directory, respectively.

shell.file('foo.txt')

expect Ratch::FileNotFound do
shell.file('not.txt')
end

shell.dir('zoo')

expect Ratch::FileNotFound do
shell.dir('not')
end

== Entries

Shell#entries works just like Dir.entries except that each path is returned
as a Pathname object. As with Dir.entries, the order is the file system's
own, so we sort them here.

shell = Ratch::Shell.new

shell.entries.sort.assert == ['.', '..', 'foo.txt', 'zoo'].map{|f| Pathname.new(f)}

The #file_entries method limits the list to files only.

shell.file_entries.assert == ['foo.txt'].map{|f| Pathname.new(f)}

Whereas #directory_entries (or #dir_entries) limits the list to directories.

shell.directory_entries.sort.assert == ['.', '..', 'zoo'].map{|f| Pathname.new(f)}

Of course, having to deal with the '.' and '..' is annoying most of the time
so Shell provides more convenient methods. Like #entries, #pathnames
provides a list of entries less the dot paths.

shell.pathnames.sort.assert == ['foo.txt', 'zoo'].map{|f| Pathname.new(f)}

The #directories (also #folders) method limits this list to directories only.

shell.directories.assert == ['zoo'].map{|f| Pathname.new(f)}

And #files works out to be the same as #file_entries.

shell.files.assert == ['foo.txt'].map{|f| Pathname.new(f)}

== Globbing

shell = Ratch::Shell.new

shell.glob('*').assert == ['foo.txt', 'zoo']

== File Testing

Shell provides an interface to most of FileTest's functions.

shell.assert.exist?('foo.txt')
shell.assert.file?('foo.txt')
shell.refute.directory?('foo.txt')
shell.assert.directory?('zoo')

== File IO

Shell can be used to easily read the contents of a file.

shell = Ratch::Shell.new

shell.read('foo.txt').assert == 'SAMPLE FOO.TXT'

As well as write a new file.

shell.write('baz.txt', 'BAZ TEXT')
shell.read('baz.txt').assert == 'BAZ TEXT'

Or append to an existing file.

shell.append('baz.txt', ' 2')
shell.read('baz.txt').assert == 'BAZ TEXT 2'

The shell command supports many file methods, including #rm.

shell.rm('baz.txt')

== Standing Elsewhere

A shell's paths are taken from the directory it stands at, which need not
be the directory the program is running in. The program's own working
directory is left alone.

zoo = Ratch::Shell.new('zoo')

zoo.read('bar.txt').assert == 'SAMPLE ZOO/BAR.TXT'

zoo.files.assert == ['bar.txt'].map{|f| Pathname.new(f)}

Dir.pwd.assert == shell.work.to_s

So shells at different directories can be used side by side.

shell.cp('foo.txt', 'zoo')

zoo.assert.file?('foo.txt')

== Pretending

In +noop+ mode a shell goes through the motions and changes nothing.

pretend = Ratch::Shell.new(:noop=>true)

pretend.rm('foo.txt')

shell.assert.file?('foo.txt')

== System Calls

The Shell class can also call out to system commands, with #sh. They
run from the shell's directory. It returns true if the command succeeded.

zoo.sh('echo hello > out.txt').assert == true

zoo.read('out.txt').assert == "hello\n"

zoo.sh('exit 1').assert == false

A command can also be called as a method of #cmd.

zoo.cmd.touch('made.txt')

zoo.assert.file?('made.txt')
34 changes: 34 additions & 0 deletions demo/02_script.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
= Ratch::Script

The Ratch::Script class is the context used for evaluating Ratch-based
batch files, aka shell scripts.

require 'ratch/script'

In most every respect the Script class behaves like Ratch::Shell, except that
its initializer takes a file name to be evaluated.

Let's say we have a Ratch script called 'foo.ratch':

#!/usr/bin/env ratch

@test_message = "Hello World!"

We can load the script via Ratch::Script.new.

script = Ratch::Script.new('foo.ratch')

The file name can be accessed from the script using #script_file.

script.script_file.assert == 'foo.ratch'

To execute a script, use the `#execute!` method, or its alias `#run!`.

script.execute!

We can see that the script did indeed execute by looking for the instance
variable it set.

msg = script.instance_variable_get('@test_message')
msg.assert == "Hello World!"

71 changes: 71 additions & 0 deletions demo/03_batch.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
= Ratch::Batch

The Batch class makes it possible to work with multiple files
at once as easily as one would work with a single file.

require 'ratch/batch'

The demonstration sets up a sample directory consisting of the following
entries in a temporary directory:

foo.txt
bar.txt
zoo/
zoo/one.txt
zoo/two.txt

To create a new Batch instance, pass the base directory and any
inclusion patterns to the initializer.

batch = Ratch::Batch.new('.', '*.txt')

In this example we will get every `.txt` file at the toplevel of the base
directory.

batch.to_a.assert = ['bar.txt', 'foo.txt'].to_pathnames

To get a list of files relative to the base directory, use #entries.

batch.entries.assert = ['bar.txt', 'foo.txt'].to_pathnames

Internally a Batch instance tracks the files selected via a Ratch::FileList
object. This can be directly accessed via the #list method.

batch.file_list.assert.is_a?(Ratch::FileList)

batch.file_list.to_a.assert = ['bar.txt', 'foo.txt']

As with the delegated FileTest methods of the Shell class, Batch can test
the set of files in aggregate. For example, to ensure all the entries
are files we can use the #file? method.

batch.assert.file?

Likewise to ensure none of the entries are directories we can use the #directory?
method.

batch.refute.directory?

The Batch list can be reduced to just files or just directories via the #file!
and #directory! methods.

batch = Ratch::Batch.new('.', '*')
batch.file!
batch.list.assert = ['bar.txt', 'foo.txt']

batch = Ratch::Batch.new('.', '*')
batch.directory!
batch.list.assert = ['zoo']

The Batch class is enumerable, both #each and #size are defined.

batch = Ratch::Batch.new('.', '*.txt')

batch.each do |pathname|
pathname.assert.is_a?(Pathname)
end

batch.size.assert = 2

Any enumerable method is likewise applicable.

Loading
Loading