Skip to content

Commit

Permalink
Merge pull request #64 from HubSpot/download-node
Browse files Browse the repository at this point in the history
Download node and prettier, rather than bundling
  • Loading branch information
jhaber authored Feb 3, 2022
2 parents 5b198e4 + 66f2d86 commit db63697
Show file tree
Hide file tree
Showing 41 changed files with 628 additions and 519 deletions.
55 changes: 21 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,24 @@ To format additional directories or file types via the commandline, you can pass

If you want to customize the behavior of prettier, you can use a normal prettier configuration [file](https://prettier.io/docs/en/configuration.html). Alternatively, you can configure prettier directly via the Maven plugin using the following options:

| Name | -D property name | Default Value | Description |
| ---- | ---------------- | ------------- | ----------- |
| skip | N/A | false | If set to true, plugin execution will be skipped |
| fail | N/A | true | **Only appplies to `check` goal**. If set to true, the plugin execution will fail if any unformatted files are found |
| generateDiff | N/A | false | **Only appplies to `check` goal. Be sure to have to sh and diff in your PATH**. If set to true, a diff will be generated between the current code and the prettier-formatted code. |
| diffGenerator | prettier.diffGenerator | _ | **Only appplies to `check` goal**. Can be used to supply a custom implementation of [`DiffGenerator`](https://github.com/HubSpot/prettier-maven-plugin/blob/master/prettier-maven-plugin/src/main/java/com/hubspot/maven/plugins/prettier/diff/DiffGenerator.java)
| nodeVersion | prettier.nodeVersion | 12.13.0 | Controls version of Node used to run prettier-java. Valid values can be found [here](https://github.com/HubSpot/prettier-maven-plugin/tree/master/prettier-maven-plugin/src/main/binaries/node) |
| nodePath | prettier.nodePath | - | Can be used to supply your own node executable, rather than using one of the versions bundled with the plugin. To use the version of node on your `$PATH`, you can simply set this option to `node`. NOTE: `nodeVersion` option has no effect when using `nodePath`. |
| prettierJavaVersion | prettier.prettierJavaVersion | 0.7.0 | Controls version of prettier-java that is used. Valid values can be found [here](https://github.com/HubSpot/prettier-maven-plugin/tree/master/prettier-maven-plugin/src/main/binaries/prettier-java) |
| printWidth | prettier.printWidth | `null` | If set, will be passed to prettier as `--print-width`. More information [here](https://prettier.io/docs/en/options.html#print-width) |
| tabWidth | prettier.tabWidth | `null` | If set, will be passed to prettier as `--tab-width`. More information [here](https://prettier.io/docs/en/options.html#tab-width) |
| useTabs | prettier.useTabs | `null` | If set, will be passed to prettier as `--use-tabs`. More information [here](https://prettier.io/docs/en/options.html#tabs) |
| endOfLine | prettier.endOfLine | `null` | If set, will be passed to prettier as `--end-of-line`. More information [here](https://prettier.io/docs/en/options.html#end-of-line) |
| ignoreConfigFile | prettier.ignoreConfigFile | `false` | If set to true, pretter will be invoked with `--no-config`. More information [here](https://prettier.io/docs/en/cli.html#--no-config) |
| ignoreEditorConfig | prettier.ignoreEditorConfig | `false` | If set to true, pretter will be invoked with `--no-editorconfig`. More information [here](https://prettier.io/docs/en/cli.html#--no-editorconfig) |
| inputGlobs | prettier.inputGlobs | `src/{main,test}/java/**/*.java` | Controls the input paths passed to prettier, useful for formatting additional directories or file types. More information [here](https://prettier.io/docs/en/cli.html#file-patterns) |

### Developing the plugin

For convenience, this plugin bundles Node, prettier, and prettier-java. Over time, these bundled dependencies will need to be kept up to date. Below are some directions for adding new versions of Node and prettier-java.

#### Adding new versions of node

You can download binaries for Linux/OSX/Windows from here:
https://nodejs.org/en/about/releases/

1. Make a new folder located at `prettier-maven-plugin/src/main/binaries/node/{node-version}` and drop the binaries in there using the existing name formatting.
2. Update the pom.xml to attach these new binaries

#### Adding new versions of prettier-java

1. Run `./create-prettier-java-zip.sh {prettier-java-version}` which will spit out the location of a zip file
2. Make a new folder located at `src/main/binaries/prettier-java/{prettier-java-version}` and copy the zip file under there
3. Update the pom.xml to attach this zip file
| Name | -D property name | Default Value | Description |
|---------------------|------------------------------|----------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| skip | N/A | false | If set to true, plugin execution will be skipped |
| fail | N/A | true | **Only appplies to `check` goal**. If set to true, the plugin execution will fail if any unformatted files are found |
| generateDiff | N/A | false | **Only appplies to `check` goal. Be sure to have to sh and diff in your PATH**. If set to true, a diff will be generated between the current code and the prettier-formatted code. |
| diffGenerator | prettier.diffGenerator | _ | **Only appplies to `check` goal**. Can be used to supply a custom implementation of [`DiffGenerator`](https://github.com/HubSpot/prettier-maven-plugin/blob/master/prettier-maven-plugin/src/main/java/com/hubspot/maven/plugins/prettier/diff/DiffGenerator.java)
| nodeVersion | prettier.nodeVersion | 16.13.1 | Controls version of Node used to run prettier-java. |
| nodePath | prettier.nodePath | - | Can be used to supply your own node executable, rather than having the plugin download it. To use the version of node on your `$PATH`, you can simply set this option to `node`. |
| npmPath | prettier.npmPath | - | Can be used to supply your own npm executable, rather than having the plugin download it. To use the version of npm on your `$PATH`, you can simply set this option to `npm`. |
| prettierJavaVersion | prettier.prettierJavaVersion | 0.7.0 | Controls version of prettier-java that is used. |
| printWidth | prettier.printWidth | `null` | If set, will be passed to prettier as `--print-width`. More information [here](https://prettier.io/docs/en/options.html#print-width) |
| tabWidth | prettier.tabWidth | `null` | If set, will be passed to prettier as `--tab-width`. More information [here](https://prettier.io/docs/en/options.html#tab-width) |
| useTabs | prettier.useTabs | `null` | If set, will be passed to prettier as `--use-tabs`. More information [here](https://prettier.io/docs/en/options.html#tabs) |
| endOfLine | prettier.endOfLine | `null` | If set, will be passed to prettier as `--end-of-line`. More information [here](https://prettier.io/docs/en/options.html#end-of-line) |
| ignoreConfigFile | prettier.ignoreConfigFile | `false` | If set to true, pretter will be invoked with `--no-config`. More information [here](https://prettier.io/docs/en/cli.html#--no-config) |
| ignoreEditorConfig | prettier.ignoreEditorConfig | `false` | If set to true, pretter will be invoked with `--no-editorconfig`. More information [here](https://prettier.io/docs/en/cli.html#--no-editorconfig) |
| inputGlobs | prettier.inputGlobs | `src/{main,test}/java/**/*.java` | Controls the input paths passed to prettier, useful for formatting additional directories or file types. More information [here](https://prettier.io/docs/en/cli.html#file-patterns) |

### Note

For convenience, this plugin downloads Node, prettier, and prettier-java as needed. Node is downloaded from https://nodejs.org/dist/ and prettier-plugin-java is downloaded via npm
18 changes: 0 additions & 18 deletions create-prettier-java-zip.sh

This file was deleted.

5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@
<artifactId>assertj-core</artifactId>
<version>3.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-verifier</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
8 changes: 0 additions & 8 deletions prettier-maven-plugin-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-verifier</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -51,9 +46,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>all</parallel>
<threadCount>1</threadCount>
<perCoreThreadCount>true</perCoreThreadCount>
<environmentVariables>
<PLUGIN_VERSION>${project.version}</PLUGIN_VERSION>
</environmentVariables>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.hubspot.maven.plugins.prettier;

import static org.assertj.core.api.Assertions.catchThrowable;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand All @@ -11,17 +11,19 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.collect.ImmutableSet;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;

public abstract class AbstractPrettierMojoTest {
private static final Logger LOG = Logger.getLogger(AbstractPrettierMojoTest.class.getName());

protected static final String JAVA_GOOD_FORMATTING = "java-good-formatting/*.java";
protected static final String JAVA_BAD_FORMATTING = "java-bad-formatting/*.java";
protected static final String JAVA_INVALID_SYNTAX = "java-invalid-syntax/*.java";
Expand All @@ -32,29 +34,47 @@ public abstract class AbstractPrettierMojoTest {
protected static final String EMPTY = "empty/*.java";
protected static final String BUILD_SUCCESS = "BUILD SUCCESS";
protected static final String BUILD_FAILURE = "BUILD FAILURE";
private static final Set<String> PRETTIER_JAVA_VERSIONS_TO_TEST = findPrettierJavaVersionsToTest();
private static final Set<String> PRETTIER_JAVA_VERSIONS_TO_TEST = ImmutableSet.of("1.5.0", "1.6.1");

protected static Set<String> getPrettierJavaVersionsToTest() {
return PRETTIER_JAVA_VERSIONS_TO_TEST;
}

protected static MavenResult runMaven(TestConfiguration testConfiguration)
throws IOException, VerificationException {
protected static MavenResult runMaven(TestConfiguration testConfiguration) throws IOException {
Path temp = setupTestDirectory(testConfiguration);

Verifier verifier = new Verifier(temp.toAbsolutePath().toString());
verifier.setAutoclean(false);
Throwable t = catchThrowable(() -> verifier.executeGoal("validate"));
verifier.resetStreams();

return new MavenResult(
t == null,
verifier
.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false)
.stream()
.map(Verifier::stripAnsi)
.collect(Collectors.joining("\n"))
LOG.log(
Level.INFO,
"Testing prettier-java={0}, goal={1}, input={2}",
new Object[] {
testConfiguration.getPrettierJavaVersion(),
testConfiguration.getGoal(),
testConfiguration.getInputGlobs()
}
);

List<String> command = Arrays.asList(
"mvn",
"-e",
"--batch-mode",
"-Dstyle.color=never",
"-Daether.artifactResolver.snapshotNormalization=false",
"-Daether.connector.resumeDownloads=false",
"validate"
);

Process process = new ProcessBuilder(command.toArray(new String[0]))
.directory(temp.toFile())
.redirectErrorStream(true)
.start();

try (InputStreamReader reader = new InputStreamReader(new BufferedInputStream(process.getInputStream()), StandardCharsets.UTF_8)) {
String output = CharStreams.toString(reader);
boolean success = process.waitFor() == 0;
return new MavenResult(success, output);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while running maven", e);
}
}

protected static boolean isNewishVersion(String prettierJavaVersion) {
Expand Down Expand Up @@ -118,40 +138,4 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)

return temp;
}

private static Set<String> findPrettierJavaVersionsToTest() {
Path baseDirectory = Paths.get("../prettier-maven-plugin/src/main/binaries/prettier-java");

// keep the build time down by not testing every older version
Set<String> ignoredPrettierJavaVersions = ImmutableSet.of(
"0.6.0",
"0.7.1",
"0.8.0",
"0.8.2",
"0.8.3",
"1.0.1",
"1.1.0",
"c08da7b2b0486f59980a01cb99c6f0756725450a",
"6cf5cfdf76550ab4418a6e900696ba35eaa0fbc8",
"a24aa13714b850ab3d0f4e3f07414137c33321a1"
);

try (Stream<Path> files = Files.walk(baseDirectory)) {
return files
.filter(Files::isRegularFile)
.map(Path::getFileName)
.map(Path::toString)
.map(
prettierJavaZip ->
prettierJavaZip.substring(
"prettier-java-".length(),
prettierJavaZip.length() - ".zip".length()
)
)
.filter(prettierJavaVersion -> !ignoredPrettierJavaVersions.contains(prettierJavaVersion))
.collect(Collectors.toSet());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
package com.hubspot.maven.plugins.prettier;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.junit.Test;

import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
import com.hubspot.maven.plugins.prettier.TestConfiguration.Goal;

public class CheckMojoTest extends AbstractPrettierMojoTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ public static Builder newBuilder() {
return new Builder();
}

public String getPrettierJavaVersion() {
return prettierJavaVersion;
}

public List<String> getInputGlobs() {
return inputGlobs;
}

public Goal getGoal() {
return goal;
}

public String render(String template) {
return template
.replace(
"${nodeVersion}",
MoreObjects.firstNonNull(System.getenv("PRETTIER_NODE_VERSION"), "12.13.0")
MoreObjects.firstNonNull(System.getenv("PRETTIER_NODE_VERSION"), "16.13.2")
)
.replace("${pluginVersion}", System.getenv("PLUGIN_VERSION"))
.replace("${prettierJavaVersion}", prettierJavaVersion)
Expand Down
Loading

0 comments on commit db63697

Please sign in to comment.