* Meant to be stateless, so thread safe. - * @author Tomas Mysik + * @author Tomas Mysik, Phil Lello */ public abstract class ConfigAction { public static enum Type { @@ -123,6 +134,49 @@ return XDebugStarterFactory.getInstance() != null; } + // TODO: Decide how much effort these should put into detecting targets. + // Testing for build.xml is cheap and easy, probing build.xml via XPath + // to detect targets would be better, but then opens up a can of worms over + // includes, etc. + // It would probably be good to support Maven too. + private DataObject getBuildXmlDataObject() { + FileObject buildXml = project.getHelper().getProjectDirectory().getFileObject("build.xml"); + DataObject buildXmlDO = null; + try { + if (buildXml != null) buildXmlDO = DataObject.find(buildXml); + } catch (DataObjectNotFoundException ex) { + } + return buildXmlDO; + } + + private boolean isBuildXmlPresent() { + return getBuildXmlDataObject() != null; + } + + public boolean isBuildProjectEnabled() { + return isBuildXmlPresent(); + } + + public boolean isRebuildProjectEnabled() { + return isBuildXmlPresent(); + } + + public void buildProject() { + try { + new AntBuildExecutionSupportItem(getBuildXmlDataObject(), new String[]{"build"}).startRunning(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + + public void rebuildProject() { + try { + new AntBuildExecutionSupportItem(getBuildXmlDataObject(), new String[]{"rebuild"}).startRunning(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + public abstract boolean isValid(boolean indexFileNeeded); public abstract boolean isRunFileEnabled(Lookup context); @@ -146,4 +200,56 @@ } return true; } + + private class AntBuildExecutionSupportItem implements BuildExecutionSupport.Item, TaskListener { + private final AntTargetExecutor mTargetExecutor; + private final String[] mTargets; + private final AntProjectCookie mProjectCookie; + private ExecutorTask mExecutorTask; + + AntBuildExecutionSupportItem(DataObject buildFileObject, String[] targets) { + mProjectCookie = buildFileObject.getCookie(AntProjectCookie.class); + mTargets = targets; + mTargetExecutor = AntTargetExecutor.createTargetExecutor(new Env()); + } + + @Override + public String getDisplayName() { + return "Running Ant Build"; + } + + @Override + public void repeatExecution() { + stopRunning(); + try { + startRunning(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + + @Override + public boolean isRunning() { + return (mExecutorTask == null)?false:true; + } + + @Override + public void stopRunning() { + if (mExecutorTask != null) mExecutorTask.stop(); + mExecutorTask.waitFinished(); + taskFinished(mExecutorTask); + } + + public void startRunning() throws IOException { + mExecutorTask = mTargetExecutor.execute(mProjectCookie, mTargets); + mExecutorTask.addTaskListener(this); + BuildExecutionSupport.registerRunningItem(this); + } + + @Override + public void taskFinished(Task task) { + mExecutorTask = null; + BuildExecutionSupport.registerFinishedItem(this); + } + } }