-
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.
Add "applyToIDEAProject" subcommand. (#593)
Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
- Loading branch information
1 parent
25e8515
commit 4daebcd
Showing
4 changed files
with
112 additions
and
100 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
64 changes: 64 additions & 0 deletions
64
ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEACommandHelper.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,64 @@ | ||
package com.pinterest.ktlint.internal | ||
|
||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import kotlin.system.exitProcess | ||
|
||
class ApplyToIDEACommandHelper( | ||
private val applyToProject: Boolean, | ||
private val forceApply: Boolean, | ||
private val isAndroidCodeStyle: Boolean | ||
) { | ||
fun apply() { | ||
try { | ||
val workDir = Paths.get(".") | ||
|
||
if (!forceApply && !getUserAcceptanceToUpdateFiles(workDir)) { | ||
println("Update canceled.") | ||
exitProcess(1) | ||
} | ||
|
||
IntellijIDEAIntegration.apply( | ||
workDir, | ||
false, | ||
isAndroidCodeStyle, | ||
applyToProject | ||
) | ||
} 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, | ||
isAndroidCodeStyle, | ||
applyToProject | ||
) | ||
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) | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEAProjectSubCommand.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,40 @@ | ||
package com.pinterest.ktlint.internal | ||
|
||
import com.pinterest.ktlint.KtlintCommandLine | ||
import picocli.CommandLine | ||
|
||
@CommandLine.Command( | ||
description = [ | ||
"Update Intellij IDEA project settings" | ||
], | ||
aliases = ["--apply-to-idea-project"], | ||
mixinStandardHelpOptions = true, | ||
versionProvider = KtlintVersionProvider::class | ||
) | ||
class ApplyToIDEAProjectSubCommand : 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() | ||
|
||
ApplyToIDEACommandHelper( | ||
true, | ||
forceApply, | ||
ktlintCommand.android | ||
).apply() | ||
} | ||
|
||
companion object { | ||
const val COMMAND_NAME = "applyToIDEAProject" | ||
} | ||
} |