Skip to content

Commit

Permalink
Add rustfmt to the update-rustc script
Browse files Browse the repository at this point in the history
With a mechanism to ignore particular listings for various reasons
  • Loading branch information
carols10cents committed Jan 26, 2020
1 parent bc8c9f1 commit dec27ee
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ADMIN_TASKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ listing beyond the most trivial should be extracted into a file. To do that:
of user input or external events like making a web request), keep the output inline but make a
comment that contains `manual-regeneration` and instructions for manually updating the inline
output.
- If you don't want this example to even be attempted to be formatted by `rustfmt` (for example
because the example doesn't parse on purpose), add a `rustfmt-ignore` file in the listing's
directory and the reason it's not being formatted as the contents of that file (in case it's a
rustfmt bug that might get fixed someday).

## See the effect of some change on the rendered book

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This listing deliberately doesn't parse so rustfmt fails.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We have some weird comments pointing out borrowing scopes that we don't want to change;
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We have some weird comments pointing out borrowing scopes that we don't want to change;
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We have some weird comments pointing out borrowing scopes that we don't want to change;
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We have some weird comments pointing out borrowing scopes that we don't want to change;
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We have some weird comments pointing out borrowing scopes that we don't want to change;
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We have some weird comments pointing out borrowing scopes that we don't want to change;
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We have some weird comments pointing out borrowing scopes that we don't want to change;
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We have some weird comments pointing out borrowing scopes that we don't want to change;
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This listing is used for demonstrating how to set up a workspace, but the workspace isn't
completely set up yet, so rustfmt complains the crate mentioned in Cargo.toml doesn't exist yet.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This listing deliberately doesn't parse so rustfmt fails.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 80
5 changes: 3 additions & 2 deletions tools/src/bin/release_listings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fn main() -> Result<(), Box<dyn Error>> {
//
// - `target` directories
// - `output.txt` files used to display output in the book
// - `rustfmt-ignore` files used to signal to update-rustc.sh the listing shouldn't be formatted
// - anchor comments or snip comments
// - empty `main` functions in `lib.rs` files used to trick rustdoc
fn copy_cleaned_listing_files(from: PathBuf, to: PathBuf) -> Result<(), Box<dyn Error>> {
Expand All @@ -84,8 +85,8 @@ fn copy_cleaned_listing_files(from: PathBuf, to: PathBuf) -> Result<(), Box<dyn
copy_cleaned_listing_files(item_path, output_item)?;
}
} else {
// Don't copy output files
if item_name != "output.txt" {
// Don't copy output files or files that tell update-rustc.sh not to format
if item_name != "output.txt" && item_name != "rustfmt-ignore" {
let item_extension = item_path.extension();
if item_extension.is_some() && item_extension.unwrap() == "rs" {
copy_cleaned_rust_file(item_name, &item_path, &output_item)?;
Expand Down
14 changes: 13 additions & 1 deletion tools/update-rustc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ set -eu
echo 'Building book into `tmp/book-before` before updating...'
mdbook build -d tmp/book-before

# TODO: Rustfmt all listings here
# Rustfmt all listings
echo 'Formatting all listings...'
find -s listings -name Cargo.toml -print0 | while IFS= read -r -d '' f; do
dir_to_fmt=$(dirname $f)

# There are a handful of listings we don't want to rustfmt and skipping doesn't work;
# those will have a file in their directory that explains why.
if [ ! -f "${dir_to_fmt}/rustfmt-ignore" ]; then
cd $dir_to_fmt
cargo fmt --all && true
cd - > /dev/null
fi
done

# Get listings without anchor comments in tmp by compiling a release listings artifact
echo 'Generate listings without anchor comments...'
Expand Down

0 comments on commit dec27ee

Please sign in to comment.