Skip to content

Commit

Permalink
Add project description diff (gh-1005)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbo-indeed committed Oct 27, 2019
1 parent 99b0a4b commit c933acd
Show file tree
Hide file tree
Showing 16 changed files with 797 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ public class MutableProjectDescription implements ProjectDescription {

private String baseDirectory;

public MutableProjectDescription() {
super();
}

protected MutableProjectDescription(final MutableProjectDescription source) {
super();
setPlatformVersion(source.getPlatformVersion());
setBuildSystem(source.getBuildSystem());
setPackaging(source.getPackaging());
setLanguage(source.getLanguage());
source.getRequestedDependencies().forEach(this::addDependency);
setGroupId(source.getGroupId());
setArtifactId(source.getArtifactId());
setVersion(source.getVersion());
setName(source.getName());
setDescription(source.getDescription());
setApplicationName(source.getApplicationName());
setPackageName(source.getPackageName());
setBaseDirectory(source.getBaseDirectory());
}

@Override
public MutableProjectDescription createCopy() {
return new MutableProjectDescription(this);
}

@Override
public Version getPlatformVersion() {
return this.platformVersion;
}
Expand All @@ -69,6 +96,7 @@ public void setPlatformVersion(Version platformVersion) {
this.platformVersion = platformVersion;
}

@Override
public BuildSystem getBuildSystem() {
return this.buildSystem;
}
Expand All @@ -77,6 +105,7 @@ public void setBuildSystem(BuildSystem buildSystem) {
this.buildSystem = buildSystem;
}

@Override
public Packaging getPackaging() {
return this.packaging;
}
Expand All @@ -85,6 +114,7 @@ public void setPackaging(Packaging packaging) {
this.packaging = packaging;
}

@Override
public Language getLanguage() {
return this.language;
}
Expand All @@ -101,10 +131,12 @@ public Dependency addDependency(String id, Dependency.Builder<?> builder) {
return addDependency(id, builder.build());
}

@Override
public Map<String, Dependency> getRequestedDependencies() {
return Collections.unmodifiableMap(this.requestedDependencies);
}

@Override
public String getGroupId() {
return this.groupId;
}
Expand All @@ -113,6 +145,7 @@ public void setGroupId(String groupId) {
this.groupId = groupId;
}

@Override
public String getArtifactId() {
return this.artifactId;
}
Expand All @@ -121,6 +154,7 @@ public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}

@Override
public String getVersion() {
return this.version;
}
Expand All @@ -129,6 +163,7 @@ public void setVersion(String version) {
this.version = version;
}

@Override
public String getName() {
return this.name;
}
Expand All @@ -137,6 +172,7 @@ public void setName(String name) {
this.name = name;
}

@Override
public String getDescription() {
return this.description;
}
Expand All @@ -145,6 +181,7 @@ public void setDescription(String description) {
this.description = description;
}

@Override
public String getApplicationName() {
return this.applicationName;
}
Expand All @@ -153,6 +190,7 @@ public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}

