-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce "applyToIDEA" subcommand. (#554)
This subcommand replaces --apply-to-idea option. Option itself is still supported, but deprecated. Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
- Loading branch information
1 parent
b3f7cf4
commit b63cd7f
Showing
3 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEAGloballySubCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |