Skip to content

Commit

Permalink
feat(jelly): Add jelly xml declaration (#75)
Browse files Browse the repository at this point in the history
* 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 `<?jelly escape-by-default='true'?>` 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 <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
4 people authored Nov 11, 2024
1 parent c08e927 commit 55883b7
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/main/java/org/openrewrite/jenkins/AddJellyXmlDeclaration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 = "<?jelly escape-by-default='true'?>";

/**
* 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 `<?jelly escape-by-default='true'?>` 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<ExecutionContext> getVisitor() {
return new PlainTextVisitor<ExecutionContext>() {

/**
* 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;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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(
"""
<j:jelly xmlns:j="jelly:core">
<h1>Simple Example</h1>
</j:jelly>
""",
"""
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core">
<h1>Simple Example</h1>
</j:jelly>
""",
spec -> spec.path("example.jelly")
)
);
}

@Test
void addXmlDeclarationToJellyFile() {
rewriteRun(
spec -> spec.expectedCyclesThatMakeChanges(1),
//language=xml
text(
"""
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
<st:contentType value="text/html"/>
<h1>Hello, World!</h1>
</j:jelly>
""",
"""
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
<st:contentType value="text/html"/>
<h1>Hello, World!</h1>
</j:jelly>
""",
spec -> spec.path("example.jelly")
)
);
}

@Test
void doNotAddXmlDeclarationIfAlreadyPresent() {
//language=xml
rewriteRun(
spec -> spec.expectedCyclesThatMakeChanges(0),
text(
"""
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
<st:contentType value="text/html"/>
<h1>Hello, World!</h1>
</j:jelly>
""",
spec -> spec.path("example.jelly")
)
);
}
}

0 comments on commit 55883b7

Please sign in to comment.