@Override
public String getPackageName() {
if (StringUtils.hasText(this.packageName)) {
return this.packageName;
Expand All @@ -167,6 +205,7 @@ public void setPackageName(String packageName) {
this.packageName = packageName;
}

@Override
public String getBaseDirectory() {
return this.baseDirectory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ public interface ProjectDescription {
*/
String getBaseDirectory();

/**
* ProjectDescription implementations should implement this to create a copy of
* themselves. However, the default implementation throws
* UnsupportedOperationException.
* @return never
* @throws UnsupportedOperationException always
*/
default ProjectDescription createCopy() {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

import io.spring.initializr.generator.project.diff.DefaultProjectDescriptionDiffFactory;
import io.spring.initializr.generator.project.diff.ProjectDescriptionDiff;
import io.spring.initializr.generator.project.diff.ProjectDescriptionDiffFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportSelector;
Expand All @@ -40,6 +46,8 @@
*/
public class ProjectGenerator {

private static final Log logger = LogFactory.getLog(ProjectGenerator.class);

private final Consumer<ProjectGenerationContext> contextConsumer;

private final Supplier<? extends ProjectGenerationContext> contextFactory;
Expand Down Expand Up @@ -119,9 +127,20 @@ public <T> T generate(ProjectDescription description, ProjectAssetGenerator<T> p

private Supplier<ProjectDescription> resolve(ProjectDescription description, ProjectGenerationContext context) {
return () -> {

if (description instanceof MutableProjectDescription) {

final MutableProjectDescription mutableProjectDesc = (MutableProjectDescription) description;

// Find the diff factory and use it to create and register a diff bean
final ProjectDescriptionDiffFactory diffFactory = context
.getBeanProvider(ProjectDescriptionDiffFactory.class)
.getIfAvailable(() -> new DefaultProjectDescriptionDiffFactory());
final ProjectDescriptionDiff diff = diffFactory.create(mutableProjectDesc);
context.registerBean(ProjectDescriptionDiff.class, () -> diff);

context.getBeanProvider(ProjectDescriptionCustomizer.class).orderedStream()
.forEach((customizer) -> customizer.customize((MutableProjectDescription) description));
.forEach((customizer) -> customizer.customize(mutableProjectDesc));
}
return description;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.spring.initializr.generator.project.diff;

import io.spring.initializr.generator.project.ProjectDescription;

/**
* A default {@link ProjectDescriptionDiffFactory} implementation that creates default
* {@link ProjectDescriptionDiff} instances.
*
* @author Chris Bono
*/
public class DefaultProjectDescriptionDiffFactory implements ProjectDescriptionDiffFactory<ProjectDescription> {

@Override
public ProjectDescriptionDiff create(final ProjectDescription description) {
return new ProjectDescriptionDiff(description);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.spring.initializr.generator.project.diff;

import java.lang.reflect.Field;
import java.util.Objects;
import java.util.function.BiConsumer;

import io.spring.initializr.generator.buildsystem.BuildSystem;
import io.spring.initializr.generator.language.Language;
import io.spring.initializr.generator.packaging.Packaging;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.version.Version;

import org.springframework.util.ReflectionUtils;

/**
* Provides a convenient API for determining if certain fields on a
* {@link ProjectDescription} were modified.
*
* @author Chris Bono
*/
public class ProjectDescriptionDiff {

private final ProjectDescription original;

/**
* Construct a {@link ProjectDescriptionDiff} that creates and uses a copy of the
* specified description as its source.
* @param original the description to copy as the source
*/
public ProjectDescriptionDiff(final ProjectDescription original) {
this.original = original.createCopy();
}

/**
* Gets the copy of the originally specified description that is being tracked.
* @return copy of the originally specified description
*/
public ProjectDescription getOriginal() {
return this.original;
}

/**
* Optionally calls the specified consumer if the {@code platformVersion} is different
* on the original source description and the specified current description.
* @param current the description to test against
* @param consumer to call if the property has changed
*/
public void ifPlatformVersionChanged(final ProjectDescription current,
final BiConsumer<Version, Version> consumer) {
if (!Objects.equals(this.original.getPlatformVersion(), current.getPlatformVersion())) {
consumer.accept(this.original.getPlatformVersion(), current.getPlatformVersion());
}
}

// TODO remaining method javadocs

public void ifBuildSystemChanged(final ProjectDescription current,
final BiConsumer<BuildSystem, BuildSystem> consumer) {
if (!Objects.equals(this.original.getBuildSystem(), current.getBuildSystem())) {
consumer.accept(this.original.getBuildSystem(), current.getBuildSystem());
}
}

public void ifPackagingChanged(final ProjectDescription current, final BiConsumer<Packaging, Packaging> consumer) {
if (!Objects.equals(this.original.getPackaging(), current.getPackaging())) {
consumer.accept(this.original.getPackaging(), current.getPackaging());
}
}

public void ifLanguageChanged(final ProjectDescription current, final BiConsumer<Language, Language> consumer) {
if (!Objects.equals(this.original.getLanguage(), current.getLanguage())) {
consumer.accept(this.original.getLanguage(), current.getLanguage());
}
}

public void ifGroupIdChanged(final ProjectDescription current, final BiConsumer<String, String> consumer) {
if (!Objects.equals(this.original.getGroupId(), current.getGroupId())) {
consumer.accept(this.original.getGroupId(), current.getGroupId());
}
}

public void ifArtifactIdChanged(final ProjectDescription current, final BiConsumer<String, String> consumer) {
if (!Objects.equals(this.original.getArtifactId(), current.getArtifactId())) {
consumer.accept(this.original.getArtifactId(), current.getArtifactId());
}
}

public void ifVersionChanged(final ProjectDescription current, final BiConsumer<String, String> consumer) {
if (!Objects.equals(this.original.getVersion(), current.getVersion())) {
consumer.accept(this.original.getVersion(), current.getVersion());
}
}

public void ifNameChanged(final ProjectDescription current, final BiConsumer<String, String> consumer) {
if (!Objects.equals(this.original.getName(), current.getName())) {
consumer.accept(this.original.getName(), current.getName());
}
}

public void ifDescriptionChanged(final ProjectDescription current, final BiConsumer<String, String> consumer) {
if (!Objects.equals(this.original.getDescription(), current.getDescription())) {
consumer.accept(this.original.getDescription(), current.getDescription());
}
}

public void ifApplicationNameChanged(final ProjectDescription current, final BiConsumer<String, String> consumer) {
if (!Objects.equals(this.original.getApplicationName(), current.getApplicationName())) {
consumer.accept(this.original.getApplicationName(), current.getApplicationName());
}
}

public void ifPackageNameChanged(final ProjectDescription current, final BiConsumer<String, String> consumer) {
if (!Objects.equals(this.original.getPackageName(), current.getPackageName())) {
consumer.accept(this.original.getPackageName(), current.getPackageName());
}
}

public void ifBaseDirectoryChanged(final ProjectDescription current, final BiConsumer<String, String> consumer) {
if (!Objects.equals(this.original.getBaseDirectory(), current.getBaseDirectory())) {
consumer.accept(this.original.getBaseDirectory(), current.getBaseDirectory());
}
}

public <V> void ifPropertyChanged(final ProjectDescription current, final String property,
final Class<V> propertyClass, final BiConsumer<V, V> consumer) {
final V originalValue = getPropertyValueReflectively(this.original, property);
final V currentValue = getPropertyValueReflectively(current, property);
if (!Objects.equals(originalValue, currentValue)) {
consumer.accept(originalValue, currentValue);
}
}

private <V> V getPropertyValueReflectively(final ProjectDescription description, final String property) {
final Class descriptionClass = description.getClass();
final Field field = ReflectionUtils.findField(descriptionClass, property);
if (field == null) {
throw new IllegalArgumentException(
String.format("No property named '%s' in '%s'.", property, descriptionClass.getSimpleName()));
}
ReflectionUtils.makeAccessible(field);
return (V) ReflectionUtils.getField(field, description);
}

}
Loading

0 comments on commit c933acd

Please sign in to comment.