Skip to content

Commit

Permalink
Added DirectoryBuilder and renamed FileContent to FileBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
renelink committed Aug 28, 2024
1 parent a9ca6b4 commit 121ca09
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 52 deletions.
4 changes: 2 additions & 2 deletions modules/lis-gradle-project-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Once you created a GradleProjectBuilder you can write
content to the build and settings files using the convenience methods.

```java
FileContent fileContent = projectBuilder.buildFile();
fileContent.append(printWriter -> {
FileContent fileBuilder = projectBuilder.buildFile();
fileBuilder.append(printWriter -> {
printWriter.println("plugins {");
printWriter.println(" `java-library`");
printWriter.println("}");
Expand Down
1 change: 1 addition & 0 deletions modules/lis-gradle-project-builder/parentFolder/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,26 @@

import static java.util.Objects.requireNonNull;

public class AbstractProjectBuilder {
private final Path projectRootDir;
public class AbstractProjectBuilder extends DirectoryBuilder {
private final ScriptLanguage scriptLanguage;
private final Path buildFile;
private final FileBuilder buildFile;

public AbstractProjectBuilder(Path projectRootDir, ScriptLanguage scriptLanguage) throws IOException {
this.projectRootDir = requireNonNull(projectRootDir);
super(projectRootDir);
this.scriptLanguage = requireNonNull(scriptLanguage);

buildFile = this.projectRootDir.resolve(scriptLanguage.buildFileName());

if (!Files.isDirectory(projectRootDir)) {
Files.createDirectories(this.projectRootDir);
}

Files.createFile(buildFile);
buildFile = file(scriptLanguage.buildFileName());
}

protected Path getProjectRootDir() {
return projectRootDir;
return getPath();
}

public ScriptLanguage getScriptLanguage() {
return scriptLanguage;
}

public FileContent buildFile() {
return new FileContent(buildFile);
public FileBuilder buildFile() {
return buildFile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.link_intersystems.gradle.project.builder;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static java.util.Objects.requireNonNull;

public class DirectoryBuilder {

private Path directory;

public DirectoryBuilder(Path directory) throws IOException {
if (!Files.exists(directory)) {
Files.createDirectories(directory);
}
this.directory = requireNonNull(directory);
}

public Path getPath() {
return directory;
}

public DirectoryBuilder directory(String relativePath) throws IOException {
return directory(Paths.get(relativePath), DirectoryBuilder::new);
}


public <T extends DirectoryBuilder> T directory(String relativePath, DirectoryBuilderFactory<T> directoryBuilderFactory) throws IOException {
return directory(Paths.get(relativePath), directoryBuilderFactory);
}

public DirectoryBuilder directory(Path relativePath) throws IOException {
return directory(relativePath, DirectoryBuilder::new);
}

public <T extends DirectoryBuilder> T directory(Path relativePath, DirectoryBuilderFactory<T> directoryBuilderFactory) throws IOException {
Path nextDirectoryPath = relativePath.subpath(0, 1);
return directoryBuilderFactory.create(directory.resolve(nextDirectoryPath));
}

public FileBuilder file(String relativeFilePath) throws IOException {
return file(relativeFilePath, FileBuilder::new);
}

public <T extends FileBuilder> T file(String relativeFilePath, FileBuilderFactory<T> fileBuilderFactory) throws IOException {
return file(Paths.get(relativeFilePath), fileBuilderFactory);
}

public FileBuilder file(Path relativeFilePath) throws IOException {
return file(relativeFilePath, FileBuilder::new);
}

public <T extends FileBuilder> T file(Path relativeFilePath, FileBuilderFactory<T> fileBuilderFactory) throws IOException {
int nameCount = relativeFilePath.getNameCount();
if (nameCount == 1) {
return fileBuilderFactory.create(directory.resolve(relativeFilePath));
}

DirectoryBuilder parentDirectory = directory(relativeFilePath.getParent());
return parentDirectory.file(relativeFilePath.subpath(1, relativeFilePath.getNameCount()), fileBuilderFactory);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.link_intersystems.gradle.project.builder;

import java.io.IOException;
import java.nio.file.Path;

public interface DirectoryBuilderFactory<T extends DirectoryBuilder> {

public T create(Path path) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
import static java.nio.file.StandardOpenOption.CREATE;
import static java.util.Objects.requireNonNull;

public class FileContent {
public class FileBuilder {

private final Path filepath;

public FileContent(Path filepath) {
public FileBuilder(Path filepath) throws IOException {
if (!Files.exists(filepath)) {
Files.createFile(filepath);
}
this.filepath = requireNonNull(filepath);
}

public Path getPath() {
return filepath;
}

public void append(String contentToAppend) {
append(appender -> {
appender.append(contentToAppend);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.link_intersystems.gradle.project.builder;

import java.io.IOException;
import java.nio.file.Path;

public interface FileBuilderFactory<T extends FileBuilder> {

public T create(Path path) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

public class GradleProjectBuilder extends AbstractProjectBuilder {

private final Path settingsFile;
private final FileBuilder settingsFile;

private final Path propertiesFile;
private final PropertiesFileBuilder propertiesFile;

public static GradleProjectBuilder rootProject(Path projectRoot) throws IOException {
return rootProject(projectRoot, ScriptLanguage.KTS);
Expand All @@ -25,10 +25,8 @@ public static GradleProjectBuilder rootProject(Path projectRoot, ScriptLanguage

public GradleProjectBuilder(Path projectRoot, ScriptLanguage scriptLanguage) throws IOException {
super(projectRoot, scriptLanguage);
settingsFile = projectRoot.resolve(scriptLanguage.settingsFileName());
Files.createFile(settingsFile);

propertiesFile = projectRoot.resolve("gradle.properties");
settingsFile = file(scriptLanguage.settingsFileName());
propertiesFile = file("gradle.properties", PropertiesFileBuilder::new);

this.projectRoot = requireNonNull(projectRoot);
}
Expand All @@ -50,18 +48,12 @@ public GradleSubprojectBuilder createSubproject(String path, ScriptLanguage scri
return new GradleSubprojectBuilder(getProjectRootDir().resolve(path), scriptLanguage);
}

public FileContent settingsFile() {
return new FileContent(settingsFile);
}

public PropertiesFileContent gradleProperties() {
return new PropertiesFileContent(propertiesFile);
public FileBuilder settingsFile() {
return settingsFile;
}

public FileContent file(String path) throws IOException {
Path filepath = projectRoot.resolve(path);
Files.createDirectories(filepath.getParent());
return new FileContent(filepath);
public PropertiesFileBuilder gradleProperties() {
return propertiesFile;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.link_intersystems.gradle.project.builder;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Properties;

public class PropertiesFileContent extends FileContent {
public class PropertiesFileBuilder extends FileBuilder {

public PropertiesFileContent(Path filepath) {
public PropertiesFileBuilder(Path filepath) throws IOException {
super(filepath);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.link_intersystems.gradle.project.builder;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;

class DirectoryBuilderTest {

@Test
void build(@TempDir Path tempDir) throws IOException {
DirectoryBuilder directoryBuilder = new DirectoryBuilder(tempDir);

FileBuilder fileBuilder = directoryBuilder.file("rootFolder/parentFolder/file.txt");
fileBuilder.append("Hello");

assertThat(tempDir.resolve("rootFolder/parentFolder/file.txt")).hasContent("Hello");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,55 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;

class FileContentTest {
class FileBuilderTest {

@Test
void append(@TempDir Path tempPath) {
void append(@TempDir Path tempPath) throws IOException {
Path testFile = tempPath.resolve("test.txt");
FileContent fileContent = new FileContent(testFile);
FileBuilder fileBuilder = new FileBuilder(testFile);

fileContent.append("Hello");
fileContent.append(" World");
fileBuilder.append("Hello");
fileBuilder.append(" World");

assertThat(testFile).hasContent("Hello World");
}

@Test
void appendWithAppendable(@TempDir Path tempPath) {
void appendWithAppendable(@TempDir Path tempPath) throws IOException {

Path testFile = tempPath.resolve("test.txt");
FileContent fileContent = new FileContent(testFile);
FileBuilder fileBuilder = new FileBuilder(testFile);

fileContent.append(appendable -> {
fileBuilder.append(appendable -> {
appendable.append("Hello World", 0, 5);
});

assertThat(testFile).hasContent("Hello");
}

@Test
void appendFromResource(@TempDir Path tempPath) {
void appendFromResource(@TempDir Path tempPath) throws IOException {

Path testFile = tempPath.resolve("test.txt");
FileContent fileContent = new FileContent(testFile);
FileBuilder fileBuilder = new FileBuilder(testFile);

fileContent.append(getClass().getResource("test.txt"));
fileBuilder.append(getClass().getResource("test.txt"));

assertThat(testFile).hasContent("Hello World");
}

@Test
void print(@TempDir Path tempPath) {
void print(@TempDir Path tempPath) throws IOException {

Path testFile = tempPath.resolve("test.txt");
FileContent fileContent = new FileContent(testFile);
FileBuilder fileBuilder = new FileBuilder(testFile);

fileContent.append(printWriter -> {
fileBuilder.append(printWriter -> {
printWriter.println("plugins {");
printWriter.println("\t`java-library`");
printWriter.println("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

class PropertiesFileContentTest {
class PropertiesFileBuilderTest {

@Test
void writeProperties(@TempDir Path tempDir) throws IOException {
Expand All @@ -19,7 +19,7 @@ void writeProperties(@TempDir Path tempDir) throws IOException {
properties.setProperty("bar", "foo");

Path propertiesFilepath = tempDir.resolve("test.properties");
PropertiesFileContent propertiesContent = new PropertiesFileContent(propertiesFilepath);
PropertiesFileBuilder propertiesContent = new PropertiesFileBuilder(propertiesFilepath);
propertiesContent.append(properties);

Properties readProperties = new Properties();
Expand Down

0 comments on commit 121ca09

Please sign in to comment.