eclipse plugin: preprocess files in the project on building

eclipse plugin: added the wmltools entry in preferences dialog
This commit is contained in:
Timotei Dolean 2010-05-27 18:34:09 +00:00
parent 5763a3c486
commit b7d3a2a9bb
9 changed files with 209 additions and 15 deletions

View file

@ -67,6 +67,20 @@
menubarPath="additions">
</action>
</objectContribution>
<objectContribution
adaptable="false"
id="Wesnoth Eclipse Plugin.configFilesContributions"
nameFilter="*.cfg"
objectClass="org.eclipse.core.resources.IFile">
<action
class="wesnoth_eclipse_plugin.action.OpenScenarioInGame"
enablesFor="1"
icon="icons/wesnoth-icon_16.png"
id="action.openScenarioInGame"
label="Open Scenario in game"
menubarPath="additions">
</action>
</objectContribution>
</extension>
<extension
id="xmlProblem"

View file

@ -0,0 +1,32 @@
package wesnoth_eclipse_plugin.action;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
public class OpenScenarioInGame implements IObjectActionDelegate
{
public OpenScenarioInGame()
{
}
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart){
}
@Override
public void run(IAction action)
{
IFile selectedFile = WorkspaceUtils.getSelectedFile(WorkspaceUtils.getWorkbenchWindow());
//TODO: optimize this by checking if file really is a scenario (PersistentProperties)
}
@Override
public void selectionChanged(IAction action, ISelection selection){
}
}

View file

