1
1
package org .hypertrace .gradle .code .style ;
2
2
3
+ import build .buf .gradle .BufExtension ;
4
+ import build .buf .gradle .BufPlugin ;
5
+ import build .buf .gradle .BufSupportKt ;
3
6
import com .diffplug .gradle .spotless .SpotlessExtension ;
4
7
import com .diffplug .gradle .spotless .SpotlessPlugin ;
5
-
8
+ import java . io . File ;
6
9
import java .io .IOException ;
7
- import java .util .HashMap ;
10
+ import java .util .Map ;
8
11
import javax .annotation .Nonnull ;
9
12
import org .gradle .api .Plugin ;
10
13
import org .gradle .api .Project ;
@@ -20,13 +23,14 @@ public void apply(@Nonnull Project target) {
20
23
private void configureCodeStyle (Project project ) {
21
24
PluginContainer pluginContainer = project .getPlugins ();
22
25
pluginContainer .apply (SpotlessPlugin .class );
26
+ pluginContainer .apply (BufPlugin .class );
27
+ configureFormatting (project );
28
+ }
23
29
30
+ private void configureFormatting (Project project ) {
24
31
SpotlessExtension spotlessExtension =
25
32
project .getExtensions ().getByType (SpotlessExtension .class );
26
- configureFormatting (spotlessExtension );
27
- }
28
33
29
- private void configureFormatting (SpotlessExtension spotlessExtension ) {
30
34
spotlessExtension .java (
31
35
format -> {
32
36
format .importOrder ();
@@ -36,26 +40,34 @@ private void configureFormatting(SpotlessExtension spotlessExtension) {
36
40
});
37
41
38
42
spotlessExtension .kotlinGradle (
39
- format ->
40
- {
41
- try {
42
- format
43
- .ktlint ("0.50.0" )
44
- .editorConfigOverride (
45
- new HashMap <String , Object >() {
46
- {
47
- put ("indent_size" , "2" );
48
- }
49
- });
50
- } catch (IOException e ) {
51
- throw new RuntimeException (e );
52
- }
43
+ format -> {
44
+ try {
45
+ format .ktlint ("0.50.0" ).editorConfigOverride (Map .of ("indent_size" , "2" ));
46
+ } catch (IOException e ) {
47
+ throw new RuntimeException (e );
48
+ }
53
49
});
50
+
51
+ BufExtension bufExtension = project .getExtensions ().getByType (BufExtension .class );
52
+ spotlessExtension .protobuf (
53
+ format -> {
54
+ File bufBinary =
55
+ project
56
+ .getConfigurations ()
57
+ .getByName (BufSupportKt .BUF_BINARY_CONFIGURATION_NAME )
58
+ .getSingleFile ();
59
+ if (!bufBinary .canExecute ()) {
60
+ bufBinary .setExecutable (true );
61
+ }
62
+ format .buf (bufExtension .getToolVersion ()).pathToExe (bufBinary .getAbsolutePath ());
63
+ });
64
+ bufExtension .setEnforceFormat (false );
65
+
54
66
spotlessExtension .format (
55
67
"misc" ,
56
68
format -> {
57
- format .target ("*.md" , "src/**/*.proto" , " .gitignore" , "*.yaml" );
58
- format .indentWithSpaces (2 );
69
+ format .target ("*.md" , ".gitignore" , "*.yaml" );
70
+ format .leadingTabsToSpaces (2 );
59
71
format .trimTrailingWhitespace ();
60
72
format .endWithNewline ();
61
73
});
0 commit comments