Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Bash useful scripts

Managing multiple Rust projects

If there is a Rust project, containing multiple Rust subprojects and all of them are placed in a single parent directory and each project is a Git repository, then the following script may be helpful to issue common commands for all of them as a single command.

This script does the following:

  • lists all directories,
  • executes the specified command for each repository.
#!/usr/bin/env bash

action () {
  case "$1" in
    "clean")  set_up $1 $2 && cargo clean                             && tear_down ;;
    "build")  set_up $1 $2 && cargo build                             && tear_down ;;
    "clippy") set_up $1 $2 && cargo clippy                            && tear_down ;;
    "test")   set_up $1 $2 && cargo test -q 2>/dev/null               && tear_down ;;
    "deps")   set_up $1 $2 && cargo tree | grep --color=never dmntk   && tear_down ;;
    "status") set_up $1 $2 && git status -sb                          && tear_down ;;
    *) usage $1
  esac
}

set_up () {
  echo ""
  echo "========================"
  echo "$1:  $2"
  echo "========================"
  cd $2
}

tear_down () {
  cd ..
}

usage () {
  echo ""
  echo "unknown command: '$1'"
  echo ""
  echo "commands:"
  echo "  clean    => cargo clean "
  echo "  build    => cargo build"
  echo "  clippy   => cargo clippy"
  echo "  test     => cargo test -q 2>/dev/null"
  echo "  deps     => cargo tree | grep --color=never dmntk"
  echo "  status   => fit status -sb"
  echo ""
  exit 1
}

ls -d */ | while read line
do
  action $1 $line
done