From 3071249d81e2faadaca46ee2e9fee1e111888757 Mon Sep 17 00:00:00 2001 From: Andreas Svanberg <andreass@dsv.su.se> Date: Fri, 29 Nov 2024 11:21:44 +0100 Subject: [PATCH] Introduce Prettier for code formatting The formatting is validated during the Maven phase `verify` (on the parent project so the absolute first thing that happens). There is no automated reformatting done in the default lifecycle but there are instructions on how to execute it. Prettier is *very* opinionated with no configuration options. --- .gitattributes | 5 ++ .gitignore | 1 + .prettierrc.yaml | 4 ++ README.md | 14 +++++ package-lock.json | 138 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 9 +++ pom.xml | 51 +++++++++++++++++ 7 files changed, 222 insertions(+) create mode 100644 .gitattributes create mode 100644 .prettierrc.yaml create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..ae200f1eb7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +# Prettier enforces LF line endings in all files +# If git is configured with core.autocrlf=true, it may convert LF to CRLF when +# checking out files depending on your OS. This will cause Prettier to change it +# causing Git to show every file as modified. +*.java text eol=lf diff --git a/.gitignore b/.gitignore index 204ca95f52..a4397c6105 100755 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ fitnesse/target/ daisy-integration/target/ war/target/ api/target/ +node_modules/ diff --git a/.prettierrc.yaml b/.prettierrc.yaml new file mode 100644 index 0000000000..0a88ae5448 --- /dev/null +++ b/.prettierrc.yaml @@ -0,0 +1,4 @@ +tabWidth: 4 +printWidth: 100 +plugins: + - prettier-plugin-java diff --git a/README.md b/README.md index 749d07cb72..dc2b0cb977 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,17 @@ and get an access token. Once the token has been obtained go to the [Swagger UI](http://localhost:8080/api/swagger) to interact with the API. Click the "Authorize" button in the top right and paste the access token to log in. + +## Code formatting +This project uses [prettier-java](https://github.com/jhipster/prettier-java) +to format all Java code. To reformat the code run +`./mvnw validate frontend:npm@reformat -pl .`. +Yes it's a mouthful but unfortunately the [prettier-maven-plugin](https://github.com/HubSpot/prettier-maven-plugin) +does not work due to an [outstanding issue](https://github.com/HubSpot/prettier-maven-plugin/issues/79). + +An easier way to reformat code is to set IntelliJ to do it on save. Go to +`Settings -> Language & Frameworks -> JavaScript -> Prettier` and then check +`Automatic Prettier Configuration`, set `Run for files` to `**/*.{java}`, +and finally check `Run on save`. + +The formatting is validated by CI, but you should do it beforehand with a simple `./mvnw verify -pl .`. diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000..ffc4227614 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,138 @@ +{ + "name": "scipro", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "devDependencies": { + "prettier-plugin-java": "^2.6.5" + } + }, + "node_modules/@chevrotain/cst-dts-gen": { + "version": "11.0.3", + "resolved": "https://registry.npmjs.org/@chevrotain/cst-dts-gen/-/cst-dts-gen-11.0.3.tgz", + "integrity": "sha512-BvIKpRLeS/8UbfxXxgC33xOumsacaeCKAjAeLyOn7Pcp95HiRbrpl14S+9vaZLolnbssPIUuiUd8IvgkRyt6NQ==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@chevrotain/gast": "11.0.3", + "@chevrotain/types": "11.0.3", + "lodash-es": "4.17.21" + } + }, + "node_modules/@chevrotain/gast": { + "version": "11.0.3", + "resolved": "https://registry.npmjs.org/@chevrotain/gast/-/gast-11.0.3.tgz", + "integrity": "sha512-+qNfcoNk70PyS/uxmj3li5NiECO+2YKZZQMbmjTqRI3Qchu8Hig/Q9vgkHpI3alNjr7M+a2St5pw5w5F6NL5/Q==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@chevrotain/types": "11.0.3", + "lodash-es": "4.17.21" + } + }, + "node_modules/@chevrotain/regexp-to-ast": { + "version": "11.0.3", + "resolved": "https://registry.npmjs.org/@chevrotain/regexp-to-ast/-/regexp-to-ast-11.0.3.tgz", + "integrity": "sha512-1fMHaBZxLFvWI067AVbGJav1eRY7N8DDvYCTwGBiE/ytKBgP8azTdgyrKyWZ9Mfh09eHWb5PgTSO8wi7U824RA==", + "dev": true, + "license": "Apache-2.0" + }, + "node_modules/@chevrotain/types": { + "version": "11.0.3", + "resolved": "https://registry.npmjs.org/@chevrotain/types/-/types-11.0.3.tgz", + "integrity": "sha512-gsiM3G8b58kZC2HaWR50gu6Y1440cHiJ+i3JUvcp/35JchYejb2+5MVeJK0iKThYpAa/P2PYFV4hoi44HD+aHQ==", + "dev": true, + "license": "Apache-2.0" + }, + "node_modules/@chevrotain/utils": { + "version": "11.0.3", + "resolved": "https://registry.npmjs.org/@chevrotain/utils/-/utils-11.0.3.tgz", + "integrity": "sha512-YslZMgtJUyuMbZ+aKvfF3x1f5liK4mWNxghFRv7jqRR9C3R3fAOGTTKvxXDa2Y1s9zSbcpuO0cAxDYsc9SrXoQ==", + "dev": true, + "license": "Apache-2.0" + }, + "node_modules/chevrotain": { + "version": "11.0.3", + "resolved": "https://registry.npmjs.org/chevrotain/-/chevrotain-11.0.3.tgz", + "integrity": "sha512-ci2iJH6LeIkvP9eJW6gpueU8cnZhv85ELY8w8WiFtNjMHA5ad6pQLaJo9mEly/9qUyCpvqX8/POVUTf18/HFdw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@chevrotain/cst-dts-gen": "11.0.3", + "@chevrotain/gast": "11.0.3", + "@chevrotain/regexp-to-ast": "11.0.3", + "@chevrotain/types": "11.0.3", + "@chevrotain/utils": "11.0.3", + "lodash-es": "4.17.21" + } + }, + "node_modules/chevrotain-allstar": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/chevrotain-allstar/-/chevrotain-allstar-0.3.1.tgz", + "integrity": "sha512-b7g+y9A0v4mxCW1qUhf3BSVPg+/NvGErk/dOkrDaHA0nQIQGAtrOjlX//9OQtRlSCy+x9rfB5N8yC71lH1nvMw==", + "dev": true, + "license": "MIT", + "dependencies": { + "lodash-es": "^4.17.21" + }, + "peerDependencies": { + "chevrotain": "^11.0.0" + } + }, + "node_modules/java-parser": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/java-parser/-/java-parser-2.3.2.tgz", + "integrity": "sha512-/O42UbEHy3VVJw8W0ruHkQjW75oWvQx4QisoUDRIGir6q3/IZ4JslDMPMYEqp7LU56PYJkH5uXdQiBaCXt/Opw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "chevrotain": "11.0.3", + "chevrotain-allstar": "0.3.1", + "lodash": "4.17.21" + } + }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true, + "license": "MIT" + }, + "node_modules/lodash-es": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==", + "dev": true, + "license": "MIT" + }, + "node_modules/prettier": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz", + "integrity": "sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/prettier-plugin-java": { + "version": "2.6.5", + "resolved": "https://registry.npmjs.org/prettier-plugin-java/-/prettier-plugin-java-2.6.5.tgz", + "integrity": "sha512-2RkPNXyYpP5dRhr04pz45n+e5LXwYWTh1JXrztiCkZTGGokIGYrfwUuGa8csnDoGbP6CDPgVm8zZSIm/9I0SRQ==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "java-parser": "2.3.2", + "lodash": "4.17.21", + "prettier": "3.2.5" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000000..94e82097d1 --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "scripts": { + "format": "prettier --write \"**/src/{main,test}/**/*.java\"", + "format:check": "prettier --check \"**/src/{main,test}/**/*.java\"" + }, + "devDependencies": { + "prettier-plugin-java": "^2.6.5" + } +} diff --git a/pom.xml b/pom.xml index c96cb4a122..e69b87c989 100755 --- a/pom.xml +++ b/pom.xml @@ -396,6 +396,57 @@ <packagingExcludes>WEB-INF/web.xml</packagingExcludes> </configuration> </plugin> + <plugin> + <groupId>com.github.eirslett</groupId> + <artifactId>frontend-maven-plugin</artifactId> + <version>1.9.1</version> + + <!-- the parent execution will reformat all files including submodules --> + <inherited>false</inherited> + + <configuration> + <nodeVersion>v22.11.0</nodeVersion> + <installDirectory>${project.build.outputDirectory}</installDirectory> + </configuration> + <executions> + <execution> + <goals> + <goal>install-node-and-npm</goal> + </goals> + <phase>validate</phase> + </execution> + <execution> + <id>install</id> + <phase>validate</phase> + <goals> + <goal>npm</goal> + </goals> + <configuration> + <arguments>install</arguments> + </configuration> + </execution> + <execution> + <id>check-format</id> + <phase>verify</phase> + <goals> + <goal>npm</goal> + </goals> + <configuration> + <arguments>run format:check</arguments> + </configuration> + </execution> + <execution> + <id>reformat</id> + <phase>none</phase> + <goals> + <goal>npm</goal> + </goals> + <configuration> + <arguments>run format</arguments> + </configuration> + </execution> + </executions> + </plugin> </plugins> </build> </project> \ No newline at end of file