Skip to content

Commit

Permalink
refs cukespace#7 - removes event as it is not necessary anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Feb 1, 2018
1 parent a9cfa24 commit 67c3a44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.cukespace.arquillian.asciidoctor;

import com.github.cukespace.arquillian.asciidoctor.api.event.RenderDocsEvent;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.asciidoctor.Asciidoctor;
Expand Down Expand Up @@ -40,7 +39,7 @@ public class AsciidoctorObserver {

private static final Pattern ASCIIDOC_EXTENSION_PATTERN = Pattern.compile("^[^_.].*\\.a((sc(iidoc)?)|d(oc)?)$");

private static Map<String,Asciidoctor> asciidoctorMap = new HashMap<>();
private static Map<String, Asciidoctor> asciidoctorMap = new HashMap<>();

@Inject
private Instance<ArquillianDescriptor> descriptorInstance;
Expand All @@ -56,25 +55,27 @@ public void run() {
long initialTime = System.currentTimeMillis();
initAsciidoctor(descriptor);
getLogger().info(String.format("Asciidoctor successfully initialized in %s milliseconds", System.currentTimeMillis() - initialTime));
}catch (Exception e){
getLogger().log(Level.SEVERE, "Could not initilize Asciidoctor instance", e);
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Could not initialize Asciidoctor instance", e);
}
}
}, "arquillian-asciidoctor-thread");
adocThread.setDaemon(true);
adocThread.start();
}



private void initAsciidoctor(ArquillianDescriptor arquillianDescriptor) {

for (final ExtensionDef extensionDef : arquillianDescriptor.getExtensions()) {
if (extensionDef.getExtensionName().startsWith("asciidoctor")) {
String gemPath = get(extensionDef.getExtensionProperties(), "gemPath", "");
Asciidoctor asciidoctor = asciidoctorMap.get(gemPath);
if(asciidoctor == null){
asciidoctor = Asciidoctor.Factory.create((File.separatorChar == '\\') ? gemPath.replaceAll("\\\\", "/") : gemPath);
asciidoctorMap.put(gemPath,asciidoctor);
}
String gemPath = get(extensionDef.getExtensionProperties(), "gemPath", "");
Asciidoctor asciidoctor = asciidoctorMap.get(gemPath);
if (asciidoctor == null) {
asciidoctor = Asciidoctor.Factory.create((File.separatorChar == '\\') ? gemPath.replaceAll("\\\\", "/") : gemPath);
asciidoctorMap.put(gemPath, asciidoctor);
}
}
}
}
Expand All @@ -88,20 +89,14 @@ public void stop(@Observes final EventContext<ManagerStopping> ending) {
}
}

public void render(@Observes final RenderDocsEvent renderDocsEvent) {
ArquillianDescriptor descriptor = renderDocsEvent.getDescriptor();
initAsciidoctor(descriptor);
renderAll(descriptor);
}


private void renderAll(final ArquillianDescriptor descriptor) {
for (final ExtensionDef extensionDef : descriptor.getExtensions()) {
if (extensionDef.getExtensionName().startsWith("asciidoctor")) {
long initialTime = System.currentTimeMillis();
try {
render(extensionDef.getExtensionName(), extensionDef.getExtensionProperties());
}finally {
} finally {
getLogger().info(String.format("Execution time for extension %s: %d milliseconds", extensionDef.getExtensionName(), System.currentTimeMillis() - initialTime));
}
}
Expand Down Expand Up @@ -147,7 +142,7 @@ private void render(final String name, final Map<String, String> extensionDef) {
final String backend = get(extensionDef, "backend", "docbook");
final String doctype = extensionDef.get("doctype");
final String eruby = get(extensionDef, "eruby", "");
final boolean headerFooter = Boolean.parseBoolean(get(extensionDef, "headerFooter","true"));
final boolean headerFooter = Boolean.parseBoolean(get(extensionDef, "headerFooter", "true"));
final boolean embedAssets = Boolean.parseBoolean(extensionDef.get("embedAssets"));
final String templateDir = extensionDef.get("templateDir");
final String templateEngine = extensionDef.get("templateEngine");
Expand Down Expand Up @@ -186,8 +181,8 @@ private void render(final String name, final Map<String, String> extensionDef) {
}

final Asciidoctor asciidoctor = asciidoctorMap.get(gemPath);
if(asciidoctor == null){
throw new RuntimeException("Asciidoctor not initilizable properly.");
if (asciidoctor == null) {
throw new RuntimeException("Asciidoctor not initilizable properly.");
}

final Ruby rubyInstance = JRubyRuntimeContext.get(asciidoctor);
Expand All @@ -197,7 +192,7 @@ private void render(final String name, final Map<String, String> extensionDef) {
getLogger().warning("Using inherited external environment to resolve gems (" + gemHome + "), i.e. build is platform dependent!");
}

if(!requires.isEmpty()) {
if (!requires.isEmpty()) {
asciidoctor.requireLibraries(requires);
}

Expand Down Expand Up @@ -331,20 +326,25 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)

private void renderFile(final String name, final Asciidoctor asciidoctor, final Map<String, Object> options, File f) {
ExtensionGroup cukedoctorExtensionGroup = asciidoctor.createGroup("com.github.cukedoctor");
if (options.get("backend").toString().equalsIgnoreCase("pdf") && cukedoctorExtensionIsPresent()) {
cukedoctorExtensionGroup.unregister();
} else if(cukedoctorExtensionIsPresent()){
cukedoctorExtensionGroup.unregister();//avoid register twice
cukedoctorExtensionGroup.register();
}
asciidoctor.renderFile(f, options);
getLogger().info("Rendered " + f + " @ " + name);
boolean cukedoctorExtensionRegistered = true;
try {
if (options.get("backend").toString().equalsIgnoreCase("pdf") && cukedoctorExtensionIsPresent()) {
cukedoctorExtensionGroup.unregister();
cukedoctorExtensionRegistered = false;
}
asciidoctor.renderFile(f, options);
} finally {
if (!cukedoctorExtensionRegistered) {
cukedoctorExtensionGroup.register();
}
getLogger().info("Rendered " + f + " @ " + name);
}
}

private boolean cukedoctorExtensionIsPresent() {
try {
Class.forName("com.github.cukedoctor.extension.CukedoctorExtensionRegistry");
return true;
Class.forName("com.github.cukedoctor.extension.CukedoctorExtensionRegistry");
return true;
} catch (ClassNotFoundException e) {
return false;
}
Expand Down Expand Up @@ -381,7 +381,7 @@ private String get(final Map<String, String> extensionDef, final String key, fin
}

private Logger getLogger() {
if(LOGGER == null){
if (LOGGER == null) {
LOGGER = Logger.getLogger(AsciidoctorObserver.class.getName());
}
return LOGGER;
Expand Down

This file was deleted.

0 comments on commit 67c3a44

Please sign in to comment.