mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-19 01:58:16 +08:00
Move source file extension to Language abstraction
Closes gh-995
This commit is contained in:
@@ -53,7 +53,7 @@ public interface BuildSystem {
|
||||
* @return a {@link SourceStructure} for main assets
|
||||
*/
|
||||
default SourceStructure getMainSource(Path projectRoot, Language language) {
|
||||
return new SourceStructure(projectRoot.resolve("src/main/"), language.id());
|
||||
return new SourceStructure(projectRoot.resolve("src/main/"), language);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +63,7 @@ public interface BuildSystem {
|
||||
* @return a {@link SourceStructure} for test assets
|
||||
*/
|
||||
default SourceStructure getTestSource(Path projectRoot, Language language) {
|
||||
return new SourceStructure(projectRoot.resolve("src/test/"), language.id());
|
||||
return new SourceStructure(projectRoot.resolve("src/test/"), language);
|
||||
}
|
||||
|
||||
static BuildSystem forId(String id) {
|
||||
|
@@ -27,9 +27,12 @@ public abstract class AbstractLanguage implements Language {
|
||||
|
||||
private final String jvmVersion;
|
||||
|
||||
protected AbstractLanguage(String id, String jvmVersion) {
|
||||
private final String sourceFileExtension;
|
||||
|
||||
protected AbstractLanguage(String id, String jvmVersion, String sourceFileExtension) {
|
||||
this.id = id;
|
||||
this.jvmVersion = (jvmVersion != null) ? jvmVersion : DEFAULT_JVM_VERSION;
|
||||
this.sourceFileExtension = sourceFileExtension;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,6 +45,11 @@ public abstract class AbstractLanguage implements Language {
|
||||
return this.jvmVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sourceFileExtension() {
|
||||
return this.sourceFileExtension;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id();
|
||||
|
@@ -32,10 +32,24 @@ public interface Language {
|
||||
*/
|
||||
String DEFAULT_JVM_VERSION = "1.8";
|
||||
|
||||
/**
|
||||
* Return the language identifier.
|
||||
* @return the language id
|
||||
*/
|
||||
String id();
|
||||
|
||||
/**
|
||||
* Return the JVM version level to use.
|
||||
* @return the JVM version or {@value DEFAULT_JVM_VERSION} if not set
|
||||
*/
|
||||
String jvmVersion();
|
||||
|
||||
/**
|
||||
* Return the file extension to use for source file of this language.
|
||||
* @return the source file extension
|
||||
*/
|
||||
String sourceFileExtension();
|
||||
|
||||
static Language forId(String id, String jvmVersion) {
|
||||
return SpringFactoriesLoader.loadFactories(LanguageFactory.class, LanguageFactory.class.getClassLoader())
|
||||
.stream().map((factory) -> factory.createLanguage(id, jvmVersion)).filter(Objects::nonNull).findFirst()
|
||||
|
@@ -21,7 +21,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Provide dedicated method for directories that hold sources.
|
||||
* Provide dedicated methods for a structure that holds sources.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@@ -29,16 +29,19 @@ public class SourceStructure {
|
||||
|
||||
private final Path rootDirectory;
|
||||
|
||||
private final Language language;
|
||||
|
||||
private final Path sourcesDirectory;
|
||||
|
||||
public SourceStructure(Path rootDirectory, String sourcesDirectoryName) {
|
||||
public SourceStructure(Path rootDirectory, Language language) {
|
||||
this.rootDirectory = rootDirectory;
|
||||
this.sourcesDirectory = rootDirectory.resolve(sourcesDirectoryName);
|
||||
this.language = language;
|
||||
this.sourcesDirectory = rootDirectory.resolve(language.id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the root {@link Path} of this structure. Can be used to access additional
|
||||
* resources.
|
||||
* Return the root {@link Path directory} of this structure. Can be used to access
|
||||
* additional resources.
|
||||
* @return the root directory
|
||||
*/
|
||||
public Path getRootDirectory() {
|
||||
@@ -46,7 +49,7 @@ public class SourceStructure {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the sources {@link Path} of this structure.
|
||||
* Return the sources {@link Path directory} of this structure.
|
||||
* @return the source code directory
|
||||
*/
|
||||
public Path getSourcesDirectory() {
|
||||
@@ -54,15 +57,17 @@ public class SourceStructure {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resource a source file, creating its package structure if necessary.
|
||||
* Resolve a source file, creating its package structure if necessary. Does not create
|
||||
* the file itself.
|
||||
* @param packageName the name of the package
|
||||
* @param file the name of the file (including its extension)
|
||||
* @return the file to use to store a {@code CompilationUnit} with the specified
|
||||
* package name name
|
||||
* @param fileName the name of the file (without its extension)
|
||||
* @return the {@link Path file} to use to store a {@code CompilationUnit} with the
|
||||
* specified package and name
|
||||
* @throws IOException if an error occurred while trying to create the directory
|
||||
* structure
|
||||
*/
|
||||
public Path resolveSourceFile(String packageName, String file) throws IOException {
|
||||
public Path resolveSourceFile(String packageName, String fileName) throws IOException {
|
||||
String file = fileName + "." + this.language.sourceFileExtension();
|
||||
return createPackage(this.sourcesDirectory, packageName).resolve(file);
|
||||
}
|
||||
|
||||
|
@@ -36,7 +36,7 @@ public final class GroovyLanguage extends AbstractLanguage {
|
||||
}
|
||||
|
||||
public GroovyLanguage(String jvmVersion) {
|
||||
super(ID, jvmVersion);
|
||||
super(ID, jvmVersion, "groovy");
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -95,8 +95,7 @@ public class GroovySourceCodeWriter implements SourceCodeWriter<GroovySourceCode
|
||||
}
|
||||
|
||||
private void writeTo(SourceStructure structure, GroovyCompilationUnit compilationUnit) throws IOException {
|
||||
Path output = structure.resolveSourceFile(compilationUnit.getPackageName(),
|
||||
compilationUnit.getName() + ".groovy");
|
||||
Path output = structure.resolveSourceFile(compilationUnit.getPackageName(), compilationUnit.getName());
|
||||
try (IndentingWriter writer = this.indentingWriterFactory.createIndentingWriter("groovy",
|
||||
Files.newBufferedWriter(output))) {
|
||||
writer.println("package " + compilationUnit.getPackageName());
|
||||
|
@@ -37,7 +37,7 @@ public final class JavaLanguage extends AbstractLanguage {
|
||||
}
|
||||
|
||||
public JavaLanguage(String jvmVersion) {
|
||||
super(ID, jvmVersion);
|
||||
super(ID, jvmVersion, "java");
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -96,8 +96,7 @@ public class JavaSourceCodeWriter implements SourceCodeWriter<JavaSourceCode> {
|
||||
}
|
||||
|
||||
private void writeTo(SourceStructure structure, JavaCompilationUnit compilationUnit) throws IOException {
|
||||
Path output = structure.resolveSourceFile(compilationUnit.getPackageName(),
|
||||
compilationUnit.getName() + ".java");
|
||||
Path output = structure.resolveSourceFile(compilationUnit.getPackageName(), compilationUnit.getName());
|
||||
Files.createDirectories(output.getParent());
|
||||
try (IndentingWriter writer = this.indentingWriterFactory.createIndentingWriter("java",
|
||||
Files.newBufferedWriter(output))) {
|
||||
|
@@ -36,7 +36,7 @@ public final class KotlinLanguage extends AbstractLanguage {
|
||||
}
|
||||
|
||||
public KotlinLanguage(String jvmVersion) {
|
||||
super(ID, jvmVersion);
|
||||
super(ID, jvmVersion, "kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -61,7 +61,7 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
|
||||
}
|
||||
|
||||
private void writeTo(SourceStructure structure, KotlinCompilationUnit compilationUnit) throws IOException {
|
||||
Path output = structure.resolveSourceFile(compilationUnit.getPackageName(), compilationUnit.getName() + ".kt");
|
||||
Path output = structure.resolveSourceFile(compilationUnit.getPackageName(), compilationUnit.getName());
|
||||
Files.createDirectories(output.getParent());
|
||||
try (IndentingWriter writer = this.indentingWriterFactory.createIndentingWriter("kotlin",
|
||||
Files.newBufferedWriter(output))) {
|
||||
|
@@ -53,14 +53,14 @@ class BuildSystemTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultMainDirectory(@TempDir Path directory) {
|
||||
void defaultMainSource(@TempDir Path directory) {
|
||||
SourceStructure mainCodeStructure = BuildSystem.forId("gradle").getMainSource(directory, new JavaLanguage());
|
||||
assertThat(mainCodeStructure.getRootDirectory()).isEqualTo(directory.resolve("src/main"));
|
||||
assertThat(mainCodeStructure.getSourcesDirectory()).isEqualTo(directory.resolve("src/main/java"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultTestDirectory(@TempDir Path directory) {
|
||||
void defaultTestSource(@TempDir Path directory) {
|
||||
SourceStructure testCodeStructure = BuildSystem.forId("gradle").getTestSource(directory, new KotlinLanguage());
|
||||
assertThat(testCodeStructure.getRootDirectory()).isEqualTo(directory.resolve("src/test"));
|
||||
assertThat(testCodeStructure.getSourcesDirectory()).isEqualTo(directory.resolve("src/test/kotlin"));
|
||||
|
@@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import io.spring.initializr.generator.language.java.JavaLanguage;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
@@ -32,9 +33,11 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class SourceStructureTests {
|
||||
|
||||
public static final JavaLanguage JAVA_LANGUAGE = new JavaLanguage();
|
||||
|
||||
@Test
|
||||
void createPackage(@TempDir Path dir) throws IOException {
|
||||
Path target = new SourceStructure(dir, "java").createPackage(dir, "com.example.test");
|
||||
Path target = new SourceStructure(dir, JAVA_LANGUAGE).createPackage(dir, "com.example.test");
|
||||
assertThat(target).exists().isDirectory().isEqualByComparingTo(dir.resolve("com/example/test"));
|
||||
}
|
||||
|
||||
@@ -43,7 +46,7 @@ class SourceStructureTests {
|
||||
Path target = dir.resolve("com/example");
|
||||
Files.createDirectories(target);
|
||||
assertThat(target).exists().isDirectory();
|
||||
Path path = new SourceStructure(dir, "java").createPackage(dir, "com.example");
|
||||
Path path = new SourceStructure(dir, JAVA_LANGUAGE).createPackage(dir, "com.example");
|
||||
assertThat(path).isEqualByComparingTo(target);
|
||||
}
|
||||
|
||||
@@ -52,7 +55,8 @@ class SourceStructureTests {
|
||||
Path rootDir = dir.resolve("src/main/java/com/example");
|
||||
assertThat(rootDir).doesNotExist();
|
||||
Path target = rootDir.resolve("Test.java");
|
||||
Path path = new SourceStructure(dir, "src/main/java").resolveSourceFile("com.example", "Test.java");
|
||||
SourceStructure sourceStructure = new SourceStructure(dir.resolve("src/main"), JAVA_LANGUAGE);
|
||||
Path path = sourceStructure.resolveSourceFile("com.example", "Test");
|
||||
assertThat(path).doesNotExist().isEqualByComparingTo(target);
|
||||
assertThat(rootDir).exists().isDirectory();
|
||||
}
|
||||
@@ -64,7 +68,8 @@ class SourceStructureTests {
|
||||
assertThat(rootDir).exists().isDirectory();
|
||||
Path target = rootDir.resolve("Test.java");
|
||||
assertThat(target).doesNotExist();
|
||||
Path path = new SourceStructure(dir, "src/main/java").resolveSourceFile("com.example", "Test.java");
|
||||
SourceStructure sourceStructure = new SourceStructure(dir.resolve("src/main"), JAVA_LANGUAGE);
|
||||
Path path = sourceStructure.resolveSourceFile("com.example", "Test");
|
||||
assertThat(path).doesNotExist().isEqualByComparingTo(target);
|
||||
}
|
||||
|
||||
|
@@ -29,6 +29,7 @@ import java.util.UUID;
|
||||
|
||||
import io.spring.initializr.generator.io.IndentingWriterFactory;
|
||||
import io.spring.initializr.generator.language.Annotation;
|
||||
import io.spring.initializr.generator.language.Language;
|
||||
import io.spring.initializr.generator.language.Parameter;
|
||||
import io.spring.initializr.generator.language.SourceStructure;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -47,6 +48,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||
*/
|
||||
class GroovySourceCodeWriterTests {
|
||||
|
||||
private static final Language LANGUAGE = new GroovyLanguage();
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
@@ -285,7 +288,8 @@ class GroovySourceCodeWriterTests {
|
||||
}
|
||||
|
||||
private Path writeSourceCode(GroovySourceCode sourceCode) throws IOException {
|
||||
SourceStructure sourceStructure = new SourceStructure(this.directory, UUID.randomUUID().toString());
|
||||
Path srcDirectory = this.directory.resolve(UUID.randomUUID().toString());
|
||||
SourceStructure sourceStructure = new SourceStructure(srcDirectory, LANGUAGE);
|
||||
this.writer.writeTo(sourceStructure, sourceCode);
|
||||
return sourceStructure.getSourcesDirectory();
|
||||
}
|
||||
|
@@ -29,6 +29,7 @@ import java.util.UUID;
|
||||
|
||||
import io.spring.initializr.generator.io.IndentingWriterFactory;
|
||||
import io.spring.initializr.generator.language.Annotation;
|
||||
import io.spring.initializr.generator.language.Language;
|
||||
import io.spring.initializr.generator.language.Parameter;
|
||||
import io.spring.initializr.generator.language.SourceStructure;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -47,6 +48,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||
*/
|
||||
class JavaSourceCodeWriterTests {
|
||||
|
||||
private static final Language LANGUAGE = new JavaLanguage();
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
@@ -286,7 +289,8 @@ class JavaSourceCodeWriterTests {
|
||||
}
|
||||
|
||||
private Path writeSourceCode(JavaSourceCode sourceCode) throws IOException {
|
||||
SourceStructure sourceStructure = new SourceStructure(this.directory, UUID.randomUUID().toString());
|
||||
Path srcDirectory = this.directory.resolve(UUID.randomUUID().toString());
|
||||
SourceStructure sourceStructure = new SourceStructure(srcDirectory, LANGUAGE);
|
||||
this.writer.writeTo(sourceStructure, sourceCode);
|
||||
return sourceStructure.getSourcesDirectory();
|
||||
}
|
||||
|
@@ -28,6 +28,7 @@ import java.util.UUID;
|
||||
|
||||
import io.spring.initializr.generator.io.IndentingWriterFactory;
|
||||
import io.spring.initializr.generator.language.Annotation;
|
||||
import io.spring.initializr.generator.language.Language;
|
||||
import io.spring.initializr.generator.language.Parameter;
|
||||
import io.spring.initializr.generator.language.SourceStructure;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -46,6 +47,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||
*/
|
||||
class KotlinSourceCodeWriterTests {
|
||||
|
||||
private static final Language LANGUAGE = new KotlinLanguage();
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
@@ -343,7 +346,8 @@ class KotlinSourceCodeWriterTests {
|
||||
}
|
||||
|
||||
private Path writeSourceCode(KotlinSourceCode sourceCode) throws IOException {
|
||||
SourceStructure sourceStructure = new SourceStructure(this.directory, UUID.randomUUID().toString());
|
||||
Path srcDirectory = this.directory.resolve(UUID.randomUUID().toString());
|
||||
SourceStructure sourceStructure = new SourceStructure(srcDirectory, LANGUAGE);
|
||||
this.writer.writeTo(sourceStructure, sourceCode);
|
||||
return sourceStructure.getSourcesDirectory();
|
||||
}
|
||||
|
Reference in New Issue
Block a user