Skip to content

Commit

Permalink
Merge pull request #41 from collate/batch-mode
Browse files Browse the repository at this point in the history
Add batch mode
  • Loading branch information
sebastianquek authored Oct 17, 2016
2 parents 9fb1156 + 08dfd05 commit 6d44ba2
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 52 deletions.
4 changes: 2 additions & 2 deletions docs/Developer-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Command | `parse(String userInput)`: Analyses the given `userInput` to determine

## `Storage` Class
`Storage` is a simple class that has one public method which takes in a list of lines and saves them to a
local file in a default folder. This default folder is defined in this class.
local file in the a specified output folder. This output folder is specified in the constructor.

The current implementation saves collated data in Markdown files. These files are human-readable and editable,
and utilises syntax from [Github Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/).
Expand Down Expand Up @@ -187,7 +187,7 @@ lines of code of all code snippets and is used to calculate the overall proporti
## `SourceFile` Class
This class represents source files that contain at least one author tag. Each `SourceFile` object is
constructed with the file's path relative to the user's specified folder and the file's language.
The base folder is specified through the `collate from <FOLDER>` command.
The base folder is specified through the `collate from <INPUT_FOLDER>` command.

# GUI Component

Expand Down
46 changes: 28 additions & 18 deletions docs/User-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Now that you know what Collate is [about](../README.md), you can follow this gui
- [Viewing the summary](#viewing-the-summary)
- [Exiting](#exiting)
- [Using the Text UI](#using-the-text-ui)
- [Using the Batch mode](#using-the-batch-mode)
- [Cheatsheet](#cheatsheet)

<!-- /MarkdownTOC -->
Expand All @@ -37,11 +38,13 @@ Now that you know what Collate is [about](../README.md), you can follow this gui
![gui collate all subfolders](images/user-guide/gui-collate-all-subfolders.gif)

5. **Ask Collate to collate your code**. As shown in the above animation, you can type
`collate from <FOLDER>` to collate all files within `<FOLDER>` including subfolders.
`collate from <INPUT_FOLDER>` to collate all files within `<INPUT_FOLDER>`
including subfolders.

6. **View reports**: The reports can be found in a folder called `collated`.
6. **View reports**: The reports can be found in the same folder as the jar file.
You'll find one `.md` for each author, containing the code written by that author.
[Here](https://github.com/collate/collate/blob/master/collated-example/Sebastian.md) is an example.
[Here](https://github.com/collate/collate/blob/master/collated-example/Sebastian.md) is an example.<br>
To specify a different output folder, user the command `collate from <INPUT_FOLDER> to <OUTPUT_FOLDER>`

7. **Try more commands**:
* `view <AUTHOR'S NAME>` - See author's individual statistics e.g. `view Sebastian`
Expand Down Expand Up @@ -71,14 +74,14 @@ enter his/her author tags.

To collate files in a folder and all its sub folders, use this command.

`collate from <FOLDER>`
`collate from <INPUT_FOLDER> to <OUTPUT_FOLDER>` (the `to <OUTPUT_FOLDER>` part is optional)

> Tip: If your folder includes whitespaces, you will need to surround the folder with double inverted commas.
Examples:
* `collate from src`
* `collate from src to collated`
* `collate from .`
* `collate from C:/myProject/component1/src`
* `collate from C:/myProject/component1/src to collated/src`
* `collate from "C:/source files"`

![gui collate all subfolders](images/user-guide/gui-collate-all-subfolders.gif)
Expand All @@ -87,7 +90,7 @@ Examples:
If you would like to collate files only from the specified folder, and not files from sub folders,
you can add the `only` option.

`collate from <FOLDER> only` or `collate only from <FOLDER>`
`collate from <INPUT_FOLDER> only` or `collate only from <INPUT_FOLDER>`

> Tip: option order of the `collate` command is flexible. You need not worry about the position of these options,
just remember to start each command with the `collate` keyword.
Expand All @@ -100,15 +103,15 @@ Examples:
Collate scans the folder you specified for all types of file. What if you want to collate only `java` files?
Well, you can use the `include` option.

`collate from <FOLDER> include java`
`collate from <INPUT_FOLDER> include java`

You can include multiple filetypes by separating them with commas.

`collate from <FOLDER> include java, css, fxml`
`collate from <INPUT_FOLDER> include java, css, fxml`

Examples:
* `collate from C:/src java`
* `collate from C:/src java,md,css,txt`
* `collate from C:/src include java`
* `collate from C:/src to ./collated include java,md,css,txt`

## Viewing an author's statistics
Viewing an author's contribution statistics can be done by entering the following command.
Expand Down Expand Up @@ -150,13 +153,20 @@ Here are some examples:

![tui summary view](images/user-guide/tui-summary.gif)

## Using the batch mode

Collate Text UI can be run in 'batch mode' (i.e. non-interactive CLI) to execute a command in one shot.
Here's an example.<br>
`java -jar Collate-TUI.jar collate from src to collated`

# Cheatsheet
Command | Description
--------| ------------
`collate from <FOLDER>` | Collate all files within `<FOLDER>` including subfolders
`collate from <FOLDER> only` | Collate files in `<FOLDER>` only
`collate from <FOLDER> include <FILETYPE1>, <FILETYPE2>` | Collate `<FILETYPE1>` and `<FILETYPE2>` files in `<FOLDER>` and its subfolders
`collate from <FOLDER> only include <FILETYPE1>` | Collate `<FILETYPE1>` files in `<FOLDER>` only
`view <AUTHOR'S NAME>` | See author's individual statistics
`summary` | See default statistics summary table
`exit` | Exit Collate
`collate from <INPUT_FOLDER>` | Collates all files within `<INPUT_FOLDER>` including subfolders and puts the collated files in the same folder
`collate from <INPUT_FOLDER> to <OUTPUT_FOLDER>` | Collates all files within `<INPUT_FOLDER>` including subfolders and puts the collated files in the `<OUTPUT_FOLDER>`
`collate from <INPUT_FOLDER> only` | Collates files in `<INPUT_FOLDER>` only
`collate from <INPUT_FOLDER> include <FILETYPE1>, <FILETYPE2>` | Collates `<FILETYPE1>` and `<FILETYPE2>` files in `<INPUT_FOLDER>` and its subfolders
`collate from <INPUT_FOLDER> only include <FILETYPE1>` | Collates `<FILETYPE1>` files in `<INPUT_FOLDER>` only
`view <AUTHOR'S NAME>` | Shows author's individual statistics
`summary` | Shows default statistics summary table
`exit` | Exits Collate
19 changes: 14 additions & 5 deletions src/main/java/backend/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public enum Type {
}

private Type type;
private String directory;
private String readDirectory;
private String saveDirectory;
private boolean scanCurrentDirOnly;
private ArrayList<String> fileTypes;

Expand All @@ -37,12 +38,20 @@ public Type getCommandType() {
// "Collate" command methods
// ================================================================

public String getDirectory() {
return directory;
public String getReadDirectory() {
return readDirectory;
}

public void setDirectory(String directory) {
this.directory = directory;
public String getSaveDirectory() {
return saveDirectory;
}

public void setReadDirectory(String directory) {
this.readDirectory = directory;
}

public void setSaveDirectory(String directory) {
this.saveDirectory = directory;
}

public boolean willScanCurrentDirOnly() {
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/backend/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public class CommandParser {
private static final String USER_COMMAND_SUMMARY = "summary";
private static final String USER_COMMAND_EXIT = "exit";

private static final String[] KEYWORDS = {"from", "only", "include"};
private static final String KEYWORD_DIRECTORY = KEYWORDS[0];
private static final String KEYWORD_SCAN_CURRENT_DIR_ONLY = KEYWORDS[1];
private static final String KEYWORD_INCLUDE = KEYWORDS[2];
private static final String[] KEYWORDS = {"from", "to", "only", "include"};
private static final String KEYWORD_FROM_DIRECTORY = KEYWORDS[0];
private static final String KEYWORD_TO_DIRECTORY = KEYWORDS[1];
private static final String KEYWORD_SCAN_CURRENT_DIR_ONLY = KEYWORDS[2];
private static final String KEYWORD_INCLUDE = KEYWORDS[3];

public CommandParser() {
}
Expand Down Expand Up @@ -88,12 +89,14 @@ private ArrayList<String> getUserArguments(ArrayList<String> parameters) {
// ================================================================

private Command initCollateCommand(ArrayList<String> arguments) {
File directory = new File(findDirectory(arguments));
File fromDirectory = new File(findDirectory(arguments, KEYWORD_FROM_DIRECTORY));
File toDirectory = new File(findDirectory(arguments, KEYWORD_TO_DIRECTORY));
ArrayList<String> fileTypes = findIncludedFileTypes(arguments);

if (isValidCollateCommand(arguments, directory, fileTypes)) {
if (isValidCollateCommand(arguments, fromDirectory, fileTypes)) {
Command command = new Command(Command.Type.COLLATE);
command.setDirectory(directory.getAbsolutePath());
command.setReadDirectory(fromDirectory.getAbsolutePath());
command.setSaveDirectory(toDirectory.getAbsolutePath());
command.setScanCurrentDirOnly(hasScanCurrentDirOnlyKeyword(arguments));
command.setFileTypes(fileTypes);
return command;
Expand All @@ -102,9 +105,9 @@ private Command initCollateCommand(ArrayList<String> arguments) {
}
}

private String findDirectory(ArrayList<String> arguments) {
if (arguments.contains(KEYWORD_DIRECTORY)) {
int directoryIndex = arguments.indexOf(KEYWORD_DIRECTORY) + 1;
private String findDirectory(ArrayList<String> arguments, String directoryIdentifier) {
if (arguments.contains(directoryIdentifier)) {
int directoryIndex = arguments.indexOf(directoryIdentifier) + 1;

try {
return getFullDirectory(arguments, directoryIndex);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/backend/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Logic {
private CommandParser commandParser;
private Storage storage;
private HashMap<String, Author> authors;
private File rootDirectory;
private File readDirectory;
private Author targetAuthor;

private static final String AUTHOR_TAG = "@@author";
Expand Down Expand Up @@ -75,12 +75,12 @@ public Command.Type executeCommand(String userInput) {
private void handleCollate(Command command) {
resetVariables();

rootDirectory = new File(command.getDirectory());
storage = new Storage(rootDirectory);
readDirectory = new File(command.getReadDirectory());
storage = new Storage(command.getSaveDirectory());
boolean willScanCurrentDirOnly = command.willScanCurrentDirOnly();
ArrayList<String> fileTypes = command.getFileTypes();

traverseDirectory(rootDirectory, willScanCurrentDirOnly, fileTypes);
traverseDirectory(readDirectory, willScanCurrentDirOnly, fileTypes);
saveCollatedFiles();
}

Expand Down Expand Up @@ -201,11 +201,11 @@ private Author getAuthorByName(String authorName) {
private String generateRelativePath(String filePath) {
File newFile = new File(filePath);

if (newFile.equals(rootDirectory)) {
if (newFile.equals(readDirectory)) {
return newFile.getName();
} else {
return newFile.getAbsolutePath()
.replace(rootDirectory.getAbsolutePath(),
.replace(readDirectory.getAbsolutePath(),
STRING_EMPTY);
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/backend/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class Storage {

public static final String DEFAULT_SAVE_DIRECTORY = "collated";
private static final String SAVE_DIRECTORY_FORMAT = "%s" + File.separator + "collated";
private static final String SAVE_DIRECTORY_FORMAT = "%s";
private static final String COLLATED_FILE_PATH_FORMAT = "%s" + File.separator + "%s.md";
private static final String ERROR_FILE_NOT_FOUND = "File was not found: %s";

Expand All @@ -30,15 +30,22 @@ public Storage(File directory) {
}

public Storage() {
saveFolder = DEFAULT_SAVE_DIRECTORY;
createSaveDirectory(DEFAULT_SAVE_DIRECTORY);
this(DEFAULT_SAVE_DIRECTORY);
}

public Storage(String saveDirectory) {
saveFolder = saveDirectory;
createSaveDirectory(saveFolder);
}

private void createSaveDirectory(String directory) {
File dir = new File(directory);

if (!dir.exists()) {
dir.mkdir();
boolean isDirectoryCreated = dir.mkdirs();
if (!isDirectoryCreated) {
throw new RuntimeException("Could not create directory " + dir);
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/tui/CollateTui.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringJoiner;

import main.java.backend.Logic;
import main.java.data.Author;
Expand Down Expand Up @@ -72,6 +73,29 @@ public class CollateTui {
* @param args
*/
public static void main(String[] args) {
if (args.length == 0) {
runInteractiveMode();
} else {
runBatchMode(args);
}
}

private static void runBatchMode(String[] args) {
Logic logic = new Logic();
String command = stitchArgs(args);
System.out.println("Executing command " + command);
logic.executeCommand(command);
}

private static String stitchArgs(String args[]) {
StringJoiner joiner = new StringJoiner(" ");
for (String s: args) {
joiner.add(s);
}
return joiner.toString();
}

private static void runInteractiveMode() {
Scanner input = new Scanner(System.in);
PrintStream output = System.out;
Logic logic = new Logic();
Expand Down
14 changes: 7 additions & 7 deletions src/test/TestCommandParserAndCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public void testCollateCommand() {
// Normal collate
command = commandParser.parse("collate from \"" + FULL_TEST_RES_DIR + "\"");
assertEquals(Command.Type.COLLATE, command.getCommandType());
assertEquals(FULL_TEST_RES_DIR, command.getDirectory());
assertEquals(FULL_TEST_RES_DIR, command.getReadDirectory());
assertFalse(command.willScanCurrentDirOnly());
assertEquals(0, command.getFileTypes().size());

// Collate with extra spaces
command = commandParser.parse("collate from \"" + FULL_TEST_RES_DIR +
"\"");
assertEquals(Command.Type.COLLATE, command.getCommandType());
assertEquals(FULL_TEST_RES_DIR, command.getDirectory());
assertEquals(FULL_TEST_RES_DIR, command.getReadDirectory());
assertFalse(command.willScanCurrentDirOnly());
assertEquals(0, command.getFileTypes().size());

Expand All @@ -53,15 +53,15 @@ public void testCollateCommand() {
File.separator + "subfolder with spaces\"");
assertEquals(Command.Type.COLLATE, command.getCommandType());
assertEquals(FULL_TEST_RES_DIR + File.separator + "subfolder with spaces",
command.getDirectory());
command.getReadDirectory());
assertFalse(command.willScanCurrentDirOnly());
assertEquals(0, command.getFileTypes().size());

// Collate folder without sub-folders
command = commandParser.parse("collate from \"" + FULL_TEST_RES_DIR +
"\" only");
assertEquals(Command.Type.COLLATE, command.getCommandType());
assertEquals(FULL_TEST_RES_DIR, command.getDirectory());
assertEquals(FULL_TEST_RES_DIR, command.getReadDirectory());
assertTrue(command.willScanCurrentDirOnly());
assertEquals(0, command.getFileTypes().size());

Expand All @@ -70,7 +70,7 @@ public void testCollateCommand() {
command = commandParser.parse("collate from \"" + FULL_TEST_RES_DIR +
"\" include java, css");
assertEquals(Command.Type.COLLATE, command.getCommandType());
assertEquals(FULL_TEST_RES_DIR, command.getDirectory());
assertEquals(FULL_TEST_RES_DIR, command.getReadDirectory());
assertFalse(command.willScanCurrentDirOnly());
assertEquals(2, command.getFileTypes().size());
assertEquals("java", command.getFileTypes().get(0));
Expand All @@ -81,7 +81,7 @@ public void testCollateCommand() {
command = commandParser.parse("collate include css, FXML from \"" +
FULL_TEST_RES_DIR + "\"");
assertEquals(Command.Type.COLLATE, command.getCommandType());
assertEquals(FULL_TEST_RES_DIR, command.getDirectory());
assertEquals(FULL_TEST_RES_DIR, command.getReadDirectory());
assertFalse(command.willScanCurrentDirOnly());
assertEquals(2, command.getFileTypes().size());
assertEquals("css", command.getFileTypes().get(0));
Expand All @@ -94,7 +94,7 @@ public void testCollateCommand() {
"subfolder with spaces\" only");
assertEquals(Command.Type.COLLATE, command.getCommandType());
assertEquals(FULL_TEST_RES_DIR + File.separator + "subfolder with spaces",
command.getDirectory());
command.getReadDirectory());
assertTrue(command.willScanCurrentDirOnly());
assertEquals(1, command.getFileTypes().size());
}
Expand Down

0 comments on commit 6d44ba2

Please sign in to comment.