Skip to content

Commit 2f1ef25

Browse files
authored
Add checkstyle to build logic (#151)
* Add checkstyle toolchain for `magiclib-core` * Reformat code for `magiclib-core` * Use unix line endings on all files * Add lambda formatting rules to .editorconfig * Redetermine import order rules * Add checkstyle toolchain for `magiclib-better-dev` * Reformat code for `magiclib-better-dev` * Add checkstyle toolchain for `magiclib-minecraft-api` * Reformat code for `magiclib-minecraft-api` * Add checkstyle toolchain for `magiclib-malilib-extra` * Reformat code for `magiclib-malilib-extra` * Setup GitHub Action for checkstyle * Check cache before building * Fix... * Rename job name * lookup-only * Remove indent_size from .editorconfig * Make cache read only * Add missing job id --------- Signed-off-by: Hendrix-Shen <[email protected]>
1 parent 7a0aaa3 commit 2f1ef25

File tree

358 files changed

+2872
-1092
lines changed

Some content is hidden

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

358 files changed

+2872
-1092
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_style = space
7+
insert_final_newline = true
8+
tab_width = 4
9+
10+
[*.java]
11+
ij_continuation_indent_size = 8
12+
ij_java_class_count_to_use_import_on_demand = 999
13+
ij_java_imports_layout = *,|,com.mojang.**,net.minecraft.**,|,cpw.mods.**,net.fabricmc.**,net.minecraftforge.**,net.neoforged.**,|,com.llamalad7.mixinextras.**,org.spongepowered.**,|,top.hendrixshen.magiclib.**,|,java.**,javax.**,|,$*
14+
ij_java_replace_instanceof_and_cast = true
15+
ij_java_replace_null_check = true
16+
ij_java_replace_sum_lambda_with_method_ref = true

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Auto detect text files and perform LF normalization
2-
* text=auto
2+
* text=auto eol=lf
3+
*.bat text eol=crlf

.github/workflows/CI.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,39 @@ jobs:
1717
prepare_action_info:
1818
if: ${{ !startsWith(github.event.ref, 'refs/tags/') }}
1919
uses: ./.github/workflows/prepare_action_info.yml
20+
cache:
21+
if: ${{ !contains(github.event.head_commit.message, '[build skip]') }}
22+
uses: ./.github/workflows/cache.yml
23+
secrets: inherit
24+
needs:
25+
- prepare_action_info
26+
with:
27+
build_version_type: ${{ needs.prepare_action_info.outputs.build_version_type }}
2028
build:
2129
if: ${{ !contains(github.event.head_commit.message, '[build skip]') }}
2230
uses: ./.github/workflows/build.yml
2331
secrets: inherit
2432
needs:
33+
- cache
2534
- prepare_action_info
2635
with:
2736
build_publish: ${{ fromJSON(needs.prepare_action_info.outputs.build_publish) }}
2837
build_version_type: ${{ needs.prepare_action_info.outputs.build_version_type }}
38+
checkstyle:
39+
if: ${{ !contains(github.event.head_commit.message, '[build skip]') }}
40+
uses: ./.github/workflows/checkstyle.yml
41+
secrets: inherit
42+
needs:
43+
- cache
44+
- prepare_action_info
45+
with:
46+
build_version_type: ${{ needs.prepare_action_info.outputs.build_version_type }}
2947
publish:
3048
if: ${{ fromJSON(needs.prepare_action_info.outputs.publish_enable) }}
3149
uses: ./.github/workflows/publish.yml
3250
secrets: inherit
3351
needs:
52+
- checkstyle
3453
- build
3554
- prepare_action_info
3655
with:

.github/workflows/build.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ jobs:
2626
- name: Validate gradle wrapper
2727
uses: gradle/actions/wrapper-validation@v4
2828
- name: Cache gradle files
29-
uses: actions/cache@v4
29+
uses: actions/cache/restore@v4
3030
with:
3131
path: |
3232
~/.gradle/caches
3333
~/.gradle/wrapper
3434
./.gradle/loom-cache
3535
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle.properties', '**/*.accesswidener', 'settings.json') }}
36-
restore-keys: |
37-
${{ runner.os }}-gradle-
3836
# TODO: Remove in next major version
3937
- name: Preprocess Resources
4038
run: |

.github/workflows/cache.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: _step.cache
2+
on:
3+
workflow_call:
4+
inputs:
5+
build_version_type:
6+
type: string
7+
default: 'DEV'
8+
required: false
9+
jobs:
10+
cache:
11+
runs-on: ubuntu-24.04
12+
steps:
13+
- name: Checkout the sources
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
- name: Set up JDK 21
18+
uses: actions/setup-java@v4
19+
with:
20+
distribution: 'temurin'
21+
java-version: 21
22+
- name: Validate gradle wrapper
23+
uses: gradle/actions/wrapper-validation@v4
24+
- name: Cache gradle files
25+
uses: actions/cache@v4
26+
id: cache
27+
with:
28+
path: |
29+
~/.gradle/caches
30+
~/.gradle/wrapper
31+
./.gradle/loom-cache
32+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle.properties', '**/*.accesswidener', 'settings.json') }}
33+
restore-keys: |
34+
${{ runner.os }}-gradle-
35+
lookup-only: true
36+
- name: Init with gradle
37+
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
38+
run: |
39+
chmod +x gradlew
40+
./gradlew
41+
env:
42+
BUILD_TYPE: ${{ inputs.build_version_type }}

.github/workflows/checkstyle.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: _step.checkstyle
2+
on:
3+
workflow_call:
4+
inputs:
5+
build_version_type:
6+
type: string
7+
default: 'DEV'
8+
required: false
9+
jobs:
10+
checkstyle:
11+
runs-on: ubuntu-24.04
12+
steps:
13+
- name: Checkout the sources
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
- name: Set up JDK 21
18+
uses: actions/setup-java@v4
19+
with:
20+
distribution: 'temurin'
21+
java-version: 21
22+
- name: Validate gradle wrapper
23+
uses: gradle/actions/wrapper-validation@v4
24+
- name: Cache gradle files
25+
uses: actions/cache/restore@v4
26+
with:
27+
path: |
28+
~/.gradle/caches
29+
~/.gradle/wrapper
30+
./.gradle/loom-cache
31+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle.properties', '**/*.accesswidener', 'settings.json') }}
32+
# TODO: Remove in next major version
33+
- name: Preprocess Resources
34+
run: |
35+
chmod +x gradlew
36+
./gradlew preprocessResources
37+
env:
38+
BUILD_TYPE: ${{ inputs.build_version_type }}
39+
- name: Checkstyle with gradle
40+
run: |
41+
./gradlew checkstyleMain -x test --stacktrace
42+
env:
43+
BUILD_TYPE: ${{ inputs.build_version_type }}
44+
- name: Publish Checkstyle report
45+
uses: Juuxel/publish-checkstyle-report@v1
46+
if: ${{ failure() }}
47+
with:
48+
reports: |
49+
*/**/build/reports/checkstyle/*.xml

checkstyle-light.xml

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
3+
<module name="Checker">
4+
<property name="charset" value="UTF-8"/>
5+
<property name="fileExtensions" value="java"/>
6+
<property name="localeLanguage" value="en"/>
7+
<property name="localeCountry" value="US"/>
8+
<property name="tabWidth" value="4"/>
9+
10+
<module name="NewlineAtEndOfFile"/>
11+
12+
<!-- Enforce spaces -->
13+
<module name="FileTabCharacter">
14+
<property name="eachLine" value="true"/>
15+
</module>
16+
17+
<!-- disallow trailing whitespace -->
18+
<module name="RegexpSingleline">
19+
<property name="format" value="\s+$"/>
20+
<property name="message" value="trailing whitespace"/>
21+
</module>
22+
23+
<!-- note: RegexpMultiline shows nicer messages than Regexp, but has to be outside TreeWalker -->
24+
<!-- disallow multiple consecutive blank lines -->
25+
<module name="RegexpMultiline">
26+
<property name="format" value="\n[\t ]*\r?\n[\t ]*\r?\n"/>
27+
<property name="message" value="adjacent blank lines"/>
28+
</module>
29+
30+
<!-- disallow blank after { -->
31+
<module name="RegexpMultiline">
32+
<property name="format" value="\{[\t ]*\r?\n[\t ]*\r?\n"/>
33+
<property name="message" value="blank line after '{'"/>
34+
</module>
35+
36+
<!-- disallow blank before } -->
37+
<module name="RegexpMultiline">
38+
<property name="format" value="\n[\t ]*\r?\n[\t ]*\}"/>
39+
<property name="message" value="blank line before '}'"/>
40+
</module>
41+
42+
<!-- require blank before { in the same indentation level -->
43+
<module name="RegexpMultiline">
44+
<!-- the regex works as follows:
45+
It matches (=fails) for \n<indentation><something>\n<same indentation><control statement>[...]{\n
46+
while <something> is a single line comment, it'll look for a blank line one line earlier
47+
if <something> is a space, indicating a formatting error or ' */', it'll ignore the instance
48+
if <something> is a tab, indicating a continued line, it'll ignore the instance
49+
<control statement> is 'if', 'do', 'while', 'for', 'try' or nothing (instance initializer block)
50+
51+
- first \n: with positive lookbehind (?<=\n) to move the error marker to a more reasonable place
52+
- capture tabs for <indentation>, later referenced via \1
53+
- remaining preceding line as a non-comment (doesn't start with '/', '//', ' ' or '\t') or multiple lines where all but the first are a single line comment with the same indentation
54+
- new line
55+
- <indentation> as captured earlier
56+
- <control statement> as specified above
57+
- { before the next new line -->
58+
<property name="format" value="(?&lt;=\n)([\t]+)(?:[^/\r\n \t][^\r\n]*|/[^/\r\n][^\r\n]*|[^/\r\n][^\r\n]*(\r?\n\1//[^\r\n]*)+)\r?\n\1(|(if|do|while|for|try)[^\r\n]+)\{[\t ]*\r?\n"/>
59+
<property name="message" value="missing blank line before block at same indentation level"/>
60+
</module>
61+
62+
<!-- require blank after } in the same indentation level -->
63+
<module name="RegexpMultiline">
64+
<!-- \n<indentation>}\n<same indentation><whatever unless newline, '}' or starting with cas(e) or def(ault)> -->
65+
<property name="format" value="(?&lt;=\n)([\t]+)\}\r?\n\1(?:[^\r\n\}cd]|c[^\r\na]|ca[^\r\ns]|d[^\r\ne]|de[^\r\nf])"/>
66+
<property name="message" value="missing blank line after block at same indentation level"/>
67+
</module>
68+
69+
<module name="TreeWalker">
70+
<!-- Allow "//CHECKSTYLE.OFF: <InspectionName>" and "//CHECKSTYLE.ON: <InspectionName>" pairs to toggle some inspections -->
71+
<module name="SuppressionCommentFilter">
72+
<property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)"/>
73+
<property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)"/>
74+
<property name="checkFormat" value="$1"/>
75+
</module>
76+
77+
<!-- Ensure all imports are ship shape -->
78+
<module name="AvoidStarImport"/>
79+
<module name="IllegalImport"/>
80+
<module name="RedundantImport"/>
81+
<module name="UnusedImports"/>
82+
83+
<module name="ImportOrder">
84+
<!-- import order:
85+
// 1. other
86+
import *;
87+
// 2. Minecraft related
88+
import com.mojang.*;
89+
import net.minecraft.*;
90+
// 3. ModLoader related
91+
import cpw.mods.*;
92+
import net.fabricmc.*;
93+
import net.minecraftforge;
94+
import net.neoforged.*;
95+
// 4. Mixin related
96+
import com.llamalad7.mixinextras;
97+
import org.spongepowered.*
98+
// 5. magiclib related
99+
import top.hendrixshen.magiclib.*;
100+
// 6. java related
101+
import java.*;
102+
import javax.*; -->
103+
<property name="groups" value="*,/^(com\.mojang|net\.minecraft)\./,/^(cpw\.mods|net\.(fabricmc|minecraftforge|neoforged))\./,/^(com\.llamalad7\.mixinextras|org\.spongepowered)\./,top.hendrixshen.magiclib,/^javax?\./"/>
104+
<property name="ordered" value="true"/>
105+
<property name="separated" value="true"/>
106+
<property name="option" value="top"/>
107+
<property name="sortStaticImportsAlphabetically" value="true"/>
108+
</module>
109+
110+
<!-- Ensures braces are at the end of a line -->
111+
<module name="LeftCurly"/>
112+
<module name="RightCurly"/>
113+
114+
<!-- single line statements on one line, -->
115+
<module name="NeedBraces">
116+
<property name="tokens" value="LITERAL_IF,LITERAL_FOR,LITERAL_WHILE"/>
117+
<property name="allowSingleLineStatement" value="true"/>
118+
</module>
119+
<module name="NeedBraces">
120+
<property name="tokens" value="LITERAL_ELSE,LITERAL_DO"/>
121+
<property name="allowSingleLineStatement" value="false"/>
122+
</module>
123+
124+
<module name="EmptyLineSeparator">
125+
<property name="allowNoEmptyLineBetweenFields" value="true"/>
126+
<property name="allowMultipleEmptyLines" value="false"/>
127+
<!-- exclude METHOD_DEF and VARIABLE_DEF -->
128+
<property name="tokens" value="PACKAGE_DEF,IMPORT,STATIC_IMPORT,CLASS_DEF,INTERFACE_DEF,ENUM_DEF,STATIC_INIT,INSTANCE_INIT,CTOR_DEF"/>
129+
</module>
130+
131+
<module name="OperatorWrap"/>
132+
<module name="SeparatorWrap">
133+
<property name="tokens" value="DOT,ELLIPSIS,AT"/>
134+
<property name="option" value="nl"/>
135+
</module>
136+
<module name="SeparatorWrap">
137+
<property name="tokens" value="COMMA,SEMI"/>
138+
<property name="option" value="eol"/>
139+
</module>
140+
141+
<module name="Indentation">
142+
<property name="basicOffset" value="4"/>
143+
<property name="caseIndent" value="4"/>
144+
<property name="throwsIndent" value="4"/>
145+
<property name="arrayInitIndent" value="4"/>
146+
<property name="lineWrappingIndentation" value="8"/>
147+
</module>
148+
149+
<module name="ParenPad"/>
150+
<module name="NoWhitespaceBefore"/>
151+
<module name="NoWhitespaceAfter">
152+
<!-- allow ARRAY_INIT -->
153+
<property name="tokens" value="AT,INC,DEC,UNARY_MINUS,UNARY_PLUS,BNOT,LNOT,DOT,ARRAY_DECLARATOR,INDEX_OP"/>
154+
</module>
155+
<module name="WhitespaceAfter"/>
156+
<module name="WhitespaceAround">
157+
<!-- Allow PLUS, MINUS, MUL, DIV as they may be more readable without spaces in some cases -->
158+
<property name="tokens" value="ASSIGN,BAND,BAND_ASSIGN,BOR,BOR_ASSIGN,BSR,BSR_ASSIGN,BXOR,BXOR_ASSIGN,COLON,DIV_ASSIGN,DO_WHILE,EQUAL,GE,GT,LAMBDA,LAND,LCURLY,LE,LITERAL_CATCH,LITERAL_DO,LITERAL_ELSE,LITERAL_FINALLY,LITERAL_FOR,LITERAL_IF,LITERAL_RETURN,LITERAL_SWITCH,LITERAL_SYNCHRONIZED,LITERAL_TRY,LITERAL_WHILE,LOR,LT,MINUS_ASSIGN,MOD,MOD_ASSIGN,NOT_EQUAL,PLUS_ASSIGN,QUESTION,RCURLY,SL,SLIST,SL_ASSIGN,SR,SR_ASSIGN,STAR,STAR_ASSIGN,LITERAL_ASSERT,TYPE_EXTENSION_AND"/>
159+
</module>
160+
<module name="SingleSpaceSeparator"/>
161+
<module name="GenericWhitespace"/>
162+
163+
<module name="ArrayTypeStyle"/>
164+
<module name="DefaultComesLast">
165+
<property name="skipIfLastAndSharedWithCase" value="true"/>
166+
</module>
167+
<module name="SimplifyBooleanExpression"/>
168+
<module name="SimplifyBooleanReturn"/>
169+
<module name="StringLiteralEquality"/>
170+
171+
<module name="ModifierOrder"/>
172+
<module name="RedundantModifier"/>
173+
174+
<module name="AnnotationLocation"/>
175+
<module name="MissingOverride"/>
176+
177+
<!-- By default this allows catch blocks with only comments -->
178+
<module name="EmptyCatchBlock"/>
179+
180+
<module name="OuterTypeFilename"/>
181+
<module name="PackageDeclaration"/>
182+
183+
<!--<module name="InvalidJavadocPosition"/>-->
184+
<module name="JavadocParagraph"/>
185+
<module name="JavadocStyle"/>
186+
<module name="AtclauseOrder">
187+
<property name="tagOrder" value="@param,@return,@throws,@deprecated"/>
188+
</module>
189+
190+
<!-- Prevent var for all cases other than new instance creation -->
191+
<module name="MatchXpath">
192+
<property name="query" value="//VARIABLE_DEF[./TYPE/IDENT[@text='var'] and not(./ASSIGN/EXPR/LITERAL_NEW)]"/>
193+
</module>
194+
195+
<!-- Prevent public records in API packages -->
196+
<module name="MatchXpath">
197+
<property name="query" value="//RECORD_DEF[./MODIFIERS/LITERAL_PUBLIC][//PACKAGE_DEF//DOT/IDENT[@text='api']]"/>
198+
</module>
199+
</module>
200+
</module>

0 commit comments

Comments
 (0)