mirror of
https://gitee.com/dcren/initializr.git
synced 2025-07-15 23:13:30 +08:00
Polish "Clean artifactId and groupId"
Closes gh-924
This commit is contained in:
parent
10c7a4f32e
commit
06286e4fe9
@ -43,6 +43,8 @@ public class ProjectRequestToDescriptionConverter {
|
||||
|
||||
private static final Version VERSION_1_5_0 = Version.parse("1.5.0.RELEASE");
|
||||
|
||||
private static final char[] VALID_MAVEN_SPECIAL_CHARACTERS = new char[] { '_', '-', '.' };
|
||||
|
||||
public ProjectDescription convert(ProjectRequest request, InitializrMetadata metadata) {
|
||||
validate(request, metadata);
|
||||
String springBootVersion = getSpringBootVersion(request, metadata);
|
||||
@ -50,16 +52,14 @@ public class ProjectRequestToDescriptionConverter {
|
||||
validateDependencyRange(springBootVersion, resolvedDependencies);
|
||||
ProjectDescription description = new ProjectDescription();
|
||||
description.setApplicationName(getApplicationName(request, metadata));
|
||||
description.setArtifactId(determineValue(artificialIdCorrection(request.getArtifactId()),
|
||||
() -> metadata.getArtifactId().getContent()));
|
||||
description.setBaseDirectory(baseDirectoryCorrection(request.getBaseDir(), request.getArtifactId()));
|
||||
description.setArtifactId(getArtifactId(request, metadata));
|
||||
description.setBaseDirectory(getBaseDirectory(request.getBaseDir(), request.getArtifactId()));
|
||||
description.setBuildSystem(getBuildSystem(request, metadata));
|
||||
description
|
||||
.setDescription(determineValue(request.getDescription(), () -> metadata.getDescription().getContent()));
|
||||
description.setGroupId(
|
||||
determineValue(groupIdCorrection(request.getGroupId()), () -> metadata.getGroupId().getContent()));
|
||||
description.setGroupId(getGroupId(request, metadata));
|
||||
description.setLanguage(Language.forId(request.getLanguage(), request.getJavaVersion()));
|
||||
description.setName(determineValue(request.getName(), () -> metadata.getName().getContent()));
|
||||
description.setName(getName(request, metadata));
|
||||
description.setPackageName(getPackageName(request, metadata));
|
||||
description.setPackaging(Packaging.forId(request.getPackaging()));
|
||||
description.setPlatformVersion(Version.parse(springBootVersion));
|
||||
@ -74,36 +74,64 @@ public class ProjectRequestToDescriptionConverter {
|
||||
return (StringUtils.hasText(candidate)) ? candidate : fallback.get();
|
||||
}
|
||||
|
||||
private String baseDirectoryCorrection(String baseDir, String artifactId) {
|
||||
private String getBaseDirectory(String baseDir, String artifactId) {
|
||||
if (baseDir != null && baseDir.equals(artifactId)) {
|
||||
return coordinateCorrection(baseDir, "-");
|
||||
return cleanMavenCoordinate(baseDir, "-");
|
||||
}
|
||||
return baseDir;
|
||||
}
|
||||
|
||||
private String artificialIdCorrection(String artifactId) {
|
||||
return coordinateCorrection(artifactId, "-");
|
||||
private String getName(ProjectRequest request, InitializrMetadata metadata) {
|
||||
String name = request.getName();
|
||||
if (!StringUtils.hasText(name)) {
|
||||
return metadata.getName().getContent();
|
||||
}
|
||||
if (name.equals(request.getArtifactId())) {
|
||||
return cleanMavenCoordinate(name, "-");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private String groupIdCorrection(String groupId) {
|
||||
return coordinateCorrection(groupId, ".");
|
||||
private String getGroupId(ProjectRequest request, InitializrMetadata metadata) {
|
||||
if (!StringUtils.hasText(request.getGroupId())) {
|
||||
return metadata.getGroupId().getContent();
|
||||
}
|
||||
return cleanMavenCoordinate(request.getGroupId(), ".");
|
||||
}
|
||||
|
||||
private String coordinateCorrection(String coordinate, String delimiter) {
|
||||
String[] elements = coordinate.split("[^A-Za-z0-9_\\-.]+");
|
||||
if (elements.length <= 1) {
|
||||
private String getArtifactId(ProjectRequest request, InitializrMetadata metadata) {
|
||||
if (!StringUtils.hasText(request.getArtifactId())) {
|
||||
return metadata.getArtifactId().getContent();
|
||||
}
|
||||
return cleanMavenCoordinate(request.getArtifactId(), "-");
|
||||
}
|
||||
|
||||
private String cleanMavenCoordinate(String coordinate, String delimiter) {
|
||||
String[] elements = coordinate.split("[^\\w\\-.]+");
|
||||
if (elements.length == 1) {
|
||||
return coordinate;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (String element : elements) {
|
||||
if (!(element.startsWith("-") || element.startsWith("_") || element.startsWith(".")) && sb.length() > 0
|
||||
&& !(sb.charAt(sb.length() - 1) == '-' || sb.charAt(sb.length() - 1) == '_'
|
||||
|| sb.charAt(sb.length() - 1) == '.')) {
|
||||
sb.append(delimiter);
|
||||
if (shouldAppendDelimiter(element, builder)) {
|
||||
builder.append(delimiter);
|
||||
}
|
||||
sb.append(element);
|
||||
builder.append(element);
|
||||
}
|
||||
return sb.toString();
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private boolean shouldAppendDelimiter(String element, StringBuilder builder) {
|
||||
if (builder.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
for (char c : VALID_MAVEN_SPECIAL_CHARACTERS) {
|
||||
int prevIndex = builder.length() - 1;
|
||||
if (element.charAt(0) == c || builder.charAt(prevIndex) == c) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void validate(ProjectRequest request, InitializrMetadata metadata) {
|
||||
|
@ -272,36 +272,89 @@ public class ProjectRequestToDescriptionConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void conventionCorrectionForArtifactIdWithDifferentNameAsBaseDir() {
|
||||
void baseDirWhenNotSameAsArtifactIdShouldNotBeCleaned() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct ! ID @";
|
||||
String validArtifactId = "correct-ID";
|
||||
request.setArtifactId(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getArtifactId()).isEqualTo(validArtifactId);
|
||||
assertThat(description.getBaseDirectory()).isEqualTo(request.getBaseDir());
|
||||
}
|
||||
|
||||
@Test
|
||||
void conventionCorrectionForArtifactIdWithSameNameAsBaseDir() {
|
||||
void baseDirWhenSameAsArtifactIdShouldBeCleaned() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct ! ID @";
|
||||
String validArtifactId = "correct-ID";
|
||||
request.setArtifactId(artifactId);
|
||||
request.setBaseDir(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getArtifactId()).isEqualTo(validArtifactId);
|
||||
assertThat(description.getBaseDirectory()).isEqualTo(validArtifactId);
|
||||
assertThat(description.getBaseDirectory()).isEqualTo("correct-ID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void conventionCorrectionForGroupId() {
|
||||
void nameWhenNotSameAsArtifactIdShouldNotBeCleaned() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct ! ID @";
|
||||
request.setArtifactId(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getName()).isEqualTo(request.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nameWhenSameAsArtifactIdShouldBeCleaned() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct ! ID @";
|
||||
request.setArtifactId(artifactId);
|
||||
request.setName(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getName()).isEqualTo("correct-ID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void artifactIdWhenHasValidCharsOnlyShouldNotBeCleaned() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct_test";
|
||||
request.setArtifactId(artifactId);
|
||||
request.setBaseDir(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getArtifactId()).isEqualTo("correct_test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void artifactIdWhenInvalidShouldBeCleanedWithHyphenDelimiter() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct ! ID @";
|
||||
request.setArtifactId(artifactId);
|
||||
request.setBaseDir(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getArtifactId()).isEqualTo("correct-ID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void artifactIdWhenCleanedShouldNotContainHyphenBeforeOrAfterValidSpecialCharacter() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct !_!ID @";
|
||||
request.setArtifactId(artifactId);
|
||||
request.setBaseDir(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getArtifactId()).isEqualTo("correct_ID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupIdWhenInvalidShouldBeCleanedWithDotDelimiter() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String groupId = "correct ! ID12 @";
|
||||
String validGroupId = "correct.ID12";
|
||||
request.setGroupId(groupId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getGroupId()).isEqualTo(validGroupId);
|
||||
assertThat(description.getGroupId()).isEqualTo("correct.ID12");
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupIdWhenHasValidCharactersOnlyShouldNotBeCleaned() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String groupId = "correct_ID12";
|
||||
request.setGroupId(groupId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getGroupId()).isEqualTo("correct_ID12");
|
||||
}
|
||||
|
||||
private ProjectRequest createProjectRequest() {
|
||||
|
Loading…
Reference in New Issue
Block a user