Skip to content

Commit a333813

Browse files
committed
Add support for biome, diffplug#1804
1 parent eb5dc61 commit a333813

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2114
-576
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.diffplug.spotless.rome;
2+
3+
/**
4+
* The flavor of Biome to use. Exists for compatibility reason, may be removed
5+
* shortly.
6+
* <p>
7+
* Will be removed once the old Rome project is not supported anymore.
8+
*/
9+
public enum EBiomeFlavor {
10+
/** The new forked Biome project. */
11+
BIOME("biome", "1.2.0", "biome.json", "biome-%s-%s-%s",
12+
"https://github.com/biomejs/biome/releases/download/cli%%2Fv%s/biome-%s"),
13+
14+
/**
15+
* The old deprecated Rome project.
16+
*
17+
* @deprecated Will be removed once the old Rome project is not supported
18+
* anymore.
19+
*/
20+
@Deprecated
21+
ROME("rome", "12.0.0", "rome.json", "rome-%s-%s-%s",
22+
"https://github.com/rome/tools/releases/download/cli%%2Fv%s/rome-%s");
23+
24+
private final String configName;
25+
private final String defaultVersion;
26+
private final String downloadFilePattern;
27+
private final String shortName;
28+
private final String urlPattern;
29+
30+
EBiomeFlavor(String shortName, String defaultVersion, String configName, String downloadFilePattern,
31+
String urlPattern) {
32+
this.shortName = shortName;
33+
this.defaultVersion = defaultVersion;
34+
this.configName = configName;
35+
this.downloadFilePattern = downloadFilePattern;
36+
this.urlPattern = urlPattern;
37+
}
38+
39+
/**
40+
* @return The name of the default config file.
41+
*/
42+
public String configName() {
43+
return configName;
44+
}
45+
46+
/**
47+
* @return Default version to use when no version was set explicitly.
48+
*/
49+
public String defaultVersion() {
50+
return defaultVersion;
51+
}
52+
53+
/**
54+
* @return The pattern for {@link String#format(String, Object...)
55+
* String.format()} for the file name of a Biome executable for a
56+
* certain version and architecure. The first parameter is the platform,
57+
* the second is the OS, the third is the architecture.
58+
*/
59+
public String getDownloadFilePattern() {
60+
return downloadFilePattern;
61+
}
62+
63+
/**
64+
* @return The pattern for {@link String#format(String, Object...)
65+
* String.format()} for the URL where the executables can be downloaded.
66+
* The first parameter is the version, the second parameter is the OS /
67+
* platform.
68+
*/
69+
public String getUrlPattern() {
70+
return urlPattern;
71+
}
72+
73+
/**
74+
* @return The short name of this flavor, i.e. <code>rome</code> or
75+
* <code>biome</code>.
76+
*/
77+
public String shortName() {
78+
return shortName;
79+
}
80+
}

lib/src/main/java/com/diffplug/spotless/rome/RomeExecutableDownloader.java

+45-56
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
import org.slf4j.LoggerFactory;
3939

