From b5254c2d0cc57c2eb03227d391a3713b809d0b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Kapu=C5=9Bciak?= <39658211+kacperkapusciak@users.noreply.github.com> Date: Mon, 6 Sep 2021 08:48:03 +0200 Subject: [PATCH] feat: switch ktlint for spotless (#1093) Using `ktlint` directly as gradle task is very powerful and flexible but due to problems (#1085, #1090) we've decided to use formatting/linting framework - Spotless. --- android/build.gradle | 34 +++++++++++++--------------------- package.json | 11 +++-------- scripts/format-android.js | 17 +++++++++++------ scripts/lint-android.js | 22 ---------------------- 4 files changed, 27 insertions(+), 57 deletions(-) delete mode 100644 scripts/lint-android.js diff --git a/android/build.gradle b/android/build.gradle index 74616c0350..5e4d21dd29 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -10,6 +10,10 @@ buildscript { } } +plugins { + id "com.diffplug.spotless" version "5.14.1" +} + apply plugin: 'com.android.library' apply plugin: 'kotlin-android' @@ -55,26 +59,14 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.20" } -configurations { - ktlint -} - -dependencies { - ktlint "com.pinterest:ktlint:0.40.0" -} - -task ktlint(type: JavaExec, group: "verification") { - description = "Check Kotlin code style." - main = "com.pinterest.ktlint.Main" - classpath = configurations.ktlint - args project.findProperty("file") ?: "src/**/*.kt" -} +plugins.apply("com.diffplug.spotless") -check.dependsOn ktlint - -task ktlintFormat(type: JavaExec, group: "formatting") { - description = "Fix Kotlin code style deviations." - main = "com.pinterest.ktlint.Main" - classpath = configurations.ktlint - args "-F", project.findProperty("file") ?: "src/**/*.kt" +spotless { + kotlin { + target 'src/**/*.kt' + ktlint("0.40.0") + trimTrailingWhitespace() + indentWithSpaces() + endWithNewline() + } } diff --git a/package.json b/package.json index 2290f17782..0617ad72c9 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "format-ios": "find ios/ -iname *.h -o -iname *.m -o -iname *.cpp | xargs clang-format -i", "format": "yarn format-js && yarn format-android && yarn format-ios", "lint-js": "eslint --ext '.js,.ts,.tsx' --fix src", - "lint-android": "node ./scripts/lint-android.js", + "lint-android": "./android/gradlew -p android spotlessCheck -q", "lint": "yarn lint-js && yarn lint-android", "release": "npm login && release-it", "prepare": "bob build && husky install" @@ -103,13 +103,8 @@ "lint-staged": { "{src,Example}/**/*.{js,ts,tsx}": "yarn format-js", "src/**/*.{js,ts,tsx}": "yarn lint-js", - "android/**/*.kt": [ - "yarn format-android", - "yarn lint-android" - ], - "ios/**/*.{h,m,cpp}": [ - "yarn format-ios" - ] + "android/**/*.kt": "yarn format-android", + "ios/**/*.{h,m,cpp}": "yarn format-ios" }, "@react-native-community/bob": { "source": "src", diff --git a/scripts/format-android.js b/scripts/format-android.js index 6ef1cd2a0a..f0236731e3 100644 --- a/scripts/format-android.js +++ b/scripts/format-android.js @@ -1,19 +1,24 @@ /* - * This script is a wrapper for gradle & ktlintFormat to make + * This script is a wrapper for gradle & spotlessApply to make * it work properly with lint-staged. */ const { exit } = require("process"); const { exec } = require("child_process"); -// ktlintFormat task in android/build.gradle -const ktlintFormatCommand = "./android/gradlew -p android ktlintFormat -q -Pfile=" +// spotless ktlint formatting task in android/build.gradle +const spotlessApply = "./android/gradlew -p android spotlessApply"; // takes file as parameter passed by lint-staged (optional) -const fileName = process.argv[2] !== undefined ? process.argv[2] : ''; +const fileName = process.argv[2]; -// executes command with file parameter without space between them -exec(`${ktlintFormatCommand}${fileName}`, (error, stdout) => { +// https://github.com/diffplug/spotless/blob/main/plugin-gradle/IDE_HOOK.md +// creates file argument without space between arguments +const fileArgument = `-PspotlessIdeHook=${fileName}`; + +const command = fileName !== undefined ? `${spotlessApply} ${fileArgument}` : spotlessApply; + +exec(command, (error, stdout) => { if (error) { console.log(error); console.log(stdout); diff --git a/scripts/lint-android.js b/scripts/lint-android.js deleted file mode 100644 index 877ee9b6ec..0000000000 --- a/scripts/lint-android.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * This script is a wrapper for gradle & ktlint to make - * it work properly with lint-staged. - */ - -const { exit } = require("process"); -const { exec } = require("child_process"); - -// ktlint task in android/build.gradle -const ktlintCommand = "./android/gradlew -p android ktlint -q -Pfile=" - -// takes file as parameter passed by lint-staged (optional) -const fileName = process.argv[2] !== undefined ? process.argv[2] : ''; - -// executes command with file parameter without space between them -exec(`${ktlintCommand}${fileName}`, (error, stdout) => { - if (error) { - console.log(error); - console.log(stdout); - return exit(1); - } -});