From 55883b720cc483c8ca3a530789472ccd32725f67 Mon Sep 17 00:00:00 2001 From: Bruno Verachten Date: Mon, 11 Nov 2024 20:49:17 +0100 Subject: [PATCH] feat(jelly): Add jelly xml declaration (#75) * Add recipe to add XML declaration to Jelly files Fixes #1 Implement the `AddJellyXmlDeclaration` recipe to add the XML declaration to Jelly files. * **AddJellyXmlDeclaration.java** - Implement the `AddJellyXmlDeclaration` class to add the XML declaration to Jelly files. - Use `PlainTextVisitor` to modify the content of `.jelly` files. - Ensure the XML declaration `` is present in all `.jelly` files. * **AddJellyXmlDeclarationTest.java** - Implement the `AddJellyXmlDeclarationTest` class to test the `AddJellyXmlDeclaration` recipe. - Write tests to verify the XML declaration is added to Jelly files. - Ensure tests cover cases where the declaration is already present and where it is missing. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/gounthar/rewrite-jenkins/issues/1?shareId=XXXX-XXXX-XXXX-XXXX). * fix(jelly): Add documentation. * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix(jelly): Corrected stupid input from the GitHub bot. * fix(jelly): Corrected stupid input from the GitHub bot. * fix(jelly): The defaults method could be improved by adding parser configuration here instead of repeating it in individual tests. This would make the tests more maintainable. * fix(jelly): Fix contradictory cycles configuration. * fix(jelly): Fix inconsistent cycles configuration. . * fix(jelly): Improve test clarity and completeness. * fix(jelly): Improve test clarity and completeness. * fix(jelly): Improve test clarity and completeness. * fix(jelly): Optimize test implementation. * fix(jelly): Enhance test readability and documentation. * fix(jelly): Enhance cleanup robustness. * fix(jelly): Fix Javadoc formatting and parameter naming consistency. * fix(jelly): Fix DocumentExample content to match the test. * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Minor polish * Do not use file IO for tests --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek Co-authored-by: Tim te Beek --- .../jenkins/AddJellyXmlDeclaration.java | 84 ++++++++++++++++ .../jenkins/AddJellyXmlDeclarationTest.java | 95 +++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 src/main/java/org/openrewrite/jenkins/AddJellyXmlDeclaration.java create mode 100644 src/test/java/org/openrewrite/jenkins/AddJellyXmlDeclarationTest.java diff --git a/src/main/java/org/openrewrite/jenkins/AddJellyXmlDeclaration.java b/src/main/java/org/openrewrite/jenkins/AddJellyXmlDeclaration.java new file mode 100644 index 0000000..700dddf --- /dev/null +++ b/src/main/java/org/openrewrite/jenkins/AddJellyXmlDeclaration.java @@ -0,0 +1,84 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.jenkins; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.text.PlainText; +import org.openrewrite.text.PlainTextVisitor; + +/** + * Recipe to add an XML declaration to Jelly files. + */ +public class AddJellyXmlDeclaration extends Recipe { + private static final String JELLY_DECLARATION = ""; + + /** + * Returns the display name of the recipe. + * + * @return the display name of the recipe + */ + @Override + public String getDisplayName() { + return "Add XML declaration to Jelly files"; + } + + /** + * Returns the description of the recipe. + * + * @return the description of the recipe + */ + @Override + public String getDescription() { + return "Ensure the XML declaration `` is present in all `.jelly` files."; + } + + /** + * Returns a visitor that adds the XML declaration to Jelly files. + * + * @return a PlainTextVisitor that adds the XML declaration + */ + @Override + public PlainTextVisitor getVisitor() { + return new PlainTextVisitor() { + + /** + * Visits the text and adds the XML declaration if necessary. + * + * @param text the PlainText object representing the file content + * @param ctx the execution context + */ + @Override + public PlainText visitText(PlainText text, ExecutionContext ctx) { + if (text.getSourcePath().toString().endsWith(".jelly")) { + String content = text.getText().trim(); + if (content.isEmpty()) { + return text.withText(JELLY_DECLARATION); + } + String lineEnding = content.contains("\r\n") ? "\r\n" : "\n"; + if (content.toLowerCase().matches("^<\\?jelly\\s+[^>]*>") && !content.startsWith(JELLY_DECLARATION)) { + content = content.substring(content.indexOf(lineEnding) + lineEnding.length()); + } + if (!content.startsWith(JELLY_DECLARATION)) { + content = JELLY_DECLARATION + lineEnding + content; + return text.withText(content); + } + } + return text; + } + }; + } +} diff --git a/src/test/java/org/openrewrite/jenkins/AddJellyXmlDeclarationTest.java b/src/test/java/org/openrewrite/jenkins/AddJellyXmlDeclarationTest.java new file mode 100644 index 0000000..beadaf3 --- /dev/null +++ b/src/test/java/org/openrewrite/jenkins/AddJellyXmlDeclarationTest.java @@ -0,0 +1,95 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.jenkins; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.test.SourceSpecs.text; + +class AddJellyXmlDeclarationTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new AddJellyXmlDeclaration()); + } + + @DocumentExample + @Test + void addJellyXmlDeclaration() { + rewriteRun( + //language=xml + text( + """ + +

Simple Example

+ + """, + """ + + +

Simple Example

+
+ """, + spec -> spec.path("example.jelly") + ) + ); + } + + @Test + void addXmlDeclarationToJellyFile() { + rewriteRun( + spec -> spec.expectedCyclesThatMakeChanges(1), + //language=xml + text( + """ + + +

Hello, World!

+
+ """, + """ + + + +

Hello, World!

+
+ """, + spec -> spec.path("example.jelly") + ) + ); + } + + @Test + void doNotAddXmlDeclarationIfAlreadyPresent() { + //language=xml + rewriteRun( + spec -> spec.expectedCyclesThatMakeChanges(0), + text( + """ + + + +

Hello, World!

+
+ """, + spec -> spec.path("example.jelly") + ) + ); + } +}