mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-19 18:22:26 +08:00
Clean package name if necessary
This commit validates that the user provided package name is valid. Closes gh-114
This commit is contained in:
@@ -7,6 +7,7 @@ order.
|
|||||||
|
|
||||||
=== Release 1.0.0 (In progress)
|
=== Release 1.0.0 (In progress)
|
||||||
|
|
||||||
|
* https://github.com/spring-io/initializr/issues/114[#114]: clean package name if necessary
|
||||||
* https://github.com/spring-io/initializr/issues/90[#90]: migrate Groovy maven-based builds to GMavenPlus
|
* https://github.com/spring-io/initializr/issues/90[#90]: migrate Groovy maven-based builds to GMavenPlus
|
||||||
* https://github.com/spring-io/initializr/issues/140[#140]: expose dependencies meta-data
|
* https://github.com/spring-io/initializr/issues/140[#140]: expose dependencies meta-data
|
||||||
* https://github.com/spring-io/initializr/issues/168[#168]: support for dependency versions mapping
|
* https://github.com/spring-io/initializr/issues/168[#168]: support for dependency versions mapping
|
||||||
|
@@ -141,6 +141,7 @@ class ProjectRequest {
|
|||||||
if (!applicationName) {
|
if (!applicationName) {
|
||||||
this.applicationName = metadata.configuration.generateApplicationName(this.name)
|
this.applicationName = metadata.configuration.generateApplicationName(this.name)
|
||||||
}
|
}
|
||||||
|
packageName = metadata.configuration.cleanPackageName(this.packageName, metadata.packageName.content)
|
||||||
|
|
||||||
initializeRepositories(metadata, requestedVersion)
|
initializeRepositories(metadata, requestedVersion)
|
||||||
|
|
||||||
|
@@ -65,6 +65,27 @@ class InitializrConfiguration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean the specified package name if necessary. If the package name cannot
|
||||||
|
* be transformed to a valid package name, the {@code defaultPackageName}
|
||||||
|
* is used instead.
|
||||||
|
* <p>The package name cannot be cleaned if the specified {@code packageName}
|
||||||
|
* is {@code null} or if it contains an invalid character for a class identifier.
|
||||||
|
* @see Env#invalidPackageNames
|
||||||
|
*/
|
||||||
|
String cleanPackageName(String packageName, String defaultPackageName) {
|
||||||
|
if (!packageName) {
|
||||||
|
return defaultPackageName
|
||||||
|
}
|
||||||
|
String candidate = packageName.trim().split('\\W+').join('.')
|
||||||
|
if (hasInvalidChar(candidate.replace('.', '')) || env.invalidPackageNames.contains(candidate)) {
|
||||||
|
return defaultPackageName
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
candidate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static String splitCamelCase(String text) {
|
private static String splitCamelCase(String text) {
|
||||||
text.split('(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])').collect {
|
text.split('(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])').collect {
|
||||||
String s = it.toLowerCase()
|
String s = it.toLowerCase()
|
||||||
@@ -115,6 +136,14 @@ class InitializrConfiguration {
|
|||||||
'SpringBootApplication'
|
'SpringBootApplication'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of invalid package names. If such name is chosen or generated,
|
||||||
|
* the the default package name should be used instead.
|
||||||
|
*/
|
||||||
|
List<String> invalidPackageNames = [
|
||||||
|
'org.springframework'
|
||||||
|
]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Force SSL support. When enabled, any access using http generate https links.
|
* Force SSL support. When enabled, any access using http generate https links.
|
||||||
*/
|
*/
|
||||||
|
@@ -133,6 +133,13 @@
|
|||||||
"sourceType": "io.spring.initializr.metadata.InitializrConfiguration$Env",
|
"sourceType": "io.spring.initializr.metadata.InitializrConfiguration$Env",
|
||||||
"defaultValue": ["SpringApplication", "SpringBootApplication"]
|
"defaultValue": ["SpringApplication", "SpringBootApplication"]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "initializr.env.invalid-package-names",
|
||||||
|
"type": "java.util.List<java.lang.String>",
|
||||||
|
"description": "The list of invalid package names. If such name is chosen or generated, the default package name should be used instead.",
|
||||||
|
"sourceType": "io.spring.initializr.metadata.InitializrConfiguration$Env",
|
||||||
|
"defaultValue": ["org.springframework"]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "initializr.env.repositories",
|
"name": "initializr.env.repositories",
|
||||||
"type": "java.util.Map<java.lang.String,io.spring.initializr.metadata.Repository>",
|
"type": "java.util.Map<java.lang.String,io.spring.initializr.metadata.Repository>",
|
||||||
|
@@ -239,6 +239,24 @@ class ProjectRequestTests {
|
|||||||
assertEquals 'MyApplicationName', request.applicationName
|
assertEquals 'MyApplicationName', request.applicationName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cleanPackageNameWithNoName() {
|
||||||
|
def request = new ProjectRequest()
|
||||||
|
def metadata = InitializrMetadataTestBuilder.withDefaults().build()
|
||||||
|
|
||||||
|
request.resolve(metadata)
|
||||||
|
assertEquals metadata.packageName.content, request.packageName
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cleanPackageName() {
|
||||||
|
def request = new ProjectRequest()
|
||||||
|
request.packageName = 'com:foo bar'
|
||||||
|
def metadata = InitializrMetadataTestBuilder.withDefaults().build()
|
||||||
|
|
||||||
|
request.resolve(metadata)
|
||||||
|
assertEquals 'com.foo.bar', request.packageName
|
||||||
|
}
|
||||||
|
|
||||||
private static void assertBootStarter(Dependency actual, String name) {
|
private static void assertBootStarter(Dependency actual, String name) {
|
||||||
def expected = new Dependency()
|
def expected = new Dependency()
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package io.spring.initializr.metadata
|
package io.spring.initializr.metadata
|
||||||
|
|
||||||
import io.spring.initializr.metadata.InitializrConfiguration
|
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals
|
import static org.junit.Assert.assertEquals
|
||||||
@@ -59,7 +58,7 @@ class InitializrConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateApplicationNamSsimpleDash() {
|
void generateApplicationNamSimpleDash() {
|
||||||
assertEquals 'MyDemoApplication', this.properties.generateApplicationName('my-demo')
|
assertEquals 'MyDemoApplication', this.properties.generateApplicationName('my-demo')
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,6 +107,46 @@ class InitializrConfigurationTests {
|
|||||||
assertEquals this.properties.env.fallbackApplicationName, this.properties.generateApplicationName('Spring')
|
assertEquals this.properties.env.fallbackApplicationName, this.properties.generateApplicationName('Spring')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatePackageNameSimple() {
|
||||||
|
assertEquals 'com.foo', this.properties.cleanPackageName('com.foo', 'com.example')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatePackageNameSimpleUnderscore() {
|
||||||
|
assertEquals 'com.my_foo', this.properties.cleanPackageName('com.my_foo', 'com.example')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatePackageNameSimpleColon() {
|
||||||
|
assertEquals 'com.foo', this.properties.cleanPackageName('com:foo', 'com.example')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatePackageNameMultipleDashers() {
|
||||||
|
assertEquals 'com.foo', this.properties.cleanPackageName('com--foo', 'com.example')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatePackageNameMultipleSpaces() {
|
||||||
|
assertEquals 'com.foo', this.properties.cleanPackageName(' com foo ', 'com.example')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatePackageNameNull() {
|
||||||
|
assertEquals 'com.example', this.properties.cleanPackageName(null, 'com.example')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatePackageNameInvalidStartCharacter() {
|
||||||
|
assertEquals 'com.example', this.properties.cleanPackageName('0om.foo', 'com.example')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatePackageNameInvalidPackageName() {
|
||||||
|
assertEquals 'com.example', this.properties.cleanPackageName('org.springframework', 'com.example')
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void validateArtifactRepository() {
|
void validateArtifactRepository() {
|
||||||
this.properties.env.artifactRepository = 'http://foo/bar'
|
this.properties.env.artifactRepository = 'http://foo/bar'
|
||||||
|
@@ -37,6 +37,9 @@
|
|||||||
"SpringApplication",
|
"SpringApplication",
|
||||||
"SpringBootApplication"
|
"SpringBootApplication"
|
||||||
],
|
],
|
||||||
|
"invalidPackageNames": [
|
||||||
|
"org.springframework"
|
||||||
|
],
|
||||||
"repositories": {
|
"repositories": {
|
||||||
"my-api-repo-1": {
|
"my-api-repo-1": {
|
||||||
"name": "repo1",
|
"name": "repo1",
|
||||||
|
Reference in New Issue
Block a user