@ -16,6 +16,8 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import wesnoth_eclipse_plugin.Logger;
import wesnoth_eclipse_plugin.globalactions.PreprocessorActions;
import wesnoth_eclipse_plugin.preferences.PreferenceConstants;
import wesnoth_eclipse_plugin.preferences.PreferenceInitializer;
import wesnoth_eclipse_plugin.utils.AntUtils;
@ -94,7 +96,18 @@ public class SampleBuilder extends IncrementalProjectBuilder {
properties.put("wesnoth.user.dir",
PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_USER_DIR) + Path.SEPARATOR);
System.out.println("Ant result:");
System.out.println(AntUtils.runAnt(getProject().getLocation().toOSString() + "/build.xml",properties));
String result = AntUtils.runAnt(getProject().getLocation().toOSString() + "/build.xml",properties,true);
System.out.println(result);
if (result == null)
{
Logger.print("There was an error running the ant job.");
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(), "There was an error running the ant job.");
return null;
}
// create the temporary directory used by the plugin if not created
WorkspaceUtils.getTemporaryFolder();
if (kind == FULL_BUILD) {
fullBuild(monitor);
@ -110,12 +123,15 @@ public class SampleBuilder extends IncrementalProjectBuilder {
}
void checkResource(IResource resource) {
// dummy condition
if (resource instanceof IFile && resource.getName().equals("_main.cfg")) {
// config files
if (resource instanceof IFile && resource.getName().endsWith(".cfg")) {
try {
IFile file = (IFile) resource;
deleteMarkers(file);
PreprocessorActions.preprocessFile(WorkspaceUtils.getPathRelativeToUserDir(file),
WorkspaceUtils.getTemporaryFolder(), null, true);
/*
IMarker[] resIMarkers = file.findMarkers(MARKER_TYPE, false, IResource.DEPTH_ZERO);
Logger.print("found markers: " + resIMarkers.length);

View file

@ -7,9 +7,6 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.widgets.MessageBox;
import wesnoth_eclipse_plugin.Activator;
import wesnoth_eclipse_plugin.builder.ExternalToolInvoker;
import wesnoth_eclipse_plugin.preferences.PreferenceConstants;
import wesnoth_eclipse_plugin.preferences.PreferenceInitializer;

View file

@ -0,0 +1,56 @@
/**
* @author Timotei Dolean
*/
package wesnoth_eclipse_plugin.globalactions;
import java.util.ArrayList;
import java.util.List;
import wesnoth_eclipse_plugin.builder.ExternalToolInvoker;
import wesnoth_eclipse_plugin.preferences.PreferenceConstants;
import wesnoth_eclipse_plugin.preferences.PreferenceInitializer;
public class PreprocessorActions
{
/**
* preprocesses a file using the wesnoth's executable
* @param fileName the file to process
* @param targetDirectory target directory where should be put the results
* @param defines the list of additional defines to be added when preprocessing the file
* @param useThread true if the preprocessing should use a thread
* @return
*/
public static boolean preprocessFile(String fileName,String targetDirectory,List<String> defines, boolean useThread)
{
try{
List<String> arguments = new ArrayList<String>();
if (defines != null && !defines.isEmpty())
{
String argument = "-p=";
for(int i=0;i<defines.size()-1;i++)
{
argument += (defines.get(i) + ",");
}
argument += defines.get(defines.size()-1);
}
else
{
arguments.add("-p");
}
arguments.add(fileName);
arguments.add(targetDirectory);
ExternalToolInvoker wesnoth = new ExternalToolInvoker(
PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_EXEC_PATH),
arguments, useThread);
System.out.printf("preprocessing : %s\n", arguments);
wesnoth.run();
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

View file

@ -7,6 +7,7 @@ public class PreferenceConstants {
public static final String P_WESNOTH_EXEC_PATH = "wesnoth_exec_path";
public static final String P_WESNOTH_WORKING_DIR = "wesnoth_working_dir";
public static final String P_WESNOTH_WMLTOOLS_DIR = "wesnoth_wmltools_dir";
public static final String P_WESNOTH_USER_DIR = "wesnoth_user_dir";
}

View file

@ -1,8 +1,13 @@
package wesnoth_eclipse_plugin.preferences;
import java.io.File;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
@ -25,6 +30,8 @@ public class WesnothEditorPreferences
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
private DirectoryFieldEditor wmlToolsField;
public WesnothEditorPreferences() {
super(GRID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
@ -45,9 +52,36 @@ public class WesnothEditorPreferences
"Working directory:", getFieldEditorParent()));
addField(new DirectoryFieldEditor(PreferenceConstants.P_WESNOTH_USER_DIR,
"User data directory:", getFieldEditorParent()));
wmlToolsField = new DirectoryFieldEditor(PreferenceConstants.P_WESNOTH_WMLTOOLS_DIR,
"WML* tools directory:", getFieldEditorParent());
addField(wmlToolsField);
}
public void init(IWorkbench workbench) {
}
@Override
protected void checkState()
{
super.checkState();
setValid(false);
if (!(new File(wmlToolsField.getStringValue() + Path.SEPARATOR + "wmllint").exists()))
{
setErrorMessage("wmllint cannot be found in the wml tools path");
return;
}
setErrorMessage(null);
setValid(true);
}
@Override
public void propertyChange(PropertyChangeEvent event)
{
super.propertyChange(event);
if (event.getProperty().equals(FieldEditor.VALUE))
checkState();
}
}

View file

@ -20,9 +20,10 @@ public class AntUtils
* Runs the specified ant file, and returns the output of the runned file
* @param antFile
* @param properties the hasmap with userproperties to be added to the ant file
* @return
* @param recordOutput true if the output of the runned file should be recorded and returned
* @return null if the build didn't success
*/
public static String runAnt(String antFile, HashMap<String,String> properties)
public static String runAnt(String antFile, HashMap<String,String> properties, boolean recordOutput)
{
final Project project = new Project();
ByteArrayOutputStream out=null;
@ -30,7 +31,8 @@ public class AntUtils
try
{
out = new ByteArrayOutputStream();
project.addBuildListener(AntUtils.createLogger(out));
if (recordOutput)
project.addBuildListener(AntUtils.createLogger(out));
project.init();
File buildFile = new File(antFile);
ProjectHelper.configureProject(project, buildFile);
@ -43,13 +45,14 @@ public class AntUtils
project.setUserProperty(key,value);
}
project.executeTarget(project.getDefaultTarget());
return out.toString();
}
catch (RuntimeException exc)
catch (Exception exc)
{
exc.printStackTrace();
return null;
}
return out.toString();
}
/**

View file

@ -3,16 +3,24 @@
*/
package wesnoth_eclipse_plugin.utils;
import java.io.File;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import wesnoth_eclipse_plugin.Activator;
import wesnoth_eclipse_plugin.preferences.PreferenceConstants;
import wesnoth_eclipse_plugin.preferences.PreferenceInitializer;
public class WorkspaceUtils
{
private static String temporaryFolder_ = "";
public static IProject getSelectedProject(IWorkbenchWindow window)
{
IStructuredSelection selection = getSelectedStructuredSelection(window);
@ -43,19 +51,52 @@ public class WorkspaceUtils
public static IStructuredSelection getSelectedStructuredSelection(IWorkbenchWindow window)
{
if (window == null)
{
System.out.println("WokbenchWindow NULL!!");
return null;
}
if (!(window.getSelectionService().getSelection() instanceof IStructuredSelection))
return null;
return (IStructuredSelection)window.getSelectionService().getSelection();
}
/**
* Returns the first WorkbenchWindow available.
* This is not always the same with ActiveWorkbecnWindow
* @return
*/
public static IWorkbenchWindow getWorkbenchWindow()
{
if (Activator.getDefault().getWorkbench().getWorkbenchWindowCount() == 0)
return null;
return Activator.getDefault().getWorkbench().getWorkbenchWindows()[0];
}
/**
* Returns the temporary folder where the plugin can write resources
* @return
*/
public static String getTemporaryFolder()
{
if (temporaryFolder_.isEmpty())
{
temporaryFolder_ = System.getProperty("java.io.tmpdir") + Path.SEPARATOR +
"wesnoth_plugin" + Path.SEPARATOR;
File tmpFile = new File(temporaryFolder_);
if (!tmpFile.exists())
tmpFile.mkdirs();
}
return temporaryFolder_;
}
/**
* Returns the resource path relative to the user directory
* @param resource the resource to be computed
* @return
*/
public static String getPathRelativeToUserDir(IResource resource)
{
return PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_USER_DIR) + Path.SEPARATOR +
"data/add-ons/" + resource.getProject().getName() +
Path.SEPARATOR + resource.getProjectRelativePath().toOSString();
}
}