Skip to content

Commit

Permalink
Merge pull request dita-ot#3616 from dita-ot/feature/cli-repeat
Browse files Browse the repository at this point in the history
Add repeat option to CLI to run process N times
  • Loading branch information
jelovirt committed Oct 26, 2020
2 parents c38b3d8 + 97a9978 commit aaea8fc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/dita/dost/invoker/ConversionArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class ConversionArguments extends Arguments {
* Project file
*/
File projectFile;
int repeat = 1;

public final List<String> inputs = new ArrayList<>();
private final List<String> resources = new ArrayList<>();
Expand Down Expand Up @@ -142,6 +143,8 @@ ConversionArguments parse(final String[] arguments) {
handleArgFilter(arg, args, ARGUMENTS.get(getArgumentName(arg)));
} else if (isLongForm(arg, "-resource") || arg.equals("-r")) {
handleArgResource(arg, args, ARGUMENTS.get(getArgumentName(arg)));
} else if (isLongForm(arg, "-repeat")) {
handleArgRepeat(arg, args);
} else if (ARGUMENTS.containsKey(getArgumentName(arg))) {
definedProps.putAll(handleParameterArg(arg, args, ARGUMENTS.get(getArgumentName(arg))));
} else if (getPluginArguments().containsKey(getArgumentName(arg))) {
Expand Down Expand Up @@ -301,6 +304,14 @@ private void handleArgPropertyFile(final String arg, final Deque<String> args) {
propertyFiles.addElement(entry.getValue());
}

private void handleArgRepeat(final String arg, final Deque<String> args) {
final Map.Entry<String, String> entry = parse(arg.substring(2), args);
if (entry.getValue() == null) {
throw new BuildException("You must repeat number");
}
repeat = Integer.parseInt(entry.getValue());
}

/**
* Handle the --nice argument.
*/
Expand Down Expand Up @@ -378,6 +389,7 @@ void printUsage(final boolean compact) {
buf
.options("l", "logfile", "file", locale.getString("conversion.option.logfile"))
.options(null, "propertyfile", "file", locale.getString("conversion.option.propertyfile"))
.options(null, "repeat", "num", locale.getString("conversion.option.repeat"))
.options("t", "temp", "dir", locale.getString("conversion.option.temp"));
final Set<String> builtin = ARGUMENTS.values().stream().map(arg -> arg.property).collect(Collectors.toSet());
final List<Element> params = toList(Plugins.getPluginConfiguration().getElementsByTagName("param"));
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/org/dita/dost/invoker/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class Main extends org.apache.tools.ant.Main implements AntMain {
* Set of properties that can be used by tasks.
*/
private List<Map<String, Object>> projectProps;
private int repeat;

/**
* Whether or not this instance has successfully been constructed and is
Expand Down Expand Up @@ -195,15 +196,27 @@ public void startAnt(final String[] args, final Properties additionalUserPropert
// expect the worst
int exitCode = 1;
try {
try {
for (Map<String, Object> props : projectProps) {
runBuild(coreLoader, props);
final long[] durations = new long[repeat];
for (int i = 0; i < repeat; i++) {
final long start = System.currentTimeMillis();
try {
for (Map<String, Object> props : projectProps) {
runBuild(coreLoader, props);
}
exitCode = 0;
} catch (final ExitStatusException ese) {
exitCode = ese.getStatus();
if (exitCode != 0) {
throw ese;
}
}
exitCode = 0;
} catch (final ExitStatusException ese) {
exitCode = ese.getStatus();
if (exitCode != 0) {
throw ese;
final long end = System.currentTimeMillis();
durations[i] = end - start;
}
if (repeat > 1) {
for (int i = 0; i < durations.length; i++) {
System.out.println(String.format(locale.getString("conversion.repeatDuration"),
i + 1, durations[i]));
}
}
} catch (final BuildException be) {
Expand Down Expand Up @@ -344,6 +357,7 @@ private void processArgs(final String[] arguments) {
} else {
projectProps = handleProject(conversionArgs.projectFile, definedProps);
}
repeat = conversionArgs.repeat;
// default values
if (!definedProps.containsKey(ANT_OUTPUT_DIR)) {
definedProps.put(ANT_OUTPUT_DIR, new File(new File("."), "out").getAbsolutePath());
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/cli_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ conversion.option.filter=Filter and flagging files. This option can be passed mu
conversion.option.output=Output directory
conversion.option.logfile=Write log messages to file
conversion.option.propertyfile=Load all properties from file
conversion.option.repeat=Performs the transformation N times
conversion.repeatDuration=%d %dms
conversion.option.temp=Temporary directory
conversion.error.input_and_transformation_not_defined=Input file and transformation type not defined
conversion.error.transformation_not_defined=Transformation type not defined
Expand Down

0 comments on commit aaea8fc

Please sign in to comment.