Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
Merge pull request #3 from TimNekk/develop
Browse files Browse the repository at this point in the history
Added output file support
  • Loading branch information
TimNekk authored Dec 30, 2022
2 parents b1c70b6 + e4e3b5b commit d63e229
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 58 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div align="center">
<h1>File Merger</h1>
<p>CLI tool for merging files considering dependencies</p>
<img src="https://cdn0.iconfinder.com/data/icons/file-58/512/merge-file-document-1024.png" height="300" alt="Logo">
</div>


### Restrictions

In order for this tool to find the dependencies, the files must contain a directive with the following format:

require ‘relative/path/from/root’

In case of a circular dependency, the tool will print an error and exit.

In case of a missing dependency, the tool will print a warning and continue.

### Usage

1. Run the application.
2. Enter the path to the root directory. _(Can be relative or absolute)_
3. Enter the path to the output file. _(Can be relative or absolute)_

### Example

To test the tool, you can use `example/` folder. It should work without any errors and warnings.

1. Run the application.
2. Type `example`
3. Press enter.
4. Type `output.txt`
5. Press enter.

The tool will print a mering order and create a file `output.txt` in the root directory with the merged content.
2 changes: 1 addition & 1 deletion example/Folder 2/File 2-2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require ‘Folder 1/File 1-1’
require ‘Folder 2/File 2-2
require ‘Folder 2/File 2-1

In pretium dictum lacinia. In rutrum, neque a dignissim maximus, dolor mi pretium ante, nec
volutpat justo dolor non nulla. Vivamus nec suscipit nisl, ornare luctus erat. Aliquam eget est
Expand Down
1 change: 0 additions & 1 deletion example/Folder 3/InFolder 3/File 3-1

This file was deleted.

4 changes: 4 additions & 0 deletions example/Folder 3/SubFolder 3/File 3-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue.
Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.
Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.
Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus.
60 changes: 33 additions & 27 deletions src/timnekk/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package timnekk;

import timnekk.exceptions.CanNotGetDependenciesException;
import timnekk.exceptions.DependenciesGettingException;
import timnekk.exceptions.CircularDependencyException;
import timnekk.exceptions.FileWritingException;
import timnekk.models.DependenciesFinder;
import timnekk.models.FileDependenciesFinder;
import timnekk.models.Graph;
Expand All @@ -19,12 +20,33 @@ public Application(InputStream inputStream, OutputStream outputStream) {
}

public void run() {
File root = getRootDirectory();
getGraphOfFiles(root).ifPresent(this::printGraphInfo);
File root = getFileFromUser("Enter root directory: ");
File output = getFileFromUser("Enter output file: ");

Optional<Graph<File>> graph = getGraphOfFiles(root);
if (graph.isEmpty()) {
return;
}

Optional<List<File>> sortedFiles = getSortedListOfFiles(graph.get());
if (sortedFiles.isEmpty()) {
return;
}

if (graph.get().hasMissingDependencies()) {
printStream.println("\nMissing dependencies: ");
printMissingDependencies(graph.get());
}

printStream.println("\nFiles merging order:");
printFilesMergingOrder(sortedFiles.get());

mergeContent(sortedFiles.get(), output);
printStream.println("\nFiles merged successfully to " + output.getAbsolutePath());
}

