2016-04-01 20:48:11 -07:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
|
#
|
2025-06-03 00:18:05 +02:00
|
|
|
# This runs Python files, fish scripts (*.fish), and Rust files
|
|
|
|
|
# through their respective code formatting programs.
|
2016-04-01 20:48:11 -07:00
|
|
|
#
|
2025-06-03 00:18:05 +02:00
|
|
|
# `--all`: Format all eligible files instead of the ones specified as arguments.
|
|
|
|
|
# `--check`: Instead of reformatting, fail if a file is not formatted correctly.
|
|
|
|
|
# `--force`: Proceed without asking if uncommitted changes are detected.
|
|
|
|
|
# Only relevant if `--all` is specified but `--check` is not specified.
|
|
|
|
|
|
2020-05-15 07:56:06 +02:00
|
|
|
set -l fish_files
|
|
|
|
|
set -l python_files
|
2023-06-01 18:14:12 +02:00
|
|
|
set -l rust_files
|
2020-05-15 07:56:06 +02:00
|
|
|
set -l all no
|
2016-04-01 20:48:11 -07:00
|
|
|
|
2025-06-03 00:18:05 +02:00
|
|
|
argparse all check force -- $argv
|
|
|
|
|
or exit $status
|
|
|
|
|
|
|
|
|
|
if set -l -q _flag_all
|
2016-04-01 20:48:11 -07:00
|
|
|
set all yes
|
2025-06-03 00:18:05 +02:00
|
|
|
if set -q argv[1]
|
|
|
|
|
echo "Unexpected arguments: '$argv'"
|
|
|
|
|
exit 1
|
|
|
|
|
end
|
2016-04-01 20:48:11 -07:00
|
|
|
end
|
|
|
|
|
|
2025-09-13 10:18:32 +02:00
|
|
|
set -l workspace_root (status dirname)/..
|
2016-04-01 20:48:11 -07:00
|
|
|
|
|
|
|
|
if test $all = yes
|
2025-06-03 00:18:05 +02:00
|
|
|
if not set -l -q _flag_force; and not set -l -q _flag_check
|
|
|
|
|
# Potential for false positives: Not all fish files are formatted, see the `fish_files`
|
|
|
|
|
# definition below.
|
|
|
|
|
set -l relevant_uncommitted_changes (git status --porcelain --short --untracked-files=all | sed -e 's/^ *[^ ]* *//' | grep -E '.*\.(fish|py|rs)$')
|
|
|
|
|
if set -q relevant_uncommitted_changes[1]
|
|
|
|
|
for changed_file in $relevant_uncommitted_changes
|
|
|
|
|
echo $changed_file
|
|
|
|
|
end
|
|
|
|
|
echo
|
|
|
|
|
echo 'You have uncommitted changes (listed above). Are you sure you want to restyle?'
|
|
|
|
|
read -P 'y/N? ' -n1 -l ans
|
|
|
|
|
if not string match -qi y -- $ans
|
|
|
|
|
exit 1
|
|
|
|
|
end
|
2022-06-16 18:45:46 +02:00
|
|
|
end
|
2016-04-12 19:57:07 -07:00
|
|
|
end
|
2025-09-13 10:18:32 +02:00
|
|
|
set fish_files $workspace_root/{benchmarks,build_tools,etc,share}/**.fish
|
2025-10-08 17:40:31 +02:00
|
|
|
set python_files $workspace_root
|
2016-04-01 20:48:11 -07:00
|
|
|
else
|
2025-06-03 00:18:05 +02:00
|
|
|
# Format the files specified as arguments.
|
|
|
|
|
set -l files $argv
|
2019-05-03 19:15:25 +02:00
|
|
|
set fish_files (string match -r '^.*\.fish$' -- $files)
|
|
|
|
|
set python_files (string match -r '^.*\.py$' -- $files)
|
2023-06-01 18:14:12 +02:00
|
|
|
set rust_files (string match -r '^.*\.rs$' -- $files)
|
2016-04-01 20:48:11 -07:00
|
|
|
end
|
|
|
|
|
|
2019-05-03 19:20:45 +02:00
|
|
|
set -l red (set_color red)
|
|
|
|
|
set -l green (set_color green)
|
2025-06-03 00:18:05 +02:00
|
|
|
set -l yellow (set_color yellow)
|
2019-05-03 19:20:45 +02:00
|
|
|
set -l normal (set_color normal)
|
|
|
|
|
|
2025-10-22 18:47:15 +02:00
|
|
|
function die -V red -V normal
|
|
|
|
|
echo $red$argv[1]$normal
|
|
|
|
|
exit 1
|
|
|
|
|
end
|
|
|
|
|
|
2019-05-03 19:15:25 +02:00
|
|
|
if set -q fish_files[1]
|
2016-04-01 20:48:11 -07:00
|
|
|
if not type -q fish_indent
|
2025-06-03 00:18:05 +02:00
|
|
|
echo
|
|
|
|
|
echo $yellow'Could not find `fish_indent` in `$PATH`.'$normal
|
2025-07-24 08:48:48 +02:00
|
|
|
exit 127
|
|
|
|
|
end
|
|
|
|
|
echo === Running "$green"fish_indent"$normal"
|
|
|
|
|
if set -l -q _flag_check
|
2025-10-22 18:47:15 +02:00
|
|
|
fish_indent --check -- $fish_files
|
|
|
|
|
or die "Fish files are not formatted correctly."
|
2025-07-24 08:48:48 +02:00
|
|
|
else
|
|
|
|
|
fish_indent -w -- $fish_files
|
2016-04-01 20:48:11 -07:00
|
|
|
end
|
|
|
|
|
end
|
2019-05-03 19:15:25 +02:00
|
|
|
|
|
|
|
|
if set -q python_files[1]
|
2025-10-08 17:40:31 +02:00
|
|
|
if not type -q ruff
|
2019-05-03 19:15:25 +02:00
|
|
|
echo
|
2025-10-08 17:40:31 +02:00
|
|
|
echo $yellow'Please install `ruff` to style python'$normal
|
2025-10-06 13:21:08 +02:00
|
|
|
exit 127
|
|
|
|
|
end
|
2025-10-08 17:40:31 +02:00
|
|
|
echo === Running "$green"ruff format"$normal"
|
2025-10-06 13:21:08 +02:00
|
|
|
if set -l -q _flag_check
|
2025-10-22 18:47:15 +02:00
|
|
|
ruff format --check $python_files
|
|
|
|
|
or die "Python files are not formatted correctly."
|
2025-10-06 13:21:08 +02:00
|
|
|
else
|
2025-10-08 17:40:31 +02:00
|
|
|
ruff format $python_files
|
2019-05-03 19:15:25 +02:00
|
|
|
end
|
|
|
|
|
end
|
2023-06-01 18:14:12 +02:00
|
|
|
|
2025-10-26 13:32:52 +01:00
|
|
|
if test $all = yes; or set -q rust_files[1]
|
|
|
|
|
if not cargo fmt --version >/dev/null
|
|
|
|
|
echo
|
|
|
|
|
echo $yellow'Please install "rustfmt" to style Rust, e.g. via:'
|
|
|
|
|
echo "rustup component add rustfmt"$normal
|
|
|
|
|
exit 127
|
|
|
|
|
end
|
|
|
|
|
|
build_tools/style.fish: check that {rustfmt,Cargo}.toml edition specs are in sync
Commit 1fe6b288779 (rustfmt.toml: specify edition to allow 2024 syntax,
2025-10-19) mentions that "cargo fmt" has different behavior than
"rustfmt" before that commit. Probably because when .rustfmt.toml
exists, rustfmt implicitly uses a different edition (2018?) that
doesn't support c"" yet. That commit bumped the edition to 2024,
which caused yet another deviation from "cargo fmt":
Error writing files: failed to resolve mod `tests`: cannot parse /home/johannes/git/fish-shell/src/wutil/tests.rs
error: expected identifier, found reserved keyword `gen`
--> /home/johannes/git/fish-shell/src/tests/topic_monitor.rs:48:9
|
48 | for gen in &mut gens_list {
| ^^^ expected identifier, found reserved keyword
This has since been fixed by
00784248db8 (Update to rust 2024 edition, 2025-10-22).
Let's add a test so that such changes won't randomly break "rustfmt"
again.
Fix that by using 2021 edition, like we do in Cargo.toml.
In future, rustfmt should probably default to a current edition (or
maybe read the edition from Cargo.toml?) Not yet sure which one is the
upstream issue, maybe https://github.com/rust-lang/rustfmt/issues/5650
2025-10-22 18:47:15 +02:00
|
|
|
set -l edition_spec string match -r '^edition\s*=.*'
|
|
|
|
|
test "$($edition_spec <Cargo.toml)" = "$($edition_spec <.rustfmt.toml)"
|
|
|
|
|
or die "Cargo.toml and .rustfmt.toml use different editions"
|
|
|
|
|
|
2025-10-26 13:32:52 +01:00
|
|
|
echo === Running "$green"rustfmt"$normal"
|
|
|
|
|
if set -l -q _flag_check
|
|
|
|
|
if test $all = yes
|
|
|
|
|
cargo fmt --all --check
|
|
|
|
|
else
|
2025-10-22 18:47:15 +02:00
|
|
|
rustfmt --check --files-with-diff $rust_files
|
2025-06-03 00:18:05 +02:00
|
|
|
end
|
2025-10-26 13:32:52 +01:00
|
|
|
or die "Rust files are not formatted correctly."
|
2023-06-01 18:14:12 +02:00
|
|
|
else
|
2025-10-26 13:32:52 +01:00
|
|
|
if test $all = yes
|
|
|
|
|
cargo fmt --all
|
|
|
|
|
else
|
2025-07-24 08:48:48 +02:00
|
|
|
rustfmt $rust_files
|
2025-06-03 00:18:05 +02:00
|
|
|
end
|
2023-06-01 18:14:12 +02:00
|
|
|
end
|
|
|
|
|
end
|