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

Make the npm steps roundtrip serializable #2135

Merged
merged 8 commits into from
May 29, 2024
4 changes: 4 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/FileSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public static Promised promise(Iterable<File> files) {
return new Promised(MoreIterables.toNullHostileList(files), null);
}

public static Promised promise(File file) {
return new Promised(List.of(file), null);
}

/** Returns all of the files in this signature, throwing an exception if there are more or less than 1 file. */
public Collection<File> files() {
return Collections.unmodifiableList(files);
Expand Down
29 changes: 7 additions & 22 deletions lib/src/main/java/com/diffplug/spotless/npm/EslintConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,37 +16,22 @@
package com.diffplug.spotless.npm;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import javax.annotation.Nullable;

import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.ThrowingEx;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class EslintConfig implements Serializable {

private static final long serialVersionUID = -6196834313082791248L;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
@Nullable
private final transient File eslintConfigPath;
private static final long serialVersionUID = 1L;

@SuppressWarnings("unused")
private final FileSignature eslintConfigPathSignature;

private final FileSignature.Promised eslintConfigPathSignature;
private final String eslintConfigJs;

public EslintConfig(@Nullable File eslintConfigPath, @Nullable String eslintConfigJs) {
try {
this.eslintConfigPath = eslintConfigPath;
this.eslintConfigPathSignature = eslintConfigPath != null ? FileSignature.signAsList(this.eslintConfigPath) : FileSignature.signAsList();
this.eslintConfigJs = eslintConfigJs;
} catch (IOException e) {
throw ThrowingEx.asRuntime(e);
}
this.eslintConfigPathSignature = eslintConfigPath == null ? null : FileSignature.promise(eslintConfigPath);
this.eslintConfigJs = eslintConfigJs;
}

public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) {
Expand All @@ -55,7 +40,7 @@ public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) {

@Nullable
public File getEslintConfigPath() {
return eslintConfigPath;
return eslintConfigPathSignature == null ? null : eslintConfigPathSignature.get().getOnlyFile();
}

@Nullable
Expand All @@ -64,7 +49,7 @@ public String getEslintConfigJs() {
}

public EslintConfig verify() {
if (eslintConfigPath == null && eslintConfigJs == null) {
if (eslintConfigPathSignature == null && eslintConfigJs == null) {
throw new IllegalArgumentException("ESLint must be configured using either a configFile or a configJs - but both are null.");
}
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,8 +39,6 @@
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.npm.EslintRestService.FormatOption;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class EslintFormatterStep {

private static final Logger logger = LoggerFactory.getLogger(EslintFormatterStep.class);
Expand Down Expand Up @@ -81,12 +79,10 @@ public static FormatterStep create(Map<String, String> devDependencies, Provisio
}

private static class State extends NpmFormatterStepStateBase implements Serializable {
private static final long serialVersionUID = 1L;

private static final long serialVersionUID = -539537027004745812L;
private final EslintConfig origEslintConfig;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
private transient EslintConfig eslintConfigInUse;
private EslintConfig eslintConfigInUse;

State(String stepName, Map<String, String> devDependencies, File projectDir, File buildDir, File cacheDir, NpmPathResolver npmPathResolver, EslintConfig eslintConfig) throws IOException {
super(stepName,
Expand All @@ -102,15 +98,14 @@ private static class State extends NpmFormatterStepStateBase implements Serializ
projectDir,
buildDir,
cacheDir,
npmPathResolver::resolveNpmExecutable,
npmPathResolver::resolveNodeExecutable));
npmPathResolver));
this.origEslintConfig = requireNonNull(eslintConfig.verify());
this.eslintConfigInUse = eslintConfig;
}

@Override
protected void prepareNodeServerLayout() throws IOException {
super.prepareNodeServerLayout();
protected void prepareNodeServerLayout(NodeServerLayout nodeServerLayout) throws IOException {
super.prepareNodeServerLayout(nodeServerLayout);
if (origEslintConfig.getEslintConfigPath() != null) {
// If any config files are provided, we need to make sure they are at the same location as the node modules
// as eslint will try to resolve plugin/config names relatively to the config file location and some
Expand All @@ -126,9 +121,10 @@ protected void prepareNodeServerLayout() throws IOException {
public FormatterFunc createFormatterFunc() {
try {
logger.info("Creating formatter function (starting server)");
ServerProcessInfo eslintRestServer = npmRunServer();
Runtime runtime = toRuntime();
ServerProcessInfo eslintRestServer = runtime.npmRunServer();
EslintRestService restService = new EslintRestService(eslintRestServer.getBaseUrl());
return Closeable.ofDangerous(() -> endServer(restService, eslintRestServer), new EslintFilePathPassingFormatterFunc(locations.projectDir(), nodeServerLayout.nodeModulesDir(), eslintConfigInUse, restService));
return Closeable.ofDangerous(() -> endServer(restService, eslintRestServer), new EslintFilePathPassingFormatterFunc(locations.projectDir(), runtime.nodeServerLayout().nodeModulesDir(), eslintConfigInUse, restService));
} catch (IOException e) {
throw ThrowingEx.asRuntime(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 DiffPlug
* Copyright 2022-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,43 +16,29 @@
package com.diffplug.spotless.npm;

import java.io.File;
import java.io.IOException;

import javax.annotation.Nullable;

import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.ThrowingEx;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class EslintTypescriptConfig extends EslintConfig {

private static final long serialVersionUID = -126864670181617006L;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
@Nullable
private final transient File typescriptConfigPath;
private static final long serialVersionUID = 2L;

@SuppressWarnings("unused")
private final FileSignature typescriptConfigPathSignature;
private final FileSignature.Promised typescriptConfigPathSignature;

public EslintTypescriptConfig(@Nullable File eslintConfigPath, @Nullable String eslintConfigJs, @Nullable File typescriptConfigPath) {
super(eslintConfigPath, eslintConfigJs);
try {
this.typescriptConfigPath = typescriptConfigPath;
this.typescriptConfigPathSignature = typescriptConfigPath != null ? FileSignature.signAsList(this.typescriptConfigPath) : FileSignature.signAsList();
} catch (IOException e) {
throw ThrowingEx.asRuntime(e);
}
this.typescriptConfigPathSignature = typescriptConfigPath != null ? FileSignature.promise(typescriptConfigPath) : null;
}

@Override
public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) {
return new EslintTypescriptConfig(eslintConfigPath, this.getEslintConfigJs(), this.typescriptConfigPath);
return new EslintTypescriptConfig(eslintConfigPath, this.getEslintConfigJs(), getTypescriptConfigPath());
}

@Nullable
public File getTypescriptConfigPath() {
return typescriptConfigPath;
return typescriptConfigPathSignature == null ? null : this.typescriptConfigPathSignature.get().getOnlyFile();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,36 +19,23 @@

import java.io.File;
import java.io.Serializable;
import java.util.function.Supplier;

import javax.annotation.Nonnull;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

class NpmFormatterStepLocations implements Serializable {

private static final long serialVersionUID = -1055408537924029969L;
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
private final transient File projectDir;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
private final transient File buildDir;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
private final transient File cacheDir;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
private final transient Supplier<File> npmExecutable;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
private final transient Supplier<File> nodeExecutable;
private final File projectDir;
private final File buildDir;
private final File cacheDir;
private final NpmPathResolver resolver;

public NpmFormatterStepLocations(@Nonnull File projectDir, @Nonnull File buildDir, File cacheDir, @Nonnull Supplier<File> npmExecutable, @Nonnull Supplier<File> nodeExecutable) {
public NpmFormatterStepLocations(@Nonnull File projectDir, @Nonnull File buildDir, File cacheDir, @Nonnull NpmPathResolver resolver) {
this.projectDir = requireNonNull(projectDir);
this.buildDir = requireNonNull(buildDir);
this.cacheDir = cacheDir;
this.npmExecutable = requireNonNull(npmExecutable);
this.nodeExecutable = requireNonNull(nodeExecutable);
this.resolver = requireNonNull(resolver);
}

public File projectDir() {
Expand All @@ -64,10 +51,10 @@ public File cacheDir() {
}

public File npmExecutable() {
return npmExecutable.get();
return resolver.resolveNpmExecutable();
}

public File nodeExecutable() {
return nodeExecutable.get();
return resolver.resolveNodeExecutable();
}
}
Loading
Loading