Skip to content

Commit

Permalink
feat: switch ktlint for spotless (#1093)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kacperkapusciak authored Sep 6, 2021
1 parent 2deb18a commit b5254c2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 57 deletions.
34 changes: 13 additions & 21 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ buildscript {
}
}

plugins {
id "com.diffplug.spotless" version "5.14.1"
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

Expand Down Expand Up @@ -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()
}
}
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 11 additions & 6 deletions scripts/format-android.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
22 changes: 0 additions & 22 deletions scripts/lint-android.js

This file was deleted.

0 comments on commit b5254c2

Please sign in to comment.