Skip to content

Commit

Permalink
Add Moderne CLI to recipes (#80)
Browse files Browse the repository at this point in the history
So that people can be aware of the fact that the Moderne CLi exists
and can be used to run recipes.

Co-authored-by: Mike Sol <[email protected]>
  • Loading branch information
mike-solomon and mike-solomon authored Oct 12, 2023
1 parent 664c3b5 commit 2825445
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
69 changes: 61 additions & 8 deletions src/main/kotlin/org/openrewrite/RecipeMarkdownGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1043,25 +1043,38 @@ class RecipeMarkdownGenerator : Runnable {
gradlePluginVersion,
mavenPluginVersion,
suppressMaven,
suppressGradle
suppressGradle,
getCliSnippet(recipeDescriptor),
)
} else {
writeSnippetsWithConfigurationWithoutDependency(
exampleRecipeName,
gradlePluginVersion,
mavenPluginVersion,
suppressMaven,
suppressGradle
suppressGradle,
getCliSnippet(recipeDescriptor),
)
}
} else {
if (origin.isFromCoreLibrary()) {
writeSnippetsFromCoreLibrary(
recipeDescriptor, gradlePluginVersion, mavenPluginVersion, suppressMaven, suppressGradle
recipeDescriptor,
gradlePluginVersion,
mavenPluginVersion,
suppressMaven,
suppressGradle,
getCliSnippet(recipeDescriptor),
)
} else {
writeSnippetForOtherLibrary(
origin, recipeDescriptor, gradlePluginVersion, mavenPluginVersion, suppressMaven, suppressGradle
origin,
recipeDescriptor,
gradlePluginVersion,
mavenPluginVersion,
suppressMaven,
suppressGradle,
getCliSnippet(recipeDescriptor),
)
}
}
Expand Down Expand Up @@ -1184,12 +1197,36 @@ class RecipeMarkdownGenerator : Runnable {
return diffContent.toString()
}

private fun getCliSnippet(
recipeDescriptor: RecipeDescriptor,
): String {
val lastPeriod = recipeDescriptor.name.lastIndexOf('.')
var trimmedRecipeName = recipeDescriptor.name

if (lastPeriod >= 0) {
trimmedRecipeName = recipeDescriptor.name.substring(lastPeriod + 1)
}

return """
{% tab title="Moderne CLI" %}
You will need to have configured the [Moderne CLI](https://docs.moderne.io/moderne-cli/cli-intro) on your machine before you can run the following command.
{% code title="shell" %}
```shell
mod run <PATH> --recipe $trimmedRecipeName
```
{% endcode %}
{% endtab %}
""".trimIndent()
}

