Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse .NET .vbproj and .fsproj files as XML #4518

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 41 additions & 28 deletions rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,43 @@
import org.openrewrite.xml.tree.Xml;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

public class XmlParser implements Parser {
private static final Set<String> ACCEPTED_FILE_EXTENSIONS = new HashSet<>(Arrays.asList(
"xml",
"wsdl",
"xhtml",
"xsd",
"xsl",
"xslt",
"xmi",
"tld",
"xjb",
"jsp",
// Datastage file formats that are all xml under the hood
"det",
"pjb",
"qjb",
"sjb",
"prt",
"srt",
"psc",
"ssc",
"tbd",
"tfm",
"dqs",
"stp",
"dcn",
"pst",
// .NET project files
"csproj",
"vbproj",
"fsproj"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we also should add "targets" and "props" from the .NET ecosystem here or not. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those were intentionally kept as plaintext, given any ambiguity in those file extensions being used elsewhere:
https://github.com/moderneinc/moderne-cli/pull/2117/files

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. So allowing these mappings to be more dynamic (e.g. parse .props files as XML in .NET projects) seems like it will at some point become necessary.


@Override
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
Expand Down Expand Up @@ -76,34 +110,13 @@ public Stream<SourceFile> parse(@Language("xml") String... sources) {
@Override
public boolean accept(Path path) {
String p = path.toString();
return p.endsWith(".xml") ||
p.endsWith(".wsdl") ||
p.endsWith(".xhtml") ||
p.endsWith(".xsd") ||
p.endsWith(".xsl") ||
p.endsWith(".xslt") ||
p.endsWith(".xmi") ||
p.endsWith(".tld") ||
p.endsWith(".xjb") ||
p.endsWith(".jsp") ||
// Datastage file formats that are all xml under the hood
p.endsWith(".det") ||
p.endsWith(".pjb") ||
p.endsWith(".qjb") ||
p.endsWith(".sjb") ||
p.endsWith(".prt") ||
p.endsWith(".srt") ||
p.endsWith(".psc") ||
p.endsWith(".ssc") ||
p.endsWith(".tbd") ||
p.endsWith(".tfm") ||
p.endsWith(".dqs") ||
p.endsWith(".stp") ||
p.endsWith(".dcn") ||
p.endsWith(".pst") ||
// C# project files
p.endsWith(".csproj") ||
path.endsWith("packages.config");
int dot = p.lastIndexOf('.');
if (0 < dot && dot < (p.length() - 1)) {
if (ACCEPTED_FILE_EXTENSIONS.contains(p.substring(dot + 1))) {
return true;
}
}
return path.endsWith("packages.config");
}

@Override
Expand Down
33 changes: 32 additions & 1 deletion rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
package org.openrewrite.xml;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.xml.tree.Xml;

import java.nio.file.Paths;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.openrewrite.test.RewriteTest.toRecipe;
import static org.openrewrite.xml.Assertions.xml;
Expand Down Expand Up @@ -229,7 +236,7 @@ void parseDocTypeWithoutExternalId() {
"""
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration >

<configuration scan="true">
<root>
<level>WARN</level>
Expand Down Expand Up @@ -372,4 +379,28 @@ void preserveWhitespaceOnEntities() {
)
);
}

@DisabledOnOs(OS.WINDOWS)
@ParameterizedTest
@ValueSource(strings = {
"foo.xml",
"proj.csproj",
"/foo/bar/baz.jsp",
"packages.config"
})
void acceptWithValidPaths(String path) {
assertThat(new XmlParser().accept(Paths.get(path))).isTrue();
}

@DisabledOnOs(OS.WINDOWS)
@ParameterizedTest
@ValueSource(strings = {
".xml",
"foo.xml.",
"file.cpp",
"/foo/bar/baz.xml.txt"
})
void acceptWithInvalidPaths(String path) {
assertThat(new XmlParser().accept(Paths.get(path))).isFalse();
}
}