Skip to content

Commit

Permalink
DRYness: consolidate duplicated code
Browse files Browse the repository at this point in the history
I've added a dependency on devtools-common from all of the extension build plugins, so that I can eliminate duplicated code I introduced in the last changeset.

The implementation is just copied across from the duplicated versions. I rewrote the test to use webcompere system stubs, for more control and more consistent behaviour. (And avoiding irritating CI-only failures.)

I left bootstrap out of the changeset to avoid a circular dependency. Since I removed the changes to the bootstrap bom, which is the parent of the extension-maven-plugin, so I need to set the version manually for that plugin.
  • Loading branch information
holly-cummins committed Oct 21, 2022
1 parent 45db96a commit dafa8d2
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 51 deletions.
1 change: 1 addition & 0 deletions devtools/gradle/gradle-extension-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
implementation platform("io.quarkus:quarkus-bom:${version}")
implementation project(":gradle-model")
implementation "io.quarkus:quarkus-core-deployment"
implementation "io.quarkus:quarkus-devtools-common"
implementation "io.quarkus:quarkus-bootstrap-gradle-resolver"
implementation "com.fasterxml.jackson.core:jackson-databind"
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml"
Expand Down
4 changes: 4 additions & 0 deletions devtools/gradle/gradle-extension-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-devtools-common</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -39,6 +38,7 @@
import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.bootstrap.model.ApplicationModelBuilder;
import io.quarkus.devtools.project.extensions.ScmInfoProvider;
import io.quarkus.extension.gradle.QuarkusExtensionConfiguration;
import io.quarkus.extension.gradle.dsl.Capability;
import io.quarkus.extension.gradle.dsl.RemovedResource;
Expand Down Expand Up @@ -311,7 +311,7 @@ private void computeArtifactCoords(ObjectNode extObject) {
}

