Skip to content

Commit

Permalink
Merge pull request #2279 from DamnClin/executable-file
Browse files Browse the repository at this point in the history
Allow executable files in modules
  • Loading branch information
pascalgrimaud authored Jun 28, 2022
2 parents 2318406 + ac10806 commit c1f9171
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import tech.jhipster.lite.error.domain.Assert;

record JHipsterModuleFile(JHipsterFileContent content, JHipsterDestination destination) {
record JHipsterModuleFile(JHipsterFileContent content, JHipsterDestination destination, boolean executable) {
public JHipsterModuleFile {
Assert.notNull("content", content);
Assert.notNull("destination", destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ private JHipsterModuleFilesBuilder(JHipsterModuleBuilder module) {
}

public JHipsterModuleFilesBuilder add(JHipsterSource source, JHipsterDestination destination) {
files.add(new JHipsterModuleFile(new JHipsterFileContent(source), destination));
files.add(new JHipsterModuleFile(new JHipsterFileContent(source), destination, false));

return this;
}

public JHipsterModuleFilesBuilder addExecutable(JHipsterSource source, JHipsterDestination destination) {
files.add(new JHipsterModuleFile(new JHipsterFileContent(source), destination, true));

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public byte[] content(ProjectFilesReader files) {
return file.content().read(files, context);
}

public boolean isNotExecutable() {
return !file.executable();
}

static class TemplatedFileBuilder {

private JHipsterModuleFile file;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package tech.jhipster.lite.generator.module.infrastructure.secondary;

import static java.nio.file.attribute.PosixFilePermission.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import tech.jhipster.lite.common.domain.FileUtils;
import tech.jhipster.lite.common.domain.Generated;
import tech.jhipster.lite.common.domain.ProjectFilesReader;
import tech.jhipster.lite.error.domain.GeneratorException;
import tech.jhipster.lite.generator.module.domain.TemplatedFile;
Expand All @@ -17,13 +23,18 @@
class FileSystemJHipsterModuleFiles {

private static final Logger log = LoggerFactory.getLogger(FileSystemJHipsterModuleFiles.class);
private static final Set<PosixFilePermission> EXECUTABLE_FILE_PERMISSIONS = buildExecutableFilePermission();

private final ProjectFilesReader files;

public FileSystemJHipsterModuleFiles(ProjectFilesReader files) {
this.files = files;
}

private static Set<PosixFilePermission> buildExecutableFilePermission() {
return Set.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_WRITE, GROUP_EXECUTE);
}

void create(JHipsterProjectFolder projectFolder, TemplatedFiles files) {
files.get().forEach(writeFile(projectFolder));
}
Expand All @@ -36,10 +47,25 @@ private Consumer<TemplatedFile> writeFile(JHipsterProjectFolder projectFolder) {
Files.createDirectories(file.folder(projectFolder));
Files.write(filePath, file.content(files));

setExecutable(file, filePath);

log.debug("{} added", filePath);
} catch (IOException e) {
throw new GeneratorException("Can't write file to " + filePath.toString() + ": " + e.getMessage(), e);
}
};
}

@Generated(reason = "Ensuring posix FS whill be a nightmare :)")
private void setExecutable(TemplatedFile file, Path filePath) throws IOException {
if (!FileUtils.isPosix()) {
return;
}

if (file.isNotExecutable()) {
return;
}

Files.setPosixFilePermissions(filePath, EXECUTABLE_FILE_PERMISSIONS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static JHipsterModule module() {
.and()
.files()
.add(from("init/gitignore"), to(".gitignore"))
.addExecutable(from("init/.husky/pre-commit"), to(".husky/pre-commit"))
.batch(from("server/javatool/base"), to("src/main/java/com/company/myapp/errors"))
.template("Assert.java.mustache")
.template("AssertionException.java.mustache")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

@UnitTest
@ExtendWith(LogSpy.class)
class FileSystemModulesRepositoryTest {
class FileSystemJHipsterModulesRepositoryTest {

private final LogSpy logs;

public FileSystemModulesRepositoryTest(LogSpy logs) {
public FileSystemJHipsterModulesRepositoryTest(LogSpy logs) {
this.logs = logs;
}

Expand All @@ -33,6 +33,7 @@ void shouldApplyModule() {
"documentation/cucumber-integration.md",
".gitignore"
)
.createExecutableFiles(".husky/pre-commit")
.createFile("src/main/java/com/company/myapp/MyApp.java")
.containing("com.test.myapp")
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ private Consumer<Path> assertFileExist(SoftAssertions assertions) {
return path -> assertions.assertThat(Files.exists(path)).as(fileNotFoundMessage(path, projectFolder)).isTrue();
}

public ModuleAsserter createExecutableFiles(String... files) {
assertThat(files).as("Can't check null files for a module").isNotNull();

SoftAssertions assertions = new SoftAssertions();
Stream.of(files).map(file -> projectFolder.filePath(file)).forEach(assertFileIsExecutable(assertions));
assertions.assertAll();

return this;
}

private Consumer<Path> assertFileIsExecutable(SoftAssertions assertions) {
return path ->
assertions
.assertThat(Files.exists(path) && Files.isExecutable(path))
.as(() -> "File " + path + " is not executable (or doesn't exist) in project folder, found " + projectFiles(projectFolder))
.isTrue();
}

public ModuleAsserter doNotCreateFiles(String... files) {
assertThat(files).as("Can't check null files as not created for a module").isNotNull();

Expand Down

0 comments on commit c1f9171

Please sign in to comment.