Skip to content

Commit

Permalink
Add recipe to add XML declaration to Jelly files
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
gounthar committed Oct 28, 2024
1 parent 8336dfd commit 29c896a
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/main/java/org/openrewrite/jenkins/AddJellyXmlDeclaration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2023 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Recipe to add an XML declaration to Jelly files.
*/
public class AddJellyXmlDeclaration extends Recipe {

private static final Logger LOG = LoggerFactory.getLogger(AddJellyXmlDeclaration.class);

@Override
public String getDisplayName() {
return "Add XML declaration to Jelly files";
}

@Override
public String getDescription() {
return "Ensure the XML declaration `<?jelly escape-by-default='true'?>` is present in all `.jelly` files.";
}

@Override
public PlainTextVisitor<ExecutionContext> getVisitor() {
return new PlainTextVisitor<ExecutionContext>() {
public static final String JELLY_DECLARATION = "<?jelly escape-by-default='true'?>";

@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
if (text == null || text.getSourcePath() == null) {
return text;
}
if (text.getSourcePath().toString().endsWith(".jelly")) {
LOG.debug("Processing Jelly file: {}", text.getSourcePath());
String content = text.getText();
if (content.trim().isEmpty()) {
LOG.debug("Adding declaration to empty file");
return text.withText(JELLY_DECLARATION);
}
String lineEnding = content.contains("\r\n") ? "\r\n" : "\n";
if (content.trim().toLowerCase().matches("^<\\?jelly\\s+[^>]*>") && !content.startsWith(JELLY_DECLARATION)) {
LOG.warn("Found malformed Jelly declaration in {}", text.getSourcePath());
LOG.debug("Adding missing declaration");
content = content.substring(content.indexOf(lineEnding) + lineEnding.length());
}
if (!content.startsWith(JELLY_DECLARATION)) {
LOG.debug("Declaration already present");
content = JELLY_DECLARATION + lineEnding + content;
return text.withText(content);
}
}
return text;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.openrewrite.jelly;

import static org.openrewrite.test.SourceSpecs.text;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.openrewrite.Recipe;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.text.PlainTextParser;

/**
* Test class for the AddJellyXmlDeclaration recipe.
*/
public class AddJellyXmlDeclarationTest implements RewriteTest {

@Override
public Recipe getRecipe() {
return new AddJellyXmlDeclaration();
}

@Test
void addXmlDeclarationToJellyFile(@TempDir Path tempDir) throws IOException {
Path inputFile = tempDir.resolve("example.jelly");
Files.writeString(inputFile, """
<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>
""");

Path expectedFile = tempDir.resolve("expected.jelly");
Files.writeString(expectedFile, """
<?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>
""");

rewriteRun(
spec -> spec.recipe(new AddJellyXmlDeclaration())
.parser(PlainTextParser.builder())
.expectedCyclesThatMakeChanges(1)
.cycles(1),
text(Files.readString(inputFile), Files.readString(expectedFile)));
}

@Test
void doNotAddXmlDeclarationIfAlreadyPresent(@TempDir Path tempDir) throws IOException {
Path inputFile = tempDir.resolve("example.jelly");
Files.writeString(inputFile, """
<?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>
""");

rewriteRun(
spec -> spec.recipe(new AddJellyXmlDeclaration())
.parser(PlainTextParser.builder())
.expectedCyclesThatMakeChanges(1)
.cycles(0),
text(Files.readString(inputFile)));
}
}

0 comments on commit 29c896a

Please sign in to comment.