Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 45 additions & 21 deletions .github/workflows/follow-releases.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
name: Follow open-java-format releases

# The pages install the formatter version that zensical.toml names as ojf_version. Once an hour this
# compares it with the latest release of openjavaformat/open-java-format. When a new release is out,
# that line is changed, the result is pushed to main, and Documentation is run on it to deploy the
# site. That run has to be explicit: a push made with the workflow's own token starts no
# push-triggered workflow. Nothing but that line changes: the pages name earlier versions as
# history, on Migrate for instance, and those stay as written.
# The pages install the formatter versions that zensical.toml names. Once an hour this compares
# them with the releases of openjavaformat/open-java-format:
#
# - ojf_version with the latest release;
# - ojf_17_version with the newest release of the 17 line, which is never the latest release, and
# ojf_17_native_version with the newest 21 release of the same minor version, whose native binary
# the 17 line's Gradle plugin runs.
#
# When a new release is out, those lines are changed, the result is pushed to main, and Documentation
# is run on it to deploy the site. That run has to be explicit: a push made with the workflow's own
# token starts no push-triggered workflow. Nothing but those lines changes: the pages name earlier
# versions as history, on Migrate for instance, and those stay as written.

on:
schedule:
Expand All @@ -31,29 +37,47 @@ jobs:
env:
GH_TOKEN: ${{ github.token }}
run: |
current=$(sed -n 's/^ojf_version = "\(.*\)"$/\1/p' zensical.toml)
release=$(gh release view --repo openjavaformat/open-java-format --json tagName,publishedAt)
latest=$(jq -r .tagName <<< "$release")
published=$(jq -r '.publishedAt[:10]' <<< "$release")
# The latest release is the one published last, and a fix for an older line can come out
# after a newer line started: only a higher version number is a bump.
newer=$(printf '%s\n' "$current" "$latest" | sort -V | tail -n 1)
if [ "$latest" = "$current" ] || [ "$newer" != "$latest" ]; then
echo "Already on $current; the latest release is $latest."
# The newest published release whose tag starts with $1, drafts and prereleases left out.
newest() {
gh release list --repo openjavaformat/open-java-format --limit 1000 \
--json tagName,isDraft,isPrerelease \
--jq '.[] | select((.isDraft or .isPrerelease) | not) | .tagName' \
| grep -E "^${1//./\\.}" | sort -V | tail -n 1 || true
}
bumped=()
# Sets the line `$1 = "..."` of zensical.toml to the version $2, when $2 is higher. A fix for an
# older line can come out after a newer line started: only a higher version number is a bump.
bump() {
local key=$1 version=$2 current higher
current=$(sed -n "s/^$key = \"\(.*\)\"$/\1/p" zensical.toml)
higher=$(printf '%s\n' "$current" "$version" | sort -V | tail -n 1)
if [ -z "$version" ] || [ "$version" = "$current" ] || [ "$higher" != "$version" ]; then
echo "$key stays $current; the release found is ${version:-none}."
return
fi
# The dots are escaped so that sed takes them literally.
sed -i "s/^$key = \"${current//./\\.}\"$/$key = \"$version\"/" zensical.toml
bumped+=("$key $current -> $version")
}

bump ojf_version "$(gh release view --repo openjavaformat/open-java-format --json tagName --jq .tagName)"
bump ojf_17_version "$(newest 17.)"
minor=$(sed -n 's/^ojf_17_version = "17\.\([0-9]*\)\..*"$/\1/p' zensical.toml)
bump ojf_17_native_version "$(newest "21.${minor}.")"

if [ "${#bumped[@]}" -eq 0 ]; then
exit 0
fi

# The dots are escaped so that sed takes them literally.
sed -i "s/^ojf_version = \"${current//./\\.}\"$/ojf_version = \"$latest\"/" zensical.toml
git diff

git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
git commit --all --quiet --file - <<COMMIT
Install open-java-format $latest
Install the newest releases of open-java-format

$(printf '%s\n' "${bumped[@]}")

The pages install $latest, which openjavaformat/open-java-format
released on $published. Made by the "$GITHUB_WORKFLOW" workflow:
Made by the "$GITHUB_WORKFLOW" workflow:
$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
COMMIT
gh auth setup-git
Expand Down
56 changes: 55 additions & 1 deletion docs/get-started/gradle.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Gradle plugin

