Skip to content

Commit

Permalink
Add support for javac-style @params files
Browse files Browse the repository at this point in the history
Fixes #77

MOE_MIGRATED_REVID=172163309
  • Loading branch information
sormuras authored and cushon committed Oct 14, 2017
1 parent 1d60175 commit 598f248
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@

package com.google.googlejavaformat.java;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

Expand All @@ -25,11 +34,15 @@ final class CommandLineOptionsParser {

private static final Splitter COMMA_SPLITTER = Splitter.on(',');
private static final Splitter COLON_SPLITTER = Splitter.on(':');
private static final Splitter ARG_SPLITTER =
Splitter.on(CharMatcher.breakingWhitespace()).omitEmptyStrings().trimResults();

/** Parses {@link CommandLineOptions}. */
static CommandLineOptions parse(Iterable<String> options) {
CommandLineOptions.Builder optionsBuilder = CommandLineOptions.builder();
Iterator<String> it = options.iterator();
List<String> expandedOptions = new ArrayList<>();
expandParamsFiles(options, expandedOptions);
Iterator<String> it = expandedOptions.iterator();
while (it.hasNext()) {
String option = it.next();
if (!option.startsWith("-")) {
Expand Down Expand Up @@ -158,4 +171,29 @@ private static Range<Integer> parseRange(String arg) {
throw new IllegalArgumentException(arg);
}
}

/**
* Pre-processes an argument list, expanding arguments of the form {@code @filename} by reading
* the content of the file and appending whitespace-delimited options to {@code arguments}.
*/
private static void expandParamsFiles(Iterable<String> args, List<String> expanded) {
for (String arg : args) {
if (arg.isEmpty()) {
continue;
}
if (!arg.startsWith("@")) {
expanded.add(arg);
} else if (arg.startsWith("@@")) {
expanded.add(arg.substring(1));
} else {
Path path = Paths.get(arg.substring(1));
try {
String sequence = new String(Files.readAllBytes(path), UTF_8);
expandParamsFiles(ARG_SPLITTER.split(sequence), expanded);
} catch (IOException e) {
throw new UncheckedIOException(path + ": could not read file: " + e.getMessage(), e);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.common.base.Joiner;

/** Checked exception class for formatter command-line usage errors. */
public final class UsageException extends Exception {
final class UsageException extends Exception {

private static final Joiner NEWLINE_JOINER = Joiner.on(System.lineSeparator());

Expand All @@ -37,7 +37,7 @@ public final class UsageException extends Exception {
" -",
" Format stdin -> stdout",
" --aosp, -aosp, -a",
" Use AOSP style instead of Google Style (4-space indentation)",
" Use AOSP style instead of Google Style (4-space indentation).",
" --fix-imports-only",
" Fix import order and remove any unused imports, but do no other formatting.",
" --skip-sorting-imports",
Expand All @@ -56,9 +56,11 @@ public final class UsageException extends Exception {
" --offset, -offset",
" Character offset to format (0-based; default is all).",
" --help, -help, -h",
" Print this usage statement",
" Print this usage statement.",
" --version, -version, -v",
" Print the version.",
" @<filename>",
" Read options and filenames from file.",
"",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
package com.google.googlejavaformat.java;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.fail;

import com.google.common.collect.Range;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** {@link CommandLineOptionsParser}Test */
@RunWith(JUnit4.class)
public class CommandLineOptionsParserTest {

@Rule public TemporaryFolder testFolder = new TemporaryFolder();

@Test
public void defaults() {
CommandLineOptions options = CommandLineOptionsParser.parse(Collections.<String>emptyList());
Expand Down Expand Up @@ -152,4 +160,19 @@ public void illegalLines() {
}
}

@Test
public void paramsFile() throws IOException {
Path outer = testFolder.newFile("outer").toPath();
Path exit = testFolder.newFile("exit").toPath();
Path nested = testFolder.newFile("nested").toPath();

String[] args = {"--dry-run", "@" + exit, "L", "@" + outer, "Q"};

Files.write(exit, "--set-exit-if-changed".getBytes(UTF_8));
Files.write(outer, ("M\n@" + nested.toAbsolutePath() + "\nP").getBytes(UTF_8));
Files.write(nested, "ℕ\n\n \n@@O\n".getBytes(UTF_8));

CommandLineOptions options = CommandLineOptionsParser.parse(Arrays.asList(args));
assertThat(options.files()).containsExactly("L", "M", "ℕ", "@O", "P", "Q");
}
}

0 comments on commit 598f248

Please sign in to comment.