Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch ktlint for spotless #1093

Merged
merged 2 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.