The plugin adds a task that formats the lines you changed, and it keeps IntelliJ IDEA on the same
formatter version as the build. It needs Gradle 9 and a Gradle daemon running on Java 21 or later.
formatter version as the build. It needs Gradle 9 and a Gradle daemon running on Java 21 or later; on
Java 17 to 20, see [Builds on JDK 17](#builds-on-jdk-17).

## Apply the plugin

Expand Down Expand Up @@ -77,6 +78,59 @@ even when the native binary is switched on. See [Spotless](spotless.md).
The task looks at `git diff HEAD` and formats only the changed lines of the `.java` files in it.
With a clean working tree it has nothing to do.

## Builds on JDK 17

A Gradle daemon on Java 17 to 20 cannot load the plugin above, which is built for Java 21. Apply
17.x instead, the formatter and the plugin built for Java 17:

=== "Groovy"

``` groovy title="build.gradle"
plugins {
id 'java'
id 'dev.openjavaformat.java-format' version '{{ ojf_17_version }}'
}
```

=== "Kotlin"

``` kotlin title="build.gradle.kts"
plugins {
java
id("dev.openjavaformat.java-format") version "{{ ojf_17_version }}"
}
```

On every file that javac 17 parses, a 17.x formats exactly as the 21.x of the same minor version:
{{ ojf_17_version }} as {{ ojf_17_native_version }}. Syntax newer than Java 17 needs 21.x. The rest of
this page holds for 17.x as well.

17.x has no native binary of its own. With `openjavaformat.native.formatter`, name the 21 release
whose binary the plugin runs; the binary does not depend on the JDK the build runs on:

``` properties title="gradle.properties"
openjavaformat.native.formatter=true
openjavaformat.native.formatter.version={{ ojf_17_native_version }}
```

Without the second line the plugin asks for `open-java-format-native` in its own version, which
does not exist, and the build fails to resolve it.

Dependabot offers 21.x to a build on 17.x, as a major version up. To keep the build on 17.x, ignore
the major updates:

``` yaml title=".github/dependabot.yml"
version: 2
updates:
- package-ecosystem: gradle
directory: /
schedule:
interval: weekly
ignore:
- dependency-name: "dev.openjavaformat*"
update-types: ["version-update:semver-major"]
```

## Multi-project builds

Declare the plugin once in the root project and apply it wherever there is Java code. Each project
Expand Down
3 changes: 2 additions & 1 deletion docs/get-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ the same formatter, so they all produce the same files; the one exception is the

The current version is **{{ ojf_version }}**. Everything except the native binaries needs Java 21 or
later, and the Gradle plugin needs Gradle 9. The Maven plugin has version numbers of its own; the
current one is **{{ maven_plugin_version }}**.
current one is **{{ maven_plugin_version }}**. On Java 17 to 20, the [Gradle plugin](gradle.md#builds-on-jdk-17)
and [Spotless](spotless.md#maven) take **{{ ojf_17_version }}**, built for Java 17.

## In the build

Expand Down
8 changes: 7 additions & 1 deletion docs/get-started/spotless.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ so the `spotless` block needs no formatter step. If it still calls `palantirJava
that line.

In an [Android module](gradle.md#android-projects) the step formats the Java files of the Android
source sets. A `target` in the `spotless` block replaces them.
source sets. A `target` in the `spotless` block replaces them. A Gradle daemon on Java 17 to 20
applies {{ ojf_17_version }} instead, as in [Builds on JDK 17](gradle.md#builds-on-jdk-17).

=== "Groovy"

Expand Down Expand Up @@ -79,6 +80,11 @@ option comes with Spotless Maven plugin 3.12.0
21.0.2 or later, because the step asks for the `PALANTIR` style, and Maven has to run on Java 21 or
later. Unlike the Gradle step, it needs no `--add-exports` flags.

On Java 17 to 20, name {{ ojf_17_version }} as the version, the formatter built for Java 17. On every
file that javac 17 parses it formats exactly as {{ ojf_17_native_version }}, so a build that checks the
format on JDK 17 in one place and on JDK 21 in another sees one output. Syntax newer than Java 17
needs 21.x.

``` xml title="pom.xml"
<plugin>
<groupId>com.diffplug.spotless</groupId>
Expand Down
5 changes: 5 additions & 0 deletions zensical.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ toggle.name = "Switch to system preference"
# extra.version: the theme reads that one as the switch for a version selector.
[project.extra]
ojf_version = "21.1.2"
# The 17 line, for builds on JDK 17 to 20, and the 21 release of the same minor version, whose native binary its
# Gradle plugin runs. follow-releases.yml moves these two with each release as well: a 17.x is never the latest
# release, so it looks for the newest of each line.
ojf_17_version = "17.1.0"
ojf_17_native_version = "21.1.2"
# The Maven plugin is released from its own repository, openjavaformat/fmt-maven-plugin, with version
# numbers of its own. follow-maven-plugin-releases.yml bumps it on each release of the plugin.
maven_plugin_version = "2.27.0.2"
Expand Down