Skip to content

Commit ed4e5c2

Browse files
committed
Move RomeStepConfig into its own file since it's so big.
1 parent 6c8bff1 commit ed4e5c2

File tree

2 files changed

+273
-239
lines changed

2 files changed

+273
-239
lines changed

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

Lines changed: 0 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.Serializable;
2323
import java.nio.charset.Charset;
2424
import java.nio.file.Files;
25-
import java.nio.file.Paths;
2625
import java.util.ArrayList;
2726
import java.util.Arrays;
2827
import java.util.Collection;
@@ -38,7 +37,6 @@
3837
import org.gradle.api.Action;
3938
import org.gradle.api.GradleException;
4039
import org.gradle.api.Project;
41-
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
4240
import org.gradle.api.file.ConfigurableFileTree;
4341
import org.gradle.api.file.FileCollection;
4442
import org.gradle.api.plugins.BasePlugin;
@@ -65,7 +63,6 @@
6563
import com.diffplug.spotless.generic.TrimTrailingWhitespaceStep;
6664
import com.diffplug.spotless.npm.NpmPathResolver;
6765
import com.diffplug.spotless.npm.PrettierFormatterStep;
68-
import com.diffplug.spotless.rome.RomeStep;
6966

7067
import groovy.lang.Closure;
7168

@@ -652,242 +649,6 @@ protected FormatterStep createStep() {
652649
}
653650
}
654651

