Skip to content

Instantly share code, notes, and snippets.

@azriel91
Created December 7, 2018 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azriel91/0ef7f8c7aea671b1c2237a3706656a2b to your computer and use it in GitHub Desktop.
Save azriel91/0ef7f8c7aea671b1c2237a3706656a2b to your computer and use it in GitHub Desktop.
#! /bin/bash
#
# This is ~~unsafe~~ bash.
#
# Rudimentary automation to update Rust code to use idiomatic 2018.
#
# If you have spaces in your paths, this script will probably not work for you.
#
# Author: azriel
# Moves src/submodule/mod.rs to src/submodule.rs
function rename_mod_rs_to_module {
for f in $(find . -type f -name "mod.rs")
do
echo mv $f ${f%%/mod.rs}.rs
done
}
# Expects GNU `sed`. Not sure if BSD `sed` works here (OS X users, check `sed --version`).
function cargo_toml_insert_edition {
for f in $(find . -type f -name "Cargo.toml")
do
# Skip file if it already has edition string
#
# Rudimentary detection for workspace Cargo.toml:
# Only adds the edition if there is an `authors` key.
# Note: it expects the `authors` key to be one line.
#
# Idempotent - safe to run multiple times.
grep -qF 'edition =' $f || sed -i 's/^\(authors = .\+\)$/\1\nedition = "2018"/' $f
done
}
function rogue {
rename_mod_rs_to_module
cargo fix --edition
cargo_toml_insert_edition
cargo fix --edition-idioms
git add .
git commit -m "Migrate to Rust 2018"
}
function historian {
rename_mod_rs_to_module
git add .
git commit -m 'Renamed all `mod.rs` to the module short name.'
cargo fix --edition
git add .
git commit -m 'Run `cargo fix --edition` over repository.'
cargo_toml_insert_edition
git add .
git commit -m 'Specify `edition = "2018"` in `Cargo.toml`.'
cargo fix --edition-idioms
git add .
git commit -m 'Run `cargo fix --edition-idioms` over repository.'
}
function archeologist {
rename_mod_rs_to_module
cargo test --all
git add .
git commit -m 'Renamed all `mod.rs` to the module short name.'
cargo fix --edition
cargo test --all
git add .
git commit -m 'Run `cargo fix --edition` over repository.'
cargo_toml_insert_edition
cargo test --all
git add .
git commit -m 'Specify `edition = "2018"` in `Cargo.toml`.'
cargo fix --edition-idioms
cargo test --all
git add .
git commit -m 'Run `cargo fix --edition-idioms` over repository.'
}
# Stop on first error
set -e
archeologist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment