1 19 20 package org.netbeans.modules.ruby.rubyproject; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Stack ; 25 import org.netbeans.api.project.Project; 26 import org.netbeans.api.project.ProjectManager; 27 import org.netbeans.modules.ruby.rubyproject.ui.customizer.RubyProjectProperties; 28 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper; 29 import org.netbeans.modules.ruby.spi.project.support.rake.EditableProperties; 30 import org.netbeans.modules.ruby.spi.project.support.rake.ProjectGenerator; 31 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils; 32 import org.netbeans.modules.ruby.spi.project.support.rake.ReferenceHelper; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.filesystems.Repository; 36 import org.openide.filesystems.FileStateInvalidException; 37 import org.openide.loaders.DataFolder; 38 import org.openide.loaders.DataObject; 39 import org.openide.modules.SpecificationVersion; 40 import org.openide.util.Mutex; 41 import org.openide.util.MutexException; 42 import org.openide.ErrorManager; 43 import org.openide.util.NbBundle; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.NodeList ; 47 48 51 public class RubyProjectGenerator { 52 53 55 public static final String DEFAULT_SRC_NAME = "src.dir"; 56 public static final String DEFAULT_TEST_SRC_NAME = "test.src.dir"; 57 58 private RubyProjectGenerator() {} 59 60 67 public static RakeProjectHelper createProject(File dir, String name, String mainClass, String manifestFile) throws IOException { 68 FileObject dirFO = createProjectDir (dir); 69 RakeProjectHelper h = createProject(dirFO, name, "lib", "test", mainClass, manifestFile, false); Project p = ProjectManager.getDefault().findProject(dirFO); 72 ProjectManager.getDefault().saveProject(p); 73 FileObject srcFolder = dirFO.createFolder("lib"); dirFO.createFolder("test"); if ( mainClass != null ) { 76 createMainClass( mainClass, srcFolder, "Templates/Ruby/main.rb" ); 77 } 78 createMainClass( "Rakefile.rb", srcFolder, "Templates/Ruby/rakefile.rb" ); 79 return h; 80 } 81 82 public static RakeProjectHelper createProject(final File dir, final String name, 83 final File [] sourceFolders, final File [] testFolders, final String manifestFile) throws IOException { 84 assert sourceFolders != null && testFolders != null: "Package roots can't be null"; final FileObject dirFO = createProjectDir (dir); 86 final RakeProjectHelper h = createProject(dirFO, name, null, null, null, manifestFile, false); 88 final RubyProject p = (RubyProject) ProjectManager.getDefault().findProject(dirFO); 89 final ReferenceHelper refHelper = p.getReferenceHelper(); 90 try { 91 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 92 public Void run() throws Exception { 93 Element data = h.getPrimaryConfigurationData(true); 94 Document doc = data.getOwnerDocument(); 95 NodeList nl = data.getElementsByTagNameNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); 96 assert nl.getLength() == 1; 97 Element sourceRoots = (Element ) nl.item(0); 98 nl = data.getElementsByTagNameNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); assert nl.getLength() == 1; 100 Element testRoots = (Element ) nl.item(0); 101 for (int i=0; i<sourceFolders.length; i++) { 102 String propName; 103 if (i == 0) { 104 propName = "src.dir"; } 107 else { 108 String name = sourceFolders[i].getName(); 109 propName = name + ".dir"; } 111 112 int rootIndex = 1; 113 EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 114 while (props.containsKey(propName)) { 115 rootIndex++; 116 propName = name + rootIndex + ".dir"; } 118 String srcReference = refHelper.createForeignFileReference(sourceFolders[i], RubyProject.SOURCES_TYPE_RUBY); 119 Element root = doc.createElementNS (RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); root.setAttribute ("id",propName); sourceRoots.appendChild(root); 122 props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 123 props.put(propName,srcReference); 124 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); } 126 for (int i = 0; i < testFolders.length; i++) { 127 if (!testFolders[i].exists()) { 128 testFolders[i].mkdirs(); 129 } 130 String propName; 131 if (i == 0) { 132 propName = "test.src.dir"; } 135 else { 136 String name = testFolders[i].getName(); 137 propName = "test." + name + ".dir"; } 139 int rootIndex = 1; 140 EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 141 while (props.containsKey(propName)) { 142 rootIndex++; 143 propName = "test." + name + rootIndex + ".dir"; } 145 String testReference = refHelper.createForeignFileReference(testFolders[i], RubyProject.SOURCES_TYPE_RUBY); 146 Element root = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE, "root"); root.setAttribute("id", propName); testRoots.appendChild(root); 149 props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); props.put(propName, testReference); 151 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); 152 } 153 h.putPrimaryConfigurationData(data,true); 154 ProjectManager.getDefault().saveProject (p); 155 return null; 156 } 157 }); 158 } catch (MutexException me ) { 159 ErrorManager.getDefault().notify (me); 160 } 161 return h; 162 } 163 164 private static RakeProjectHelper createProject(FileObject dirFO, String name, 165 String srcRoot, String testRoot, String mainClass, String manifestFile, boolean isLibrary) throws IOException { 166 RakeProjectHelper h = ProjectGenerator.createProject(dirFO, RubyProjectType.TYPE); 167 Element data = h.getPrimaryConfigurationData(true); 168 Document doc = data.getOwnerDocument(); 169 Element nameEl = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); nameEl.appendChild(doc.createTextNode(name)); 171 data.appendChild(nameEl); 172 EditableProperties ep = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 176 Element sourceRoots = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); if (srcRoot != null) { 178 Element root = doc.createElementNS (RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); root.setAttribute ("id","src.dir"); sourceRoots.appendChild(root); 181 ep.setProperty("src.dir", srcRoot); } 183 data.appendChild (sourceRoots); 184 Element testRoots = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); if (testRoot != null) { 186 Element root = doc.createElementNS (RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); root.setAttribute ("id","test.src.dir"); testRoots.appendChild (root); 189 ep.setProperty("test.src.dir", testRoot); } 191 data.appendChild (testRoots); 192 h.putPrimaryConfigurationData(data, true); 193 if (!isLibrary) { 208 ep.setProperty(RubyProjectProperties.MAIN_CLASS, mainClass == null ? "" : mainClass); } 210 211 251 263 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, ep); 267 return h; 268 } 269 270 private static FileObject createProjectDir (File dir) throws IOException { 271 Stack <String > stack = new Stack <String >(); 272 while (!dir.exists()) { 273 stack.push (dir.getName()); 274 dir = dir.getParentFile(); 275 } 276 FileObject dirFO = FileUtil.toFileObject (dir); 277 if (dirFO == null) { 278 refreshFileSystem(dir); 279 dirFO = FileUtil.toFileObject (dir); 280 } 281 assert dirFO != null; 282 while (!stack.isEmpty()) { 283 dirFO = dirFO.createFolder(stack.pop()); 284 } 285 return dirFO; 286 } 287 288 private static void refreshFileSystem (final File dir) throws FileStateInvalidException { 289 File rootF = dir; 290 while (rootF.getParentFile() != null) { 291 rootF = rootF.getParentFile(); 292 } 293 FileObject dirFO = FileUtil.toFileObject(rootF); 294 assert dirFO != null : "At least disk roots must be mounted! " + rootF; dirFO.getFileSystem().refresh(false); 296 } 297 298 299 private static void createMainClass( String mainClassName, FileObject srcFolder, String templateName ) throws IOException { 300 301 int lastDotIdx = mainClassName.lastIndexOf( '/' ); 302 String mName, pName; 303 if ( lastDotIdx == -1 ) { 304 mName = mainClassName.trim(); 305 pName = null; 306 } 307 else { 308 mName = mainClassName.substring( lastDotIdx + 1 ).trim(); 309 pName = mainClassName.substring( 0, lastDotIdx ).trim(); 310 } 311 312 if ( mName.length() == 0 ) { 313 return; 314 } 315 316 FileObject mainTemplate = Repository.getDefault().getDefaultFileSystem().findResource( templateName ); 317 318 if ( mainTemplate == null ) { 319 return; } 321 322 DataObject mt = DataObject.find( mainTemplate ); 323 324 FileObject pkgFolder = srcFolder; 325 if ( pName != null ) { 326 String fName = pName.replace( '.', '/' ); pkgFolder = FileUtil.createFolder( srcFolder, fName ); 328 } 329 DataFolder pDf = DataFolder.findFolder( pkgFolder ); 330 int extension = mName.lastIndexOf('.'); 332 if (extension != -1) { 333 mName = mName.substring(0, extension); 334 } 335 mt.createFromTemplate( pDf, mName ); 337 338 } 339 } 340 341 342 | Popular Tags |