1 19 20 package org.netbeans.modules.ruby.railsprojects; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Stack ; 25 import org.netbeans.modules.ruby.railsprojects.server.WebrickServer; 26 import org.netbeans.modules.ruby.rubyproject.execution.ExecutionService; 27 import org.netbeans.api.project.Project; 28 import org.netbeans.api.project.ProjectManager; 29 import org.netbeans.modules.ruby.rubyproject.execution.ExecutionDescriptor; 30 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation; 31 import org.netbeans.modules.ruby.rubyproject.execution.DirectoryFileLocator; 32 import org.netbeans.modules.ruby.rubyproject.execution.RegexpOutputRecognizer; 33 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper; 34 import org.netbeans.modules.ruby.spi.project.support.rake.EditableProperties; 35 import org.netbeans.modules.ruby.spi.project.support.rake.ProjectGenerator; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.filesystems.Repository; 39 import org.openide.filesystems.FileStateInvalidException; 40 import org.openide.loaders.DataFolder; 41 import org.openide.loaders.DataObject; 42 import org.openide.util.Task; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 46 49 public class RailsProjectGenerator { 50 public static final RegexpOutputRecognizer RAILS_GENERATOR = 51 new RegexpOutputRecognizer("^ ( create| force|identical| skip)\\s+([\\w|/]+\\.(rb|rhtml|yml|))$", 52 2, -1, -1); 54 public static final String DEFAULT_SRC_NAME = "src.dir"; 55 public static final String DEFAULT_TEST_SRC_NAME = "test.src.dir"; 56 57 private RailsProjectGenerator() {} 58 59 66 public static RakeProjectHelper createProject(File dir, String name, boolean create) throws IOException { 67 FileObject dirFO = createProjectDir (dir); 68 69 if (create) { 71 Task task = new ExecutionService(new ExecutionDescriptor("Generate Rails Project", dir.getParentFile(), 72 RubyInstallation.getInstance().getRails(), name). 73 fileLocator(new DirectoryFileLocator(dirFO)). 74 addOutputRecognizer(RAILS_GENERATOR)). 75 run(); 76 task.waitFinished(); 77 dirFO.getFileSystem().refresh(true); 78 } 79 80 82 RakeProjectHelper h = createProject(dirFO, name); Project p = ProjectManager.getDefault().findProject(dirFO); 84 ProjectManager.getDefault().saveProject(p); 85 92 WebrickServer server = p.getLookup().lookup(WebrickServer.class); 94 if (server != null) { 95 server.ensureRunning(); 96 } 97 return h; 98 } 99 100 182 private static RakeProjectHelper createProject(FileObject dirFO, String name) throws IOException { 184 RakeProjectHelper h = ProjectGenerator.createProject(dirFO, RailsProjectType.TYPE); 185 Element data = h.getPrimaryConfigurationData(true); 186 Document doc = data.getOwnerDocument(); 187 Element nameEl = doc.createElementNS(RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); nameEl.appendChild(doc.createTextNode(name)); 189 data.appendChild(nameEl); 190 EditableProperties ep = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 194 h.putPrimaryConfigurationData(data, true); 211 229 269 281 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, ep); 285 return h; 286 } 287 288 private static FileObject createProjectDir (File dir) throws IOException { 289 Stack <String > stack = new Stack <String >(); 290 while (!dir.exists()) { 291 stack.push (dir.getName()); 292 dir = dir.getParentFile(); 293 } 294 FileObject dirFO = FileUtil.toFileObject (dir); 295 if (dirFO == null) { 296 refreshFileSystem(dir); 297 dirFO = FileUtil.toFileObject (dir); 298 } 299 assert dirFO != null; 300 while (!stack.isEmpty()) { 301 dirFO = dirFO.createFolder(stack.pop()); 302 } 303 return dirFO; 304 } 305 306 private static void refreshFileSystem (final File dir) throws FileStateInvalidException { 307 File rootF = dir; 308 while (rootF.getParentFile() != null) { 309 rootF = rootF.getParentFile(); 310 } 311 FileObject dirFO = FileUtil.toFileObject(rootF); 312 assert dirFO != null : "At least disk roots must be mounted! " + rootF; dirFO.getFileSystem().refresh(false); 314 } 315 316 317 private static void createMainClass( String mainClassName, FileObject srcFolder, String templateName ) throws IOException { 318 319 int lastDotIdx = mainClassName.lastIndexOf( '/' ); 320 String mName, pName; 321 if ( lastDotIdx == -1 ) { 322 mName = mainClassName.trim(); 323 pName = null; 324 } 325 else { 326 mName = mainClassName.substring( lastDotIdx + 1 ).trim(); 327 pName = mainClassName.substring( 0, lastDotIdx ).trim(); 328 } 329 330 if ( mName.length() == 0 ) { 331 return; 332 } 333 334 FileObject mainTemplate = Repository.getDefault().getDefaultFileSystem().findResource( templateName ); 335 336 if ( mainTemplate == null ) { 337 return; } 339 340 DataObject mt = DataObject.find( mainTemplate ); 341 342 FileObject pkgFolder = srcFolder; 343 if ( pName != null ) { 344 String fName = pName.replace( '.', '/' ); pkgFolder = FileUtil.createFolder( srcFolder, fName ); 346 } 347 DataFolder pDf = DataFolder.findFolder( pkgFolder ); 348 int extension = mName.lastIndexOf('.'); 350 if (extension != -1) { 351 mName = mName.substring(0, extension); 352 } 353 mt.createFromTemplate( pDf, mName ); 355 356 } 357 } 358 359 360 | Popular Tags |