format_toml_files•2.13 kB
#!/usr/bin/env bash
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Format all TOML files in the project.
set -euo pipefail
TOP_DIR=$(git rev-parse --show-toplevel)
if command -v taplo >/dev/null 2>&1; then
  if [ ! -f "${TOP_DIR}/taplo.toml" ]; then
    echo "error: config file not found at ${TOP_DIR}/taplo.toml"
    exit 1
  fi
  FORMATTER_COMMAND="taplo format --config ${TOP_DIR}/taplo.toml"
  if command -v rust-parallel >/dev/null 2>&1; then
    FORMATTER_COMMAND="rust-parallel -j4 ${FORMATTER_COMMAND}"
  else
    echo "warning: it is recommended to install https://crates.io/crates/rust-parallel for faster formatting"
  fi
  pushd "${TOP_DIR}"
  if command -v fd >/dev/null 2>&1; then
    echo "Using fd"
    fd -e toml \
      --exclude '**/*.egg-info/**' \
      --exclude '**/.dist/**' \
      --exclude '**/.next/**' \
      --exclude '**/.output/**' \
      --exclude '**/.pytest_cache/**' \
      --exclude '**/.venv/**' \
      --exclude '**/__pycache__/**' \
      --exclude '**/bazel-*/**' \
      --exclude '**/build/**' \
      --exclude '**/develop-eggs/**' \
      --exclude '**/dist/**' \
      --exclude '**/eggs/**' \
      --exclude '**/node_modules/**' \
      --exclude '**/sdist/**' \
      --exclude '**/site/**' \
      --exclude '**/target/**' \
      --exclude '**/venv/**' \
      --exclude '**/wheels/**' |
      ${FORMATTER_COMMAND}
  else
    echo "Please install https://github.com/sharkdp/fd to find files to format."
  fi
  popd
else
  echo "Please install https://github.com/tamasfe/taplo to format TOML files."
fi