-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #923 from Tapchicoma/701/cli
Add 'generateEditorConfig' CLI subcommand.
- Loading branch information
Showing
5 changed files
with
118 additions
and
35 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
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
58 changes: 58 additions & 0 deletions
58
ktlint/src/main/kotlin/com/pinterest/ktlint/internal/GenerateEditorConfigSubCommand.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,58 @@ | ||
package com.pinterest.ktlint.internal | ||
|
||
import com.pinterest.ktlint.KtlintCommandLine | ||
import com.pinterest.ktlint.core.KtLint | ||
import com.pinterest.ktlint.core.api.FeatureInAlphaState | ||
import picocli.CommandLine | ||
|
||
@CommandLine.Command( | ||
description = [ | ||
"EXPERIMENTAL!!! Generate kotlin style section for '.editorconfig' file.", | ||
"Add output content into '.editorconfig' file" | ||
], | ||
mixinStandardHelpOptions = true, | ||
versionProvider = KtlintVersionProvider::class | ||
) | ||
class GenerateEditorConfigSubCommand : Runnable { | ||
@CommandLine.ParentCommand | ||
private lateinit var ktlintCommand: KtlintCommandLine | ||
|
||
@CommandLine.Spec | ||
private lateinit var commandSpec: CommandLine.Model.CommandSpec | ||
|
||
@OptIn(FeatureInAlphaState::class) | ||
override fun run() { | ||
commandSpec.commandLine().printHelpOrVersionUsage() | ||
|
||
// For now we are using CLI invocation dir as path to load existing '.editorconfig' | ||
val generatedEditorConfig = KtLint.generateKotlinEditorConfigSection( | ||
KtLint.Params( | ||
fileName = "./test.kt", | ||
text = "", | ||
ruleSets = ktlintCommand.rulesets | ||
.loadRulesets( | ||
ktlintCommand.experimental, | ||
ktlintCommand.debug | ||
) | ||
.map { it.value.get() }, | ||
debug = ktlintCommand.debug, | ||
cb = { _, _ -> Unit } | ||
) | ||
) | ||
|
||
if (generatedEditorConfig.isNotBlank()) { | ||
println( | ||
""" | ||
[*.{kt,kts}] | ||
$generatedEditorConfig | ||
""".trimIndent() | ||
) | ||
} else { | ||
println("Nothing to add to .editorconfig file") | ||
} | ||
} | ||
|
||
companion object { | ||
internal const val COMMAND_NAME = "generateEditorConfig" | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
ktlint/src/main/kotlin/com/pinterest/ktlint/internal/RuleSetsLoader.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,33 @@ | ||
package com.pinterest.ktlint.internal | ||
|
||
import com.pinterest.ktlint.core.RuleSetProvider | ||
import java.net.URLClassLoader | ||
import java.util.ServiceLoader | ||
|
||
/** | ||
* Load given list of paths to rulesets jars into map of ruleset providers. | ||
* | ||
* @return map of ruleset ids to ruleset providers | ||
*/ | ||
internal fun JarFiles.loadRulesets( | ||
loadExperimental: Boolean, | ||
debug: Boolean | ||
) = ServiceLoader | ||
.load( | ||
RuleSetProvider::class.java, | ||
URLClassLoader(toFilesURIList().toTypedArray()) | ||
) | ||
.associateBy { | ||
val key = it.get().id | ||
// standard should go first | ||
if (key == "standard") "\u0000$key" else key | ||
} | ||
.filterKeys { loadExperimental || it != "experimental" } | ||
.toSortedMap() | ||
.also { | ||
if (debug) { | ||
it.forEach { entry -> | ||
println("[DEBUG] Discovered ruleset with \"${entry.key}\" id.") | ||
} | ||
} | ||
} |