diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java index e84748fbc5e..071b43cb600 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java @@ -48,7 +48,7 @@ class ProjectGenerator { ProjectGenerationResponse response = this.initializrService.generate(request); String fileName = (request.getOutput() != null ? request.getOutput() : response .getFileName()); - if (request.isExtract()) { + if (shouldExtract(request, response)) { if (isZipArchive(response)) { extractProject(response, request.getOutput(), force); return; @@ -68,6 +68,20 @@ class ProjectGenerator { writeProject(response, fileName, force); } + /** + * Detect if the project should be extracted. + */ + private boolean shouldExtract(ProjectGenerationRequest request, ProjectGenerationResponse response) { + if (request.isExtract()) { + return true; + } + // An explicit name has been provided for an archive and there is no extension in it + if (isZipArchive(response) && request.getOutput() != null && !request.getOutput().contains(".")) { + return true; + } + return false; + } + private boolean isZipArchive(ProjectGenerationResponse entity) { if (entity.getContentType() != null) { try { diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java index f976c410160..378c9eab1e1 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java @@ -114,7 +114,7 @@ public class InitCommandTests extends AbstractHttpClientMockTests { assertEquals(ExitStatus.OK, this.command.run("--extract", folder.getAbsolutePath())); File archiveFile = new File(folder, "test.txt"); - assertTrue("Archive not extracted properly " + folder.getAbsolutePath() + assertTrue("Archive not extracted properly " + archiveFile.getAbsolutePath() + " not found", archiveFile.exists()); } @@ -127,10 +127,50 @@ public class InitCommandTests extends AbstractHttpClientMockTests { mockSuccessfulProjectGeneration(request); assertEquals(ExitStatus.OK, this.command.run(folder.getAbsolutePath() + "/")); File archiveFile = new File(folder, "test.txt"); - assertTrue("Archive not extracted properly " + folder.getAbsolutePath() + assertTrue("Archive not extracted properly " + archiveFile.getAbsolutePath() + " not found", archiveFile.exists()); } + @Test + public void generateProjectArchiveExtractedByDefault() throws Exception { + String fileName = UUID.randomUUID().toString(); + assertFalse("No dot in filename", fileName.contains(".")); + byte[] archive = createFakeZipArchive("test.txt", "Fake content"); + MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( + "application/zip", "demo.zip", archive); + mockSuccessfulProjectGeneration(request); + File file = new File(fileName); + File archiveFile = new File(file, "test.txt"); + try { + assertEquals(ExitStatus.OK, this.command.run(fileName)); + assertTrue("Archive not extracted properly " + archiveFile.getAbsolutePath() + + " not found", archiveFile.exists()); + } + finally { + archiveFile.delete(); + file.delete(); + } + } + + @Test + public void generateProjectFileSavedAsFileByDefault() throws Exception { + String fileName = UUID.randomUUID().toString(); + String content = "Fake Content"; + byte[] archive = content.getBytes(); + MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( + "application/octet-stream", "pom.xml", archive); + mockSuccessfulProjectGeneration(request); + File file = new File(fileName); + try { + assertEquals(ExitStatus.OK, this.command.run(fileName)); + assertTrue("File not saved properly", file.exists()); + assertTrue("Should not be a directory", file.isFile()); + } + finally { + file.delete(); + } + } + @Test public void generateProjectAndExtractUnsupportedArchive() throws Exception { File folder = this.temporaryFolder.newFolder();