Smarter output detection for generated projects
Previously, specifying a simple target name for a regular project would store the (zip) archive in a file matching the target name. Only adding a slash at the end of the name allows to extract it as a directory. It turns out that such convention is not easy to catch and if a simple name is provided on the command-line, the user probably wants to create a directory with such a name with the content of the project. Note that if a build file is required and the name does not have any extension, we still store a file with the required name as auto-detecting the extension to use is not that easy. Fixes gh-2056
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user