mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-18 17:48:14 +08:00
Generate .gitignore
This commit improves the generator so that a `.gitignore` is added to every project. The basic content of that file is determined by the build system. If an agent is recognized, specific build tool settings are added as well. This commit adds support for STS, IntelliJ IDEA and NetBeans. Closes gh-131
This commit is contained in:
@@ -20,6 +20,7 @@ import groovy.util.logging.Slf4j
|
||||
import io.spring.initializr.InitializrException
|
||||
import io.spring.initializr.metadata.Dependency
|
||||
import io.spring.initializr.metadata.InitializrMetadataProvider
|
||||
import io.spring.initializr.util.Agent
|
||||
import io.spring.initializr.util.GroovyTemplate
|
||||
import io.spring.initializr.util.Version
|
||||
|
||||
@@ -140,6 +141,8 @@ class ProjectGenerator {
|
||||
writeMavenWrapper(dir)
|
||||
}
|
||||
|
||||
generateGitIgnore(dir, request)
|
||||
|
||||
def applicationName = request.applicationName
|
||||
def language = request.language
|
||||
|
||||
@@ -210,6 +213,19 @@ class ProjectGenerator {
|
||||
eventPublisher.publishEvent(event)
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a {@code .gitignore} file for the specified {@link ProjectRequest}
|
||||
* @param dir the root directory of the project
|
||||
* @param request the request to handle
|
||||
*/
|
||||
protected void generateGitIgnore(File dir, ProjectRequest request) {
|
||||
def model = [:]
|
||||
def agent = extractAgent(request)
|
||||
model['agent'] = agent ? agent.id.id : null
|
||||
model['build'] = isGradleBuild(request) ? 'gradle' : 'maven'
|
||||
write(new File(dir, '.gitignore'), 'gitignore.tmpl', model)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the specified {@link ProjectRequest} and return the model to use
|
||||
* to generate the project
|
||||
@@ -305,6 +321,16 @@ class ProjectGenerator {
|
||||
"import $type$end"
|
||||
}
|
||||
|
||||
private static Agent extractAgent(ProjectRequest request) {
|
||||
if (request.parameters['user-agent']) {
|
||||
Agent agent = Agent.fromUserAgent(request.parameters['user-agent'])
|
||||
if (agent) {
|
||||
return agent
|
||||
}
|
||||
}
|
||||
null
|
||||
}
|
||||
|
||||
private static isGradleBuild(ProjectRequest request) {
|
||||
return 'gradle'.equals(request.build)
|
||||
}
|
||||
|
@@ -0,0 +1,23 @@
|
||||
<% if (build=='maven') { %>target/
|
||||
!.mvn/wrapper/maven-wrapper.jar<% } else { %>.gradle
|
||||
/build/
|
||||
!gradle/wrapper/gradle-wrapper.jar<% } %><% if ('sts'==agent) { %>
|
||||
|
||||
### STS ###
|
||||
.classpath
|
||||
.project
|
||||
.settings<% } %><% if ('intellijidea'==agent) { %>
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr<% } %><% if ('netbeans'==agent) { %>
|
||||
|
||||
### NetBeans ###
|
||||
nbproject/private/
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/<% } %>
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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
|
||||
*
|
||||
* http://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
|
||||
|
||||
import io.spring.initializr.util.Agent
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.junit.runners.Parameterized
|
||||
|
||||
import org.springframework.core.io.ClassPathResource
|
||||
import org.springframework.core.io.Resource
|
||||
|
||||
/**
|
||||
* Project generator tests for {@code .gitignore}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
class ProjectGeneratorGitIgnoreTests extends AbstractProjectGeneratorTests {
|
||||
|
||||
@Parameterized.Parameters(name = "{0}")
|
||||
static Object[] parameters() {
|
||||
def list = []
|
||||
list << 'STS 3.7.2'
|
||||
list << 'IntelliJ IDEA'
|
||||
list << 'nb-springboot-plugin/0.1'
|
||||
list << 'HTTPie/0.8.0'
|
||||
list << 'Googlebot-Mobile'
|
||||
list
|
||||
}
|
||||
|
||||
private final String userAgent
|
||||
private final Agent agent
|
||||
|
||||
ProjectGeneratorGitIgnoreTests(String userAgent) {
|
||||
this.userAgent = userAgent
|
||||
this.agent = Agent.fromUserAgent(userAgent)
|
||||
}
|
||||
|
||||
@Test
|
||||
void gitIgnoreMaven() {
|
||||
def request = createProjectRequest()
|
||||
request.type = 'maven-project'
|
||||
def project = generateProject(request)
|
||||
project.sourceCodeAssert(".gitignore")
|
||||
.equalsTo(getResourceFor('maven'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void gitIgnoreGradle() {
|
||||
def request = createProjectRequest()
|
||||
request.type = 'gradle-project'
|
||||
def project = generateProject(request)
|
||||
project.sourceCodeAssert(".gitignore")
|
||||
.equalsTo(getResourceFor('gradle'))
|
||||
}
|
||||
|
||||
private Resource getResourceFor(String build) {
|
||||
String id = agent ? agent.id.id : 'none'
|
||||
return new ClassPathResource("project/$build/gitignore-${id}.gen")
|
||||
}
|
||||
|
||||
@Override
|
||||
ProjectRequest createProjectRequest(String... styles) {
|
||||
def request = super.createProjectRequest(styles)
|
||||
request.parameters['user-agent'] = this.userAgent
|
||||
request
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
.gradle
|
||||
/build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
@@ -0,0 +1,9 @@
|
||||
.gradle
|
||||
/build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
@@ -0,0 +1,11 @@
|
||||
.gradle
|
||||
/build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
### NetBeans ###
|
||||
nbproject/private/
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
@@ -0,0 +1,3 @@
|
||||
.gradle
|
||||
/build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
@@ -0,0 +1,8 @@
|
||||
.gradle
|
||||
/build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
@@ -0,0 +1,2 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
@@ -0,0 +1,8 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
@@ -0,0 +1,10 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### NetBeans ###
|
||||
nbproject/private/
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
@@ -0,0 +1,2 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
@@ -0,0 +1,7 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
@@ -221,7 +221,7 @@ class MainController extends AbstractInitializrController {
|
||||
|
||||
new AntBuilder().zip(destfile: download) {
|
||||
zipfileset(dir: dir, includes: wrapperScript, filemode: 755)
|
||||
zipfileset(dir: dir, includes: '**', excludes: wrapperScript)
|
||||
zipfileset(dir: dir, includes: '**,', excludes: wrapperScript, defaultexcludes: 'no')
|
||||
}
|
||||
upload(download, dir, generateFileName(request, 'zip'), 'application/zip')
|
||||
}
|
||||
@@ -238,7 +238,7 @@ class MainController extends AbstractInitializrController {
|
||||
|
||||
new AntBuilder().tar(destfile: download, compression: 'gzip') {
|
||||
zipfileset(dir: dir, includes: wrapperScript, filemode: 755)
|
||||
zipfileset(dir: dir, includes: '**', excludes: wrapperScript)
|
||||
zipfileset(dir: dir, includes: '**', excludes: wrapperScript, defaultexcludes: 'no')
|
||||
}
|
||||
upload(download, dir, generateFileName(request, 'tgz'), 'application/x-compress')
|
||||
}
|
||||
|
@@ -51,7 +51,9 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
|
||||
|
||||
@Test
|
||||
void simpleZipProject() {
|
||||
downloadZip('/starter.zip?style=web&style=jpa').isJavaProject().isMavenProject()
|
||||
downloadZip('/starter.zip?style=web&style=jpa').isJavaProject()
|
||||
.hasFile('.gitignore')
|
||||
.isMavenProject()
|
||||
.hasStaticAndTemplatesResources(true).pomAssert()
|
||||
.hasDependenciesCount(3)
|
||||
.hasSpringBootStarterDependency('web')
|
||||
@@ -61,7 +63,9 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
|
||||
|
||||
@Test
|
||||
void simpleTgzProject() {
|
||||
downloadTgz('/starter.tgz?style=org.acme:foo').isJavaProject().isMavenProject()
|
||||
downloadTgz('/starter.tgz?style=org.acme:foo').isJavaProject()
|
||||
.hasFile('.gitignore')
|
||||
.isMavenProject()
|
||||
.hasStaticAndTemplatesResources(false).pomAssert()
|
||||
.hasDependenciesCount(2)
|
||||
.hasDependency('org.acme', 'foo', '1.3.5')
|
||||
|
Reference in New Issue
Block a user