/plugins/AntFarm/trunk/antfarm/AntFarmShell.java

# · Java · 370 lines · 295 code · 52 blank · 23 comment · 46 complexity · 3717b0870735066607520d8f03666b45 MD5 · raw file

  1. /*
  2. * AntFarmShell.java - Plugin for running Ant builds from jEdit.
  3. * Copyright (C) 2001 Brian Knowles
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package antfarm;
  20. import java.awt.Color;
  21. import java.io.File;
  22. import java.util.Enumeration;
  23. import java.util.Properties;
  24. import java.util.Vector;
  25. import org.apache.tools.ant.Project;
  26. import org.apache.tools.ant.Target;
  27. import org.gjt.sp.jedit.View;
  28. import org.gjt.sp.jedit.jEdit;
  29. import console.Console;
  30. import console.Output;
  31. import console.Shell;
  32. public class AntFarmShell extends Shell
  33. {
  34. private Project _currentProject;
  35. private File _currentBuildFile;
  36. private String _currentTarget;
  37. private TargetRunner _targetRunner;
  38. private AntFarm _antBrowser;
  39. private Output _output;
  40. public AntFarmShell()
  41. {
  42. super("Ant");
  43. }
  44. public static AntFarm getAntFarm(View view)
  45. {
  46. return (AntFarm) view.getDockableWindowManager().getDockable(AntFarmPlugin.NAME);
  47. }
  48. public static void reloadCurrentProject(View view)
  49. {
  50. String projectPath = AntFarmPlugin.ANT_SHELL.getCurrentProjectPath();
  51. getAntFarm(view).reloadAntBuildFile(projectPath);
  52. }
  53. static String getAntCommandFragment(Properties properties)
  54. {
  55. if (properties == null)
  56. {
  57. return "";
  58. }
  59. StringBuffer command = new StringBuffer();
  60. Enumeration ee = properties.keys();
  61. String current;
  62. while (ee.hasMoreElements())
  63. {
  64. current = (String) ee.nextElement();
  65. command.append(" -D").append(current).append("=");
  66. command.append(properties.getProperty(current));
  67. }
  68. return command.toString();
  69. }
  70. public String getCurrentProjectPath()
  71. {
  72. if (_currentBuildFile == null)
  73. return "";
  74. return FileUtils.getAbsolutePath(_currentBuildFile);
  75. }
  76. // ----- Begin Shell implementation -----
  77. public void printInfoMessage(Output output)
  78. {
  79. printUsage(jEdit.getProperty(AntFarmPlugin.NAME + ".shell.msg.info"), null, output);
  80. printCurrentProjectInfo(null, output);
  81. }
  82. public void execute(Console console, String input, Output output, Output error, String command)
  83. {
  84. stop(console);
  85. _output = output;
  86. command = command.trim();
  87. if (command.startsWith("!"))
  88. {
  89. if (_currentProject == null)
  90. {
  91. output.print(console.getErrorColor(),
  92. jEdit.getProperty(AntFarmPlugin.NAME
  93. + ".shell.msg.non-selected"));
  94. output.commandDone();
  95. return;
  96. }
  97. _currentTarget = command.indexOf(" ") > 0 ? command.substring(1, command
  98. .indexOf(" ")) : command.substring(1);
  99. if (_currentTarget.equals(""))
  100. {
  101. _currentTarget = _currentProject.getDefaultTarget();
  102. }
  103. Target target = (Target) _currentProject.getTargets().get(_currentTarget);
  104. if (target == null)
  105. {
  106. printUsage("Not a valid target: " + _currentTarget, console
  107. .getErrorColor(), output);
  108. output.commandDone();
  109. return;
  110. }
  111. _targetRunner = new TargetRunner(target, _currentBuildFile, console
  112. .getView(), output, AntCommandParser
  113. .parseAntCommandProperties(command));
  114. }
  115. else if (command.equals("?"))
  116. {
  117. printUsage("", null, output);
  118. Vector buildFiles = getAntFarm(console).getAntBuildFiles();
  119. output.print(null, jEdit.getProperty(AntFarmPlugin.NAME
  120. + ".shell.label.available-files"));
  121. String fileList = "";
  122. for (int i = 0; i < buildFiles.size(); i++)
  123. {
  124. fileList += "(" + (i + 1) + ") " + buildFiles.elementAt(i) + "\n";
  125. }
  126. output.print(console.getInfoColor(), fileList);
  127. printCurrentProjectInfo(console.getInfoColor(), output);
  128. output.commandDone();
  129. }
  130. else if (command.startsWith("="))
  131. {
  132. Vector buildFiles = getAntFarm(console).getAntBuildFiles();
  133. try
  134. {
  135. int fileNumber = Integer.parseInt(command.substring(1));
  136. if (fileNumber - 1 >= buildFiles.size())
  137. {
  138. printUsage(jEdit.getProperty(AntFarmPlugin.NAME
  139. + ".shell.msg.no-build-file"), console
  140. .getErrorColor(), output);
  141. output.commandDone();
  142. return;
  143. }
  144. String buildFilePath = (String) buildFiles
  145. .elementAt(fileNumber - 1);
  146. File buildFile = new File(buildFilePath);
  147. Project project = null;
  148. try
  149. {
  150. project = getAntFarm(console).getProject(
  151. buildFile.getAbsolutePath());
  152. setCurrentProject(project, buildFile, console);
  153. }
  154. catch (Exception e)
  155. {
  156. setCurrentProject(null, buildFile, console);
  157. output.print(console.getErrorColor(), jEdit
  158. .getProperty(AntFarmPlugin.NAME
  159. + ".shell.msg.broken-file")
  160. + buildFile + "\n" + e);
  161. output.commandDone();
  162. return;
  163. }
  164. output.commandDone();
  165. }
  166. catch (NumberFormatException e)
  167. {
  168. printUsage(jEdit.getProperty(AntFarmPlugin.NAME
  169. + ".shell.msg.invalid-usage"), console.getErrorColor(),
  170. output);
  171. output.commandDone();
  172. return;
  173. }
  174. }
  175. else if (command.startsWith("+"))
  176. {
  177. String filePath = command.substring(1);
  178. try
  179. {
  180. // Get the provided path, or look up the tree.
  181. File file = getFile(filePath, console);
  182. filePath = FileUtils.getAbsolutePath(file);
  183. // try to reload the project
  184. getAntFarm(console).reloadAntBuildFile(filePath);
  185. // initiate the project
  186. Project project = getAntFarm(console).getProject(filePath);
  187. setCurrentProject(project, file, console);
  188. getAntFarm(console).addAntBuildFile(filePath);
  189. }
  190. catch (Exception e)
  191. {
  192. output.print(console.getErrorColor(), jEdit
  193. .getProperty(AntFarmPlugin.NAME + ".shell.msg.broken-file")
  194. + filePath + "\n" + e);
  195. output.commandDone();
  196. return;
  197. }
  198. output.commandDone();
  199. }
  200. else if (!command.equals(""))
  201. {
  202. printUsage(jEdit.getProperty(AntFarmPlugin.NAME
  203. + ".shell.msg.invalid-usage"), console.getErrorColor(), output);
  204. }
  205. output.commandDone();
  206. }
  207. public void stop(Console console)
  208. {
  209. if (_targetRunner != null)
  210. {
  211. if (_targetRunner.isAlive())
  212. {
  213. _targetRunner.stop();
  214. if (_output != null)
  215. {
  216. _output.print(console.getErrorColor(), jEdit
  217. .getProperty(AntFarmPlugin.NAME
  218. + ".shell.msg.killed"));
  219. }
  220. }
  221. _targetRunner.resetLogging();
  222. _targetRunner = null;
  223. }
  224. }
  225. public boolean waitFor(Console console)
  226. {
  227. if (_targetRunner != null)
  228. {
  229. try
  230. {
  231. synchronized (_targetRunner)
  232. {
  233. _targetRunner.join();
  234. _targetRunner = null;
  235. }
  236. }
  237. catch (InterruptedException ie)
  238. {
  239. return false;
  240. }
  241. }
  242. return true;
  243. }
  244. // ----- End Shell implementation -----
  245. private void setCurrentProject(Project currentProject, File currentBuildFile,
  246. Console console)
  247. {
  248. boolean printMessage = false;
  249. if (!currentBuildFile.equals(_currentBuildFile))
  250. printMessage = true;
  251. _currentBuildFile = currentBuildFile;
  252. _currentProject = currentProject;
  253. if (console.getShell() == AntFarmPlugin.ANT_SHELL && printMessage)
  254. {
  255. printCurrentProjectInfo(console.getInfoColor(), console.getOutput());
  256. }
  257. }
  258. private AntFarm getAntFarm(Console console)
  259. {
  260. AntFarm antBrowser = (AntFarm) console.getView().getDockableWindowManager()
  261. .getDockable(AntFarmPlugin.NAME);
  262. if (antBrowser != null)
  263. {
  264. return antBrowser;
  265. }
  266. if (_antBrowser == null)
  267. {
  268. _antBrowser = new AntFarm(console.getView());
  269. }
  270. return _antBrowser;
  271. }
  272. private File getFile(String filePath, Console console)
  273. {
  274. if (filePath == null || filePath.length() == 0)
  275. {
  276. filePath = "build.xml";
  277. }
  278. File file = new File(filePath);
  279. if (file.isAbsolute())
  280. {
  281. return file;
  282. }
  283. File directory = new File(console.getView().getBuffer().getPath());
  284. return FileUtils.findFile(directory, filePath);
  285. }
  286. private void printCurrentProjectInfo(Color color, Output output)
  287. {
  288. if (_currentProject == null)
  289. return;
  290. output.print(null, jEdit.getProperty(AntFarmPlugin.NAME
  291. + ".shell.label.current-file"));
  292. String projectName = _currentProject.getName() != null ? _currentProject.getName()
  293. : "Untitled";
  294. output.print(color, projectName + " (" + _currentBuildFile.getAbsolutePath()
  295. + ")\n");
  296. output.print(null, jEdit.getProperty(AntFarmPlugin.NAME
  297. + ".shell.label.available-targets"));
  298. String info = "";
  299. Enumeration targets = _currentProject.getTargets().keys();
  300. while (targets.hasMoreElements())
  301. {
  302. String target = (String) targets.nextElement();
  303. info += target + printDefaultLabel(_currentProject, target) + "\t";
  304. }
  305. output.print(color, info);
  306. }
  307. private String printDefaultLabel(Project project, String target)
  308. {
  309. if (project.getDefaultTarget().equals(target))
  310. return " [default]";
  311. return "";
  312. }
  313. private void printUsage(String additionalMessage, Color color, Output output)
  314. {
  315. output.print(color, additionalMessage);
  316. output.print(null, jEdit.getProperty(AntFarmPlugin.NAME + ".shell.msg.usage"));
  317. }
  318. }