4040
/**
41-
* Downloader for the Rome executable:
42-
* <a href="https://github.com/rome/tools">https://github.com/rome/tools</a>.
41+
* Downloader for the Biome executable:
42+
* <a href="https://github.com/biomejs/biome">https://github.com/biomejs/biome</a>.
4343
*/
4444
final class RomeExecutableDownloader {
4545
private static final Logger logger = LoggerFactory.getLogger(RomeExecutableDownloader.class);
@@ -51,15 +51,7 @@ final class RomeExecutableDownloader {
5151

5252
/**
5353
* The pattern for {@link String#format(String, Object...) String.format()} for
54-
* the file name of a Rome executable for a certain version and architecure. The
55-
* first parameter is the platform, the second is the OS, the third is the
56-
* architecture.
57-
*/
58-
private static final String DOWNLOAD_FILE_PATTERN = "rome-%s-%s-%s";
59-
60-
/**
61-
* The pattern for {@link String#format(String, Object...) String.format()} for
62-
* the platform part of the Rome executable download URL. First parameter is the
54+
* the platform part of the Biome executable download URL. First parameter is the
6355
* OS, second parameter the architecture, the third the file extension.
6456
*/
6557
private static final String PLATFORM_PATTERN = "%s-%s%s";
@@ -70,39 +62,36 @@ final class RomeExecutableDownloader {
7062
*/
7163
private static final OpenOption[] READ_OPTIONS = {StandardOpenOption.READ};
7264

73-
/**
74-
* The pattern for {@link String#format(String, Object...) String.format()} for
75-
* the URL where the Rome executables can be downloaded. The first parameter is
76-
* the version, the second parameter is the OS / platform.
77-
*/
78-
private static final String URL_PATTERN = "https://github.com/rome/tools/releases/download/cli%%2Fv%s/rome-%s";
79-
8065
/**
8166
* {@link OpenOption Open options} for creating a new file, overwriting the
8267
* existing file if present.
8368
*/
8469
private static final OpenOption[] WRITE_OPTIONS = {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING,
8570
StandardOpenOption.WRITE};
8671

87-
private Path downloadDir;
72+
private final Path downloadDir;
73+
74+
private final EBiomeFlavor flavor;
8875

8976
/**
90-
* Creates a new downloader for the Rome executable. The executable files are
77+
* Creates a new downloader for the Biome executable. The executable files are
9178
* stored in the given download directory.
9279
*
93-
* @param downloadDir Directory where
80+
* @param flavor Flavor of Biome to use.
81+
* @param downloadDir Directory where to store the downloaded executable.
9482
*/
95-
public RomeExecutableDownloader(Path downloadDir) {
83+
public RomeExecutableDownloader(EBiomeFlavor flavor, Path downloadDir) {
84+
this.flavor = flavor;
9685
this.downloadDir = downloadDir;
9786
}
9887

9988
/**
100-
* Downloads the Rome executable for the current platform from the network to
89+
* Downloads the Biome executable for the current platform from the network to
10190
* the download directory. When the executable exists already, it is
10291
* overwritten.
10392
*
104-
* @param version Desired Rome version.
105-
* @return The path to the Rome executable.
93+
* @param version Desired Biome version.
94+
* @return The path to the Biome executable.
10695
* @throws IOException When the executable cannot be downloaded from
10796
* the network or the file system could not be
10897
* accessed.
@@ -121,7 +110,7 @@ public Path download(String version) throws IOException, InterruptedException {
121110
if (executableDir != null) {
122111
Files.createDirectories(executableDir);
123112
}
124-
logger.info("Attempting to download Rome from '{}' to '{}'", url, executablePath);
113+
logger.info("Attempting to download Biome from '{}' to '{}'", url, executablePath);
125114
var request = HttpRequest.newBuilder(URI.create(url)).GET().build();
126115
var handler = BodyHandlers.ofFile(executablePath, WRITE_OPTIONS);
127116
var response = HttpClient.newBuilder().followRedirects(Redirect.NORMAL).build().send(request, handler);
@@ -133,19 +122,19 @@ public Path download(String version) throws IOException, InterruptedException {
133122
throw new IOException("Failed to download file from " + url + ", file is empty or does not exist");
134123
}
135124
writeChecksumFile(downloadedFile, checksumPath);
136-
logger.debug("Rome was downloaded successfully to '{}'", downloadedFile);
125+
logger.debug("Biome was downloaded successfully to '{}'", downloadedFile);
137126
return downloadedFile;
138127
}
139128

140129
/**
141-
* Ensures that the Rome executable for the current platform exists in the
130+
* Ensures that the Biome executable for the current platform exists in the
142131
* download directory. When the executable does not exist in the download
143-
* directory, an attempt is made to download the Rome executable from the
132+
* directory, an attempt is made to download the Biome executable from the
144133
* network. When the executable exists already, no attempt to download it again
145134
* is made.
146135
*
147-
* @param version Desired Rome version.
148-
* @return The path to the Rome executable.
136+
* @param version Desired Biome version.
137+
* @return The path to the Biome executable.
149138
* @throws IOException When the executable cannot be downloaded from
150139
* the network or the file system could not be
151140
* accessed.
@@ -157,23 +146,23 @@ public Path download(String version) throws IOException, InterruptedException {
157146
*/
158147
public Path ensureDownloaded(String version) throws IOException, InterruptedException {
159148
var platform = Platform.guess();
160-
logger.debug("Ensuring that Rome for platform '{}' is downloaded", platform);
149+
logger.debug("Ensuring that Biome for platform '{}' is downloaded", platform);
161150
var existing = findDownloaded(version);
162151
if (existing.isPresent()) {
163-
logger.debug("Rome was already downloaded, using executable at '{}'", existing.get());
152+
logger.debug("Biome was already downloaded, using executable at '{}'", existing.get());
164153
return existing.get();
165154
} else {
166-
logger.debug("Rome was not yet downloaded, attempting to download executable");
155+
logger.debug("Biome was not yet downloaded, attempting to download executable");
167156
return download(version);
168157
}
169158
}
170159

171160
/**
172-
* Attempts to find the Rome executable for the current platform in the download
161+
* Attempts to find the Biome executable for the current platform in the download
173162
* directory. No attempt is made to download the executable from the network.
174163
*
175-
* @param version Desired Rome version.
176-
* @return The path to the Rome executable.
164+
* @param version Desired Biome version.
165+
* @return The path to the Biome executable.
177166
* @throws IOException When the executable does not exists in the
178167
* download directory, or when the file system
179168
* could not be accessed.
@@ -184,7 +173,7 @@ public Path ensureDownloaded(String version) throws IOException, InterruptedExce
184173
public Optional<Path> findDownloaded(String version) throws IOException {
185174
var platform = Platform.guess();
186175
var executablePath = getExecutablePath(version, platform);
187-
logger.debug("Checking rome executable at {}", executablePath);
176+
logger.debug("Checking Biome executable at {}", executablePath);
188177
return checkFileWithChecksum(executablePath) ? Optional.ofNullable(executablePath) : Optional.empty();
189178
}
190179

@@ -248,12 +237,12 @@ private String computeChecksum(Path file, String algorithm) throws IOException {
248237
}
249238

250239
/**
251-
* Finds the code name for the given operating system used by the Rome
240+
* Finds the code name for the given operating system used by the Biome
252241
* executable download URL.
253242
*
254243
* @param os Desired operating system.
255-
* @return Code name for the Rome download URL.
256-
* @throws IOException When the given OS is not supported by Rome.
244+
* @return Code name for the Biome download URL.
245+
* @throws IOException When the given OS is not supported by Biome.
257246
*/
258247
private String getArchitectureCodeName(Architecture architecture) throws IOException {
259248
switch (architecture) {
@@ -281,28 +270,28 @@ private Path getChecksumPath(Path file) {
281270
}
282271

283272
/**
284-
* Finds the URL from which the Rome executable can be downloaded.
273+
* Finds the URL from which the Biome executable can be downloaded.
285274
*
286-
* @param version Desired Rome version.
275+
* @param version Desired Biome version.
287276
* @param platform Desired platform.
288-
* @return The URL for the Rome executable.
289-
* @throws IOException When the platform is not supported by Rome.
277+
* @return The URL for the Biome executable.
278+
* @throws IOException When the platform is not supported by Biome.
290279
*/
291280
private String getDownloadUrl(String version, Platform platform) throws IOException {
292281
var osCodeName = getOsCodeName(platform.getOs());
293282
var architectureCodeName = getArchitectureCodeName(platform.getArchitecture());
294283
var extension = getDownloadUrlExtension(platform.getOs());
295284
var platformString = String.format(PLATFORM_PATTERN, osCodeName, architectureCodeName, extension);
296-
return String.format(URL_PATTERN, version, platformString);
285+
return String.format(flavor.getUrlPattern(), version, platformString);
297286
}
298287

299288
/**
300-
* Finds the file extension of the Rome download URL for the given operating
289+
* Finds the file extension of the Biome download URL for the given operating
301290
* system.
302291
*
303292
* @param os Desired operating system.
304-
* @return Extension for the Rome download URL.
305-
* @throws IOException When the given OS is not supported by Rome.
293+
* @return Extension for the Biome download URL.
294+
* @throws IOException When the given OS is not supported by Biome.
306295
*/
307296
private String getDownloadUrlExtension(OS os) throws IOException {
308297
switch (os) {
@@ -318,27 +307,27 @@ private String getDownloadUrlExtension(OS os) throws IOException {
318307
}
319308

320309
/**
321-
* Finds the path on the file system for the Rome executable with a given
310+
* Finds the path on the file system for the Biome executable with a given
322311
* version and platform.
323312
*
324-
* @param version Desired Rome version.
313+
* @param version Desired Biome version.
325314
* @param platform Desired platform.
326-
* @return The path for the Rome executable.
315+
* @return The path for the Biome executable.
327316
*/
328317
private Path getExecutablePath(String version, Platform platform) {
329318
var os = platform.getOs().name().toLowerCase(Locale.ROOT);
330319
var arch = platform.getArchitecture().name().toLowerCase(Locale.ROOT);
331-
var fileName = String.format(DOWNLOAD_FILE_PATTERN, os, arch, version);
320+
var fileName = String.format(flavor.getDownloadFilePattern(), os, arch, version);
332321
return downloadDir.resolve(fileName);
333322
}
334323

335324
/**
336-
* Finds the code name for the given operating system used by the Rome
325+
* Finds the code name for the given operating system used by the Biome
337326
* executable download URL.
338327
*
339328
* @param os Desired operating system.
340-
* @return Code name for the Rome download URL.
341-
* @throws IOException When the given OS is not supported by Rome.
329+
* @return Code name for the Biome download URL.
330+
* @throws IOException When the given OS is not supported by Biome.
342331
*/
343332
private String getOsCodeName(OS os) throws IOException {
344333
switch (os) {

0 commit comments

Comments
 (0)