Skip to content
rubyworksPublic

About

Ruby Batch Scripts

Resources

Stars

4 stars

Watchers

0 watching

Forks

Latest commit

 

History

74 Commits

Folders and files

Repository files navigation

Source Code / Report Issue <img src=“https://github.com/rubyworks/ratch/actions/workflows/ci.yml/badge.svg” alt=“CI” />

A file system shell in code, and batch scripts in Ruby.

Ratch needs Ruby 3.1 or newer.

A Ratch::Shell stands at a directory and works from there, as a shell prompt does. Paths given to it are taken from its directory, whatever the process’s own working directory is.

require 'ratch'

shell = Ratch::Shell.new('project')

shell.file?('README.md')            #=> true
shell.glob('lib/**/*.rb')           #=> ["lib/project.rb", ...]
shell.read('VERSION')               #=> "1.0.0\n"

shell.mkdir_p('tmp/cache')
shell.cp('README.md', 'tmp')
shell.write('tmp/stamp', Time.now.to_s)
shell.sh('git status')              #=> true, if the command succeeded

It has the tests of FileTest, the operations of FileUtils (mkdir, rm, cp, mv, ln, install, chmod, touch and their variations), read, write and append, glob, and sh to run a command.

Moving about:

lib = shell / 'lib'                 # a new shell at project/lib

shell.cd('lib')                     # move this shell
shell.cd('..')

shell.cd('lib') do                  # move it for a block only
  shell.files                       #=> [#<Pathname:project.rb>, ...]
end

A shell can be put in a mode in which it only pretends, to rehearse a script before it touches anything:

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

shell.rm_r('tmp')                   # prints "rm -r tmp" and removes nothing
shell.sh('make install')            # prints the command and doesn't run it

The modes are :noop (change nothing), :trace (report each step to standard error), :dryrun (both) and :quiet. The first three can also be given to a single command:

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

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

A Ratch::Batch does the same over a list of files.

batch = shell.batch('**/*.txt')

batch.file?                         # are they all files?
batch.cp('backup')
batch.rename{ |name| name.sub('.txt', '.text') }

It keeps its files in a Ratch::FileList, which is Rake’s lazy file list: patterns are resolved when the list is first read, and version control directories, backup and core files are left out.

The ratch command runs a Ruby script inside a shell at the present directory, so the script can use all of the above without a receiver.

#!/usr/bin/env ratch

glob('*.md').each do |file|
  cp file, 'site'
end

sh 'rsync -a site/ host:/var/www'

The shell’s modes can be given on the command line, and anything else is in arguments:

$ ./publish.ratch --dryrun

These modules add to a shell when included in a subclass:

Ratch::ZlibUtils

gzip and ungzip

Ratch::TarUtils

tar, untar, tar_gzip and untar_gzip

Ratch::ConfigUtils

configuration, to load a YAML file

Ratch::RDocUtils

rdoc, to generate documentation (needs the rdoc gem on Ruby 4)

Ratch::EmailUtils

email, to send a message by SMTP (needs the net-smtp gem)

Ratch::XDGUtils

xdg (needs the xdg gem)

Ratch::CLI

takes options and arguments from the command line

The email delivery itself is not covered by the tests.

Ratch also comes with the ludo command, which stands for “lookup and do”. It looks for an executable script by name in the present directory and then in each directory above it, and runs the first it finds, from the directory it is in.

$ cd project/lib/project
$ ludo script/test --verbose        # runs project/script/test
$ gem install ratch

Copyright © 2008 Thomas Sawyer, Rubyworks

Ratch is distributed under the terms of the Apache License, Version 2.0. See LICENSE.txt for details.

About

Ruby Batch Scripts

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages