Skip to content

Commit 1108c14

Browse files
authored
Adjustments for biome CSS support (#2259)
2 parents 9556c8d + 0146ad9 commit 1108c14

File tree

25 files changed

+716
-261
lines changed

25 files changed

+716
-261
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Changed
1414
* Support configuring the Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))
1515

16+
* Add explicit support for JSONC / CSS via biome, via the file extensions `.css` and `.jsonc`.
17+
([#2259](https://github.com/diffplug/spotless/pull/2259))
18+
1619
## [3.0.0.BETA2] - 2024-08-25
1720
### Changed
1821
* Support toning down sortPom logging. ([#2185](https://github.com/diffplug/spotless/pull/2185))

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ concerning what you are working on:
164164

165165
```shell
166166
# Run only from test from the "lib" project
167-
gradlew :testlib:test --tests com.diffplug.spotless.generic.IndentStepTest
167+
./gradlew :testlib:test --tests com.diffplug.spotless.generic.IndentStepTest
168168

169169
# Run only one test from the "plugin-maven" project
170-
gradlew :plugin-maven:test --tests com.diffplug.spotless.maven.pom.SortPomMavenTest
170+
./gradlew :plugin-maven:test --tests com.diffplug.spotless.maven.pom.SortPomMavenTest
171171

172172
# Run only one test from the "plugin-gradle" project
173-
gradlew :plugin-gradle:test --tests com.diffplug.gradle.spotless.FreshMarkExtensionTest
173+
./gradlew :plugin-gradle:test --tests com.diffplug.gradle.spotless.FreshMarkExtensionTest
174174
```
175175

176176
## Check and format code

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class BiomeStep {
5555
/**
5656
* The language (syntax) of the input files to format. When <code>null</code> or
5757
* the empty string, the language is detected automatically from the file name.
58-
* Currently the following languages are supported by Biome:
58+
* Currently, the following languages are supported by Biome:
5959
* <ul>
6060
* <li>js (JavaScript)</li>
6161
* <li>jsx (JavaScript + JSX)</li>
@@ -65,7 +65,9 @@ public class BiomeStep {
6565
* <li>tsx (TypeScript + JSX)</li>
6666
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
6767
* extension)</li>
68+
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
6869
* <li>json (JSON)</li>
70+
* <li>jsonc (JSON + comments)</li>
6971
* </ul>
7072
*/
7173
private String language;
@@ -274,7 +276,9 @@ public BiomeStep withConfigPath(String configPath) {
274276
* <li>tsx (TypeScript + JSX)</li>
275277
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
276278
* extension)</li>
279+
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
277280
* <li>json (JSON)</li>
281+
* <li>jsonc (JSON + comments)</li>
278282
* </ul>
279283
*
280284
* @param language The language of the files to format.
@@ -450,7 +454,7 @@ private String format(ProcessRunner runner, String input, File file) throws IOEx
450454
* expected language / syntax. Biome always determined the language from the file
451455
* extension. This method returns the file name for the desired language when a
452456
* language was requested explicitly, or the file name of the input file for
453-
* auto detection.
457+
* auto-detection.
454458
*
455459
* @param file File to be formatted.
456460
* @return The file name to pass to the Biome executable.
@@ -479,6 +483,10 @@ private String resolveFileName(File file) {
479483
return "tsx".equals(ext) ? name : "file.tsx";
480484
case "json":
481485
return "json".equals(ext) ? name : "file.json";
486+
case "jsonc":
487+
return "jsonc".equals(ext) ? name : "file.jsonc";
488+
case "css":
489+
return "css".equals(ext) ? name : "file.css";
482490
// so that we can support new languages such as css or yaml when Biome adds
483491
// support for them without having to change the code
484492
default:

plugin-gradle/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

55
## [Unreleased]
66

7+
* Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general
8+
formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
9+
([#2259](https://github.com/diffplug/spotless/pull/2259))
10+
711
## [7.0.0.BETA2] - 2024-08-25
812
### Changed
913
* Support toning down sortPom logging. ([#2185](https://github.com/diffplug/spotless/pull/2185))

plugin-gradle/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,22 @@ spotless {
11111111
}
11121112
```
11131113
1114+
## CSS
1115+
1116+
`com.diffplug.gradle.spotless.CssExtension` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/7.0.0.BETA2/com/diffplug/gradle/spotless/CssExtension.html), [code](https://github.com/diffplug/spotless/blob/main/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/CssExtension.java)
1117+
1118+
```gradle
1119+
spotless {
1120+
css {
1121+
target 'css/**/*.css' // default: '**/*.css'
1122+
1123+
biome('1.8.3') // has its own section below
1124+
}
1125+
}
1126+
```
1127+
1128+
Note regarding biome: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
1129+
11141130
## Prettier
11151131
11161132
[homepage](https://prettier.io/). [changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md). [official plugins](https://prettier.io/docs/en/plugins.html#official-plugins). [community plugins](https://prettier.io/docs/en/plugins.html#community-plugins). Prettier is a formatter that can format almost every anything - JavaScript, JSX, Angular, Vue, Flow, TypeScript, CSS, Less, SCSS, HTML, JSON, GraphQL, Markdown (including GFM and MDX), and YAML. It can format even more [using plugins](https://prettier.io/docs/en/plugins.html) (PHP, Ruby, Swift, XML, Apex, Elm, Java (!!), Kotlin, pgSQL, .properties, solidity, svelte, toml, shellscript, ...).
@@ -1311,6 +1327,8 @@ is pretty fast. It can currently format JavaScript, TypeScript, JSX, and JSON, a
13111327
You can use Biome in any language-specific format for supported languages, but
13121328
usually you will be creating a generic format.
13131329
1330+
Note regarding biome: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
1331+
13141332
```gradle
13151333
spotless {
13161334
format 'styling', {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected FormatterStep createStep() {
170170
/**
171171
* Gets the language (syntax) of the input files to format. When
172172
* <code>null</code> or the empty string, the language is detected automatically
173-
* from the file name. Currently the following languages are supported by Biome:
173+
* from the file name. Currently, the following languages are supported by Biome:
174174
* <ul>
175175
* <li>js (JavaScript)</li>
176176
* <li>jsx (JavaScript + JSX)</li>
@@ -180,7 +180,9 @@ protected FormatterStep createStep() {
180180
* <li>tsx (TypeScript + JSX)</li>
181181
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
182182
* extension)</li>
183+
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
183184
* <li>json (JSON)</li>
185+
* <li>jsonc (JSON + comments)</li>
184186
* </ul>
185187
*
186188
* @return The language of the input files.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import javax.inject.Inject;
19+
20+
import com.diffplug.spotless.biome.BiomeFlavor;
21+
22+
/** Gradle step for formatting CSS files. */
23+
public class CssExtension extends FormatExtension {
24+
private static final String CSS_FILE_EXTENSION = "**/*.css";
25+
26+
static final String NAME = "css";
27+
28+
@Inject
29+
public CssExtension(SpotlessExtension spotless) {
30+
super(spotless);
31+
}
32+
33+
/** If the user hasn't specified files, assume all CSS files should be checked. */
34+
@Override
35+
protected void setupTask(SpotlessTask task) {
36+
if (target == null) {
37+
target = parseTarget(CSS_FILE_EXTENSION);
38+
}
39+
super.setupTask(task);
40+
}
41+
42+
/**
43+
* Adds the default version of the biome formatter.
44+
* Defaults to downloading the default Biome version from the network. To work
45+
* offline, you can specify the path to the Biome executable via
46+
* {@code biome().pathToExe(...)}.
47+
*/
48+
public BiomeCss biome() {
49+
return biome(null);
50+
}
51+
52+
/**
53+
* Adds the given version of the biome formatter.
54+
* Defaults to downloading the default Biome version from the network. To work
55+
* offline, you can specify the path to the Biome executable via
56+
* {@code biome().pathToExe(...)}.
57+
* @param version Biome version to use.
58+
*/
59+
public BiomeCss biome(String version) {
60+
var biomeConfig = new BiomeCss(version);
61+
addStep(biomeConfig.createStep());
62+
return biomeConfig;
63+
}
64+
65+
/**
66+
* Biome formatter step for CSS.
67+
*/
68+
public class BiomeCss extends BiomeStepConfig<BiomeCss> {
69+
/**
70+
* Creates a new Biome formatter step config for formatting CSS files. Unless
71+
* overwritten, the given Biome version is downloaded from the network.
72+
*
73+
* @param version Biome version to use.
74+
*/
75+
public BiomeCss(String version) {
76+
super(getProject(), CssExtension.this::replaceStep, BiomeFlavor.BIOME, version);
77+
}
78+
79+
@Override
80+
protected String getLanguage() {
81+
return "css";
82+
}
83+
84+
@Override
85+
protected BiomeCss getThis() {
86+
return this;
87+
}
88+
}
89+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,9 @@ public BiomeGeneric(String version) {
780780
* <li>tsx (TypeScript + JSX)</li>
781781
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
782782
* extension)</li>
783+
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
783784
* <li>json (JSON)</li>
785+
* <li>jsonc (JSON + comments)</li>
784786
* </ul>
785787
*
786788
* @param language The language of the files to format.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ public void go(Action<GoExtension> closure) {
231231
format(GoExtension.NAME, GoExtension.class, closure);
232232
}
233233

234+
/** Configures the special CSS-specific extension. */
235+
public void css(Action<CssExtension> closure) {
236+
requireNonNull(closure);
237+
format(CssExtension.NAME, CssExtension.class, closure);
238+
}
239+
234240
/** Configures the special POM-specific extension. */
235241
public void pom(Action<PomExtension> closure) {
236242
requireNonNull(closure);

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

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

22-
import java.io.IOException;
23-
2422
import org.junit.jupiter.api.Test;
2523
import org.owasp.encoder.Encode;
2624

25+
/**
26+
* Tests for the Biome formatter used via the Gradle spotless plugin.
27+
*/
2728
class BiomeIntegrationTest extends GradleIntegrationHarness {
29+
/**
30+
* Tests that biome can be used as a JSON formatting step, using biome 1.8.3 which
31+
* requires opt-in.
32+
*
33+
* @throws Exception When a test failure occurs.
34+
*/
35+
@Test
36+
void asCssStepExperimental() throws Exception {
37+
setFile("build.gradle").toLines(
38+
"plugins {",
39+
" id 'com.diffplug.spotless'",
40+
"}",
41+
"repositories { mavenCentral() }",
42+
"spotless {",
43+
" css {",
44+
" target '**/*.css'",
45+
" biome('1.8.3').configPath('configs')",
46+
" }",
47+
"}");
48+
setFile("biome_test.css").toResource("biome/css/fileBefore.css");
49+
setFile("configs/biome.json").toResource("biome/config/css-enabled.json");
50+
51+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
52+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
53+
assertFile("biome_test.css").sameAsResource("biome/css/fileAfter.css");
54+
}
55+
56+
/**
57+
* Tests that biome can be used as a JSON formatting step, using biome 1.9.0 which
58+
* does not require opt-in.
59+
*
60+
* @throws Exception When a test failure occurs.
61+
*/
62+
@Test
63+
void asCssStepStable() throws Exception {
64+
setFile("build.gradle").toLines(
65+
"plugins {",
66+
" id 'com.diffplug.spotless'",
67+
"}",
68+
"repositories { mavenCentral() }",
69+
"spotless {",
70+
" css {",
71+
" target '**/*.css'",
72+
" biome('1.9.0')",
73+
" }",
74+
"}");
75+
setFile("biome_test.css").toResource("biome/css/fileBefore.css");
76+
77+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
78+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
79+
assertFile("biome_test.css").sameAsResource("biome/css/fileAfter.css");
80+
}
81+
2882
/**
2983
* Tests that biome can be used as a generic formatting step.
3084
*
3185
* @throws Exception When a test failure occurs.
3286
*/
3387
@Test
34-
void asGenericStep() throws IOException {
88+
void asGenericStep() throws Exception {
3589
setFile("build.gradle").toLines(
3690
"plugins {",
3791
" id 'com.diffplug.spotless'",

plugin-maven/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
66
### Changed
77
* Leverage local repository for Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))
88

9+
* Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general
10+
formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
11+
([#2259](https://github.com/diffplug/spotless/pull/2259))
12+
913
## [2.44.0.BETA2] - 2024-08-25
1014
### Changed
1115
* Support toning down sortPom logging. ([#2185](https://github.com/diffplug/spotless/pull/2185))

plugin-maven/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,30 @@ Standard Go formatter, part of Go distribution.
11181118
</gofmt>
11191119
```
11201120

1121+
## CSS
1122+
1123+
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/css/Css.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/css).
1124+
1125+
```xml
1126+
<configuration>
1127+
<css>
1128+
<!-- These are the defaults, you can override if you want -->
1129+
<includes>
1130+
<include>src/main/css/**/*.css</include>
1131+
<include>src/test/css/**/*.css</include>
1132+
</includes>
1133+
1134+
<biome /> <!-- has its own section below -->
1135+
1136+
<licenseHeader>
1137+
<content>/* (C)$YEAR */</content> <!-- or <file>${project.basedir}/license-header</file> -->
1138+
</licenseHeader>
1139+
</css>
1140+
</configuration>
1141+
```
1142+
1143+
Note regarding biome: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
1144+
11211145
## Prettier
11221146

11231147
[homepage](https://prettier.io/). [changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md). [official plugins](https://prettier.io/docs/en/plugins.html#official-plugins). [community plugins](https://prettier.io/docs/en/plugins.html#community-plugins). Prettier is a formatter that can format almost every anything - JavaScript, JSX, Angular, Vue, Flow, TypeScript, CSS, Less, SCSS, HTML, JSON, GraphQL, Markdown (including GFM and MDX), and YAML. It can format even more [using plugins](https://prettier.io/docs/en/plugins.html) (PHP, Ruby, Swift, XML, Apex, Elm, Java (!!), Kotlin, pgSQL, .properties, solidity, svelte, toml, shellscript, ...).
@@ -1340,6 +1364,8 @@ is pretty fast. It can currently format JavaScript, TypeScript, JSX, and JSON, a
13401364
You can use Biome in any language-specific format for supported languages, but
13411365
usually you will be creating a generic format.
13421366

1367+
Note regarding CSS: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
1368+
13431369
```xml
13441370
<configuration>
13451371
<formats>
@@ -1355,7 +1381,7 @@ usually you will be creating a generic format.
13551381
<!-- (optional) Path to the directory with the biome.json conig file -->
13561382
<configPath>${project.basedir}/path/to/config/dir</configPath>
13571383

1358-
<!-- (optional) Biome will auto detect the language based on the file extension. -->
1384+
<!-- (optional) Biome will auto-detect the language based on the file extension. -->
13591385
<!-- See below for possible values. -->
13601386
<language>ts</language>
13611387
</prettier>

0 commit comments

Comments
 (0)