Skip to content

Commit

Permalink
Merge pull request #11255 from gastaldi/add_extension
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi authored Aug 7, 2020
2 parents 4a68e9e + c869bc7 commit 34d4129
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class AbstractGradleBuildFile extends BuildFile {

private final Path rootProjectPath;

private AtomicReference<Model> modelReference = new AtomicReference<>();
private final AtomicReference<Model> modelReference = new AtomicReference<>();

public AbstractGradleBuildFile(final Path projectDirPath, final QuarkusPlatformDescriptor platformDescriptor) {
this(projectDirPath, platformDescriptor, null);
Expand Down Expand Up @@ -61,11 +61,29 @@ protected boolean addDependency(AppArtifactCoords coords, boolean managed) {
}

static boolean addDependencyInModel(Model model, AppArtifactCoords coords, boolean managed) {
StringBuilder newDependency = new StringBuilder()
.append(" implementation '")
.append(coords.getGroupId())
.append(":")
.append(coords.getArtifactId());
boolean isBOM = "pom".equals(coords.getType());
StringBuilder newDependency;
if (isBOM) {
// Check if BOM is not included already
String resolvedPlatform = String
.format("%s:%s", getProperty(model, "quarkusPlatformGroupId"),
getProperty(model, "quarkusPlatformArtifactId"));
String thisBOM = String.format("%s:%s", coords.getGroupId(), coords.getArtifactId());
if (thisBOM.equals(resolvedPlatform)) {
// BOM matches the platform, no need to do anything
return false;
}
newDependency = new StringBuilder()
.append(" implementation enforcedPlatform(\"")
.append(thisBOM).append(":").append(coords.getVersion())
.append("\")'");
} else {
newDependency = new StringBuilder()
.append(" implementation '")
.append(coords.getGroupId())
.append(":")
.append(coords.getArtifactId());
}
if (!managed &&
(coords.getVersion() != null && !coords.getVersion().isEmpty())) {
newDependency.append(":").append(coords.getVersion());
Expand Down Expand Up @@ -109,18 +127,22 @@ protected void removeDependency(AppArtifactKey key) {

@Override
public String getProperty(String propertyName) {
final String property = getModel().getPropertiesContent().getProperty(propertyName);
if (property != null || getModel().getRootPropertiesContent() == null) {
return property;
}
return getModel().getRootPropertiesContent().getProperty(propertyName);
return getProperty(getModel(), propertyName);
}

@Override
public BuildTool getBuildTool() {
return BuildTool.GRADLE;
}

static String getProperty(Model model, String propertyName) {
final String property = model.getPropertiesContent().getProperty(propertyName);
if (property != null || model.getRootPropertiesContent() == null) {
return property;
}
return model.getRootPropertiesContent().getProperty(propertyName);
}

private Model getModel() {
return modelReference.updateAndGet(model -> {
if (model == null) {
Expand All @@ -139,7 +161,7 @@ protected void refreshData() {
this.modelReference.set(null);
}

private boolean hasRootProjectFile(final String fileName) throws IOException {
private boolean hasRootProjectFile(final String fileName) {
if (rootProjectPath == null) {
return false;
}
Expand Down Expand Up @@ -185,17 +207,17 @@ private Model readModel() throws IOException {
return new Model(settingsContent, buildContent, propertiesContent, rootSettingsContent, rootPropertiesContent);
}

protected String getBuildContent() throws IOException {
protected String getBuildContent() {
return getModel().getBuildContent();
}

static class Model {
private String settingsContent;
private String buildContent;
private Properties propertiesContent;
private final Properties propertiesContent;

private String rootSettingsContent;
private Properties rootPropertiesContent;
private final String rootSettingsContent;
private final Properties rootPropertiesContent;

public Model(String settingsContent, String buildContent, Properties propertiesContent, String rootSettingsContent,
Properties rootPropertiesContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public void testAddAndRemoveExtension() throws IOException, URISyntaxException,

final Path build = projectDir.toPath().resolve("build.gradle");
assertThat(build).exists();
assertThat(new String(Files.readAllBytes(build))).contains("implementation 'io.quarkus:quarkus-hibernate-orm'");
assertThat(new String(Files.readAllBytes(build)))
.contains("implementation 'io.quarkus:quarkus-hibernate-orm'")
.doesNotContain("implementation enforcedPlatform('io.quarkus:quarkus-bom:")
.doesNotContain("implementation 'io.quarkus:quarkus-bom:");

runGradleWrapper(projectDir, ":removeExtension", "--extensions=hibernate-orm");
assertThat(new String(Files.readAllBytes(build))).doesNotContain("implementation 'io.quarkus:quarkus-hibernate-orm'");
Expand Down

0 comments on commit 34d4129

Please sign in to comment.