Use file-reader-writer (#290)
* Use file-reader-writer * Update changelog
This commit is contained in:
parent
7efd65c652
commit
ad0fc315f1
10 changed files with 43 additions and 118 deletions
|
@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
* next-release
|
||||||
|
|
||||||
|
** Changed
|
||||||
|
|
||||||
|
- Use `file-reader-writer` to replace own implementations (#290)
|
||||||
|
|
||||||
* [5.4.0] - 2020-03-18
|
* [5.4.0] - 2020-03-18
|
||||||
|
|
||||||
** Changed
|
** Changed
|
||||||
|
|
|
@ -41,6 +41,7 @@
|
||||||
<kemitix.checkstyle.ruleset.level>5-complexity</kemitix.checkstyle.ruleset.level>
|
<kemitix.checkstyle.ruleset.level>5-complexity</kemitix.checkstyle.ruleset.level>
|
||||||
<kemitix.checkstyle.ruleset.location>net/kemitix/checkstyle-${kemitix.checkstyle.ruleset.level}.xml</kemitix.checkstyle.ruleset.location>
|
<kemitix.checkstyle.ruleset.location>net/kemitix/checkstyle-${kemitix.checkstyle.ruleset.level}.xml</kemitix.checkstyle.ruleset.location>
|
||||||
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
|
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
|
||||||
|
<file-reader-writer.version>1.0.0</file-reader-writer.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@ -56,6 +57,11 @@
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.kemitix</groupId>
|
||||||
|
<artifactId>file-reader-writer</artifactId>
|
||||||
|
<version>${file-reader-writer.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package net.kemitix.checkstyle.ruleset.builder;
|
package net.kemitix.checkstyle.ruleset.builder;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import net.kemitix.files.FileReaderWriter;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@ -37,4 +38,14 @@ public class BuilderConfiguration {
|
||||||
private static <K, V> AbstractMap.SimpleEntry<K, V> entry(final K key, final V value) {
|
private static <K, V> AbstractMap.SimpleEntry<K, V> entry(final K key, final V value) {
|
||||||
return new AbstractMap.SimpleEntry<>(key, value);
|
return new AbstractMap.SimpleEntry<>(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrapper for reading and writing files.
|
||||||
|
*
|
||||||
|
* @return An instance of {@link FileReaderWriter}.
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public FileReaderWriter fileReaderWriter() {
|
||||||
|
return new FileReaderWriter();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
package net.kemitix.checkstyle.ruleset.builder;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class FileReader {
|
|
||||||
private static final String NEWLINE = "\n";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads the content from the file path.
|
|
||||||
*
|
|
||||||
* <p>File content will be read using UTF-8 encoding and line endings will
|
|
||||||
* be replaced with a newline (\n).</p>
|
|
||||||
*
|
|
||||||
* @param path the file to read
|
|
||||||
* @throws IOException if there is an error.
|
|
||||||
*/
|
|
||||||
public String read(final Path path) throws IOException {
|
|
||||||
return String.join(NEWLINE,
|
|
||||||
Files.readAllLines(path, StandardCharsets.UTF_8));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
package net.kemitix.checkstyle.ruleset.builder;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.StandardOpenOption;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class FileWriter {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes the content to the file path, replacing any existing file.
|
|
||||||
*
|
|
||||||
* <p>File content will be written using UTF-8 encoding.</p>
|
|
||||||
*
|
|
||||||
* @param path the file to write
|
|
||||||
* @param content the content to write
|
|
||||||
* @throws IOException if there is an error.
|
|
||||||
*/
|
|
||||||
public void write(
|
|
||||||
final Path path,
|
|
||||||
final String content
|
|
||||||
) throws IOException {
|
|
||||||
Files.write(path,
|
|
||||||
content.getBytes(StandardCharsets.UTF_8),
|
|
||||||
StandardOpenOption.TRUNCATE_EXISTING);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,10 +1,11 @@
|
||||||
package net.kemitix.checkstyle.ruleset.builder;
|
package net.kemitix.checkstyle.ruleset.builder;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import net.kemitix.files.FileReaderWriter;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.io.File;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,16 +20,15 @@ class ReadmeWriter implements CommandLineRunner {
|
||||||
private final TemplateProperties templateProperties;
|
private final TemplateProperties templateProperties;
|
||||||
private final OutputProperties outputProperties;
|
private final OutputProperties outputProperties;
|
||||||
private final ReadmeBuilder readmeBuilder;
|
private final ReadmeBuilder readmeBuilder;
|
||||||
private final FileReader fileReader;
|
private final FileReaderWriter fileReaderWriter;
|
||||||
private final FileWriter fileWriter;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(final String... args) throws Exception {
|
public void run(final String... args) throws Exception {
|
||||||
Path templatePath = templateProperties.getReadmeTemplate();
|
File templateFile = templateProperties.getReadmeTemplate();
|
||||||
String templateBody = fileReader.read(templatePath);
|
String templateBody = fileReaderWriter.read(templateFile);
|
||||||
Path outputPath = Paths.get(outputProperties.getReadme());
|
File outputFile = Paths.get(outputProperties.getReadme()).toFile();
|
||||||
String outputBody = readmeBuilder.build(templateBody);
|
String outputBody = readmeBuilder.build(templateBody);
|
||||||
fileWriter.write(outputPath, outputBody);
|
fileReaderWriter.write(outputFile, outputBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import lombok.Setter;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,7 +27,7 @@ class TemplateProperties {
|
||||||
/**
|
/**
|
||||||
* Template for README.md file.
|
* Template for README.md file.
|
||||||
*/
|
*/
|
||||||
private Path readmeTemplate;
|
private File readmeTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The directory containing the README fragments.
|
* The directory containing the README fragments.
|
||||||
|
|
|
@ -1,41 +0,0 @@
|
||||||
package net.kemitix.checkstyle.ruleset.builder;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.rules.TemporaryFolder;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
public class FileWriterReaderTest {
|
|
||||||
|
|
||||||
|
|
||||||
private String line1 = UUID.randomUUID().toString();
|
|
||||||
private String line2 = UUID.randomUUID().toString();
|
|
||||||
private String body = line1 + "\n" + line2;
|
|
||||||
|
|
||||||
@org.junit.Rule
|
|
||||||
public TemporaryFolder folder = new TemporaryFolder();
|
|
||||||
|
|
||||||
private Path path;
|
|
||||||
|
|
||||||
private FileWriter writer = new FileWriter();
|
|
||||||
private FileReader reader = new FileReader();
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws IOException {
|
|
||||||
path = folder.newFile().toPath();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void writeTheReadFile() throws IOException {
|
|
||||||
//when
|
|
||||||
writer.write(path, body);
|
|
||||||
String read = reader.read(path);
|
|
||||||
//then
|
|
||||||
assertThat(read).isEqualTo(body);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +1,14 @@
|
||||||
package net.kemitix.checkstyle.ruleset.builder;
|
package net.kemitix.checkstyle.ruleset.builder;
|
||||||
|
|
||||||
|
import net.kemitix.files.FileReaderWriter;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@ -28,16 +29,13 @@ public class ReadmeWriterTest {
|
||||||
private OutputProperties outputProperties = new OutputProperties();
|
private OutputProperties outputProperties = new OutputProperties();
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private FileReader fileReader;
|
private FileReaderWriter fileReaderWriter;
|
||||||
|
|
||||||
@Mock
|
|
||||||
private FileWriter fileWriter;
|
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private ReadmeWriter readmeWriter;
|
private ReadmeWriter readmeWriter;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private Path templatePath;
|
private File templateFile;
|
||||||
private String outputPath = UUID.randomUUID().toString();
|
private String outputPath = UUID.randomUUID().toString();
|
||||||
private String templateBody = UUID.randomUUID().toString();
|
private String templateBody = UUID.randomUUID().toString();
|
||||||
private String formattedOutput = UUID.randomUUID().toString();
|
private String formattedOutput = UUID.randomUUID().toString();
|
||||||
|
@ -48,11 +46,11 @@ public class ReadmeWriterTest {
|
||||||
|
|
||||||
readmeWriter =
|
readmeWriter =
|
||||||
new ReadmeWriter(templateProperties, outputProperties,
|
new ReadmeWriter(templateProperties, outputProperties,
|
||||||
readmeBuilder, fileReader, fileWriter);
|
readmeBuilder, fileReaderWriter);
|
||||||
|
|
||||||
templateProperties.setReadmeTemplate(templatePath);
|
templateProperties.setReadmeTemplate(templateFile);
|
||||||
outputProperties.setReadme(outputPath);
|
outputProperties.setReadme(outputPath);
|
||||||
given(fileReader.read(templatePath))
|
given(fileReaderWriter.read(templateFile))
|
||||||
.willReturn(templateBody);
|
.willReturn(templateBody);
|
||||||
given(readmeBuilder.build(templateBody))
|
given(readmeBuilder.build(templateBody))
|
||||||
.willReturn(formattedOutput);
|
.willReturn(formattedOutput);
|
||||||
|
@ -63,7 +61,8 @@ public class ReadmeWriterTest {
|
||||||
//when
|
//when
|
||||||
readmeWriter.run();
|
readmeWriter.run();
|
||||||
//then
|
//then
|
||||||
verify(fileWriter).write(Paths.get(outputPath), formattedOutput);
|
verify(fileReaderWriter)
|
||||||
|
.write(Paths.get(outputPath).toFile(), formattedOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import org.assertj.core.api.SoftAssertions;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
@ -25,7 +26,7 @@ public class TemplatePropertiesTest {
|
||||||
public void setAndGet() throws Exception {
|
public void setAndGet() throws Exception {
|
||||||
//given
|
//given
|
||||||
final Path checkstyleXml = Paths.get("checkstyle.xml");
|
final Path checkstyleXml = Paths.get("checkstyle.xml");
|
||||||
final Path readmeTemplate = Paths.get("readme.md");
|
final File readmeTemplate = Paths.get("readme.md").toFile();
|
||||||
final Path readmeFragments = Paths.get("readme.dir");
|
final Path readmeFragments = Paths.get("readme.dir");
|
||||||
//when
|
//when
|
||||||
templateProperties.setCheckstyleXml(checkstyleXml);
|
templateProperties.setCheckstyleXml(checkstyleXml);
|
||||||
|
|
Loading…
Reference in a new issue