private fun BufferedWriter.writeSnippetsWithConfigurationWithoutDependency(
exampleRecipeName: String,
gradlePluginVersion: String,
mavenPluginVersion: String,
suppressMaven: Boolean,
suppressGradle: Boolean
suppressGradle: Boolean,
cliSnippet: String,
) {
val gradleSnippet = if (suppressGradle) "" else """
{% tab title="Gradle" %}
Expand All @@ -1212,6 +1249,7 @@ class RecipeMarkdownGenerator : Runnable {
2. Run `gradle rewriteRun` to run the recipe.
{% endtab %}
""".trimIndent()

val mavenSnippet = if (suppressMaven) "" else """
{% tab title="Maven" %}
1. Add the following to your `pom.xml` file:
Expand All @@ -1238,12 +1276,14 @@ class RecipeMarkdownGenerator : Runnable {
2. Run `mvn rewrite:run` to run the recipe.
{% endtab %}
""".trimIndent()

writeln(
"""
Now that `$exampleRecipeName` has been defined activate it in your build file:
{% tabs %}
$gradleSnippet
$mavenSnippet
$cliSnippet
{% endtabs %}
""".trimIndent()
)
Expand All @@ -1255,7 +1295,8 @@ $mavenSnippet
gradlePluginVersion: String,
mavenPluginVersion: String,
suppressMaven: Boolean,
suppressGradle: Boolean
suppressGradle: Boolean,
cliSnippet: String,
) {
val gradleSnippet = if (suppressGradle) "" else """
{% tab title="Gradle" %}
Expand All @@ -1282,6 +1323,7 @@ $mavenSnippet
2. Run `gradle rewriteRun` to run the recipe.
{% endtab %}
""".trimIndent()

val mavenSnippet = if (suppressMaven) "" else """
{% tab title="Maven" %}
1. Add the following to your `pom.xml` file:
Expand Down Expand Up @@ -1315,12 +1357,14 @@ $mavenSnippet
2. Run `mvn rewrite:run` to run the recipe.
{% endtab %}
""".trimIndent()

writeln(
"""
Now that `$exampleRecipeName` has been defined activate it and take a dependency on ${origin.groupId}:${origin.artifactId}:${origin.version} in your build file:
{% tabs %}
$gradleSnippet
$mavenSnippet
$cliSnippet
{% endtabs %}
""".trimIndent()
)
Expand All @@ -1331,12 +1375,14 @@ $mavenSnippet
gradlePluginVersion: String,
mavenPluginVersion: String,
suppressMaven: Boolean,
suppressGradle: Boolean
suppressGradle: Boolean,
cliSnippet: String,
) {
writeln(
"This recipe has no required configuration parameters and comes from a rewrite core library. " +
"It can be activated directly without adding any dependencies."
)

val gradleSnippet = if (suppressGradle) "" else """
{% tab title="Gradle" %}
1. Add the following to your `build.gradle` file:
Expand Down Expand Up @@ -1390,6 +1436,7 @@ $mavenSnippet
2. Run `gradle --init-script init.gradle rewriteRun` to run the recipe.
{% endtab %}
""".trimIndent()

val mavenSnippet = if (suppressMaven) "" else """
{% tab title="Maven POM" %}
1. Add the following to your `pom.xml` file:
Expand Down Expand Up @@ -1431,6 +1478,7 @@ $mavenSnippet
{% tabs %}
$gradleSnippet
$mavenSnippet
$cliSnippet
{% endtabs %}
""".trimIndent()
)
Expand All @@ -1442,13 +1490,15 @@ $mavenSnippet
gradlePluginVersion: String,
mavenPluginVersion: String,
suppressMaven: Boolean,
suppressGradle: Boolean
suppressGradle: Boolean,
cliSnippet: String,
) {
writeln(
"This recipe has no required configuration options. It can be activated by adding a dependency on " +
"`${origin.groupId}:${origin.artifactId}:${origin.version}` in your build file or by running a shell " +
"command (in which case no build changes are needed): "
)

val gradleSnippet = if (suppressGradle) "" else """
{% tab title="Gradle" %}
1. Add the following to your `build.gradle` file:
Expand Down Expand Up @@ -1505,6 +1555,7 @@ $mavenSnippet
2. Run `gradle --init-script init.gradle rewriteRun` to run the recipe.
{% endtab %}
""".trimIndent()

val mavenSnippet = if (suppressMaven) "" else """
{% tab title="Maven POM" %}
1. Add the following to your `pom.xml` file:
Expand Down Expand Up @@ -1550,11 +1601,13 @@ $mavenSnippet
{% endcode %}
{% endtab %}
""".trimIndent()

writeln(
"""
{% tabs %}
$gradleSnippet
$mavenSnippet
$cliSnippet
{% endtabs %}
""".trimIndent()
)
Expand Down
20 changes: 10 additions & 10 deletions src/main/resources/recipeDescriptors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ rewrite-concourse:
artifactId: "rewrite-concourse"
rewrite-core:
artifactId: "rewrite-core"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.DeleteSourceFiles:
name: "org.openrewrite.DeleteSourceFiles"
Expand Down Expand Up @@ -849,7 +849,7 @@ rewrite-github-actions:
artifactId: "rewrite-github-actions"
rewrite-gradle:
artifactId: "rewrite-gradle"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.gradle.AddDependency:
name: "org.openrewrite.gradle.AddDependency"
Expand Down Expand Up @@ -1414,7 +1414,7 @@ rewrite-gradle:
artifactId: "rewrite-gradle"
rewrite-groovy:
artifactId: "rewrite-groovy"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.groovy.format.OmitParenthesesForLastArgumentLambda:
name: "org.openrewrite.groovy.format.OmitParenthesesForLastArgumentLambda"
Expand All @@ -1433,7 +1433,7 @@ rewrite-groovy:
artifactId: "rewrite-groovy"
rewrite-hcl:
artifactId: "rewrite-hcl"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.hcl.DeleteContent:
name: "org.openrewrite.hcl.DeleteContent"
Expand Down Expand Up @@ -1584,7 +1584,7 @@ rewrite-hibernate:
artifactId: "rewrite-hibernate"
rewrite-java:
artifactId: "rewrite-java"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.java.AddApache2LicenseHeader:
name: "org.openrewrite.java.AddApache2LicenseHeader"
Expand Down Expand Up @@ -3353,7 +3353,7 @@ rewrite-jhipster:
artifactId: "rewrite-jhipster"
rewrite-json:
artifactId: "rewrite-json"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.json.ChangeKey:
name: "org.openrewrite.json.ChangeKey"
Expand Down Expand Up @@ -4271,7 +4271,7 @@ rewrite-logging-frameworks:
artifactId: "rewrite-logging-frameworks"
rewrite-maven:
artifactId: "rewrite-maven"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.maven.AddCommentToMavenDependency:
name: "org.openrewrite.maven.AddCommentToMavenDependency"
Expand Down Expand Up @@ -7899,7 +7899,7 @@ rewrite-okhttp:
artifactId: "rewrite-okhttp"
rewrite-properties:
artifactId: "rewrite-properties"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.properties.AddProperty:
name: "org.openrewrite.properties.AddProperty"
Expand Down Expand Up @@ -12579,7 +12579,7 @@ rewrite-testing-frameworks:
artifactId: "rewrite-testing-frameworks"
rewrite-xml:
artifactId: "rewrite-xml"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.xml.AddCommentToXmlTag:
name: "org.openrewrite.xml.AddCommentToXmlTag"
Expand Down Expand Up @@ -12785,7 +12785,7 @@ rewrite-xml:
artifactId: "rewrite-xml"
rewrite-yaml:
artifactId: "rewrite-yaml"
version: "8.7.3"
version: "8.7.4"
markdownRecipeDescriptors:
org.openrewrite.yaml.AppendToSequence:
name: "org.openrewrite.yaml.AppendToSequence"
Expand Down

0 comments on commit 2825445

Please sign in to comment.