Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjustments for biome CSS support #2259

Merged
merged 2 commits into from
Sep 23, 2024
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

* Add explicit support for JSONC / CSS via biome, via the file extensions `.css` and `.jsonc`.
([#2259](https://github.com/diffplug/spotless/pull/2259))

## [3.0.0.BETA2] - 2024-08-25
### Changed
* Support toning down sortPom logging. ([#2185](https://github.com/diffplug/spotless/pull/2185))
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ concerning what you are working on:

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

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

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

## Check and format code
Expand Down
12 changes: 10 additions & 2 deletions lib/src/main/java/com/diffplug/spotless/biome/BiomeStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class BiomeStep {
/**
* The language (syntax) of the input files to format. When <code>null</code> or
* the empty string, the language is detected automatically from the file name.
* Currently the following languages are supported by Biome:
* Currently, the following languages are supported by Biome:
* <ul>
* <li>js (JavaScript)</li>
* <li>jsx (JavaScript + JSX)</li>
Expand All @@ -65,7 +65,9 @@ public class BiomeStep {
* <li>tsx (TypeScript + JSX)</li>
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
* extension)</li>
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
* <li>json (JSON)</li>
* <li>jsonc (JSON + comments)</li>
* </ul>
*/
private String language;
Expand Down Expand Up @@ -274,7 +276,9 @@ public BiomeStep withConfigPath(String configPath) {
* <li>tsx (TypeScript + JSX)</li>
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
* extension)</li>
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
* <li>json (JSON)</li>
* <li>jsonc (JSON + comments)</li>
* </ul>
*
* @param language The language of the files to format.
Expand Down Expand Up @@ -450,7 +454,7 @@ private String format(ProcessRunner runner, String input, File file) throws IOEx
* expected language / syntax. Biome always determined the language from the file
* extension. This method returns the file name for the desired language when a
* language was requested explicitly, or the file name of the input file for
* auto detection.
* auto-detection.
*
* @param file File to be formatted.
* @return The file name to pass to the Biome executable.
Expand Down Expand Up @@ -479,6 +483,10 @@ private String resolveFileName(File file) {
return "tsx".equals(ext) ? name : "file.tsx";
case "json":
return "json".equals(ext) ? name : "file.json";
case "jsonc":
return "jsonc".equals(ext) ? name : "file.jsonc";
case "css":
return "css".equals(ext) ? name : "file.css";
// so that we can support new languages such as css or yaml when Biome adds
// support for them without having to change the code
default:
Expand Down
4 changes: 4 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

* Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general
formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
([#2259](https://github.com/diffplug/spotless/pull/2259))

## [7.0.0.BETA2] - 2024-08-25
### Changed
* Support toning down sortPom logging. ([#2185](https://github.com/diffplug/spotless/pull/2185))
Expand Down
18 changes: 18 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,22 @@ spotless {
}
```

## CSS

`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)

```gradle
spotless {
css {
target 'css/**/*.css' // default: '**/*.css'

biome('1.8.3') // has its own section below
}
}
```

Note regarding biome: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).

## Prettier

[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, ...).
Expand Down Expand Up @@ -1311,6 +1327,8 @@ is pretty fast. It can currently format JavaScript, TypeScript, JSX, and JSON, a
You can use Biome in any language-specific format for supported languages, but
usually you will be creating a generic format.

Note regarding biome: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).

```gradle
spotless {
format 'styling', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ protected FormatterStep createStep() {
/**
* Gets the language (syntax) of the input files to format. When
* <code>null</code> or the empty string, the language is detected automatically
* from the file name. Currently the following languages are supported by Biome:
* from the file name. Currently, the following languages are supported by Biome:
* <ul>
* <li>js (JavaScript)</li>
* <li>jsx (JavaScript + JSX)</li>
Expand All @@ -180,7 +180,9 @@ protected FormatterStep createStep() {
* <li>tsx (TypeScript + JSX)</li>
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
* extension)</li>
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
* <li>json (JSON)</li>
* <li>jsonc (JSON + comments)</li>
* </ul>
*
* @return The language of the input files.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2024 DiffPlug
*
* 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.
*/
package com.diffplug.gradle.spotless;

import javax.inject.Inject;

import com.diffplug.spotless.biome.BiomeFlavor;

/** Gradle step for formatting CSS files. */
public class CssExtension extends FormatExtension {
private static final String CSS_FILE_EXTENSION = "**/*.css";

static final String NAME = "css";

@Inject
public CssExtension(SpotlessExtension spotless) {
super(spotless);
}

/** If the user hasn't specified files, assume all CSS files should be checked. */
@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
target = parseTarget(CSS_FILE_EXTENSION);
}
super.setupTask(task);
}

/**
* Adds the default version of the biome formatter.
* Defaults to downloading the default Biome version from the network. To work
* offline, you can specify the path to the Biome executable via
* {@code biome().pathToExe(...)}.
*/
public BiomeCss biome() {
return biome(null);
}

/**
* Adds the given version of the biome formatter.
* Defaults to downloading the default Biome version from the network. To work
* offline, you can specify the path to the Biome executable via
* {@code biome().pathToExe(...)}.
* @param version Biome version to use.
*/
public BiomeCss biome(String version) {
var biomeConfig = new BiomeCss(version);
addStep(biomeConfig.createStep());
return biomeConfig;
}

/**
* Biome formatter step for CSS.
*/
public class BiomeCss extends BiomeStepConfig<BiomeCss> {
/**
* Creates a new Biome formatter step config for formatting CSS files. Unless
* overwritten, the given Biome version is downloaded from the network.
*
* @param version Biome version to use.
*/
public BiomeCss(String version) {
super(getProject(), CssExtension.this::replaceStep, BiomeFlavor.BIOME, version);
}

@Override
protected String getLanguage() {
return "css";
}

@Override
protected BiomeCss getThis() {
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@ public BiomeGeneric(String version) {
* <li>tsx (TypeScript + JSX)</li>
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
* extension)</li>
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
* <li>json (JSON)</li>
* <li>jsonc (JSON + comments)</li>
* </ul>
*
* @param language The language of the files to format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ public void go(Action<GoExtension> closure) {
format(GoExtension.NAME, GoExtension.class, closure);
}

/** Configures the special CSS-specific extension. */
public void css(Action<CssExtension> closure) {
requireNonNull(closure);
format(CssExtension.NAME, CssExtension.class, closure);
}

/** Configures the special POM-specific extension. */
public void pom(Action<PomExtension> closure) {
requireNonNull(closure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,73 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.owasp.encoder.Encode;

/**
* Tests for the Biome formatter used via the Gradle spotless plugin.
*/
class BiomeIntegrationTest extends GradleIntegrationHarness {
/**
* Tests that biome can be used as a JSON formatting step, using biome 1.8.3 which
* requires opt-in.
*
* @throws Exception When a test failure occurs.
*/
@Test
void asCssStepExperimental() throws Exception {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" css {",
" target '**/*.css'",
" biome('1.8.3').configPath('configs')",
" }",
"}");
setFile("biome_test.css").toResource("biome/css/fileBefore.css");
setFile("configs/biome.json").toResource("biome/config/css-enabled.json");

var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("biome_test.css").sameAsResource("biome/css/fileAfter.css");
}

/**
* Tests that biome can be used as a JSON formatting step, using biome 1.9.0 which
* does not require opt-in.
*
* @throws Exception When a test failure occurs.
*/
@Test
void asCssStepStable() throws Exception {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" css {",
" target '**/*.css'",
" biome('1.9.0')",
" }",
"}");
setFile("biome_test.css").toResource("biome/css/fileBefore.css");

var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("biome_test.css").sameAsResource("biome/css/fileAfter.css");
}

/**
* Tests that biome can be used as a generic formatting step.
*
* @throws Exception When a test failure occurs.
*/
@Test
void asGenericStep() throws IOException {
void asGenericStep() throws Exception {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
Expand Down
4 changes: 4 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

* Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general
formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
([#2259](https://github.com/diffplug/spotless/pull/2259))

## [2.44.0.BETA2] - 2024-08-25
### Changed
* Support toning down sortPom logging. ([#2185](https://github.com/diffplug/spotless/pull/2185))
Expand Down
28 changes: 27 additions & 1 deletion plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,30 @@ Standard Go formatter, part of Go distribution.
</gofmt>
```

## CSS

[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).

```xml
<configuration>
<css>
<!-- These are the defaults, you can override if you want -->
<includes>
<include>src/main/css/**/*.css</include>
<include>src/test/css/**/*.css</include>
</includes>

<biome /> <!-- has its own section below -->

<licenseHeader>
<content>/* (C)$YEAR */</content> <!-- or <file>${project.basedir}/license-header</file> -->
</licenseHeader>
</css>
</configuration>
```

Note regarding biome: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).

## Prettier

[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, ...).
Expand Down Expand Up @@ -1340,6 +1364,8 @@ is pretty fast. It can currently format JavaScript, TypeScript, JSX, and JSON, a
You can use Biome in any language-specific format for supported languages, but
usually you will be creating a generic format.

Note regarding CSS: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).

```xml
<configuration>
<formats>
Expand All @@ -1355,7 +1381,7 @@ usually you will be creating a generic format.
<!-- (optional) Path to the directory with the biome.json conig file -->
<configPath>${project.basedir}/path/to/config/dir</configPath>

<!-- (optional) Biome will auto detect the language based on the file extension. -->
<!-- (optional) Biome will auto-detect the language based on the file extension. -->
<!-- See below for possible values. -->
<language>ts</language>
</prettier>
Expand Down
Loading
Loading