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

Match C# artifactId and version in DependencyInsight #120

Merged
merged 2 commits into from
Aug 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,37 @@
*/
package org.openrewrite.csharp.dependencies;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.csharp.dependencies.trait.PackageReference;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.maven.table.DependenciesInUse;
import org.openrewrite.semver.Semver;

@Value
@EqualsAndHashCode(callSuper = false)
public class DependencyInsight extends Recipe {
transient DependenciesInUse dependenciesInUse = new DependenciesInUse(this);

@Option(displayName = "Package pattern",
description = "Package glob pattern used to match dependencies.",
example = "Microsoft*",
required = false)
@Nullable
String packagePattern;

@Option(displayName = "Version",
description = "Match only dependencies with the specified version. " +
"Node-style [version selectors](https://docs.openrewrite.org/reference/dependency-version-selectors) may be used. " +
"All versions are searched by default.",
example = "1.x",
required = false)
@Nullable
String version;

@Override
public String getDisplayName() {
return "Dependency insight for C#";
Expand All @@ -35,9 +56,28 @@ public String getDescription() {
return "Finds dependencies in `*.csproj` and `packages.config`.";
}

@Override
public Validated<Object> validate() {
Validated<Object> v = super.validate();
if (version != null) {
v = v.and(Semver.validate(version, null));
}
return v;
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new PackageReference.Matcher().asVisitor((ref, ctx) -> {
if (packagePattern != null &&
!StringUtils.matchesGlob(ref.getInclude(), packagePattern)) {
return ref.getTree();
}

if (version != null &&
!Semver.validate(version, null).getValue().isValid(null, ref.getVersion())) {
return ref.getTree();
}

dependenciesInUse.insertRow(ctx, new DependenciesInUse.Row(
null,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DependencyInsightTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new DependencyInsight());
spec.recipe(new DependencyInsight(null, null));
}

@Test
Expand Down Expand Up @@ -105,4 +105,87 @@ void packagesConfig() {
)
);
}

@Test
void matchesArtifactId() {
// Taken from https://learn.microsoft.com/en-us/nuget/reference/packages-config
rewriteRun(
spec -> spec.recipe(new DependencyInsight("Microsoft.*", null)),
xml(
//language=xml
"""
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net46" />
<package id="Newtonsoft.Json" version="8.0.3" allowedVersions="[8,10)" targetFramework="net46" />
</packages>
""",
//language=xml
"""
<?xml version="1.0" encoding="utf-8"?>
<packages>
<!--~~(Microsoft.CodeDom.Providers.DotNetCompilerPlatform:1.0.0)~~>--><package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net46" />
<package id="Newtonsoft.Json" version="8.0.3" allowedVersions="[8,10)" targetFramework="net46" />
</packages>
""",
spec -> spec.path("packages.config")
)
);
}

@Test
void matchesVersion() {
// Taken from https://learn.microsoft.com/en-us/nuget/reference/packages-config
rewriteRun(
spec -> spec.recipe(new DependencyInsight(null, "2.1.x")),
xml(
//language=xml
"""
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Web.Xdt" version="2.1.1" targetFramework="net46" />
<package id="NuGet.Core" version="2.11.1" targetFramework="net46" />
<package id="WebActivatorEx" version="2.1.0" targetFramework="net46" />
</packages>
""",
//language=xml
"""
<?xml version="1.0" encoding="utf-8"?>
<packages>
<!--~~(Microsoft.Web.Xdt:2.1.1)~~>--><package id="Microsoft.Web.Xdt" version="2.1.1" targetFramework="net46" />
<package id="NuGet.Core" version="2.11.1" targetFramework="net46" />
<!--~~(WebActivatorEx:2.1.0)~~>--><package id="WebActivatorEx" version="2.1.0" targetFramework="net46" />
</packages>
""",
spec -> spec.path("packages.config")
)
);
}

@Test
void matchesArtifactIdAndVersion() {
// Taken from https://learn.microsoft.com/en-us/nuget/reference/packages-config
rewriteRun(
spec -> spec.recipe(new DependencyInsight("Microsoft.*", "2.1.x")),
xml(
//language=xml
"""
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Web.Xdt" version="2.1.1" targetFramework="net46" />
<package id="WebActivatorEx" version="2.1.0" targetFramework="net46" />
</packages>
""",
//language=xml
"""
<?xml version="1.0" encoding="utf-8"?>
<packages>
<!--~~(Microsoft.Web.Xdt:2.1.1)~~>--><package id="Microsoft.Web.Xdt" version="2.1.1" targetFramework="net46" />
<package id="WebActivatorEx" version="2.1.0" targetFramework="net46" />
</packages>
""",
spec -> spec.path("packages.config")
)
);
}
}
Loading