Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow executable files in modules #2279

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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