private File getRootDirectory() {
printStream.println("Enter the path to the root directory: ");
private File getFileFromUser(String promptMessage) {
printStream.println(promptMessage);
String path = scanner.nextLine();
return new File(path);
}
Expand All @@ -41,31 +63,12 @@ private Optional<Graph<File>> getGraphOfFiles(File root) {

try {
return Optional.of(new Graph<>(files, dependenciesFinder));
} catch (CanNotGetDependenciesException e) {
} catch (DependenciesGettingException e) {
printStream.println(e.getMessage());
return Optional.empty();
}
}

private void printGraphInfo(Graph<File> graph) {
Optional<List<File>> sortedFiles = getSortedListOfFiles(graph);

if (sortedFiles.isEmpty()) {
return;
}

if (graph.hasMissingDependencies()) {
printStream.println("\nMissing dependencies: ");
printMissingDependencies(graph);
}

printStream.println("\nFiles merging order:");
printFilesMergingOrder(sortedFiles.get());

printStream.println("\nMerged content:");
printMergedContent(sortedFiles.get());
}

private Optional<List<File>> getSortedListOfFiles(Graph<File> graph) {
try {
return Optional.of(graph.getSortedListOfItems());
Expand All @@ -83,13 +86,16 @@ private void printFilesMergingOrder(Collection<File> files) {
}
}

private void printMergedContent(Collection<File> files) {
private void mergeContent(Collection<File> files, File outputFile) {
Set<File> notReadFiles = new HashSet<>();
for (File file : files) {
try {
FileUtils.printFile(file, printStream);
FileUtils.addFileContentToFile(file, outputFile);
} catch (FileNotFoundException e) {
notReadFiles.add(file);
} catch (FileWritingException e) {
printStream.println(e.getMessage());
return;
}
}

Expand Down
26 changes: 14 additions & 12 deletions src/timnekk/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package timnekk;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import timnekk.exceptions.FileWritingException;

import java.io.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Scanner;
Expand Down Expand Up @@ -30,16 +30,18 @@ public static Set<File> getFiles(File directory) {
return files;
}

/**
* Prints contents of a file
*
* @param file File to print
* @throws FileNotFoundException if the file can not be read
*/
public static void printFile(File file, PrintStream printStream) throws FileNotFoundException {

public static void addFileContentToFile(File file, File fileToAdd)
throws FileNotFoundException, FileWritingException {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
printStream.println(scanner.nextLine());

try (Writer writer = new FileWriter(fileToAdd, true)) {
while (scanner.hasNextLine()) {
writer.write(scanner.nextLine());
writer.write(System.lineSeparator());
}
} catch (IOException e) {
throw new FileWritingException("Could not write to file: " + fileToAdd.getAbsolutePath(), e);
}
}
}
7 changes: 0 additions & 7 deletions src/timnekk/exceptions/CanNotGetDependenciesException.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/timnekk/exceptions/DependenciesGettingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package timnekk.exceptions;

public class DependenciesGettingException extends Exception {
public DependenciesGettingException(String message, Throwable cause) {
super(message, cause);
}
}
7 changes: 7 additions & 0 deletions src/timnekk/exceptions/FileWritingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package timnekk.exceptions;

public class FileWritingException extends Exception {
public FileWritingException(String message, Throwable cause) {
super(message, cause);
}
}
6 changes: 3 additions & 3 deletions src/timnekk/models/DependenciesFinder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package timnekk.models;

import timnekk.exceptions.CanNotGetDependenciesException;
import timnekk.exceptions.DependenciesGettingException;

import java.util.Set;

Expand All @@ -10,7 +10,7 @@ public interface DependenciesFinder<E> {
*
* @param item Item to find dependencies of
* @return Set of items that the item depends on
* @throws CanNotGetDependenciesException if dependencies can not be gotten
* @throws DependenciesGettingException if dependencies can not be gotten
*/
Set<E> findDependencies(E item) throws CanNotGetDependenciesException;
Set<E> findDependencies(E item) throws DependenciesGettingException;
}
8 changes: 4 additions & 4 deletions src/timnekk/models/FileDependenciesFinder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package timnekk.models;

import timnekk.exceptions.CanNotGetDependenciesException;
import timnekk.exceptions.DependenciesGettingException;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -23,16 +23,16 @@ public FileDependenciesFinder(File projectRoot) {
*
* @param file File to find dependencies of
* @return Set of files that the file depends on
* @throws CanNotGetDependenciesException if the file can not be read
* @throws DependenciesGettingException if the file can not be read
*/
public Set<File> findDependencies(File file) throws CanNotGetDependenciesException {
public Set<File> findDependencies(File file) throws DependenciesGettingException {
Set<File> dependencies = new HashSet<>();

Scanner scanner;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
throw new CanNotGetDependenciesException(
throw new DependenciesGettingException(
"Can not read file to find dependencies: " + file.getAbsolutePath(), e);
}

Expand Down
6 changes: 3 additions & 3 deletions src/timnekk/models/Graph.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package timnekk.models;

import timnekk.exceptions.CanNotGetDependenciesException;
import timnekk.exceptions.DependenciesGettingException;
import timnekk.exceptions.CircularDependencyException;

import java.util.*;
Expand All @@ -10,7 +10,7 @@ public final class Graph<E> {
private final Set<E> missingDependencies = new HashSet<>();
private final DependenciesFinder<E> dependenciesFinder;

public Graph(Set<E> items, DependenciesFinder<E> dependenciesFinder) throws CanNotGetDependenciesException {
public Graph(Set<E> items, DependenciesFinder<E> dependenciesFinder) throws DependenciesGettingException {
this.dependenciesFinder = dependenciesFinder;

createNodes(items);
Expand All @@ -29,7 +29,7 @@ private void createNodes(Set<E> items) {
}
}

private void addNodesDependencies() throws CanNotGetDependenciesException {
private void addNodesDependencies() throws DependenciesGettingException {
for (Node<E> node : nodes) {
Set<E> dependentItems = dependenciesFinder.findDependencies(node.getValue());

Expand Down

0 comments on commit d63e229

Please sign in to comment.