Skip to content

Commit b7c829e

Browse files
authored
biome step: Allow specifying path to JSON config file directly, requires biome 2.0 or higher (#2548)
2 parents 7203e24 + b79b2d3 commit b7c829e

File tree

13 files changed

+225
-60
lines changed

13 files changed

+225
-60
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Allow specifying path to Biome JSON config file directly in `biome` step. Requires biome 2.x. ([#2548](https://github.com/diffplug/spotless/pull/2548))
1315

1416
## Changed
1517
* Bump default `gson` version to latest `2.11.0` -> `2.13.1`. ([#2414](https://github.com/diffplug/spotless/pull/2414))

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ If you get something running, we'd love to host your plugin within this repo as
163163

164164
To run all tests, simply do
165165

166-
> gradlew test
166+
> ./gradlew test
167167
168168
Since that takes some time, you might only want to run the tests
169169
concerning what you are working on:

lib/src/main/java/com/diffplug/spotless/biome/BiomeSettings.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
*/
1616
package com.diffplug.spotless.biome;
1717

18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
1821
/**
1922
* Settings and constants for Biome to use.
2023
*/
2124
public final class BiomeSettings {
25+
private static final Logger logger = LoggerFactory.getLogger(BiomeSettings.class);
26+
2227
private final static String CONFIG_NAME = "biome.json";
2328
private final static String DEFAULT_VERSION = "1.2.0";
2429
private final static String DOWNLOAD_FILE_PATTERN = "biome-%s-%s-%s";
@@ -72,4 +77,37 @@ public static String getUrlPattern(String version) {
7277
public static String shortName() {
7378
return SHORT_NAME;
7479
}
80+
81+
/**
82+
* Checks if the version of Biome is equal to or higher than the given major, minor, and patch version.
83+
* @param version The version string to check, e.g. "1.2.3".
84+
* @param major The major version to compare against.
85+
* @param minor The minor version to compare against.
86+
* @param patch The patch version to compare against.
87+
* @return true if the version is higher than or equal to the given major, minor, and patch version,
88+
*/
89+
public static boolean versionHigherThanOrEqualTo(String version, int major, int minor, int patch) {
90+
try {
91+
final var versionParts = version.split("\\.");
92+
if (versionParts.length < 3) {
93+
return false;
94+
}
95+
final var actualMajor = Integer.parseInt(versionParts[0]);
96+
final var actualMinor = Integer.parseInt(versionParts[1]);
97+
final var actualPatch = Integer.parseInt(versionParts[2]);
98+
if (actualMajor > major) {
99+
return true;
100+
}
101+
if (actualMajor == major && actualMinor > minor) {
102+
return true;
103+
}
104+
if (actualMajor == major && actualMinor == minor && actualPatch > patch) {
105+
return true;
106+
}
107+
return actualMajor == major && actualMinor == minor && actualPatch == patch;
108+
} catch (final Exception e) {
109+
logger.warn("Failed to parse biome version string '{}'. Expected format is 'major.minor.patch'.", version, e);
110+
return false;
111+
}
112+
}
75113
}

lib/src/main/java/com/diffplug/spotless/biome/BiomeStep.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,21 @@ private static String resolveNameAgainstPath(String name) throws IOException, In
191191
/**
192192
* Checks the Biome config path. When the config path does not exist or when it
193193
* does not contain a file named {@code biome.json}, an error is thrown.
194+
* @param configPath The path to validate.
195+
* @param version The version of Biome.
194196
*/
195-
private static void validateBiomeConfigPath(String configPath) {
197+
private static void validateBiomeConfigPath(String configPath, String version) {
196198
if (configPath == null) {
197199
return;
198200
}
201+
var atLeastV2 = BiomeSettings.versionHigherThanOrEqualTo(version, 2, 0, 0);
199202
var path = Paths.get(configPath);
200-
var config = path.resolve(BiomeSettings.configName());
203+
var configFile = Files.isRegularFile(path) && atLeastV2 ? path : path.resolve(BiomeSettings.configName());
201204
if (!Files.exists(path)) {
202205
throw new IllegalArgumentException("Biome config directory does not exist: " + path);
203206
}
204-
if (!Files.exists(config)) {
205-
throw new IllegalArgumentException("Biome config does not exist: " + config);
207+
if (!Files.exists(configFile)) {
208+
throw new IllegalArgumentException("Biome config does not exist: " + configFile);
206209
}
207210
}
208211

@@ -240,11 +243,10 @@ public FormatterStep create() {
240243
}
241244

242245
/**
243-
* Sets the path to the directory with the {@code biome.json} config file. When
244-
* no config path is set, the default configuration is used.
246+
* Sets the path to the Biome configuration. Must be either a directory with a file named {@code biome.json}, or
247+
* a file with the Biome config as JSON. When no config path is set, the default configuration is used.
245248
*
246-
* @param configPath Config path to use. Must point to a directory which contains
247-
* a file named {@code biome.json}.
249+
* @param configPath Config path to use.
248250
* @return This builder instance for chaining method calls.
249251
*/
250252
public BiomeStep withConfigPath(String configPath) {
@@ -296,7 +298,7 @@ public BiomeStep withLanguage(String language) {
296298
private State createState() throws IOException, InterruptedException {
297299
var resolvedPathToExe = resolveExe();
298300
validateBiomeExecutable(resolvedPathToExe);
299-
validateBiomeConfigPath(configPath);
301+
validateBiomeConfigPath(configPath, version);
300302
logger.debug("Using Biome executable located at '{}'", resolvedPathToExe);
301303
var exeSignature = FileSignature.signAsList(Collections.singleton(new File(resolvedPathToExe)));
302304
makeExecutable(resolvedPathToExe);

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Allow specifying path to Biome JSON config file directly in `biome` step. Requires biome 2.x. ([#2548](https://github.com/diffplug/spotless/pull/2548))
68

79
## Changed
810
* Bump default `gson` version to latest `2.11.0` -> `2.13.1`. ([#2414](https://github.com/diffplug/spotless/pull/2414))

plugin-gradle/README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,14 +1432,14 @@ spotless {
14321432
target 'src/*/webapp/**/*.js'
14331433
14341434
// Download Biome from the network if not already downloaded, see below for more info
1435-
biome('1.2.0')
1435+
biome('2.1.0')
14361436
14371437
// (optional) Path to the directory with the biome.json conig file
1438-
biome('1.2.0').configPath("path/config/dir")
1438+
biome('2.1.0').configPath("path/config/dir")
14391439
14401440
// (optional) Biome will auto detect the language based on the file extension.
14411441
// See below for possible values.
1442-
biome('1.2.0').language("js")
1442+
biome('2.1.0').language("js")
14431443
}
14441444
}
14451445
```
@@ -1451,15 +1451,15 @@ more formats:
14511451
spotless {
14521452
format 'biome-js', {
14531453
target '**/*.js'
1454-
biome('1.2.0')
1454+
biome('2.1.0')
14551455
}
14561456
format 'biome-ts', {
14571457
target '**/*.ts'
1458-
biome('1.2.0')
1458+
biome('2.1.0')
14591459
}
14601460
format 'biome-json', {
14611461
target '**/*.json'
1462-
biome('1.2.0')
1462+
biome('2.1.0')
14631463
}
14641464
}
14651465
```
@@ -1476,6 +1476,7 @@ Note: Due to a limitation of biome, if the name of a file matches a pattern in
14761476
the `ignore` option of the specified `biome.json` configuration file, it will not be
14771477
formatted, even if included in the biome configuration section of the Gradle settings
14781478
file.
1479+
14791480
You could specify a different `biome.json` configuration file without an `ignore`
14801481
pattern to circumvent this.
14811482
@@ -1553,7 +1554,29 @@ spotless {
15531554
target '**/*.js','**/*.ts','**/*.json'
15541555
// Must point to the directory with the "biome.json" config file -->
15551556
// Relative paths are resolved against the project's base directory -->
1556-
biome('1.2.0').configPath('./config')
1557+
biome('2.1.0').configPath('./config')
1558+
}
1559+
}
1560+
```
1561+
1562+
__If spotless does not format any files__, this might be because you excluded those files in you biome.json
1563+
configuration file. If you are using biome 2.x, you can create a custom config file for spotless and inherit from
1564+
your main config file like this:
1565+
1566+
```jsonc
1567+
// biome-spotless.json
1568+
{
1569+
"extends": "./biome.json",
1570+
"include": ["**"]
1571+
}
1572+
```
1573+
1574+
Then simply specify the path to this file in your spotless configuration:
1575+
1576+
```gradle
1577+
spotless {
1578+
format 'biome', {
1579+
biome('2.1.0').configPath('./config/biome-spotless.json')
15571580
}
15581581
}
15591582
```

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/BiomeStepConfig.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131

3232
public abstract class BiomeStepConfig<Self extends BiomeStepConfig<Self>> {
3333
/**
34-
* Optional path to the directory with configuration file for Biome. The file
35-
* must be named {@code biome.json}. When none is given, the default
36-
* configuration is used. If this is a relative path, it is resolved against the
37-
* project's base directory.
34+
* Optional path to the configuration file for Biome. Must be either a directory that contains a file named
35+
* {@code biome.json}, or a file that contains the Biome config as JSON. When none is given, the default
36+
* configuration is used. If this is a relative path, it is resolved against the project's base directory.
3837
*/
3938
@Nullable private Object configPath;
4039

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertTrue;
2121

22+
import java.io.File;
23+
2224
import org.junit.jupiter.api.Test;
2325
import org.owasp.encoder.Encode;
2426

@@ -230,6 +232,34 @@ void configPathAbsolute() throws Exception {
230232
assertFile("biome_test.js").sameAsResource("biome/js/longLineAfter120.js");
231233
}
232234

235+
/**
236+
* Tests that a path to a Biome config JSON file can be specified (requires biome 2.x).
237+
*
238+
* @throws Exception When a test failure occurs.
239+
*/
240+
@Test
241+
void configPathFile() throws Exception {
242+
var path = newFile("configs").getAbsolutePath();
243+
var file = new File(path, "biome.json").getAbsolutePath();
244+
setFile("build.gradle").toLines(
245+
"plugins {",
246+
" id 'com.diffplug.spotless'",
247+
"}",
248+
"repositories { mavenCentral() }",
249+
"spotless {",
250+
" format 'mybiome', {",
251+
" target '**/*.js'",
252+
" biome('2.1.0').configPath('" + Encode.forJava(file) + "')",
253+
" }",
254+
"}");
255+
setFile("biome_test.js").toResource("biome/js/longLineBefore.js");
256+
setFile("configs/biome.json").toResource("biome/config/line-width-120.json");
257+
258+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
259+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
260+
assertFile("biome_test.js").sameAsResource("biome/js/longLineAfter120.js");
261+
}
262+
233263
/**
234264
* Tests that a path to the directory with the biome.json config file can be
235265
* specified. Uses a config file with a line width of 120.

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Allow specifying path to Biome JSON config file directly in `biome` step. Requires biome 2.x. ([#2548](https://github.com/diffplug/spotless/pull/2548))
68

79
## Changed
810
* Bump default `gson` version to latest `2.11.0` -> `2.13.1`. ([#2414](https://github.com/diffplug/spotless/pull/2414))

plugin-maven/README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,9 +1530,9 @@ Note regarding CSS: Biome supports formatting CSS as of 1.8.0 (experimental, opt
15301530

15311531
<biome>
15321532
<!-- Download Biome from the network if not already downloaded, see below for more info -->
1533-
<version>1.2.0</version>
1533+
<version>2.1.1</version>
15341534

1535-
<!-- (optional) Path to the directory with the biome.json conig file -->
1535+
<!-- (optional) Path to the directory with the biome.json config file -->
15361536
<configPath>${project.basedir}/path/to/config/dir</configPath>
15371537

15381538
<!-- (optional) Biome will auto-detect the language based on the file extension. -->
@@ -1583,7 +1583,7 @@ To download the Biome binary from the network, just specify a version:
15831583

15841584
```xml
15851585
<biome>
1586-
<version>1.2.0</version>
1586+
<version>2.1.0</version>
15871587
</biome>
15881588
```
15891589

@@ -1594,7 +1594,7 @@ Biome binaries (defaults to `~/.m2/repository/com/diffplug/spotless/spotless-dat
15941594

15951595
```xml
15961596
<biome>
1597-
<version>1.2.0</version>
1597+
<version>2.1.0</version>
15981598
<!-- Relative paths are resolved against the project's base directory -->
15991599
<downloadDir>${user.home}/biome</downloadDir>
16001600
</biome>
@@ -1631,10 +1631,31 @@ Biome is used. To use a custom configuration:
16311631
<biome>
16321632
<!-- Must point to the directory with the "biome.json" config file -->
16331633
<!-- Relative paths are resolved against the project's base directory -->
1634+
<!-- Starting with biome 2.x, you can also pass the path to a JSON config file directly. -->
16341635
<configPath>${project.basedir}</configPath>
16351636
</biome>
16361637
```
16371638

1639+
__If spotless does not format any files__, this might be because you excluded those files in you biome.json
1640+
configuration file. If you are using biome 2.x, you can create a custom config file for spotless and inherit from
1641+
your main config file like this:
1642+
1643+
```jsonc
1644+
// biome-spotless.json
1645+
{
1646+
"extends": "./biome.json",
1647+
"include": ["**"]
1648+
}
1649+
```
1650+
1651+
Then simply specify the path to this file in your spotless configuration:
1652+
1653+
```xml
1654+
<biome>
1655+
<configPath>${project.basedir}/biome-spotless.json</configPath>
1656+
</biome>
1657+
```
1658+
16381659
### Biome input language
16391660

16401661
By default, Biome detects the language / syntax of the files to format

0 commit comments

Comments
 (0)