-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate .gitattributes for Maven and Gradle
Closes gh-1569
- Loading branch information
1 parent
c0e18b8
commit 6b2774d
Showing
8 changed files
with
291 additions
and
6 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...tor-spring/src/main/java/io/spring/initializr/generator/spring/scm/git/GitAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2012-2024 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.spring.scm.git; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Project's {@code .gitattributes}. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
public class GitAttributes { | ||
|
||
private final List<Line> lines = new ArrayList<>(); | ||
|
||
/** | ||
* Adds a new pattern with attributes. | ||
* @param pattern the pattern | ||
* @param attribute the first attribute | ||
* @param remainingAttributes the remaining attributes | ||
*/ | ||
public void add(String pattern, String attribute, String... remainingAttributes) { | ||
List<String> attributes = new ArrayList<>(); | ||
attributes.add(attribute); | ||
attributes.addAll(Arrays.asList(remainingAttributes)); | ||
this.lines.add(new Line(pattern, attributes)); | ||
} | ||
|
||
void write(PrintWriter writer) { | ||
for (Line line : this.lines) { | ||
line.write(writer); | ||
} | ||
} | ||
|
||
boolean isEmpty() { | ||
return this.lines.isEmpty(); | ||
} | ||
|
||
private record Line(String pattern, List<String> attributes) { | ||
void write(PrintWriter writer) { | ||
writer.print(this.pattern); | ||
writer.print(' '); | ||
writer.print(String.join(" ", this.attributes)); | ||
writer.println(); | ||
} | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...src/main/java/io/spring/initializr/generator/spring/scm/git/GitAttributesContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2012-2024 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.spring.scm.git; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import io.spring.initializr.generator.project.contributor.ProjectContributor; | ||
|
||
/** | ||
* A {@link ProjectContributor} that contributes a {@code .gitattributes} file to a | ||
* project. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
public class GitAttributesContributor implements ProjectContributor { | ||
|
||
private final GitAttributes gitAttributes; | ||
|
||
public GitAttributesContributor(GitAttributes gitAttributes) { | ||
this.gitAttributes = gitAttributes; | ||
} | ||
|
||
@Override | ||
public void contribute(Path projectRoot) throws IOException { | ||
if (this.gitAttributes.isEmpty()) { | ||
return; | ||
} | ||
Path file = Files.createFile(projectRoot.resolve(".gitattributes")); | ||
try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(file))) { | ||
this.gitAttributes.write(writer); | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
.../src/main/java/io/spring/initializr/generator/spring/scm/git/GitAttributesCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.spring.scm.git; | ||
|
||
import org.springframework.core.Ordered; | ||
|
||
/** | ||
* Callback for customizing a project's {@link GitAttributes}. Invoked with an | ||
* {@link Ordered order} of {@code 0} by default, considering overriding | ||
* {@link #getOrder()} to customize this behaviour. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
@FunctionalInterface | ||
public interface GitAttributesCustomizer extends Ordered { | ||
|
||
void customize(GitAttributes gitAttributes); | ||
|
||
@Override | ||
default int getOrder() { | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...pring/src/test/java/io/spring/initializr/generator/spring/scm/git/GitAttributesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2012-2024 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.spring.scm.git; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link GitAttributes}. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
class GitAttributesTests { | ||
|
||
@Test | ||
void shouldWriteGitAttributes() { | ||
GitAttributes attributes = new GitAttributes(); | ||
attributes.add("/gradlew", "text", "eof=lf"); | ||
attributes.add("*.bat", "text", "eol=crlf"); | ||
attributes.add("*.jar", "binary"); | ||
String written = writeToString(attributes); | ||
assertThat(written).isEqualToNormalizingNewlines(""" | ||
/gradlew text eof=lf | ||
*.bat text eol=crlf | ||
*.jar binary | ||
"""); | ||
} | ||
|
||
private String writeToString(GitAttributes attributes) { | ||
StringWriter stringWriter = new StringWriter(); | ||
try (PrintWriter printWriter = new PrintWriter(stringWriter)) { | ||
attributes.write(printWriter); | ||
} | ||
return stringWriter.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters