Skip to content

Commit

Permalink
Add support for Gradle Kotlin DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
mgorniew committed Aug 11, 2020
1 parent 27788be commit 5c260f4
Show file tree
Hide file tree
Showing 55 changed files with 1,357 additions and 219 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkus.gradle;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
Expand All @@ -14,30 +13,17 @@
import org.gradle.tooling.model.eclipse.EclipseExternalDependency;
import org.gradle.tooling.model.eclipse.EclipseProject;

import io.quarkus.devtools.project.buildfile.AbstractGradleBuildFile;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

public class GradleBuildFileFromConnector extends AbstractGradleBuildFile {
public final class ConnectorDependencyResolver {

private List<Dependency> dependencies = null;

public GradleBuildFileFromConnector(final Path projectDirPath, final QuarkusPlatformDescriptor platformDescriptor) {
super(projectDirPath, platformDescriptor);
}

public GradleBuildFileFromConnector(Path projectDirPath, QuarkusPlatformDescriptor platformDescriptor,
Path rootProjectPath) {
super(projectDirPath, platformDescriptor, rootProjectPath);
}

@Override
public List<Dependency> getDependencies() throws IOException {
List<Dependency> getDependencies(String buildContent, Path projectDirPath) {
if (dependencies == null) {
EclipseProject eclipseProject = null;
if (getBuildContent() != null) {
if (buildContent != null) {
try {
ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(getProjectDirPath().toFile())
.forProjectDirectory(projectDirPath.toFile())
.connect();
eclipseProject = connection.getModel(EclipseProject.class);
} catch (BuildException e) {
Expand All @@ -61,13 +47,11 @@ private Dependency gradleModuleVersionToDependency(EclipseExternalDependency eed
Dependency dependency = new Dependency();
if (eed == null || eed.getGradleModuleVersion() == null) {
// local dependencies are ignored
System.err.println("Found null dependency:" + eed);
return null;
}
dependency.setGroupId(eed.getGradleModuleVersion().getGroup());
dependency.setArtifactId(eed.getGradleModuleVersion().getName());
dependency.setVersion(eed.getGradleModuleVersion().getVersion());
return dependency;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.gradle;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import org.apache.maven.model.Dependency;

import io.quarkus.devtools.project.buildfile.AbstractGroovyGradleBuildFile;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

public class GroovyBuildFileFromConnector extends AbstractGroovyGradleBuildFile {

private final ConnectorDependencyResolver dependencyResolver = new ConnectorDependencyResolver();

public GroovyBuildFileFromConnector(final Path projectDirPath, final QuarkusPlatformDescriptor platformDescriptor) {
super(projectDirPath, platformDescriptor);
}

public GroovyBuildFileFromConnector(Path projectDirPath, QuarkusPlatformDescriptor platformDescriptor,
Path rootProjectPath) {
super(projectDirPath, platformDescriptor, rootProjectPath);
}

@Override
public List<Dependency> getDependencies() throws IOException {
return dependencyResolver.getDependencies(getBuildContent(), getProjectDirPath());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.gradle;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import org.apache.maven.model.Dependency;

import io.quarkus.devtools.project.buildfile.AbstractKotlinGradleBuildFile;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

public class KotlinBuildFileFromConnector extends AbstractKotlinGradleBuildFile {

private final ConnectorDependencyResolver dependencyResolver = new ConnectorDependencyResolver();

public KotlinBuildFileFromConnector(Path projectDirPath, QuarkusPlatformDescriptor platformDescriptor) {
super(projectDirPath, platformDescriptor);
}

public KotlinBuildFileFromConnector(Path projectDirPath, QuarkusPlatformDescriptor platformDescriptor,
Path rootProjectPath) {
super(projectDirPath, platformDescriptor, rootProjectPath);
}

@Override
public List<Dependency> getDependencies() throws IOException {
return dependencyResolver.getDependencies(getBuildContent(), getProjectDirPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,7 +16,8 @@

import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.buildfile.BuildFile;
import io.quarkus.gradle.GradleBuildFileFromConnector;
import io.quarkus.gradle.GroovyBuildFileFromConnector;
import io.quarkus.gradle.KotlinBuildFileFromConnector;
import io.quarkus.platform.descriptor.CombinedQuarkusPlatformDescriptor;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
import io.quarkus.platform.descriptor.resolver.json.QuarkusJsonPlatformDescriptorResolver;
Expand Down Expand Up @@ -71,9 +73,16 @@ protected QuarkusPlatformDescriptor platformDescriptor() {
protected BuildFile getGradleBuildFile() {
final Path projectDirPath = getProject().getProjectDir().toPath();
final Path rootProjectPath = getProject().getParent() != null ? getProject().getRootProject().getProjectDir().toPath()
: null;
return new GradleBuildFileFromConnector(projectDirPath, platformDescriptor(),
rootProjectPath);
: projectDirPath;
if (Files.exists(rootProjectPath.resolve("settings.gradle.kts"))
&& Files.exists(projectDirPath.resolve("build.gradle.kts"))) {
return new KotlinBuildFileFromConnector(projectDirPath, platformDescriptor(), rootProjectPath);
} else if (Files.exists(rootProjectPath.resolve("settings.gradle"))
&& Files.exists(projectDirPath.resolve("build.gradle"))) {
return new GroovyBuildFileFromConnector(projectDirPath, platformDescriptor(), rootProjectPath);
}
throw new GradleException(
"Mixed DSL is not supported. Both build and settings file need to use either Kotlin or Groovy DSL");
}

@Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,22 @@
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
abstract class AbstractBuildFileTest {

class GradleBuildFileTest {
abstract String getProperty(String propertyName) throws IOException;

private static GradleBuildFileFromConnector buildFile;

@BeforeAll
public static void beforeAll() throws URISyntaxException {
URL url = GradleBuildFileTest.class.getClassLoader().getResource("gradle-project");
URI uri = url.toURI();
Path gradleProjectPath = Paths.get(uri);
final QuarkusPlatformDescriptor descriptor = Mockito.mock(QuarkusPlatformDescriptor.class);
buildFile = new GradleBuildFileFromConnector(gradleProjectPath, descriptor);
}
abstract List<Dependency> getDependencies() throws IOException;

@Test
void testGetDependencies() throws IOException {
List<Dependency> dependencies = buildFile.getDependencies();
List<Dependency> dependencies = getDependencies();
assertThat(dependencies).isNotEmpty();
List<String> depsString = new ArrayList<>();
for (Iterator<Dependency> depIter = dependencies.iterator(); depIter.hasNext();) {
Expand All @@ -53,13 +37,13 @@ void testGetDependencies() throws IOException {

@Test
void testGetProperty() throws IOException {
assertNull(buildFile.getProperty("toto"));
assertEquals("0.23.2", buildFile.getProperty("quarkusVersion"));
assertNull(getProperty("toto"));
assertEquals("0.23.2", getProperty("quarkusVersion"));
}

@Test
void testGetTestClosure() throws IOException {
assertNull(buildFile.getProperty("test"));
assertNull(getProperty("test"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.gradle;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.junit.jupiter.api.BeforeAll;
import org.mockito.Mockito;

import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

class GroovyBuildFileTest extends AbstractBuildFileTest {

private static GroovyBuildFileFromConnector buildFile;

@BeforeAll
public static void beforeAll() throws URISyntaxException {
URL url = GroovyBuildFileTest.class.getClassLoader().getResource("gradle-project");
URI uri = url.toURI();
Path gradleProjectPath = Paths.get(uri);
final QuarkusPlatformDescriptor descriptor = Mockito.mock(QuarkusPlatformDescriptor.class);
buildFile = new GroovyBuildFileFromConnector(gradleProjectPath, descriptor);
}

@Override
String getProperty(String propertyName) throws IOException {
return buildFile.getProperty(propertyName);
}

@Override
List<Dependency> getDependencies() throws IOException {
return buildFile.getDependencies();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.gradle;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.junit.jupiter.api.BeforeAll;
import org.mockito.Mockito;

import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

class KotlinBuildFileTest extends AbstractBuildFileTest {

private static KotlinBuildFileFromConnector buildFile;

@BeforeAll
public static void beforeAll() throws URISyntaxException {
URL url = KotlinBuildFileTest.class.getClassLoader().getResource("gradle-kts-project");
URI uri = url.toURI();
Path gradleProjectPath = Paths.get(uri);
final QuarkusPlatformDescriptor descriptor = Mockito.mock(QuarkusPlatformDescriptor.class);
buildFile = new KotlinBuildFileFromConnector(gradleProjectPath, descriptor);
}

@Override
String getProperty(String propertyName) throws IOException {
return buildFile.getProperty(propertyName);
}

@Override
List<Dependency> getDependencies() throws IOException {
return buildFile.getDependencies();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
java
}

repositories {
mavenLocal()
mavenCentral()
}

val quarkusVersion: String by project

dependencies {
implementation("io.quarkus:quarkus-jsonp")
implementation("io.quarkus:quarkus-jsonb")
constraints {
implementation("io.quarkus:quarkus-jsonb:0.10.0") {
because("to test constraints")
}
}
implementation(enforcedPlatform("io.quarkus:quarkus-bom:${quarkusVersion}"))
implementation("io.quarkus:quarkus-resteasy")

testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.rest-assured:rest-assured")
}

tasks {
test {
dependsOn("cleanTest")
useJUnitPlatform()

// @NativeImageTest and JVM mode tests can't be mixed in the same run
setForkEvery(1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkusVersion = 0.23.2
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void execute() throws MojoExecutionException {

BuildTool buildToolEnum;
try {
buildToolEnum = BuildTool.valueOf(buildTool.toUpperCase());
buildToolEnum = BuildTool.findTool(buildTool);
} catch (IllegalArgumentException e) {
String validBuildTools = String.join(",",
Arrays.asList(BuildTool.values()).stream().map(BuildTool::toString).collect(Collectors.toList()));
Expand All @@ -204,11 +204,12 @@ public void execute() throws MojoExecutionException {
}

success = createProject.execute().isSuccess();

if (!codestartsEnabled) {
File createdDependenciesBuildFile = new File(projectRoot, buildToolEnum.getDependenciesFile());
if (BuildTool.MAVEN.equals(buildToolEnum)) {
createMavenWrapper(createdDependenciesBuildFile, ToolsUtils.readQuarkusProperties(platform));
} else if (BuildTool.GRADLE.equals(buildToolEnum)) {
} else if (BuildTool.GRADLE.equals(buildToolEnum) || BuildTool.GRADLE_KOTLIN_DSL.equals(buildToolEnum)) {
createGradleWrapper(platform, projectDirPath);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Gradle
.gradle/
build/
Loading

0 comments on commit 5c260f4

Please sign in to comment.