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

Upgrade C# dependency versions #121

Merged
merged 1 commit into from
Aug 28, 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
@@ -0,0 +1,59 @@
/*
* 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.csharp.dependencies;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.csharp.dependencies.trait.PackageReference;
import org.openrewrite.internal.StringUtils;

@Value
@EqualsAndHashCode(callSuper = false)
public class UpgradeDependencyVersion extends Recipe {
@Option(displayName = "Package pattern",
description = "Package glob pattern used to match dependencies.",
example = "Microsoft*")
String packagePattern;

@Option(displayName = "New version",
description = "An exact version number.",
example = "12.3")
String newVersion;

@Override
public String getDisplayName() {
return "Upgrade C# dependency versions";
}

@Override
public String getDescription() {
return "Upgrades dependencies in `*.csproj` and `packages.config`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new PackageReference.Matcher().asVisitor((ref, ctx) -> {
if (StringUtils.matchesGlob(ref.getInclude(), packagePattern)) {
return ref.withVersion(newVersion);
}
return ref.getTree();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Xml.Tag withVersion(String newVersion) {
InMemoryExecutionContext ctx = new InMemoryExecutionContext();
tag = (Xml.Tag) new ChangeTagAttribute("//PackageReference", "Version", newVersion, this.version, null)
.getVisitor().visitNonNull(tag, ctx);
tag = (Xml.Tag) new ChangeTagAttribute("/packages/package", "version", newVersion, this.version, null)
tag = (Xml.Tag) new ChangeTagAttribute("//package", "version", newVersion, this.version, null)
.getVisitor().visitNonNull(tag, ctx);
}
return tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.csharp.dependencies;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.xml.Assertions.xml;

class UpgradeDependencyVersionTest implements RewriteTest {

@Test
@DocumentExample
void packagesConfig() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("Microsoft.*", "2.1.2")),
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>
<package id="Microsoft.Web.Xdt" version="2.1.2" targetFramework="net46" />
<package id="WebActivatorEx" version="2.1.0" targetFramework="net46" />
</packages>
""",
spec -> spec.path("packages.config")
)
);
}

@Test
void csproj() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("Contoso.Utility.SomeOther*", "3.6.2")),
xml(
//language=xml
"""
<Project>
<ItemGroup>
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.1" />
<PackageReference Include="Contoso.Utility.SomeOtherUsefulStuff" Version="3.6.0" />
</ItemGroup>
</Project>
""",
//language=xml
"""
<Project>
<ItemGroup>
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.1" />
<PackageReference Include="Contoso.Utility.SomeOtherUsefulStuff" Version="3.6.2" />
</ItemGroup>
</Project>
""",
spec -> spec.path("MyFirst.csproj")
)
);
}
}
Loading