1 package org.antlr.works.project; 2 3 import org.antlr.xjlib.appkit.utils.XJDialogProgress; 4 import org.antlr.xjlib.appkit.utils.XJDialogProgressDelegate; 5 import org.antlr.xjlib.foundation.XJUtils; 6 import org.antlr.works.components.project.CContainerProject; 7 import org.antlr.works.engine.EngineRuntime; 8 import org.antlr.works.utils.StreamWatcherDelegate; 9 10 import javax.swing.*; 11 import java.io.File ; 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 47 48 public class ProjectBuilder implements StreamWatcherDelegate, XJDialogProgressDelegate { 49 50 protected CContainerProject project; 51 protected XJDialogProgress progress; 52 53 protected boolean cancel; 54 protected int buildingProgress; 55 56 protected ProjectFileItem fileToBuild; 57 protected ThreadExecution currentThread; 58 59 public ProjectBuilder(CContainerProject project) { 60 this.project = project; 61 this.progress = new XJDialogProgress(project.getXJFrame(), true); 62 this.progress.setDelegate(this); 63 } 64 65 public List <ProjectBuildList.BuildFile> getListOfDirtyBuildFiles(String type) { 66 return project.getBuildList().getDirtyBuildFilesOfType(type); 67 } 68 69 public List <ProjectBuildList.BuildFile> buildListOfBuildFilesOfType(List <String > filesOnDisk, String fileType) { 70 ProjectBuildList buildList = project.getBuildList(); 71 72 74 File [] files = new File (project.getSourcePath()).listFiles(); 75 for (int i = 0; i < files.length; i++) { 76 File file = files[i]; 77 String filePath = file.getAbsolutePath(); 78 if(!ProjectFileItem.getFileType(filePath).equals(fileType)) 79 continue; 80 81 if(buildList.isFileExisting(filePath, fileType)) 82 buildList.handleExternalModification(filePath, fileType); 83 else 84 buildList.addFile(filePath, fileType); 85 } 86 87 89 for (Iterator <ProjectBuildList.BuildFile> iterator = buildList.getBuildFilesOfType(fileType).iterator(); iterator.hasNext();) 90 { 91 ProjectBuildList.BuildFile buildFile = iterator.next(); 92 if(!new File (buildFile.getFilePath()).exists()) { 93 buildList.removeFile(buildFile.getFilePath(), fileType); 95 } 96 } 97 98 return getListOfDirtyBuildFiles(fileType); 99 } 100 101 public List <ProjectBuildList.BuildFile> buildListOfGrammarBuildFiles() { 102 List <String > filesOnDisk = new ArrayList <String >(); 103 for (Iterator iterator = project.getFileEditorItems().iterator(); iterator.hasNext();) { 104 ProjectFileItem item = (ProjectFileItem) iterator.next(); 105 filesOnDisk.add(item.getFilePath()); 106 } 107 return buildListOfBuildFilesOfType(filesOnDisk, ProjectFileItem.FILE_TYPE_GRAMMAR); 108 } 109 110 public List <ProjectBuildList.BuildFile> buildListOfJavaBuildFiles() { 111 List <String > filesOnDisk = new ArrayList <String >(); 112 File [] files = new File (project.getSourcePath()).listFiles(); 113 for (int i = 0; i < files.length; i++) { 114 File file = files[i]; 115 filesOnDisk.add(file.getAbsolutePath()); 116 } 117 return buildListOfBuildFilesOfType(filesOnDisk, ProjectFileItem.FILE_TYPE_JAVA); 118 } 119 120 public boolean generateGrammarBuildFiles(List <ProjectBuildList.BuildFile> buildFiles) { 121 for (Iterator <ProjectBuildList.BuildFile> iterator = buildFiles.iterator(); iterator.hasNext() && !cancel;) { 122 ProjectBuildList.BuildFile buildFile = iterator.next(); 123 124 String file = buildFile.getFilePath(); 125 String libPath = buildFile.getFileFolder(); 126 String outputPath = buildFile.getFileFolder(); 127 128 setProgressStepInfo("Generating \""+ XJUtils.getLastPathComponent(file)+"\"..."); 129 130 String error = EngineRuntime.runANTLR(null, file, libPath, outputPath, this); 131 if(error != null) { 132 project.buildReportError(error); 133 return false; 134 } else { 135 buildFile.setDirty(false); 136 project.changeDone(); 137 } 138 } 139 return true; 140 } 141 142 public boolean compileFile(String file) { 143 String outputPath = project.getSourcePath(); 144 String error = EngineRuntime.compileFiles(null, new String [] { file }, outputPath, this); 145 if(error != null) { 146 project.buildReportError(error); 147 return false; 148 } else { 149 return true; 150 } 151 } 152 153 public boolean compileJavaBuildFiles(List <ProjectBuildList.BuildFile> buildFiles) { 154 for (Iterator <ProjectBuildList.BuildFile> iterator = buildFiles.iterator(); iterator.hasNext() && !cancel;) { 155 ProjectBuildList.BuildFile buildFile = iterator.next(); 156 String file = buildFile.getFilePath(); 157 setProgressStepInfo("Compiling \""+ XJUtils.getLastPathComponent(file)+"\"..."); 158 if(!compileFile(file)) 159 return false; 160 else { 161 buildFile.setDirty(false); 162 project.changeDone(); 163 } 164 } 165 return true; 166 } 167 168 public void setProgressStepInfo(String info) { 169 progress.setInfo(info); 170 progress.setProgress(++buildingProgress); 171 } 172 173 public boolean performBuild() { 174 List <ProjectBuildList.BuildFile> grammars = buildListOfGrammarBuildFiles(); 175 List <ProjectBuildList.BuildFile> javas = buildListOfJavaBuildFiles(); 176 177 int total = grammars.size()+javas.size(); 178 if(total == 0) 179 return true; 180 181 progress.setIndeterminate(false); 182 progress.setProgress(0); 183 progress.setProgressMax(total); 184 185 if(generateGrammarBuildFiles(grammars) && !cancel) { 186 if(grammars.size() > 0) { 187 190 javas = buildListOfJavaBuildFiles(); 191 total = grammars.size()+javas.size(); 192 progress.setProgressMax(total); 193 } 194 if(compileJavaBuildFiles(javas)) 195 return true; 196 } 197 return false; 198 } 199 200 public void performBuildFile() { 201 String type = fileToBuild.getFileType(); 202 List <ProjectBuildList.BuildFile> files; 203 204 if(type.equals(ProjectFileItem.FILE_TYPE_GRAMMAR)) 205 files = buildListOfGrammarBuildFiles(); 206 else if(type.equals(ProjectFileItem.FILE_TYPE_JAVA)) 207 files = buildListOfJavaBuildFiles(); 208 else 209 return; 210 211 for (Iterator <ProjectBuildList.BuildFile> iterator = files.iterator(); iterator.hasNext();) { 212 ProjectBuildList.BuildFile buildFile = iterator.next(); 213 if(buildFile.getFilePath().equals(fileToBuild.getFilePath())) { 214 List <ProjectBuildList.BuildFile> f = Collections.singletonList(buildFile); 215 if(type.equals(ProjectFileItem.FILE_TYPE_GRAMMAR)) 216 generateGrammarBuildFiles(f); 217 else if(type.equals(ProjectFileItem.FILE_TYPE_JAVA)) 218 compileJavaBuildFiles(f); 219 } 220 } 221 } 222 223 public void prepare() { 224 cancel = false; 225 buildingProgress = 0; 226 currentThread = null; 227 } 228 229 public void buildFile(ProjectFileItem fileItem) { 230 fileToBuild = fileItem; 231 232 progress.setCancellable(true); 233 progress.setTitle("Build"); 234 progress.setInfo("Building..."); 235 progress.setIndeterminate(true); 236 237 prepare(); 238 239 currentThread = new ThreadExecution(new Runnable () { 240 public void run() { 241 performBuildFile(); 242 progress.close(); 243 } 244 }); 245 246 progress.runModal(); 247 } 248 249 public void buildAll() { 250 progress.setCancellable(true); 251 progress.setTitle("Build"); 252 progress.setInfo("Preparing..."); 253 progress.setIndeterminate(true); 254 255 prepare(); 256 257 currentThread = new ThreadExecution(new Runnable () { 258 public void run() { 259 performBuild(); 260 progress.close(); 261 } 262 }); 263 264 progress.runModal(); 265 } 266 267 public void performRun() { 268 String error = EngineRuntime.runJava(null, project.getSourcePath(), project.getRunParameters(), ProjectBuilder.this); 269 if(error != null) { 270 project.buildReportError(error); 271 } 272 } 273 274 public void run() { 275 progress.setCancellable(true); 276 progress.setTitle("Run"); 277 progress.setInfo("Preparing..."); 278 progress.setIndeterminate(true); 279 280 prepare(); 281 282 currentThread = new ThreadExecution(new Runnable () { 283 public void run() { 284 if(performBuild() && !cancel) { 285 progress.setInfo("Running..."); 286 progress.setIndeterminate(true); 287 performRun(); 288 } 289 progress.close(); 290 } 291 }); 292 293 progress.runModal(); 294 } 295 296 299 300 public void clean() { 301 File [] files = new File (project.getSourcePath()).listFiles(); 302 for (int i = 0; i < files.length; i++) { 303 File file = files[i]; 304 String filePath = file.getAbsolutePath(); 305 if(filePath.endsWith(".class")) { 306 file.delete(); 307 } 308 } 309 310 project.getBuildList().setAllFilesToDirty(true); 312 313 project.changeDone(); 315 } 316 317 public void dialogDidCancel() { 318 if(cancel) { 319 Process p = EngineRuntime.getProcess(currentThread.t); 321 if(p != null) 322 p.destroy(); 323 } 324 cancel = true; 325 } 326 327 public void streamWatcherDidStarted() { 328 329 } 330 331 public void streamWatcherDidReceiveString(String string) { 332 project.printToConsole(string); 333 } 334 335 public void streamWatcherException(Exception e) { 336 project.printToConsole(e); 337 } 338 339 protected class ThreadExecution { 340 341 protected Runnable r; 342 protected Thread t; 343 344 public ThreadExecution(Runnable r) { 345 this.r = r; 346 launch(); 347 } 348 349 public void launch() { 350 SwingUtilities.invokeLater(new Runnable () { 351 public void run() { 352 t = new Thread (r); 353 t.start(); 354 } 355 }); 356 } 357 } 358 } 359 | Popular Tags |