Add flag for timings mode and fix it
MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/cli/AsciidoctorCliOptions.java
asciidoctorj-core/src/main/java/org/asciidoctor/cli/AsciidoctorCliOptions.java
@@ -36,12 +36,16 @@ public class AsciidoctorCliOptions {
public static final String BACKEND = "-b";
public static final String VERSION = "-V";
public static final String VERBOSE = "-v";
+ public static final String TIMINGS = "-t";
public static final char ATTRIBUTE_SEPARATOR = '=';
- public static final String MONITOR_OPTION_NAME = "monitor";
+ public static final String TIMINGS_OPTION_NAME = "timings";
@Parameter(names = { VERBOSE, "--verbose" }, description = "enable verbose mode (default: false)")
private boolean verbose = false;
+ @Parameter(names = { TIMINGS, "--timings" }, description = "enable timings mode (default: false)")
+ private boolean timings = false;
+
@Parameter(names = { VERSION, "--version" }, description = "display the version and runtime environment")
private boolean version = false;
@@ -144,6 +148,10 @@ public class AsciidoctorCliOptions {
return this.verbose;
}
+ public boolean isTimings() {
+ return this.timings;
+ }
+
public String getBackend() {
return this.backend;
}
@@ -292,10 +300,6 @@ public class AsciidoctorCliOptions {
optionsBuilder.inPlace(true);
}
- if (this.verbose) {
- optionsBuilder.option(MONITOR_OPTION_NAME, new HashMap<Object, Object>());
- }
-
attributesBuilder.attributes(getAttributes());
optionsBuilder.attributes(attributesBuilder.get());
return optionsBuilder.get();

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/cli/AsciidoctorInvoker.java
asciidoctorj-core/src/main/java/org/asciidoctor/cli/AsciidoctorInvoker.java
@@ -20,6 +20,7 @@ import org.asciidoctor.internal.RubyUtils;
import com.beust.jcommander.JCommander;
import org.jruby.Main;
+import org.jruby.runtime.builtin.IRubyObject;
public class AsciidoctorInvoker {
@@ -34,9 +35,7 @@ public class AsciidoctorInvoker {
jCommander.usage();
} else {
- JRubyAsciidoctor asciidoctor = null;
-
- asciidoctor = buildAsciidoctorJInstance(asciidoctorCliOptions);
+ JRubyAsciidoctor asciidoctor = buildAsciidoctorJInstance(asciidoctorCliOptions);
if (asciidoctorCliOptions.isVersion()) {
System.out.println("AsciidoctorJ " + asciidoctor.asciidoctorVersion() + " [http://asciidoctor.org]");
@@ -65,27 +64,16 @@ public class AsciidoctorInvoker {
}
}
+ setTimingsMode(asciidoctor, asciidoctorCliOptions, options);
+
setVerboseLevel(asciidoctor, asciidoctorCliOptions);
String output = renderInput(asciidoctor, options, inputFiles);
- if (asciidoctorCliOptions.isVerbose()) {
-
+ if (asciidoctorCliOptions.isTimings()) {
Map<String, Object> optionsMap = options.map();
- Map<Object, Object> monitor = RubyHashUtil
- .convertRubyHashMapToMap((Map<Object, Object>) optionsMap
- .get(AsciidoctorCliOptions.MONITOR_OPTION_NAME));
-
- System.out.println(String.format(
- "Time to read and parse source: %05.5f",
- monitor.get("parse")));
- System.out.println(String.format(
- "Time to render document: %05.5f",
- monitor.get("render")));
- System.out.println(String.format(
- "Total time to read, parse and render: %05.5f",
- monitor.get("load_render")));
-
+ IRubyObject timings = (IRubyObject) optionsMap.get("timings");
+ timings.callMethod(JRubyRuntimeContext.get(asciidoctor).getCurrentContext(), "print_report");
}
if (!"".equals(output.trim())) {
@@ -94,6 +82,13 @@ public class AsciidoctorInvoker {
}
}
+ private void setTimingsMode(Asciidoctor asciidoctor, AsciidoctorCliOptions asciidoctorCliOptions, Options options) {
+ if (asciidoctorCliOptions.isTimings()) {
+ options.setOption("timings",
+ JRubyRuntimeContext.get(asciidoctor).evalScriptlet("Asciidoctor::Timings.new"));
+ }
+ }
+
private void setVerboseLevel(JRubyAsciidoctor asciidoctor, AsciidoctorCliOptions asciidoctorCliOptions) {
if(asciidoctorCliOptions.isVerbose()) {
RubyUtils.setGlobalVariable(asciidoctor.getRubyRuntime(), "VERBOSE", "true");

MODIFY
asciidoctorj-core/src/test/java/org/asciidoctor/cli/WhenAsciidoctorIsCalledUsingCli.java
asciidoctorj-core/src/test/java/org/asciidoctor/cli/WhenAsciidoctorIsCalledUsingCli.java
package org.asciidoctor.cli;
+import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
@@ -117,7 +118,7 @@ public class WhenAsciidoctorIsCalledUsingCli {
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke("-D", outputDirectory.getAbsolutePath(), inputPath);
- File expectedFile = new File(inputPath.replaceFirst("\\.asciidoc$", ".html"));
+ File expectedFile = new File(outputDirectory, inputFile.getName().replaceFirst("\\.asciidoc$", ".html"));
assertThat(expectedFile.exists(), is(true));
}
@@ -231,11 +232,11 @@ public class WhenAsciidoctorIsCalledUsingCli {
File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
- new AsciidoctorInvoker().invoke("--verbose", inputPath);
+ new AsciidoctorInvoker().invoke("--timings", inputPath);
String outputConsole = output.toString();
- assertThat(outputConsole, startsWith("Time to read and parse source"));
-
+ assertThat(outputConsole, startsWith(" Time to read and parse source:"));
+ assertThat(outputConsole, not(containsString("null")));
}
@Test

Add flag for timings mode and fix it