Skip to content

Commit 549e33b

Browse files
committed
fix: work around freshmark issues
dynamically created files that are not available at configuration time proof to be a challenge.
1 parent 3b36730 commit 549e33b

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

build-logic/src/main/groovy/buildlogic.spotless-markdown-conventions.gradle

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import com.diffplug.spotless.cli.picocli.usage.DocumentedUsages
2+
13
plugins {
24
id 'buildlogic.spotless-common-conventions'
35
}
@@ -40,18 +42,35 @@ class FreshmarkPropertiesAction implements Action<Map<String, Object>> {
4042
return versionsCollected
4143
}
4244

43-
Map<String, String> getUsageHelps() {
44-
Map<String, String> usageHelps = [:]
45+
Map<String, Object> getUsageHelps() {
46+
Map<String, Object> usageHelps = [:]
4547
File outputDir = rootProject.file("app/build/generated-usages")
46-
File[] usageFiles = outputDir.listFiles()
4748

48-
usageFiles.each { usageFile ->
49-
String usage = asJsArrayOfLines(usageFile.readLines())
50-
String name = usageFile.name.substring(0, usageFile.name.indexOf('.'))
51-
usageHelps.put("usage.${name}.array".toString(), usage)
49+
DocumentedUsages.values().each { usage ->
50+
File usageFile = new File(outputDir, usage.fileName)
51+
usageHelps.put("usage.${usage.formatterStepName}.array".toString(), new UsageHelp(usageFile))
5252
}
5353
return usageHelps
5454
}
55+
}
56+
57+
58+
class UsageHelp implements Serializable {
59+
60+
private final File usageFile
61+
62+
UsageHelp(File usageFile) {
63+
this.usageFile = usageFile
64+
}
65+
66+
@Override
67+
String toString() {
68+
if (usageFile.canRead()) {
69+
return asJsArrayOfLines(usageFile.readLines())
70+
}
71+
return '[]'
72+
}
73+
5574

5675
private static String asJsArrayOfLines(List<String> lines) {
5776
return '[' +
@@ -67,4 +86,5 @@ class FreshmarkPropertiesAction implements Action<Map<String, Object>> {
6786

6887
tasks.named('spotlessFreshmark').configure {
6988
dependsOn(':app:generateUsage') // to make sure usage files are generated before spotlessFreshmark
89+
inputs.files(rootProject.project(':app').tasks.named('generateUsage').get().inputs.files)
7090
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.diffplug.spotless.cli.picocli.usage
2+
3+
enum DocumentedUsages {
4+
5+
MAIN(""),
6+
GOOGLE_JAVA_FORMAT(),
7+
LICENSE_HEADER(),
8+
PRETTIER()
9+
10+
private final String fileName
11+
12+
private final String formatterStepName
13+
14+
DocumentedUsages(){
15+
this(null)
16+
}
17+
18+
DocumentedUsages(String formatterStepName){
19+
this.formatterStepName = formatterStepName ?: name().toLowerCase().replace('_', '-')
20+
this.fileName = "${name().toLowerCase().replace('_', '-')}.spotless.usage.txt"
21+
}
22+
23+
String getFileName() {
24+
return fileName
25+
}
26+
27+
String getFormatterStepName() {
28+
return formatterStepName
29+
}
30+
31+
static List<String> getFormatterNames() {
32+
return values().collect { it.formatterStepName }
33+
}
34+
35+
static List<String> getFileNames() {
36+
return values().collect { it.fileName }
37+
}
38+
}

build-logic/src/main/groovy/com/diffplug/spotless/cli/picocli/usage/GenerateUsagePropertiesTask.groovy

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ abstract class GenerateUsagePropertiesTask extends DefaultTask {
4747
// remove all files from outputDir if any
4848
outputDir.get().deleteDir()
4949

50-
String mainUsage = getUsage("")
51-
writeUsageFile(mainUsage, "main")
52-
53-
List<String> formatterNames = findFormatterNamesFromClasspath()
50+
List<String> formatterNames = DocumentedUsages.formatterNames
5451
Map<String, String> formatterUsages = [:]
5552
formatterNames.each { formatterName ->
5653
formatterUsages.put(formatterName, getUsage(formatterName))
@@ -61,17 +58,6 @@ abstract class GenerateUsagePropertiesTask extends DefaultTask {
6158
}
6259
}
6360

64-
List<String> findFormatterNamesFromClasspath() {
65-
ClasspathScanner scanner = new ClasspathScanner(runtimeClasspath.get(), { String clazzName -> clazzName.contains("com.diffplug.spotless.cli")})
66-
CommandInfoReader commandInfoReader = new CommandInfoReader(scanner.withoutClassFilter())
67-
List<Class<?>> formatterClasses = scanner.findInstancesOf("com.diffplug.spotless.cli.steps.SpotlessCLIFormatterStep")
68-
List<String> formatterNames = []
69-
for (Class<?> formatterClass : formatterClasses) {
70-
formatterNames << commandInfoReader.readCommandName(formatterClass)
71-
}
72-
return formatterNames
73-
}
74-
7561
String getUsage(String formatterName) {
7662
ByteArrayOutputStream output = new ByteArrayOutputStream()
7763
ExecResult result = execOperations.javaexec {

0 commit comments

Comments
 (0)