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

Introduce "applyToIDEA" subcommand. #554

Merged
merged 1 commit into from
Sep 3, 2019
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ ktlint --apply-to-idea-project --android

##### Option #2

Apply to all IDEA projects:
```shell
$ ./ktlint applyToIDEA
```
Or if you want to use android specific code style:
```shell
$ ./ktlint --android applyToIDEA
```

##### Option #3

Go to <kbd>File</kbd> -> <kbd>Settings...</kbd> -> <kbd>Editor</kbd>
- <kbd>General</kbd> -> <kbd>Auto Import</kbd>
- check `Optimize imports on the fly (for current project)`.
Expand Down
12 changes: 4 additions & 8 deletions ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.pinterest.ktlint.core.Reporter
import com.pinterest.ktlint.core.ReporterProvider
import com.pinterest.ktlint.core.RuleExecutionException
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.internal.ApplyToIDEAGloballySubCommand
import com.pinterest.ktlint.internal.GitPreCommitHookSubCommand
import com.pinterest.ktlint.internal.GitPrePushHookSubCommand
import com.pinterest.ktlint.internal.IntellijIDEAIntegration
Expand Down Expand Up @@ -56,6 +57,7 @@ fun main(args: Array<String>) {
.addSubcommand(GitPreCommitHookSubCommand.COMMAND_NAME, GitPreCommitHookSubCommand())
.addSubcommand(GitPrePushHookSubCommand.COMMAND_NAME, GitPrePushHookSubCommand())
.addSubcommand(PrintASTSubCommand.COMMAND_NAME, PrintASTSubCommand())
.addSubcommand(ApplyToIDEAGloballySubCommand.COMMAND_NAME, ApplyToIDEAGloballySubCommand())
val parseResult = commandLine.parseArgs(*args)

commandLine.printHelpOrVersionUsage()
Expand All @@ -75,6 +77,7 @@ fun handleSubCommand(
is GitPreCommitHookSubCommand -> subCommand.run()
is GitPrePushHookSubCommand -> subCommand.run()
is PrintASTSubCommand -> subCommand.run()
is ApplyToIDEAGloballySubCommand -> subCommand.run()
else -> commandLine.usage(System.out, CommandLine.Help.Ansi.OFF)
}
}
Expand Down Expand Up @@ -122,13 +125,6 @@ class KtlintCommandLine {
)
var android: Boolean = false

// todo: make it a command in 1.0.0 (it's too late now as we might interfere with valid "lint" patterns)
@Option(
names = ["--apply-to-idea"],
description = ["Update Intellij IDEA settings (global)"]
)
private var apply: Boolean = false

// todo: make it a command in 1.0.0 (it's too late now as we might interfere with valid "lint" patterns)
@Option(
names = ["--apply-to-idea-project"],
Expand Down Expand Up @@ -249,7 +245,7 @@ class KtlintCommandLine {
private var patterns = ArrayList<String>()

fun run() {
if (apply || applyToProject) {
if (applyToProject) {
applyToIDEA()
exitProcess(0)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.pinterest.ktlint.internal

import com.pinterest.ktlint.KtlintCommandLine
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.system.exitProcess
import picocli.CommandLine

@CommandLine.Command(
description = [
"Update Intellij IDEA Kotlin codestyle settings (global)"
],
aliases = ["--apply-to-idea"],
mixinStandardHelpOptions = true,
versionProvider = KtlintVersionProvider::class
)
class ApplyToIDEAGloballySubCommand : Runnable {
@CommandLine.ParentCommand
private lateinit var ktlintCommand: KtlintCommandLine

@CommandLine.Spec
private lateinit var commandSpec: CommandLine.Model.CommandSpec

@CommandLine.Option(
names = ["-y"],
description = ["Overwrite existing Kotlin codestyle settings without asking"]
)
private var forceApply: Boolean = false

override fun run() {
commandSpec.commandLine().printHelpOrVersionUsage()

try {
val workDir = Paths.get(".")

if (!forceApply && !getUserAcceptanceToUpdateFiles(workDir)) {
println("Update canceled.")
exitProcess(1)
}

IntellijIDEAIntegration.apply(workDir, false, ktlintCommand.android, false)
} catch (e: IntellijIDEAIntegration.ProjectNotFoundException) {
println(".idea directory not found. Are you sure you are inside project root directory?")
exitProcess(1)
}

println(
"""
|Updated.
|Please restart your IDE.
|If you experience any issues please report them at https://github.com/pinterest/ktlint/issues.
""".trimMargin()
)
}

private fun getUserAcceptanceToUpdateFiles(workDir: Path): Boolean {
val fileList = IntellijIDEAIntegration.apply(workDir, true, ktlintCommand.android, false)
println(
"""
|The following files are going to be updated:
|${fileList.joinToString(prefix = "\t", separator = "\n\t")}
|
|Do you wish to proceed? [y/n]
|(in future, use -y flag if you wish to skip confirmation)
""".trimMargin()
)

val userInput = generateSequence { readLine() }
.filter { it.trim().isNotBlank() }
.first()

return "y".equals(userInput, ignoreCase = true)
}

companion object {
const val COMMAND_NAME = "applyToIDEA"
}
}