Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
refactor: fmt -> format
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Sep 15, 2022
1 parent 88e5da5 commit e0fc95b
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
restore-keys: bazel-cache-
- name: Check formatting
working-directory: ${{ matrix.folder }}
run: bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc run @aspect_rules_fmt//fmt check
run: bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc run @aspect_rules_format//format check
- name: bazel test //...
env:
# Bazelisk will download bazel to here, ensure it is cached between runs.
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/workspace_snippet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ set -o errexit -o nounset -o pipefail
# Set by GH actions, see
# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
TAG=${GITHUB_REF_NAME}
PREFIX="rules_fmt-${TAG:1}"
PREFIX="rules_format-${TAG:1}"
SHA=$(git archive --format=tar --prefix=${PREFIX}/ ${TAG} | gzip | shasum -a 256 | awk '{print $1}')

cat << EOF
WORKSPACE snippet:
\`\`\`starlark
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "aspect_rules_fmt",
name = "aspect_rules_format",
sha256 = "${SHA}",
strip_prefix = "${PREFIX}",
url = "https://github.com/aspect-build/rules_fmt/archive/refs/tags/${TAG}.tar.gz",
url = "https://github.com/aspect-build/rules_format/archive/refs/tags/${TAG}.tar.gz",
)
EOF

Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
docs/*.md
# Don't format our own script as its running!
fmt/fmt.sh
format/format.sh
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@aspect_rules_fmt_npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_format_npm//:defs.bzl", "npm_link_all_packages")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

npm_link_all_packages(name = "node_modules")
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ Run `bazel run //:gazelle` to keep them up-to-date.
## Using this as a development dependency of other rules

You'll commonly find that you develop in another WORKSPACE, such as
some other ruleset that depends on rules_fmt, or in a nested
some other ruleset that depends on rules_format, or in a nested
WORKSPACE in the integration_tests folder.

To always tell Bazel to use this directory rather than some release
artifact or a version fetched from the internet, run this from this
directory:

```sh
OVERRIDE="--override_repository=rules_fmt=$(pwd)/rules_fmt"
OVERRIDE="--override_repository=rules_format=$(pwd)/rules_format"
echo "common $OVERRIDE" >> ~/.bazelrc
```

This means that any usage of `@rules_fmt` on your system will point to this folder.
This means that any usage of `@rules_format` on your system will point to this folder.

## Releasing

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@ Supported languages:
Install Bazel: <https://bazel.build/install/bazelisk>

From the release you wish to use:
<https://github.com/aspect-build/rules_fmt/releases>
<https://github.com/aspect-build/rules_format/releases>
copy the WORKSPACE snippet into your `WORKSPACE` file.

## Usage

One-time re-format all files:

`bazel run @aspect_rules_fmt//fmt`
`bazel run @aspect_rules_format//format`

Install as a git pre-commit hook:

```bash
$ echo "bazel run @aspect_rules_fmt//fmt" >> .git/hooks/pre-commit
$ echo "bazel run @aspect_rules_format//format" >> .git/hooks/pre-commit
$ chmod u+x .git/hooks/pre-commit
```

Check that files are already formatted, exit non-zero if formatting is needed:

`bazel run @aspect_rules_fmt//fmt check`
`bazel run @aspect_rules_format//format check`

## Configuration

Expand All @@ -84,14 +84,14 @@ If you don't use a language in your whole repo, you can turn off fetching the to
Add some of these lines to `.bazelrc`:

```
build --@aspect_rules_fmt//fmt:java_enabled=false
build --@aspect_rules_fmt//fmt:python_enabled=false
build --@aspect_rules_fmt//fmt:swift_enabled=false
build --@aspect_rules_format//format:java_enabled=false
build --@aspect_rules_format//format:python_enabled=false
build --@aspect_rules_format//format:swift_enabled=false
```

### Changing the version of a formatter tool

Look in our `fmt/repositories.bzl` file and copy the `http_*` rule you want to modify into your WORKSPACE, above the `rules_fmt_dependencies()` call.
Look in our `format/repositories.bzl` file and copy the `http_*` rule you want to modify into your WORKSPACE, above the `rules_format_dependencies()` call.

### Ignoring files

