Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into feature/ChangeVersionValue
Browse files Browse the repository at this point in the history
  • Loading branch information
marcel-gepardec committed Jul 26, 2024
2 parents 913be28 + 84f5858 commit 1b93b20
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public InMemoryLargeSourceSet(List<SourceFile> ls) {
}

protected InMemoryLargeSourceSet(@Nullable InMemoryLargeSourceSet initialState,
@Nullable Map<SourceFile, List<Recipe>> deletions,
List<SourceFile> ls) {
@Nullable Map<SourceFile, List<Recipe>> deletions,
List<SourceFile> ls) {
this.initialState = initialState;
this.ls = ls;
this.deletions = deletions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableSet;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.Recipe.PANIC;

Expand Down Expand Up @@ -105,23 +105,23 @@ public LSS scanSources(LSS sourceSet) {
}

public LSS generateSources(LSS sourceSet) {
List<SourceFile> generatedInThisCycle = allRecipeStack.reduce(sourceSet, recipe, ctx, (acc, recipeStack) -> {
Set<SourceFile> generatedInThisCycle = allRecipeStack.reduce(sourceSet, recipe, ctx, (acc, recipeStack) -> {
Recipe recipe = recipeStack.peek();
if (recipe instanceof ScanningRecipe) {
//noinspection unchecked
ScanningRecipe<Object> scanningRecipe = (ScanningRecipe<Object>) recipe;
List<SourceFile> generated = new ArrayList<>(scanningRecipe.generate(scanningRecipe.getAccumulator(rootCursor, ctx), unmodifiableList(acc), ctx));
List<SourceFile> generated = new ArrayList<>(scanningRecipe.generate(scanningRecipe.getAccumulator(rootCursor, ctx), unmodifiableSet(acc), ctx));
generated.replaceAll(source -> addRecipesThatMadeChanges(recipeStack, source));
acc.addAll(generated);
if (!generated.isEmpty()) {
madeChangesInThisCycle.add(recipe);
}
}
return acc;
}, new ArrayList<>());
}, new TreeSet<>(Comparator.comparing(SourceFile::getSourcePath)));

// noinspection unchecked
return (LSS) sourceSet.generate(generatedInThisCycle);
return (LSS) sourceSet.generate(new ArrayList<>(generatedInThisCycle));
}

public LSS editSources(LSS sourceSet) {
Expand Down Expand Up @@ -238,7 +238,7 @@ private void recordSourceFileResult(@Nullable String beforePath, @Nullable Strin
}

private @Nullable SourceFile handleError(Recipe recipe, SourceFile sourceFile, @Nullable SourceFile after,
Throwable t) {
Throwable t) {
ctx.getOnError().accept(t);

if (t instanceof RecipeRunException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public static Validated<ExactVersion> build(String pattern) {
}
String versionOnly;
int hyphenIndex = pattern.indexOf('-');
if(hyphenIndex == -1) {
if (hyphenIndex == -1) {
versionOnly = pattern;
} else {
versionOnly = pattern.substring(0, hyphenIndex);
}
if(versionOnly.startsWith("latest") ||
versionOnly.contains("x") ||
versionOnly.contains("^") ||
versionOnly.contains("~") ||
versionOnly.contains(" ")) {
if (versionOnly.startsWith("latest") ||
versionOnly.contains("x") ||
versionOnly.contains("^") ||
versionOnly.contains("~") ||
versionOnly.contains(" ")) {
return Validated.invalid("exactVersion", pattern, "not an exact version number");
}
return Validated.valid("exactVersion", new ExactVersion(pattern));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.semver;

import org.openrewrite.Validated;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.Nullable;

public class ExactVersionWithPattern extends LatestRelease {
private final String version;

public ExactVersionWithPattern(String version, String metadataPattern) {
super(metadataPattern);
this.version = version;
}

@Override
public boolean isValid(@Nullable String currentVersion, String version) {
return super.isValid(currentVersion, version) &&
super.compare(currentVersion, version, this.version) == 0;
}

public static Validated<ExactVersionWithPattern> build(String toVersion, @Nullable String metadataPattern) {
if (StringUtils.isBlank(metadataPattern)) {
return Validated.invalid("exactVersionWithPattern", metadataPattern, "metadataPattern is null or empty");
}
if (ExactVersion.build(toVersion).isInvalid()) {
return Validated.invalid("exactVersionWithPattern", toVersion, "not an exact version number");
}
return Validated.valid("exactVersionWithPattern", new ExactVersionWithPattern(toVersion, metadataPattern));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static Validated<VersionComparator> validate(String toVersion, @Nullable
.or(TildeRange.build(toVersion, metadataPattern))
.or(CaretRange.build(toVersion, metadataPattern))
.or(SetRange.build(toVersion, metadataPattern))
.or(ExactVersionWithPattern.build(toVersion, metadataPattern))
.or(ExactVersion.build(toVersion))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean created) {
@Override
public SourceFile visit(@Nullable Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(sourceFile.getSourcePath())) {
if (Boolean.TRUE.equals(overwriteExisting) && path.equals(sourceFile.getSourcePath())) {
if (sourceFile instanceof PlainText) {
return ((PlainText) sourceFile).withText(fileContents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,4 +922,76 @@ void isAcceptable(){
sourceFile = PropertiesParser.builder().build().parse("guavaVersion=29.0-jre").findFirst().orElseThrow();
assertThat(visitor.isAcceptable(sourceFile, new InMemoryExecutionContext())).isTrue();
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4333")
void exactVersionWithExactPattern() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("com.google.guava", "guava", "32.1.1", "-jre")),
buildGradle(
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
implementation('com.google.guava:guava:29.0-jre')
}
""",
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
implementation('com.google.guava:guava:32.1.1-jre')
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4333")
void exactVersionWithRegexPattern() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("com.google.guava", "guava", "32.1.1", ".*droid")),
buildGradle(
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
implementation('com.google.guava:guava:29.0-android')
}
""",
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
implementation('com.google.guava:guava:32.1.1-android')
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1833,4 +1833,42 @@ void multipleRetainVersions() {
);
}
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4333")
void exactVersionWithPattern() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("com.google.guava", "guava", "29.0", "-jre", null, null)),
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0-jre</version>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
</dependencies>
</project>
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

import java.nio.file.Path;

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

Expand Down Expand Up @@ -128,4 +130,29 @@ void shouldOverrideDifferentSourceFileType() {
)
);
}

@Test
void shouldNotOverrideIfCreatedInSameRun() {
rewriteRun(
spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.test.CreateTextFileDuplication
displayName: Create Text File test recipe
description: Create Text File test recipe.
recipeList:
- org.openrewrite.text.CreateTextFile:
relativeFileName: duplicate.txt
overwriteExisting: false
fileContents: |
hi
- org.openrewrite.text.CreateTextFile:
relativeFileName: duplicate.txt
overwriteExisting: false
fileContents: |
hello
""", "org.openrewrite.test.CreateTextFileDuplication"),
text(null, "hi", spec -> spec.path(Path.of("duplicate.txt")))
);
}
}

0 comments on commit 1b93b20

Please sign in to comment.