Skip to content

Commit

Permalink
support mutually exclusive options
Browse files Browse the repository at this point in the history
  • Loading branch information
manaswinidas committed Apr 28, 2020
1 parent 5d73dc2 commit 2ea743e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.net.URI;
import java.nio.file.Path;

public class Configuration {
private File sourceDirectory;
Expand All @@ -20,6 +19,14 @@ public Configuration(File sourceDirectory, File outputDirectory) {
this.outputDirectory = outputDirectory;
}

public Configuration(File sourceDirectory, String outputRepo, String outputBranch) {
assert sourceDirectory.isDirectory();

this.sourceDirectory = sourceDirectory;
this.outputRepo = outputRepo;
this.outputBranch = outputBranch;
}

public Configuration(String sourcePathName, String outputPathName) {
assert new File(sourcePathName).isDirectory();
assert new File(outputPathName).isDirectory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,53 @@
import org.asciidoctor.ast.Document;
import org.asciidoctor.jruby.AsciiDocDirectoryWalker;
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "extract", mixinStandardHelpOptions = true, version = "1.0",
description = "Create a modular documentation layout from a directory of asciidoc files.")
@Command(name = "extract", mixinStandardHelpOptions = true, version = "1.0", description = "Create a modular documentation layout from a directory of asciidoc files.")
public class ExtractionRunner implements Callable<Integer> {
private List<Assembly> assemblies;
private Set<ExtractedModule> modules;
private List<Issue> issues = new ArrayList<>();
@ArgGroup(heading = "Input", exclusive = true, multiplicity = "1")
InputOptions inputOptions;
@ArgGroup(heading = "Output", exclusive = true, multiplicity = "1")
OutputOptions outputOptions;

@Option(names = {"-s", "--sourceDir"}, description = "Directory containing the input asciidoc files.")
File inputDir;
static class InputOptions {
@Option(names = { "-s", "--sourceDir" }, description = "Directory containing the input asciidoc files.")
File inputDir;

@Option(names = {"-o", "--outputDir"}, description = "Directory to place generated modules and assemblies.")
File outputDir;
@ArgGroup(exclusive = false)
GitInputOptions gitInputOptions;
}

static class OutputOptions {
@Option(names = { "-o", "--outputDir" }, description = "Directory to place generated modules and assemblies.")
File outputDir;

@ArgGroup(exclusive = false)
GitOutputOptions gitOutputOptions;
}

@Option(names = {"-sr", "--sourceRepo"}, description = "Git URL to the source repository.")
String sourceRepo;
static class GitInputOptions {
@Option(names = { "-sr", "--sourceRepo" }, description = "Git URL to the source repository.", required = true)
String sourceRepo;

@Option(names = {"-sb", "--sourceBranch"}, defaultValue = "master", description = "Branch in source repository.")
String sourceBranch;
@Option(names = { "-sb",
"--sourceBranch" }, defaultValue = "master", description = "Branch in source repository.")
String sourceBranch;
}

@Option(names = {"-or", "--outputRepo"}, description = "Git URL to the output repository.")
String outputRepo;
static class GitOutputOptions {
@Option(names = { "-or", "--outputRepo" }, description = "Git URL to the output repository.", required = true)
String outputRepo;

@Option(names = {"-ob", "--outputBranch"}, defaultValue = "master", description = "Branch in output repository.")
String outputBranch;
@Option(names = { "-ob",
"--outputBranch" }, defaultValue = "master", description = "Branch in output repository.")
String outputBranch;
}

public ExtractionRunner() {
this.assemblies = new ArrayList<>();
Expand All @@ -73,7 +93,7 @@ public static void main(String... args) {

@Override
public Integer call() {
var config = new Configuration(this.inputDir, this.outputDir);
var config = new Configuration(this.inputOptions.inputDir, this.outputOptions.outputDir);
var preprocessor = new ReaderPreprocessor();

OptionsBuilder optionsBuilder = OptionsBuilder.options();
Expand Down Expand Up @@ -104,7 +124,8 @@ public Integer call() {
}

/**
* returns true if all went well. false if there was some problem with uniqueness.
* returns true if all went well. false if there was some problem with
* uniqueness.
*/
private void findSections(Document doc, List<String> lines) {
// TODO: This should probably be configurable
Expand Down Expand Up @@ -133,14 +154,11 @@ private void writeAssemblies(Configuration config) {
this.assemblies.forEach(a -> {
try {
// Create any directories that need to be created
Path assembliesDir = Files.createDirectories(config.getOutputDirectory().toPath().resolve("assemblies"));
Path assembliesDir = Files
.createDirectories(config.getOutputDirectory().toPath().resolve("assemblies"));
var outputFile = Paths.get(assembliesDir.toString(), a.getFilename());
try (Writer output = new FileWriter(outputFile.toFile())) {
output.append(templateStart)
.append("\n")
.append(a.getSource())
.append("\n")
.append(templateEnd);
output.append(templateStart).append("\n").append(a.getSource()).append("\n").append(templateEnd);
}
} catch (IOException e) {
// TODO: We blew-up in an unexpected way, handle this
Expand Down Expand Up @@ -193,10 +211,11 @@ private void moveNonadoc(Configuration config) {
var destinationDir = assetsDir.toFile().toPath();
var adocExtRegex = Pattern.compile("^[^_.].*\\.a((sc(iidoc)?)|d(oc)?)$");

Files.walkFileTree(sourceDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
Integer.MAX_VALUE, new SimpleFileVisitor<>() {
Files.walkFileTree(sourceDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
new SimpleFileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
var targetDir = destinationDir.resolve(sourceDir.relativize(dir));

// Create the directory structure in the new location
Expand Down

0 comments on commit 2ea743e

Please sign in to comment.