Skip to content

Commit

Permalink
Installed asciidoctor path now configurable closes #147
Browse files Browse the repository at this point in the history
  • Loading branch information
de-jcup committed Oct 12, 2018
1 parent 3b053cb commit 0b3141d
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 69 deletions.
Expand Up @@ -49,12 +49,72 @@ public DocumentHeader readDocumentHeader(File filename) {
@Override
public String convertFile(File filename, Map<String, Object> options) {

List<String> commands = buildCommands(filename, options);
String commandLineString = createCommandLineString(commands);

ProcessBuilder pb = new ProcessBuilder(commands);
AsciiDoctorConsoleUtil.output(">> rendering:"+filename.getName());
try {
StringBuffer lineStringBuffer=null;
Process process = pb.start();
try (InputStream is = process.getErrorStream();) {
int c;
lineStringBuffer = new StringBuffer();
while ((c = is.read()) != -1) {
lineStringBuffer.append((char) c);
}
String line = lineStringBuffer.toString();
if (line.isEmpty()){
AsciiDoctorConsoleUtil.output(line);
}else{
AsciiDoctorConsoleUtil.error(line);
}
}
boolean exitdone = process.waitFor(2, TimeUnit.MINUTES);
int exitCode =-1;
if (exitdone){
exitCode=process.exitValue();
}
if (EclipseDevelopmentSettings.DEBUG_LOGGING_ENABLED){
AsciiDoctorConsoleUtil.output("Called:" + commandLineString);
AsciiDoctorConsoleUtil.output("Exitcode:"+exitCode);
}
if (exitCode>0){
AsciiDoctorEclipseLogAdapter.INSTANCE.logWarn("Installed Asciidoctor rendering failed for '"+filename.getName()+"'\n\nCommandLine was:\n"+commandLineString+"\n\nResulted in exitcode:"+exitCode+", \nLast output:"+lineStringBuffer);
throw new InstalledAsciidoctorException("FAILED - Asciidoctor exitcode:"+exitCode+" - last output:"+lineStringBuffer);

}
} catch (Exception e) {
if (e instanceof InstalledAsciidoctorException){
InstalledAsciidoctorException iae = (InstalledAsciidoctorException) e;
throw iae; // already an exception from installed asciidoctor so just re-throw
}else{
AsciiDoctorEditorUtil.logError("Cannot execute installed asciidoctor\n\nCommandline was:\n"+commandLineString, e);
throw new InstalledAsciidoctorException("FAILED - Installed Asciidoctor instance was not executable, reason:"+e.getMessage());
}
}

return null;
}

protected String createCommandLineString(List<String> commands) {
StringBuilder commandLine = new StringBuilder();
for (String command : commands) {
commandLine.append(command);
commandLine.append(" ");
}
String commandLineString = commandLine.toString();
return commandLineString;
}

protected List<String> buildCommands(File filename, Map<String, Object> options) {
List<String> commands = new ArrayList<String>();
if (OSUtil.isWindows()) {
commands.add("cmd.exe");
commands.add("/C");
}
commands.add("asciidoctor");
String asciidoctorCall = createAsciidoctorCall();
commands.add(asciidoctorCall);

String outDir = null;

Expand Down Expand Up @@ -89,56 +149,23 @@ public String convertFile(File filename, Map<String, Object> options) {
}

commands.add(toWindowsSafeVariant(filename.getAbsolutePath()));
StringBuilder commandLine = new StringBuilder();
for (String command : commands) {
commandLine.append(command);
commandLine.append(" ");
}
AsciiDoctorConsoleUtil.output(">> rendering:"+filename.getName());
ProcessBuilder pb = new ProcessBuilder(commands);
try {
StringBuffer sbx=null;
Process process = pb.start();
try (InputStream is = process.getErrorStream();) {
int c;
sbx = new StringBuffer();
while ((c = is.read()) != -1) {
sbx.append((char) c);
}
String line = sbx.toString();
if (line.isEmpty()){
AsciiDoctorConsoleUtil.output(line);
}else{
AsciiDoctorConsoleUtil.error(line);
}
}
boolean exitdone = process.waitFor(2, TimeUnit.MINUTES);
int exitCode =-1;
if (exitdone){
exitCode=process.exitValue();
}
if (EclipseDevelopmentSettings.DEBUG_LOGGING_ENABLED){
AsciiDoctorConsoleUtil.output("Called:" + commandLine.toString());
AsciiDoctorConsoleUtil.output("Exitcode:"+exitCode);
}
if (exitCode>0){
AsciiDoctorEclipseLogAdapter.INSTANCE.logWarn("Installed Asciidoctor rendering failed for '"+filename.getName()+"'\n\nCommandLine was:\n"+commandLine.toString()+"\n\nResulted in exitcode:"+exitCode+", \nLast output:"+sbx);
throw new InstalledAsciidoctorException("FAILED - Asciidoctor exitcode:"+exitCode+" - last output:"+sbx);

}
} catch (Exception e) {
if (e instanceof InstalledAsciidoctorException){
InstalledAsciidoctorException iae = (InstalledAsciidoctorException) e;
throw iae; // just rethrow
}else{
AsciiDoctorEditorUtil.logError("Cannot execute installed asciidoctor\n\nCommandline was:\n"+commandLine.toString(), e);
throw new InstalledAsciidoctorException("FAILED - Installed Asciidoctor instance was not executable, reason:"+e.getMessage());
return commands;
}

protected String createAsciidoctorCall() {
StringBuilder sb = new StringBuilder();
String path =AsciiDoctorEditorPreferences.getInstance().getPathToInstalledAsciidoctor();
if (path!=null && !path.trim().isEmpty()){
sb.append(path);
if (! path.endsWith(File.separator)){
sb.append(File.separator);
}
}

return null;
sb.append("asciidoctor");
String callPath = sb.toString();
return callPath;
}

private String toWindowsSafeVariant(Object obj) {
String command = "" + obj;
boolean windowsPath = command.indexOf('\\') != -1;
Expand Down
Expand Up @@ -23,12 +23,12 @@
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
Expand Down Expand Up @@ -64,12 +64,19 @@ protected static void indent(Control control) {
}

private ArrayList<MasterButtonSlaveSelectionListener> masterSlaveListeners = new ArrayList<>();
private AccessibleDirectoryFieldEditor pathToAsciidocFieldEditor;

public AsciiDoctorEditorPreferencePage() {
super(GRID);
setPreferenceStore(getPreferences().getPreferenceStore());
}

@Override
protected Control createContents(Composite parent) {
Control control = super.createContents(parent);
return control;
}

@Override
public void init(IWorkbench workbench) {

Expand All @@ -78,13 +85,16 @@ public void init(IWorkbench workbench) {
@Override
public void performDefaults() {
super.performDefaults();
pathToAsciidocFieldEditor.setStringValue("");
masterSlaveListeners.forEach((a) -> a.updateSlaveComponent());

}

@Override
public boolean performOk() {
boolean ok = super.performOk();
// we handle the directory field special, not added as field, so setting default in this way
AsciiDoctorEditorPreferences.getInstance().setStringPreference(AsciiDoctorEditorPreferenceConstants.P_PATH_TO_INSTALLED_ASCIICDOCTOR,pathToAsciidocFieldEditor.getStringValue());
return ok;
}
protected void createDependency(Button master, Control slave) {
Expand Down Expand Up @@ -225,6 +235,15 @@ protected void createExternalPreviewParts(Composite composite) {
autorefreshSeconds.getTextControl(devNull2));
}

@Override
protected void checkState() {
super.checkState();
// we handle the directory field special, not added as field, so validating value in this way
if (! pathToAsciidocFieldEditor.checkState()) {
setValid(false);
}
}

protected void createAsciidoctorGroup(Composite composite) {

Group group = new Group(composite, SWT.NONE);
Expand All @@ -246,22 +265,42 @@ protected void createAsciidoctorGroup(Composite composite) {
group2.setLayoutData(group2Data);
group2.setLayout(new GridLayout());

// Composite devNull2a = new Composite(group2,SWT.NONE);
// AccessibleDirectoryFieldEditor pathToAsciidocFieldEditor = new AccessibleDirectoryFieldEditor(P_PATH_TO_INSTALLED_ASCIICDOCTOR.getId(),
// "Path to Asciidoctor", devNull2a);
// devNull2a.setLayout(new GridLayout());
// devNull2a.setLayoutData(new GridData(SWT.FILL,SWT.TOP, true,false));
//
// addField(pathToAsciidocFieldEditor);
//
// createDependency(useInstalledAsciidoctor.getChangeControl(devNull1),
// pathToAsciidocFieldEditor.getTextControl(devNull2a));
// createDependency(useInstalledAsciidoctor.getChangeControl(devNull1),
// pathToAsciidocFieldEditor.getLabelControl(devNull2a));
// createDependency(useInstalledAsciidoctor.getChangeControl(devNull1),
// pathToAsciidocFieldEditor.getChangeControl(devNull2a));
Composite devNull2a = new Composite(group2,SWT.NONE);
pathToAsciidocFieldEditor = new AccessibleDirectoryFieldEditor(P_PATH_TO_INSTALLED_ASCIICDOCTOR.getId(),
"Path to Asciidoctor", devNull2a);
pathToAsciidocFieldEditor.getTextControl(devNull2a).setMessage("Not defined");
pathToAsciidocFieldEditor.getTextControl(devNull2a).setToolTipText("If not defined, installed asciidoctor instance must\nbe available from PATH in environment - otherwise it must be a valid directory.");
pathToAsciidocFieldEditor.setEmptyStringAllowed(true);
pathToAsciidocFieldEditor.setErrorMessage("Invalid path to installed Asciidoctor");
devNull2a.setLayout(new GridLayout(3,false));
devNull2a.setLayoutData(new GridData(SWT.FILL,SWT.TOP, true,false));


// not:addField(pathToAsciidocFieldEditor); >>>> when not adding field as field editor it looks good. so text must be set to preferences by special code * field editors s...cks!
pseudoAddField(pathToAsciidocFieldEditor);
pathToAsciidocFieldEditor.getTextControl(devNull2a).addFocusListener(new FocusListener() {

@Override
public void focusLost(FocusEvent e) {
/* when focus lost we must check */
checkState();
}

@Override
public void focusGained(FocusEvent e) {

}
});

createDependency(useInstalledAsciidoctor.getChangeControl(devNull1),
pathToAsciidocFieldEditor.getTextControl(devNull2a),false);
createDependency(useInstalledAsciidoctor.getChangeControl(devNull1),
pathToAsciidocFieldEditor.getLabelControl(devNull2a),false);
createDependency(useInstalledAsciidoctor.getChangeControl(devNull1),
pathToAsciidocFieldEditor.getChangeControl(devNull2a),false);

Composite devNull2 = new Composite(group2,SWT.NONE);

MultiLineStringFieldEditor cliArguments = new MultiLineStringFieldEditor(P_INSTALLED_ASCIICDOCTOR_ARGUMENTS.getId(),
"Custom arguments for Asciidoctor CLI call", devNull2);
cliArguments.getTextControl().setToolTipText("Setup arguments which shall be added to CLI call of installed asciidoctor instance.\n\nYou can use multiple lines.");
Expand All @@ -287,6 +326,14 @@ protected void createAsciidoctorGroup(Composite composite) {

}

private void pseudoAddField(FieldEditor pe) {
pe.setPage(this);
pe.setPropertyChangeListener(this);
pe.setPreferenceStore(getPreferenceStore());
pe.load();

}

@Override
protected void initialize() {
super.initialize();
Expand Down
Expand Up @@ -135,6 +135,12 @@ public boolean getBooleanPreference(AsciiDoctorEditorPreferenceConstants id) {
public void setBooleanPreference(AsciiDoctorEditorPreferenceConstants id, boolean value) {
getPreferenceStore().setValue(id.getId(), value);
}


public void setStringPreference(AsciiDoctorEditorPreferenceConstants id,
String value) {
getPreferenceStore().setValue(id.getId(), value);
}

public boolean isLinkOutlineWithEditorEnabled() {
return getBooleanPreference(P_LINK_OUTLINE_WITH_EDITOR);
Expand Down Expand Up @@ -216,4 +222,9 @@ public boolean isConsoleAlwaysShownOnError() {
return getBooleanPreference(P_SHOW_ASCIIDOC_CONSOLE_ON_ERROR_OUTPUT);
}

public String getPathToInstalledAsciidoctor() {
return getStringPreference(AsciiDoctorEditorPreferenceConstants.P_PATH_TO_INSTALLED_ASCIICDOCTOR);
}


}
Expand Up @@ -5,13 +5,26 @@
import org.eclipse.swt.widgets.Composite;

public class AccessibleDirectoryFieldEditor extends DirectoryFieldEditor {

public AccessibleDirectoryFieldEditor(String name, String labelText, Composite parent) {
super(name,labelText,parent);
super(name, labelText, parent);
}

@Override
public Button getChangeControl(Composite parent) {
return super.getChangeControl(parent);
}


@Override
protected void createControl(Composite parent) {
super.createControl(parent);
}

@Override
public boolean checkState() {
return super.checkState();
}


}

0 comments on commit 0b3141d

Please sign in to comment.