From 917dbfc21bd552264fa7cea3a5fcb3954e47d33d Mon Sep 17 00:00:00 2001 From: Albert Tregnaghi Date: Fri, 12 Oct 2018 02:33:03 +0200 Subject: [PATCH] Installed asciidoctor path now configurable closes #147 --- .../InstalledAsciidoctor.java | 123 +++++++++++------- .../AsciiDoctorEditorPreferencePage.java | 83 +++++++++--- .../AsciiDoctorEditorPreferences.java | 11 ++ .../AccessibleDirectoryFieldEditor.java | 19 ++- 4 files changed, 167 insertions(+), 69 deletions(-) diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/InstalledAsciidoctor.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/InstalledAsciidoctor.java index 077370b8..7e723a43 100644 --- a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/InstalledAsciidoctor.java +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/InstalledAsciidoctor.java @@ -49,12 +49,72 @@ public DocumentHeader readDocumentHeader(File filename) { @Override public String convertFile(File filename, Map options) { + List 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 commands) { + StringBuilder commandLine = new StringBuilder(); + for (String command : commands) { + commandLine.append(command); + commandLine.append(" "); + } + String commandLineString = commandLine.toString(); + return commandLineString; + } + + protected List buildCommands(File filename, Map options) { List commands = new ArrayList(); if (OSUtil.isWindows()) { commands.add("cmd.exe"); commands.add("/C"); } - commands.add("asciidoctor"); + String asciidoctorCall = createAsciidoctorCall(); + commands.add(asciidoctorCall); String outDir = null; @@ -89,56 +149,23 @@ public String convertFile(File filename, Map 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; diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorPreferencePage.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorPreferencePage.java index 4ee2bbd4..66c928ca 100644 --- a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorPreferencePage.java +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorPreferencePage.java @@ -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; @@ -64,12 +64,19 @@ protected static void indent(Control control) { } private ArrayList 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) { @@ -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) { @@ -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); @@ -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."); @@ -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(); diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorPreferences.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorPreferences.java index ff3fe781..b3228eae 100644 --- a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorPreferences.java +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorPreferences.java @@ -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); @@ -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); + } + + } diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/presentation/AccessibleDirectoryFieldEditor.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/presentation/AccessibleDirectoryFieldEditor.java index ae76dfed..c92ab270 100644 --- a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/presentation/AccessibleDirectoryFieldEditor.java +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/presentation/AccessibleDirectoryFieldEditor.java @@ -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(); + } + + }