Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Gradle Assertions#withToolingApi to this project
- Loading branch information
1 parent
2b066d7
commit e864bc7
Showing
4 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
model/src/main/java/org/openrewrite/gradle/toolingapi/Assertions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* 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.gradle.toolingapi; | ||
|
||
import org.openrewrite.InMemoryExecutionContext; | ||
import org.openrewrite.SourceFile; | ||
import org.openrewrite.gradle.UpdateGradleWrapper; | ||
import org.openrewrite.gradle.marker.GradleProject; | ||
import org.openrewrite.gradle.marker.GradleSettings; | ||
import org.openrewrite.gradle.util.GradleWrapper; | ||
import org.openrewrite.groovy.tree.G; | ||
import org.openrewrite.internal.lang.Nullable; | ||
import org.openrewrite.marker.OperatingSystemProvenance; | ||
import org.openrewrite.properties.tree.Properties; | ||
import org.openrewrite.test.UncheckedConsumer; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.nio.file.attribute.PosixFilePermission; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class Assertions { | ||
|
||
public static UncheckedConsumer<List<SourceFile>> withToolingApi(@Nullable String version, @Nullable String distribution) { | ||
return sourceFiles -> { | ||
try { | ||
Path tempDirectory = Files.createTempDirectory("project"); | ||
// Usage of Assertions.mavenProject() might result in gradle files inside a subdirectory | ||
Path projectDir = tempDirectory; | ||
try { | ||
for (SourceFile sourceFile : sourceFiles) { | ||
if (sourceFile instanceof G.CompilationUnit) { | ||
G.CompilationUnit g = (G.CompilationUnit) sourceFile; | ||
if (g.getSourcePath().toString().endsWith(".gradle")) { | ||
Path groovyGradle = tempDirectory.resolve(g.getSourcePath()); | ||
if (!tempDirectory.equals(groovyGradle.getParent()) && tempDirectory.equals(groovyGradle.getParent().getParent())) { | ||
projectDir = groovyGradle.getParent(); | ||
} | ||
Files.createDirectories(groovyGradle.getParent()); | ||
Files.write(groovyGradle, g.printAllAsBytes()); | ||
} | ||
} else if (sourceFile instanceof Properties.File) { | ||
Properties.File f = (Properties.File) sourceFile; | ||
if (f.getSourcePath().endsWith("gradle.properties")) { | ||
Path gradleProperties = tempDirectory.resolve(f.getSourcePath()); | ||
if (!tempDirectory.equals(gradleProperties.getParent()) && tempDirectory.equals(gradleProperties.getParent().getParent())) { | ||
projectDir = gradleProperties.getParent(); | ||
} | ||
Files.createDirectories(gradleProperties.getParent()); | ||
Files.write(gradleProperties, f.printAllAsBytes()); | ||
} | ||
} | ||
} | ||
|
||
if (version != null) { | ||
GradleWrapper gradleWrapper = GradleWrapper.create(distribution, version, null, new InMemoryExecutionContext()); | ||
Files.createDirectories(projectDir.resolve("gradle/wrapper/")); | ||
Files.write(projectDir.resolve(GradleWrapper.WRAPPER_PROPERTIES_LOCATION), ("distributionBase=GRADLE_USER_HOME\n" + | ||
"distributionPath=wrapper/dists\n" + | ||
"distributionUrl=" + gradleWrapper.getPropertiesFormattedUrl() + "\n" + | ||
"distributionSha256Sum=" + gradleWrapper.getDistributionChecksum().getHexValue() + "\n" + | ||
"zipStoreBase=GRADLE_USER_HOME\n" + | ||
"zipStorePath=wrapper/dists").getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW); | ||
Files.write(projectDir.resolve(GradleWrapper.WRAPPER_JAR_LOCATION), gradleWrapper.wrapperJar().printAllAsBytes(), StandardOpenOption.CREATE_NEW); | ||
Path gradleSh = projectDir.resolve(GradleWrapper.WRAPPER_SCRIPT_LOCATION); | ||
Files.copy(requireNonNull(UpdateGradleWrapper.class.getResourceAsStream("/gradlew")), gradleSh); | ||
OperatingSystemProvenance current = OperatingSystemProvenance.current(); | ||
if (current.isLinux() || current.isMacOsX()) { | ||
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(gradleSh); | ||
permissions.add(PosixFilePermission.OWNER_EXECUTE); | ||
Files.setPosixFilePermissions(gradleSh, permissions); | ||
} | ||
Files.copy(requireNonNull(UpdateGradleWrapper.class.getResourceAsStream("/gradlew.bat")), projectDir.resolve(GradleWrapper.WRAPPER_BATCH_LOCATION)); | ||
} | ||
|
||
for (int i = 0; i < sourceFiles.size(); i++) { | ||
SourceFile sourceFile = sourceFiles.get(i); | ||
if (sourceFile.getSourcePath().toString().endsWith(".gradle")) { | ||
if (sourceFile.getSourcePath().endsWith("settings.gradle")) { | ||
OpenRewriteModel model = OpenRewriteModelBuilder.forProjectDirectory(tempDirectory.resolve(sourceFile.getSourcePath()).getParent().toFile(), null); | ||
org.openrewrite.gradle.toolingapi.GradleSettings rawSettings = model.gradleSettings(); | ||
if (rawSettings != null) { | ||
GradleSettings gradleSettings = org.openrewrite.gradle.toolingapi.GradleSettings.toMarker(rawSettings); | ||
sourceFiles.set(i, sourceFile.withMarkers(sourceFile.getMarkers().add(gradleSettings))); | ||
} | ||
} else { | ||
OpenRewriteModel model = OpenRewriteModelBuilder.forProjectDirectory(projectDir.toFile(), tempDirectory.resolve(sourceFile.getSourcePath()).toFile()); | ||
GradleProject gradleProject = org.openrewrite.gradle.toolingapi.GradleProject.toMarker(model.gradleProject()); | ||
sourceFiles.set(i, sourceFile.withMarkers(sourceFile.getMarkers().add(gradleProject))); | ||
} | ||
} | ||
} | ||
} finally { | ||
deleteDirectory(tempDirectory.toFile()); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}; | ||
} | ||
|
||
public static UncheckedConsumer<List<SourceFile>> withToolingApi(String version) { | ||
return withToolingApi(version, "bin"); | ||
} | ||
|
||
public static UncheckedConsumer<List<SourceFile>> withToolingApi() { | ||
return withToolingApi(null, null); | ||
} | ||
|
||
private static void deleteDirectory(File directoryToBeDeleted) { | ||
File[] allContents = directoryToBeDeleted.listFiles(); | ||
if (allContents != null) { | ||
for (File file : allContents) { | ||
deleteDirectory(file); | ||
} | ||
} | ||
//noinspection ResultOfMethodCallIgnored | ||
directoryToBeDeleted.delete(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
model/src/test/java/org/openrewrite/gradle/toolingapi/.editorconfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
root = true | ||
|
||
[*.java] | ||
indent_size = 4 | ||
ij_continuation_indent_size = 2 |
44 changes: 44 additions & 0 deletions
44
model/src/test/java/org/openrewrite/gradle/toolingapi/AssertionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.gradle.toolingapi; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.gradle.marker.GradleProject; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.openrewrite.gradle.Assertions.buildGradle; | ||
|
||
public class AssertionsTest implements RewriteTest { | ||
|
||
@Test | ||
void withToolingApi() { | ||
rewriteRun( | ||
spec -> spec.beforeRecipe(Assertions.withToolingApi()), | ||
//language=groovy | ||
buildGradle( | ||
""" | ||
plugins { | ||
id 'java' | ||
} | ||
""", | ||
spec -> spec.afterRecipe(cu -> { | ||
assertThat(cu.getMarkers().findFirst(GradleProject.class)).isPresent(); | ||
}) | ||
) | ||
); | ||
} | ||
} |