Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NCL4738 - Simplify code (ncl4839 incorrect reference) #161

Merged
merged 5 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ ObjectStore
*.jps
*.hprof
/.nb-gradle/

4 changes: 0 additions & 4 deletions analyzer/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
group = "org.jboss.gm.analyzer"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
geoand marked this conversation as resolved.
Show resolved Hide resolved
}

pluginBundle {
website = "https://project-ncl.github.io/gradle-manipulator/"
vcsUrl = "https://github.com/project-ncl/gradle-manipulator/tree/master/analyzer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.Collection;

Expand Down Expand Up @@ -116,11 +117,32 @@ public void ensureMissingGroupIdThrowsException() throws IOException, URISyntaxE
//noinspection ConstantConditions
org.apache.commons.io.FileUtils.copyDirectory(Paths
.get(TestUtils.class.getClassLoader().getResource(projectRoot.getName()).toURI()).toFile(), projectRoot);
// Remove the settings so we can't use a child project to establish groupId
//noinspection ResultOfMethodCallIgnored
new File(projectRoot, "settings.gradle").delete();
// Write a version so this error isn't found first.
org.apache.commons.io.FileUtils.writeStringToFile(
new File(projectRoot, "gradle.properties"), " version = \"1.1.2\"\n", Charset.defaultCharset());

assertThatExceptionOfType(ManipulationUncheckedException.class)
.isThrownBy(() -> TestUtils.align(projectRoot, true))
.withMessageContaining("Empty groupId but unable to determine a suitable replacement from any child modules");
}