Expand Down
12 changes: 6 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
workspace(name = "aspect_rules_fmt")
workspace(name = "aspect_rules_format")

load("//fmt:repositories.bzl", "rules_fmt_dependencies")
load("//format:repositories.bzl", "rules_format_dependencies")

# Fetch dependencies which users need as well
rules_fmt_dependencies()
rules_format_dependencies()

load("@buildifier_prebuilt//:defs.bzl", "buildifier_prebuilt_register_toolchains")

Expand All @@ -27,10 +27,10 @@ python_register_toolchains(
python_version = "3.10",
)

load("@aspect_rules_fmt//fmt:dependencies.bzl", "parse_dependencies")
load("@aspect_rules_format//format:dependencies.bzl", "parse_dependencies")

parse_dependencies()

load("//fmt:toolchains.bzl", "fmt_register_toolchains")
load("//format:toolchains.bzl", "format_register_toolchains")

fmt_register_toolchains()
format_register_toolchains()
2 changes: 1 addition & 1 deletion e2e/workspace/.bazelrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
build --@aspect_rules_fmt//fmt:java_enabled=false
build --@aspect_rules_format//format:java_enabled=false
2 changes: 1 addition & 1 deletion e2e/workspace/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Add a basic smoke-test target below.
"""

load("@bazel_skylib//rules:build_test.bzl", "build_test")
# load("aspect_rules_fmt//fmt:defs.bzl", "...")
# load("aspect_rules_format//format:defs.bzl", "...")

# Replace with a usage of your rule/macro
filegroup(name = "empty")
Expand Down
16 changes: 8 additions & 8 deletions e2e/workspace/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# Override http_archive for local testing
local_repository(
name = "aspect_rules_fmt",
name = "aspect_rules_format",
path = "../..",
)

#---SNIP--- Below here is re-used in the workspace snippet published on releases

######################
# rules_fmt setup #
# rules_format setup #
######################
# Fetches the rules_fmt dependencies.
# Fetches the rules_format dependencies.
# If you want to have a different version of some dependency,
# you should fetch it *before* calling this.
# Alternatively, you can skip calling this function, so long as you've
# already fetched all the dependencies.
load("@aspect_rules_fmt//fmt:repositories.bzl", "rules_fmt_dependencies")
load("@aspect_rules_format//format:repositories.bzl", "rules_format_dependencies")

rules_fmt_dependencies()
rules_format_dependencies()

# If you didn't already register a toolchain providing nodejs, do that:
load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")
Expand All @@ -34,12 +34,12 @@ python_register_toolchains(
python_version = "3.10",
)

load("@aspect_rules_fmt//fmt:dependencies.bzl", "parse_dependencies")
load("@aspect_rules_format//format:dependencies.bzl", "parse_dependencies")

parse_dependencies()

# Installs toolchains for running programs under Node, Python, etc.
# Be sure to register your own toolchains before this.
load("@aspect_rules_fmt//fmt:toolchains.bzl", "fmt_register_toolchains")
load("@aspect_rules_format//format:toolchains.bzl", "format_register_toolchains")

fmt_register_toolchains()
format_register_toolchains()
1 change: 1 addition & 0 deletions examples/hello.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div />;
16 changes: 8 additions & 8 deletions fmt/BUILD.bazel → format/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@aspect_rules_fmt_npm//:prettier/package_json.bzl", prettier = "bin")
load("@aspect_rules_fmt_pypi//:requirements.bzl", "entry_point")
load("@aspect_rules_format_npm//:prettier/package_json.bzl", prettier = "bin")
load("@aspect_rules_format_pypi//:requirements.bzl", "entry_point")
load(":langs.bzl", "LANGS")

bzl_library(
Expand Down Expand Up @@ -63,7 +63,7 @@ alias(

[
config_setting(
name = "fmt_" + s,
name = "format_" + s,
flag_values = {
s + "_enabled": "1",
},
Expand All @@ -72,24 +72,24 @@ alias(
]

sh_binary(
name = "fmt",
srcs = ["fmt.sh"],
name = "format",
srcs = ["format.sh"],
data = [
":buildifier",
":prettier",
] + select(
{
":fmt_java": [":java-format"],
":format_java": [":java-format"],
"//conditions:default": [],
},
) + select(
{
":fmt_swift": [":swiftformat"],
":format_swift": [":swiftformat"],
"//conditions:default": [],
},
) + select(
{
":fmt_python": [":black"],
":format_python": [":black"],
"//conditions:default": [],
},
),
Expand Down
10 changes: 5 additions & 5 deletions fmt/dependencies.bzl → format/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ load("@rules_python//python:pip.bzl", "pip_parse")

def parse_dependencies():
npm_translate_lock(
name = "aspect_rules_fmt_npm",
link_workspace = "aspect_rules_fmt",
pnpm_lock = "@aspect_rules_fmt//:pnpm-lock.yaml",
name = "aspect_rules_format_npm",
link_workspace = "aspect_rules_format",
pnpm_lock = "@aspect_rules_format//:pnpm-lock.yaml",
)

pip_parse(
name = "aspect_rules_fmt_pypi",
name = "aspect_rules_format_pypi",
python_interpreter_target = interpreter,
requirements_lock = "@aspect_rules_fmt//:requirements_lock.txt",
requirements_lock = "@aspect_rules_format//:requirements_lock.txt",
)
8 changes: 4 additions & 4 deletions fmt/fmt.sh → format/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function on_exit {
code=$?
if [[ $code != 0 ]]; then
echo >&2 "FAILED: A formatter tool exited with code $code"
echo >&2 "Try running 'bazel run @aspect_rules_fmt//fmt' to fix this."
echo >&2 "Try running 'bazel run @aspect_rules_format//format' to fix this."
fi
}

Expand Down Expand Up @@ -71,21 +71,21 @@ bin=$(rlocation buildifier_prebuilt/buildifier/buildifier)
}

files=$(git ls-files '*.js' '*.sh' '*.ts' '*.tsx' '*.json' '*.css' '*.html' '*.md' '*.yaml' '*.yml')
bin=$(rlocation aspect_rules_fmt/fmt/prettier.sh)
bin=$(rlocation aspect_rules_format/format/prettier.sh)
[ -n "$files" ] && [ -n "$bin" ] && {
echo "Running Prettier..."
echo $files | xargs $bin $prettiermode
}

files=$(git ls-files '*.py' '*.pyi')
bin=$(rlocation aspect_rules_fmt_pypi_black/rules_python_wheel_entry_point_black)
bin=$(rlocation aspect_rules_format_pypi_black/rules_python_wheel_entry_point_black)
[ -n "$files" ] && [ -n "$bin" ] && {
echo "Running black..."
echo $files | xargs $bin $blackmode
}

files=$(git ls-files '*.java')
bin=$(rlocation aspect_rules_fmt/fmt/java-format)
bin=$(rlocation aspect_rules_format/format/java-format)
[ -n "$files" ] && [ -n "$bin" ] && {
echo "Running java-format..."
# Setting JAVA_RUNFILES to work around https://github.com/bazelbuild/bazel/issues/12348
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion fmt/repositories.bzl → format/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def http_jar(name, **kwargs):
# changes in this function should be marked as BREAKING in the commit message
# and released only in semver majors.
# This is all fixed by bzlmod, so we just tolerate it for now.
def rules_fmt_dependencies():
def rules_format_dependencies():
"Fetch dependencies"

# The minimal version of bazel_skylib we require
Expand Down
6 changes: 3 additions & 3 deletions fmt/toolchains.bzl → format/toolchains.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@buildifier_prebuilt//:defs.bzl", "buildifier_prebuilt_register_toolchains")
load("@aspect_rules_fmt_pypi//:requirements.bzl", "install_deps")
load("@aspect_rules_fmt_npm//:repositories.bzl", "npm_repositories")
load("@aspect_rules_format_pypi//:requirements.bzl", "install_deps")
load("@aspect_rules_format_npm//:repositories.bzl", "npm_repositories")

def fmt_register_toolchains():
def format_register_toolchains():
buildifier_prebuilt_register_toolchains()

# Initialize repositories for all packages in requirements_lock.txt.
Expand Down

0 comments on commit e0fc95b

Please sign in to comment.