package diffr.diff;
import com.google.common.base.Optional;
import com.google.common.io.Files;
import diffr.util.ArgumentsProcessor;
import diffr.util.instruction.Instruction;
import diffr.util.instruction.InstructionComposer;
import diffr.util.instruction.Instructions;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
/**
* Main entry point to diffr's DIFF tool.
*
*
* Expects two arguments:
*
* - <original-file> - The original file to diff.
* - <new-file> - The new version of the original file to diff.
*
*
*
* @author Jakub D Kozlowski
* @author Sarina Gurung
* @since 0.1
*/
public final class Main {
/**
* Prints the usage of this tool.
*/
private static void printUsage() {
System.out.println("Usage: \n" +
" diffr \n" +
" diffr -o ");
}
/**
* Runs the diff tool on two files.
*
* @param args arguments to this tool.
*
* @return exit code.
*/
public static int run(String... args) {
try {
if (ArgumentsProcessor.containsHelpArgument(args)
|| (2 != args.length
&& 4 != args.length)) {
printUsage();
return -1;
}
final File firstFile = new File(args[0]);
final File secondFile = new File(args[1]);
if (!firstFile.exists()) {
System.err.println("File " + firstFile + " not found.");
return -1;
}
if (!secondFile.exists()) {
System.err.println("File " + secondFile + " not found.");
return -1;
}
final List originalFile = Files.readLines(firstFile, Charset.defaultCharset());
final List newFile = Files.readLines(secondFile, Charset.defaultCharset());
final List instructions = new Diffr(originalFile, newFile).diff();
final Optional outputFile = ArgumentsProcessor.extractOutputFile(args);
if (4 == args.length
&& outputFile.isPresent()) {
final File file = new File(outputFile.get());
final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
for (final Instruction instruction : instructions) {
Instructions.writeInstruction(instruction, bufferedWriter);
}
bufferedWriter.close();
}
else {
for (final Instruction instruction : instructions) {
System.out.println(InstructionComposer.composeString(instruction));
}
System.out.flush();
}
return 0;
}
catch (final IOException io) {
System.err.println("There was a problem reading the files: " + io);
return -1;
}
}
/**
* Invokes {@link #run(String...)} and calls {@link System#exit(int)}.
*
* @param args arguments to this tool.
*/
public static void main(String... args) {
System.exit(run(args));
}
}