@Test
public void ensureMissingVersionThrowsException() throws IOException, URISyntaxException {

final File projectRoot = tempDir.newFolder("grpc-like-layout");

//noinspection ConstantConditions
org.apache.commons.io.FileUtils.copyDirectory(Paths
.get(TestUtils.class.getClassLoader().getResource(projectRoot.getName()).toURI()).toFile(), projectRoot);
// Remove the settings so we can't use a child project to establish version
//noinspection ResultOfMethodCallIgnored
new File(projectRoot, "settings.gradle").delete();

assertThatExceptionOfType(ManipulationUncheckedException.class)
.isThrownBy(() -> TestUtils.align(projectRoot, true))
.withMessageContaining("Unable to find suitable project version");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.jboss.gm.analyzer.alignment;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.commonjava.maven.atlas.ident.ref.ProjectRef;
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.ext.common.ManipulationException;
import org.commonjava.maven.ext.common.ManipulationUncheckedException;

/**
* Used by {@link org.jboss.gm.analyzer.alignment.AlignmentTask} in order to perform the alignment
Expand All @@ -13,7 +18,7 @@
*/
public interface AlignmentService {

Response align(Request request);
Response align(Request request) throws ManipulationException;

/**
* Contains both the collected project dependencies GAVs and the project GAV.
Expand Down Expand Up @@ -41,13 +46,55 @@ List<ProjectVersionRef> getProject() {
}
}

interface Response {
/**
* Contains the resulting aligned dependencies from the dependency analyzer. It will be processed further
* by the response customizers such as DependencyOverride, ProjectVersionOverride.
*/
class Response {
private final Map<ProjectVersionRef, String> translationMap;
Map<ProjectRef, String> overrideMap;
String newProjectVersion;

// Only used by tests.
Response() {
this(new HashMap<>());
}

String getNewProjectVersion();
Response(Map<ProjectVersionRef, String> translationMap) {
this.translationMap = translationMap;
}

Map<ProjectVersionRef, String> getTranslationMap();
void setNewProjectVersion(String version) {
newProjectVersion = version;
}

void setOverrideMap(Map<ProjectRef, String> overrideMap) {
this.overrideMap = overrideMap;
}

String getNewProjectVersion() {
return newProjectVersion;
}

Map<ProjectVersionRef, String> getTranslationMap() {
return translationMap;
}

String getAlignedVersionOfGav(ProjectVersionRef gav) {
final Optional<ProjectRef> projectRef = matchingProjectRef(gav);
if (projectRef.isPresent()) {
return overrideMap.get(projectRef.get());
}
if (translationMap == null) {
throw new ManipulationUncheckedException("Translation map has not been initialised");
}
return translationMap.get(gav);
}

String getAlignedVersionOfGav(ProjectVersionRef gav);
private Optional<ProjectRef> matchingProjectRef(ProjectRef gav) {
return overrideMap == null ? Optional.empty()
: overrideMap.keySet().stream().filter(p -> p.matches(gav)).findFirst();
}
}

/**
Expand All @@ -66,19 +113,6 @@ interface RequestCustomizer {
default int order() {
return 0;
}

RequestCustomizer NOOP = new RequestCustomizer() {
@Override
public Request customize(Request request) {
return request;
}

@Override
public int order() {
return Integer.MAX_VALUE;
}
};

}

/**
Expand All @@ -90,26 +124,13 @@ public int order() {
*/
interface ResponseCustomizer {

Response customize(Response response);
Response customize(Response response) throws ManipulationException;

// Integer.MIN_VALUE is the max order. This means that if we have 2 services for example
// we with the first one to be invoked before the second, we would give the first one a
// value for order that is smaller than that of the second one (for example 0 and 10)
default int order() {
return 0;
}

ResponseCustomizer NOOP = new ResponseCustomizer() {
@Override
public Response customize(Response response) {
return response;
}

@Override
public int order() {
return Integer.MAX_VALUE;
}
};

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.jboss.gm.analyzer.alignment;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.aeonbits.owner.ConfigCache;
import org.gradle.api.Project;
import org.jboss.gm.analyzer.alignment.AlignmentService.RequestCustomizer;
import org.jboss.gm.analyzer.alignment.AlignmentService.ResponseCustomizer;
import org.jboss.gm.common.Configuration;

/**
Expand All @@ -18,25 +20,31 @@ final class AlignmentServiceFactory {
private AlignmentServiceFactory() {
}

static AlignmentService getAlignmentService(Set<Project> projects) {
Configuration configuration = ConfigCache.getOrCreate(Configuration.class);

static AlignmentService getAlignmentService(Configuration configuration, Set<Project> projects) {
return new WithCustomizersDelegatingAlignmentService(new DAAlignmentService(configuration),
getRequestCustomizers(configuration, projects),
getResponseCustomizers(configuration, projects));
}

private static List<AlignmentService.RequestCustomizer> getRequestCustomizers(Configuration configuration,
/**
* Creates the request customizers.
* Currently only a single one exists so in theory we could eliminate the List here. Keeping it for consistency
* with the response side and potential future expansion.
*
* @param configuration the current Configuration.
* @param projects the current Projects
* @return the list of Request Customizers.
*/
private static List<RequestCustomizer> getRequestCustomizers(Configuration configuration,
Set<Project> projects) {
return Collections
.singletonList(DependencyExclusionCustomizer.fromConfigurationForModule(configuration,
projects));
return Stream.of(DependencyExclusionCustomizer.fromConfigurationForModule(configuration,
projects)).filter(Objects::nonNull).collect(Collectors.toList());
}

private static List<AlignmentService.ResponseCustomizer> getResponseCustomizers(Configuration configuration,
private static List<ResponseCustomizer> getResponseCustomizers(Configuration configuration,
Set<Project> projects) {
return Arrays.asList(
DependencyOverrideCustomizer.fromConfigurationForModule(configuration, projects),
new UpdateProjectVersionCustomizer(projects, configuration));
return Stream.of(DependencyOverrideCustomizer.fromConfigurationForModule(configuration, projects),
new UpdateProjectVersionCustomizer(projects, configuration)).filter(Objects::nonNull)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.gradle.api.internal.artifacts.ivyservice.resolutionstrategy.DefaultResolutionStrategy;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.jboss.gm.analyzer.alignment.AlignmentService.Response;
import org.jboss.gm.analyzer.alignment.groovy.GMEBaseScript;
import org.jboss.gm.analyzer.alignment.io.LockFileIO;
import org.jboss.gm.analyzer.alignment.io.RepositoryExporter;
Expand Down Expand Up @@ -129,14 +130,14 @@ public void perform() {

// when the set is empty, we know that this was the last alignment task to execute.
if (cache.removeProject(projectName)) {
logger.info("Completed scanning projects; now processing for REST...");
logger.info("Completed scanning {} projects; now processing for REST...", cache.getDependencies().size());
Collection<ProjectVersionRef> allDeps = cache.getDependencies().values().stream()
.flatMap(m -> m.values().stream()).distinct().collect(Collectors.toList());

final AlignmentService alignmentService = AlignmentServiceFactory
.getAlignmentService(cache.getDependencies().keySet());
.getAlignmentService(configuration, cache.getDependencies().keySet());

final AlignmentService.Response alignmentResponse = alignmentService.align(
final Response alignmentResponse = alignmentService.align(
new AlignmentService.Request(cache.getGAV().stream()
.map(p -> new SimpleProjectVersionRef(p.asProjectRef(),
!configuration.versionSuffixSnapshot() ? Version.removeSnapshot(p.getVersionString())
Expand Down Expand Up @@ -470,7 +471,7 @@ private void updateModuleDynamicDependencies(ManipulationModel correspondingModu

private void updateModuleDependencies(ManipulationModel correspondingModule,
HashMap<RelaxedProjectVersionRef, ProjectVersionRef> allModuleDependencies,
AlignmentService.Response alignmentResponse) {
Response alignmentResponse) {

allModuleDependencies.forEach((d, p) -> {
final String newDependencyVersion = alignmentResponse.getAlignedVersionOfGav(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public DAAlignmentService(Configuration configuration) {
}

@Override
public Response align(Request request) {
public Response align(AlignmentService.Request request) {
final List<ProjectVersionRef> translateRequest = new ArrayList<>(request.getDependencies().size() + 1);

if (dependencySource == NONE) {
logger.warn("No dependencySource configured ; unable to call endpoint");
return new Response(request.getProject(), Collections.emptyMap());
return new Response(Collections.emptyMap());
}

translateRequest.addAll(request.getProject());
Expand All @@ -70,39 +70,14 @@ public Response align(Request request) {
final Map<ProjectVersionRef, String> translationMap = restEndpoint.translateVersions(translateRequest);
logger.info("REST Client returned {} ", translationMap);

return new Response(request.getProject(), translationMap);
}

private static class Response implements AlignmentService.Response {

private final Logger logger = GMLogger.getLogger(getClass());

private final List<ProjectVersionRef> refOfProject;
private final Map<ProjectVersionRef, String> translationMap;
Response result = new Response(translationMap);

Response(List<ProjectVersionRef> refOfProject, Map<ProjectVersionRef, String> translationMap) {
this.refOfProject = refOfProject;
this.translationMap = translationMap;
}

// TODO: Verify this is safe - do we need to find the highest projectref version?
// TODO: When is this used / called ?
@Override
public String getNewProjectVersion() {
logger.info("Retrieving project version {} and returning {} ", refOfProject.get(0),
translationMap.get(refOfProject.get(0)));
return translationMap.get(refOfProject.get(0));
}

@Override
public Map<ProjectVersionRef, String> getTranslationMap() {
return translationMap;
}

@Override
public String getAlignedVersionOfGav(ProjectVersionRef gav) {
return translationMap.get(gav);
if (!request.getProject().isEmpty()) {
logger.info("Retrieving project version {} and returning {} ", request.getProject().get(0),
translationMap.get(request.getProject().get(0)));
result.setNewProjectVersion(translationMap.get(request.getProject().get(0)));
}
return result;
}

static class GradleDefaultTranslator extends DefaultTranslator {
Expand Down
Loading