Added
Link Here
|
1 |
package org.netbeans.modules.php.project.ui.actions.support; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.InputStream; |
5 |
import java.io.OutputStream; |
6 |
import java.util.List; |
7 |
import org.apache.tools.ant.module.api.AntProjectCookie; |
8 |
import org.apache.tools.ant.module.api.AntTargetExecutor; |
9 |
import org.apache.tools.ant.module.api.AntTargetExecutor.Env; |
10 |
import org.apache.tools.ant.module.api.support.AntScriptUtils; |
11 |
import org.netbeans.modules.php.project.PhpProject; |
12 |
import org.netbeans.spi.project.ui.support.BuildExecutionSupport; |
13 |
import org.openide.execution.ExecutorTask; |
14 |
import org.openide.filesystems.FileObject; |
15 |
import org.openide.loaders.DataObject; |
16 |
import org.openide.loaders.DataObjectNotFoundException; |
17 |
import org.openide.util.Exceptions; |
18 |
import org.openide.util.Task; |
19 |
import org.openide.util.TaskListener; |
20 |
|
21 |
/** |
22 |
* |
23 |
* @author Phil Lello |
24 |
*/ |
25 |
|
26 |
public class AntBuildExecutionSupportItem implements BuildExecutionSupport.Item, TaskListener { |
27 |
private final AntTargetExecutor mTargetExecutor; |
28 |
private final String[] mTargets; |
29 |
private final AntProjectCookie mProjectCookie; |
30 |
private ExecutorTask mExecutorTask; |
31 |
private final DataObject mBuildDataObject; |
32 |
private final FileObject mBuildFileObject; |
33 |
|
34 |
AntBuildExecutionSupportItem(FileObject buildFileObject, String[] targets) throws DataObjectNotFoundException { |
35 |
mBuildFileObject = buildFileObject; |
36 |
mBuildDataObject = DataObject.find(mBuildFileObject); |
37 |
mProjectCookie = mBuildDataObject.getCookie(AntProjectCookie.class); |
38 |
mTargets = targets; |
39 |
mTargetExecutor = AntTargetExecutor.createTargetExecutor(new Env()); |
40 |
} |
41 |
|
42 |
@Override |
43 |
public String getDisplayName() { |
44 |
return "Running Ant Build"; |
45 |
} |
46 |
|
47 |
@Override |
48 |
public void repeatExecution() { |
49 |
stopRunning(); |
50 |
try { |
51 |
startRunning(); |
52 |
} catch (IOException ex) { |
53 |
Exceptions.printStackTrace(ex); |
54 |
} |
55 |
} |
56 |
|
57 |
@Override |
58 |
public boolean isRunning() { |
59 |
return (mExecutorTask == null)?false:true; |
60 |
} |
61 |
|
62 |
@Override |
63 |
public void stopRunning() { |
64 |
if (mExecutorTask != null) mExecutorTask.stop(); |
65 |
mExecutorTask.waitFinished(); |
66 |
taskFinished(mExecutorTask); |
67 |
} |
68 |
|
69 |
public void startRunning() throws IOException { |
70 |
mExecutorTask = mTargetExecutor.execute(mProjectCookie, mTargets); |
71 |
mExecutorTask.addTaskListener(this); |
72 |
BuildExecutionSupport.registerRunningItem(this); |
73 |
} |
74 |
|
75 |
@Override |
76 |
public void taskFinished(Task task) { |
77 |
mExecutorTask = null; |
78 |
BuildExecutionSupport.registerFinishedItem(this); |
79 |
} |
80 |
|
81 |
public static AntBuildExecutionSupportItem invokeTarget(PhpProject project, String target) |
82 |
throws Exception { |
83 |
return invokeTarget(getBuildFileObject(project), target); |
84 |
} |
85 |
|
86 |
public static AntBuildExecutionSupportItem invokeTarget(FileObject antFile, String target) |
87 |
throws Exception { |
88 |
AntBuildExecutionSupportItem item; |
89 |
item = new AntBuildExecutionSupportItem(antFile, new String[]{target}); |
90 |
item.startRunning(); |
91 |
return item; |
92 |
} |
93 |
|
94 |
public static boolean hasTarget(PhpProject project, String target) { |
95 |
return hasTarget(getBuildFileObject(project), target); |
96 |
} |
97 |
|
98 |
public static boolean hasTarget(FileObject antFile, String target) { |
99 |
boolean present = false; |
100 |
List<String> targets; |
101 |
try { |
102 |
targets = AntScriptUtils.getCallableTargetNames(antFile); |
103 |
present = targets.contains(target); |
104 |
} catch (Exception e) { |
105 |
// Any error means we can't run target, so we don't care what it is. |
106 |
} |
107 |
return present; |
108 |
} |
109 |
|
110 |
public static FileObject getBuildFileObject(PhpProject project) { |
111 |
FileObject buildXml = project.getProjectDirectory().getFileObject("build.xml"); |
112 |
return buildXml; |
113 |
} |
114 |
|
115 |
public static void addToProject(PhpProject project) { |
116 |
try { |
117 |
FileObject buildFile = project.getProjectDirectory().createData("build.xml"); |
118 |
InputStream is = AntBuildExecutionSupportItem.class.getResourceAsStream("build.template.xml"); |
119 |
OutputStream os = buildFile.getOutputStream(); |
120 |
while (is.available() != 0) { |
121 |
os.write(is.read()); |
122 |
} |
123 |
os.close(); |
124 |
is.close(); |
125 |
} catch (IOException ex) { |
126 |
Exceptions.printStackTrace(ex); |
127 |
} |
128 |
} |
129 |
|
130 |
/** |
131 |
* @return the mTargetExecutor |
132 |
*/ |
133 |
public ExecutorTask getExecutorTask() { |
134 |
return mExecutorTask; |
135 |
} |
136 |
} |