Skip to content

Commit a2bbe6c

Browse files
authored
SONARGO-71 Extend "code-style-convention" to optionally support Go, add to common Gradle modules (#7)
1 parent 586c643 commit a2bbe6c

5 files changed

Lines changed: 222 additions & 1 deletion

File tree

.editorconfig

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
root = true
4+
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
charset = utf-8
9+
indent_style = space
10+
11+
# 4 space indentation
12+
[*.{py,star}]
13+
indent_size = 4
14+
15+
[{*.kt,*.kts}]
16+
ij_continuation_indent_size = 4
17+
ij_kotlin_align_in_columns_case_branch = false
18+
ij_kotlin_align_multiline_binary_operation = false
19+
ij_kotlin_align_multiline_extends_list = false
20+
ij_kotlin_align_multiline_method_parentheses = false
21+
ij_kotlin_align_multiline_parameters = true
22+
ij_kotlin_align_multiline_parameters_in_calls = false
23+
ij_kotlin_allow_trailing_comma = true
24+
ij_kotlin_allow_trailing_comma_on_call_site = false
25+
ij_kotlin_assignment_wrap = normal
26+
ij_kotlin_blank_lines_after_class_header = 0
27+
ij_kotlin_blank_lines_around_block_when_branches = 0
28+
ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_line = 1
29+
ij_kotlin_block_comment_add_space = false
30+
ij_kotlin_block_comment_at_first_column = true
31+
ij_kotlin_call_parameters_new_line_after_left_paren = true
32+
ij_kotlin_call_parameters_right_paren_on_new_line = true
33+
ij_kotlin_call_parameters_wrap = on_every_item
34+
ij_kotlin_catch_on_new_line = false
35+
ij_kotlin_class_annotation_wrap = split_into_lines
36+
ij_kotlin_continuation_indent_for_chained_calls = false
37+
ij_kotlin_continuation_indent_for_expression_bodies = false
38+
ij_kotlin_continuation_indent_in_argument_lists = false
39+
ij_kotlin_continuation_indent_in_elvis = false
40+
ij_kotlin_continuation_indent_in_if_conditions = false
41+
ij_kotlin_continuation_indent_in_parameter_lists = false
42+
ij_kotlin_continuation_indent_in_supertype_lists = false
43+
ij_kotlin_else_on_new_line = false
44+
ij_kotlin_enum_constants_wrap = off
45+
ij_kotlin_extends_list_wrap = normal
46+
ij_kotlin_field_annotation_wrap = split_into_lines
47+
ij_kotlin_finally_on_new_line = false
48+
ij_kotlin_if_rparen_on_new_line = true
49+
ij_kotlin_import_nested_classes = false
50+
ij_kotlin_imports_layout = *
51+
ij_kotlin_insert_whitespaces_in_simple_one_line_method = true
52+
ij_kotlin_keep_blank_lines_before_right_brace = 2
53+
ij_kotlin_keep_blank_lines_in_code = 2
54+
ij_kotlin_keep_blank_lines_in_declarations = 2
55+
ij_kotlin_keep_first_column_comment = true
56+
ij_kotlin_keep_indents_on_empty_lines = false
57+
ij_kotlin_keep_line_breaks = true
58+
ij_kotlin_lbrace_on_next_line = false
59+
ij_kotlin_line_break_after_multiline_when_entry = true
60+
ij_kotlin_line_comment_add_space = false
61+
ij_kotlin_line_comment_add_space_on_reformat = false
62+
ij_kotlin_line_comment_at_first_column = true
63+
ij_kotlin_method_annotation_wrap = split_into_lines
64+
ij_kotlin_method_call_chain_wrap = normal
65+
ij_kotlin_method_parameters_new_line_after_left_paren = true
66+
ij_kotlin_method_parameters_right_paren_on_new_line = true
67+
ij_kotlin_method_parameters_wrap = on_every_item
68+
ij_kotlin_name_count_to_use_star_import = 2147483647
69+
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
70+
ij_kotlin_parameter_annotation_wrap = off
71+
ij_kotlin_space_after_comma = true
72+
ij_kotlin_space_after_extend_colon = true
73+
ij_kotlin_space_after_type_colon = true
74+
ij_kotlin_space_before_catch_parentheses = true
75+
ij_kotlin_space_before_comma = false
76+
ij_kotlin_space_before_extend_colon = true
77+
ij_kotlin_space_before_for_parentheses = true
78+
ij_kotlin_space_before_if_parentheses = true
79+
ij_kotlin_space_before_lambda_arrow = true
80+
ij_kotlin_space_before_type_colon = false
81+
ij_kotlin_space_before_when_parentheses = true
82+
ij_kotlin_space_before_while_parentheses = true
83+
ij_kotlin_spaces_around_additive_operators = true
84+
ij_kotlin_spaces_around_assignment_operators = true
85+
ij_kotlin_spaces_around_equality_operators = true
86+
ij_kotlin_spaces_around_function_type_arrow = true
87+
ij_kotlin_spaces_around_logical_operators = true
88+
ij_kotlin_spaces_around_multiplicative_operators = true
89+
ij_kotlin_spaces_around_range = false
90+
ij_kotlin_spaces_around_relational_operators = true
91+
ij_kotlin_spaces_around_unary_operator = false
92+
ij_kotlin_spaces_around_when_arrow = true
93+
ij_kotlin_variable_annotation_wrap = off
94+
ij_kotlin_while_on_new_line = false
95+
ij_kotlin_wrap_elvis_expressions = 1
96+
ij_kotlin_wrap_expression_body_functions = 1
97+
ij_kotlin_wrap_first_method_in_call_chain = false
98+
# Don't insert a new line after assignments with a block expression; see https://github.com/pinterest/ktlint/issues/2464
99+
ktlint_standard_multiline-expression-wrapping = disabled
100+
ktlint_standard_string-template-indent = disabled
101+
max_line_length = 140

gradle-modules/build.gradle.kts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@ plugins {
1919
alias(libs.plugins.spotless)
2020
}
2121

22-
val kotlinGradleDelimiter = "(package|import|plugins|pluginManagement|dependencyResolutionManagement|repositories) "
22+
dependencies {
23+
implementation(libs.diffplug.spotless)
24+
implementation(libs.diffplug.blowdryer)
25+
}
26+
2327
spotless {
28+
kotlin {
29+
ktlint().setEditorConfigPath("$rootDir/.editorconfig")
30+
targetExclude("build/**")
31+
licenseHeaderFile(rootProject.file("LICENSE_HEADER")).updateYearWithLatest(true)
32+
}
33+
34+
val kotlinGradleDelimiter = "(package|import|plugins|pluginManagement|dependencyResolutionManagement|repositories) "
2435
kotlinGradle {
36+
ktlint().setEditorConfigPath("$rootDir/.editorconfig")
2537
target("*.gradle.kts", "src/**/*.gradle.kts")
2638
licenseHeaderFile(rootProject.file("LICENSE_HEADER"), kotlinGradleDelimiter).updateYearWithLatest(true)
2739
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* SonarSource Cloud Native Gradle Modules
3+
* Copyright (C) 2024-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
import com.diffplug.blowdryer.Blowdryer
18+
import org.sonarsource.cloudnative.gradle.CodeStyleConvention
19+
20+
plugins {
21+
id("com.diffplug.spotless")
22+
}
23+
24+
val codeStyleConvention = extensions.create<CodeStyleConvention>("codeStyleConvention")
25+
codeStyleConvention.editorConfigPath.convention("$rootDir/build-logic/.editorconfig")
26+
27+
val kotlinGradleDelimiter = "(package|import|plugins|pluginManagement|dependencyResolutionManagement|repositories) "
28+
val licenseHeaderFileName = if (project.path.startsWith(":private:")) "private/LICENSE_HEADER_PRIVATE" else "LICENSE_HEADER"
29+
val licenseHeaderFile = rootProject.file(licenseHeaderFileName)
30+
31+
spotless {
32+
encoding(Charsets.UTF_8)
33+
java {
34+
importOrderFile(
35+
Blowdryer.immutableUrl(
36+
"https://raw.githubusercontent.com/SonarSource/sonar-developer-toolset/refs/heads/master/eclipse/sonar.importorder"
37+
)
38+
)
39+
removeUnusedImports()
40+
// point to immutable specific commit of sonar-formater.xml version 23
41+
eclipse("4.22")
42+
.withP2Mirrors(
43+
mapOf(
44+
"https://download.eclipse.org/eclipse/" to "https://ftp.fau.de/eclipse/eclipse/"
45+
)
46+
)
47+
.configFile(
48+
Blowdryer.immutableUrl(
49+
"https://raw.githubusercontent.com/SonarSource/sonar-developer-toolset/" +
50+
"540ef32ba22c301f6d05a5305f4e1dbd204839f3/eclipse/sonar-formatter.xml"
51+
)
52+
)
53+
licenseHeaderFile(licenseHeaderFile).updateYearWithLatest(true)
54+
targetExclude("*/generated-sources/**", "*/generated-src/**", "*/generated/sources/**")
55+
toggleOffOn()
56+
}
57+
kotlinGradle {
58+
licenseHeaderFile(licenseHeaderFile, kotlinGradleDelimiter).updateYearWithLatest(true)
59+
}
60+
format("javaMisc") {
61+
target("src/**/package-info.java")
62+
licenseHeaderFile(licenseHeaderFile, "@javax.annotation").updateYearWithLatest(true)
63+
}
64+
65+
// Apply additional project-specific configuration after the project has been evaluated and the `codeStyleConvention` extension has been initialized
66+
project.afterEvaluate {
67+
spotless {
68+
kotlinGradle {
69+
ktlint().setEditorConfigPath(codeStyleConvention.editorConfigPath)
70+
}
71+
}
72+
codeStyleConvention.spotless?.invoke(this@spotless)
73+
}
74+
}
75+
76+
tasks.check { dependsOn("spotlessCheck") }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* SonarSource Cloud Native Gradle Modules
3+
* Copyright (C) 2024-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonarsource.cloudnative.gradle
18+
19+
import com.diffplug.gradle.spotless.SpotlessExtension
20+
import org.gradle.api.provider.Property
21+
22+
interface CodeStyleConvention {
23+
val editorConfigPath: Property<String>
24+
25+
var spotless: (SpotlessExtension.() -> Unit)?
26+
27+
fun spotless(action: SpotlessExtension.() -> Unit) {
28+
spotless = action
29+
}
30+
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
[versions]
22
spotless-gradle = "6.25.0"
3+
blowdryer-gradle = "1.7.1"
34

45
[libraries]
56
diffplug-spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless-gradle" }
7+
diffplug-blowdryer = { module = "com.diffplug:blowdryer", version.ref = "blowdryer-gradle" }
68

79
[plugins]
810
spotless = { id = "com.diffplug.spotless", version.ref = "spotless-gradle" }

0 commit comments

Comments
 (0)