655-
public abstract static class RomeStepConfig<Self extends RomeStepConfig<Self>> {
656-
/**
657-
* Optional path to the directory with configuration file for Rome. The file
658-
* must be named {@code rome.json}. When none is given, the default
659-
* configuration is used. If this is a relative path, it is resolved against the
660-
* project's base directory.
661-
*/
662-
@Nullable
663-
private Object configPath;
664-
665-
/**
666-
* Optional directory where the downloaded Rome executable is placed. If this is
667-
* a relative path, it is resolved against the project's base directory.
668-
* Defaults to
669-
* <code>~/.m2/repository/com/diffplug/spotless/spotless-data/rome</code>.
670-
*/
671-
@Nullable
672-
private Object downloadDir;
673-
674-
/**
675-
* Optional path to the Rome executable. Either a <code>version</code> or a
676-
* <code>pathToExe</code> should be specified. When not given, an attempt is
677-
* made to download the executable for the given version from the network. When
678-
* given, the executable is used and the <code>version</code> parameter is
679-
* ignored.
680-
* <p>
681-
* When an absolute path is given, that path is used as-is. When a relative path
682-
* is given, it is resolved against the project's base directory. When only a
683-
* file name (i.e. without any slashes or back slash path separators such as
684-
* {@code rome}) is given, this is interpreted as the name of a command with
685-
* executable that is in your {@code path} environment variable. Use
686-
* {@code ./executable-name} if you want to use an executable in the project's
687-
* base directory.
688-
*/
689-
@Nullable
690-
private Object pathToExe;
691-
692-
/** A reference to the Gradle project for which spotless is executed. */
693-
private final Project project;
694-
695-
/** Replaces the current Rome formatter step with the given step. */
696-
private final Consumer<FormatterStep> replaceStep;
697-
698-
/**
699-
* Rome version to download, applies only when no <code>pathToExe</code> is
700-
* specified explicitly. Either a <code>version</code> or a
701-
* <code>pathToExe</code> should be specified. When not given, a default known
702-
* version is used. For stable builds, it is recommended that you always set the
703-
* version explicitly. This parameter is ignored when you specify a
704-
* <code>pathToExe</code> explicitly.
705-
*/
706-
@Nullable
707-
private String version;
708-
709-
protected RomeStepConfig(Project project, Consumer<FormatterStep> replaceStep, String version) {
710-
this.project = requireNonNull(project);
711-
this.replaceStep = requireNonNull(replaceStep);
712-
this.version = version;
713-
}
714-
715-
/**
716-
* Optional path to the directory with configuration file for Rome. The file
717-
* must be named {@code rome.json}. When none is given, the default
718-
* configuration is used. If this is a relative path, it is resolved against the
719-
* project's base directory.
720-
* @return This step for further configuration.
721-
*/
722-
public Self configPath(Object configPath) {
723-
this.configPath = configPath;
724-
replaceStep();
725-
return getThis();
726-
}
727-
728-
/**
729-
* Optional directory where the downloaded Rome executable is placed. If this is
730-
* a relative path, it is resolved against the project's base directory.
731-
* Defaults to
732-
* <code>~/.m2/repository/com/diffplug/spotless/spotless-data/rome</code>.
733-
* @return This step for further configuration.
734-
*/
735-
public Self downloadDir(Object downloadDir) {
736-
this.downloadDir = downloadDir;
737-
replaceStep();
738-
return getThis();
739-
}
740-
741-
/**
742-
* Optional path to the Rome executable. Overwrites the configured version. No
743-
* attempt is made to download the Rome executable from the network.
744-
* <p>
745-
* When an absolute path is given, that path is used as-is. When a relative path
746-
* is given, it is resolved against the project's base directory. When only a
747-
* file name (i.e. without any slashes or back slash path separators such as
748-
* {@code rome}) is given, this is interpreted as the name of a command with
749-
* executable that is in your {@code path} environment variable. Use
750-
* {@code ./executable-name} if you want to use an executable in the project's
751-
* base directory.
752-
* @return This step for further configuration.
753-
*/
754-
public Self pathToExe(Object pathToExe) {
755-
this.pathToExe = pathToExe;
756-
replaceStep();
757-
return getThis();
758-
}
759-
760-
/**
761-
* Creates a new formatter step that formats code by calling the Rome
762-
* executable, using the current configuration.
763-
*
764-
* @return A new formatter step for the Rome formatter.
765-
*/
766-
protected FormatterStep createStep() {
767-
var builder = newBuilder();
768-
if (configPath != null) {
769-
var resolvedConfigPath = project.file(configPath);
770-
builder.withConfigPath(resolvedConfigPath.toString());
771-
}
772-
builder.withLanguage(getLanguage());
773-
return builder.create();
774-
}
775-
776-
/**
777-
* Gets the language (syntax) of the input files to format. When
778-
* <code>null</code> or the empty string, the language is detected automatically
779-
* from the file name. Currently the following languages are supported by Rome:
780-
* <ul>
781-
* <li>js (JavaScript)</li>
782-
* <li>jsx (JavaScript + JSX)</li>
783-
* <li>js? (JavaScript or JavaScript + JSX, depending on the file
784-
* extension)</li>
785-
* <li>ts (TypeScript)</li>
786-
* <li>tsx (TypeScript + JSX)</li>
787-
* <li>ts? (TypeScript or TypeScript + JSX, depending on the file
788-
* extension)</li>
789-
* <li>json (JSON)</li>
790-
* </ul>
791-
*
792-
* @return The language of the input files.
793-
*/
794-
protected abstract String getLanguage();
795-
796-
/**
797-
* @return This Rome config instance.
798-
*/
799-
protected abstract Self getThis();
800-
801-
/**
802-
* Creates a new Rome step and replaces the existing Rome step in the list of
803-
* format steps.
804-
*/
805-
protected void replaceStep() {
806-
replaceStep.accept(createStep());
807-
}
808-
809-
/**
810-
* Finds the data directory that can be used for storing shared data such as
811-
* Rome executable globally. This is a directory in the local repository, e.g.
812-
* <code>~/.m2/repository/com/diffplus/spotless/spotless-data<code>.
813-
*
814-
* @return The directory for storing shared data.
815-
*/
816-
private File findDataDir() {
817-
var currentRepo = project.getRepositories().stream()
818-
.filter(r -> r instanceof MavenArtifactRepository)
819-
.map(r -> (MavenArtifactRepository) r)
820-
.filter(r -> "file".equals(r.getUrl().getScheme()))
821-
.findAny().orElse(null);
822-
// Temporarily add mavenLocal() repository to get its file URL
823-
var localRepo = currentRepo != null ? (MavenArtifactRepository) currentRepo : project.getRepositories().mavenLocal();
824-
try {
825-
// e.g. ~/.m2/repository/
826-
var repoPath = Paths.get(localRepo.getUrl().getPath());
827-
var dataPath = repoPath.resolve("com").resolve("diffplug").resolve("spotless").resolve("spotless-data");
828-
return dataPath.toAbsolutePath().toFile();
829-
} finally {
830-
// Remove mavenLocal() repository again if it was not part of the project
831-
if (currentRepo == null) {
832-
project.getRepositories().remove(localRepo);
833-
}
834-
}
835-
}
836-
837-
/**
838-
* A new builder for configuring a Rome step that either downloads the Rome
839-
* executable with the given version from the network, or uses the executable
840-
* from the given path.
841-
*
842-
* @return A builder for a Rome step.
843-
*/
844-
private RomeStep newBuilder() {
845-
if (pathToExe != null) {
846-
var resolvedPathToExe = resolvePathToExe();
847-
return RomeStep.withExePath(resolvedPathToExe);
848-
} else {
849-
var downloadDir = resolveDownloadDir();
850-
return RomeStep.withExeDownload(version, downloadDir);
851-
}
852-
}
853-
854-
/**
855-
* Resolves the path to the Rome executable. When the path is only a file name,
856-
* do not perform any resolution and interpret it as a command that must be on
857-
* the user's path. Otherwise resolve the executable path against the project's
858-
* base directory.
859-
*
860-
* @param config Configuration from the Maven Mojo execution with details about
861-
* the currently executed project.
862-
* @return The resolved path to the Rome executable.
863-
*/
864-
private String resolvePathToExe() {
865-
var fileNameOnly = pathToExe instanceof String && Paths.get(pathToExe.toString()).getNameCount() == 1;
866-
if (fileNameOnly) {
867-
return pathToExe.toString();
868-
} else {
869-
return project.file(pathToExe).toString();
870-
}
871-
}
872-
873-
/**
874-
* Resolves the directory to use for storing downloaded Rome executable. When a
875-
* {@link #downloadDir} is given, use that directory, resolved against the
876-
* current project's directory. Otherwise, use the {@code Rome} sub folder in
877-
* the shared data directory.
878-
*
879-
* @param config Configuration for this step.
880-
* @return The download directory for the Rome executable.
881-
*/
882-
private String resolveDownloadDir() {
883-
if (downloadDir != null) {
884-
return project.file(downloadDir).toString();
885-
} else {
886-
return findDataDir().toPath().resolve("rome").toString();
887-
}
888-
}
889-
}
890-
891652
/**
892653
* Generic Rome formatter step that detects the language of the input file from
893654
* the file name. It should be specified as a formatter step for a generic

0 commit comments

Comments
 (0)