private void computeSourceLocation(ObjectNode extObject) {
Map<String, String> repo = getSourceRepo();
Map<String, String> repo = ScmInfoProvider.getSourceRepo();
if (repo != null) {
ObjectNode scm = extObject.putObject("scm");
for (Map.Entry<String, String> e : repo.entrySet()) {
Expand All @@ -321,22 +321,6 @@ private void computeSourceLocation(ObjectNode extObject) {
}
}

static Map<String, String> getSourceRepo() {
// We could try and parse the .git/config file, but that will be fragile
// Let's assume we only care about the repo for official-ish builds produced via github actions
String repo = System.getenv("GITHUB_REPOSITORY");
if (repo != null) {
Map info = new HashMap();
String qualifiedRepo = "https://github.com/" + repo;
// Don't try and guess where slashes will be, just deal with any double slashes by brute force
qualifiedRepo = qualifiedRepo.replace("github.com//", "github.com/");

info.put("url", qualifiedRepo);
return info;
}
return null;
}

private void computeQuarkusCoreVersion(ObjectNode extObject) {
String coreVersion = getQuarkusCoreVersionOrNull();
if (coreVersion != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void shouldGenerateDescriptorWithCapabilities() throws IOException {
* that is increasingly hard to do on Java 17+; see https://github.com/junit-pioneer/junit-pioneer/issues/509
*/
@Test
public void shouldGenerateSourcePointer() throws IOException {
public void shouldGenerateScmInformation() throws IOException {
TestUtils.writeFile(buildFile, TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList(), ""));
File metaInfDir = new File(testProjectDir, "src/main/resources/META-INF");
metaInfDir.mkdirs();
Expand Down
11 changes: 11 additions & 0 deletions independent-projects/extension-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bootstrap-maven-resolver</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-devtools-common</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-devtools-codestarts</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import io.quarkus.bootstrap.resolver.maven.workspace.LocalProject;
import io.quarkus.bootstrap.resolver.maven.workspace.LocalWorkspace;
import io.quarkus.bootstrap.util.DependencyUtils;
import io.quarkus.devtools.project.extensions.ScmInfoProvider;
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.maven.capabilities.CapabilitiesConfig;
import io.quarkus.maven.capabilities.CapabilityConfig;
Expand Down Expand Up @@ -574,22 +575,6 @@ static String getCodestartArtifact(String originalArtifact, String projectVersio
return originalArtifact.replace("${project.version}", projectVersion);
}

static Map<String, String> getSourceRepo() {
// We could try and parse the .git/config file, but that will be fragile
// Let's assume we only care about the repo for official-ish builds produced via github actions
String repo = System.getenv("GITHUB_REPOSITORY");
if (repo != null) {
Map info = new HashMap();
String qualifiedRepo = "https://github.com/" + repo;
// Don't try and guess where slashes will be, just deal with any double slashes by brute force
qualifiedRepo = qualifiedRepo.replace("github.com//", "github.com/");

info.put("url", qualifiedRepo);
return info;
}
return null;
}

private static JsonNode getJsonElement(ObjectNode extObject, String... elements) {
JsonNode mvalue = extObject.get(elements[0]);
int i = 1;
Expand Down Expand Up @@ -689,7 +674,7 @@ public boolean visitLeave(DependencyNode node) {
}

private void addSource(ObjectNode extObject) throws MojoExecutionException {
Map<String, String> repo = getSourceRepo();
Map<String, String> repo = ScmInfoProvider.getSourceRepo();
if (repo != null) {
ObjectNode scm = extObject.putObject("scm");
for (Map.Entry<String, String> e : repo.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.quarkus.maven;

import static io.quarkus.maven.ExtensionDescriptorMojo.getCodestartArtifact;
import static io.quarkus.maven.ExtensionDescriptorMojo.getSourceRepo;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
Expand Down Expand Up @@ -104,7 +102,6 @@ public void shouldCreateMetadata()
// Lazily test that the scm is there but is an object
assertYamlContainsObject(fileContents, "scm");
assertYamlContains(fileContents, "url", "https://github.com/some/repo");

}

}
Expand Down Expand Up @@ -206,18 +203,6 @@ public void testGetCodestartArtifact() {
getCodestartArtifact("io.quarkus:my-ext:${project.version}", "999-SN"));
}

@Test
void testGetSourceControlCoordinates() {
// From maven this property should be set, running in an IDE it won't be unless specially configured
if (System.getenv("GITHUB_REPOSITORY") != null) {
Map repo = getSourceRepo();
assertNotNull(repo);
assertTrue(repo.get("url").toString().matches("https://github.com/some/repo"));
} else {
assertNull(getSourceRepo());
}
}

private ExtensionDescriptorMojo makeMojo(String dirName) throws Exception {
File basedir = getTestFile(TEST_RESOURCES + dirName);
File pom = new File(basedir, "pom.xml");
Expand Down
6 changes: 6 additions & 0 deletions independent-projects/tools/devtools-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<scope>test</scope>
<version>2.0.1</version>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.devtools.project.extensions;

import java.util.HashMap;
import java.util.Map;

public class ScmInfoProvider {

public static Map<String, String> getSourceRepo() {
// We could try and parse the .git/config file, but that will be fragile
// Let's assume we only care about the repo for official-ish builds produced via github actions
String repo = System.getenv("GITHUB_REPOSITORY");
if (repo != null) {
Map info = new HashMap();
String qualifiedRepo = "https://github.com/" + repo;
// Don't try and guess where slashes will be, just deal with any double slashes by brute force
qualifiedRepo = qualifiedRepo.replace("github.com//", "github.com/");

info.put("url", qualifiedRepo);
return info;
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.quarkus.devtools.project.extensions;

import static io.quarkus.devtools.project.extensions.ScmInfoProvider.getSourceRepo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

@ExtendWith(SystemStubsExtension.class)
public class ScmInfoProviderTest {

@SystemStub
private EnvironmentVariables environment;

@BeforeEach
public void setUp() {
environment.set("GITHUB_REPOSITORY", null);
}

@Test
public void shouldReturnNullWhenNoEnvironmentOrBuildConfigIsPresent() {
Map scm = getSourceRepo();
// We shouldn't throw an exception or get upset, we should just quietly return null
assertNull(scm);
}

@Test
void testGetSourceControlCoordinates() {
String repoName = "org/place";
environment.set("GITHUB_REPOSITORY", repoName);
Map repo = getSourceRepo();
assertNotNull(repo);
assertEquals(repo.get("url").toString(), "https://github.com/org/place");
}
}

0 comments on commit dafa8d2

Please sign in to comment.