KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > RubyProjectGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ruby.rubyproject;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Stack JavaDoc;
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 JavaDoc;
45 import org.w3c.dom.Element JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47
48 /**
49  * Creates a RubyProject from scratch according to some initial configuration.
50  */

51 public class RubyProjectGenerator {
52     
53     //static final String MINIMUM_ANT_VERSION = "1.6.5";
54

55     public static final String JavaDoc DEFAULT_SRC_NAME = "src.dir";
56     public static final String JavaDoc DEFAULT_TEST_SRC_NAME = "test.src.dir";
57     
58     private RubyProjectGenerator() {}
59     
60     /**
61      * Create a new empty Ruby project.
62      * @param dir the top-level directory (need not yet exist but if it does it must be empty)
63      * @param name the name for the project
64      * @return the helper object permitting it to be further customized
65      * @throws IOException in case something went wrong
66      */

67     public static RakeProjectHelper createProject(File JavaDoc dir, String JavaDoc name, String JavaDoc mainClass, String JavaDoc manifestFile) throws IOException JavaDoc {
68         FileObject dirFO = createProjectDir (dir);
69         // if manifestFile is null => it's TYPE_LIB
70
RakeProjectHelper h = createProject(dirFO, name, "lib", "test", mainClass, manifestFile, false); //NOI18N
71
Project p = ProjectManager.getDefault().findProject(dirFO);
72         ProjectManager.getDefault().saveProject(p);
73         FileObject srcFolder = dirFO.createFolder("lib"); // NOI18N
74
dirFO.createFolder("test"); // NOI18N
75
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 JavaDoc dir, final String JavaDoc name,
83                                                   final File JavaDoc[] sourceFolders, final File JavaDoc[] testFolders, final String JavaDoc manifestFile) throws IOException JavaDoc {
84         assert sourceFolders != null && testFolders != null: "Package roots can't be null"; //NOI18N
85
final FileObject dirFO = createProjectDir (dir);
86         // this constructor creates only java application type
87
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 JavaDoc>() {
92             public Void JavaDoc run() throws Exception JavaDoc {
93                 Element JavaDoc data = h.getPrimaryConfigurationData(true);
94                 Document JavaDoc doc = data.getOwnerDocument();
95                 NodeList JavaDoc nl = data.getElementsByTagNameNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
96                 assert nl.getLength() == 1;
97                 Element JavaDoc sourceRoots = (Element JavaDoc) nl.item(0);
98                 nl = data.getElementsByTagNameNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
99
assert nl.getLength() == 1;
100                 Element JavaDoc testRoots = (Element JavaDoc) nl.item(0);
101                 for (int i=0; i<sourceFolders.length; i++) {
102                     String JavaDoc propName;
103                     if (i == 0) {
104                         //Name the first src root src.dir to be compatible with NB 4.0
105
propName = "src.dir"; //NOI18N
106
}
107                     else {
108                         String JavaDoc name = sourceFolders[i].getName();
109                         propName = name + ".dir"; //NOI18N
110
}
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"; //NOI18N
117
}
118                     String JavaDoc srcReference = refHelper.createForeignFileReference(sourceFolders[i], RubyProject.SOURCES_TYPE_RUBY);
119                     Element JavaDoc root = doc.createElementNS (RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
120
root.setAttribute ("id",propName); //NOI18N
121
sourceRoots.appendChild(root);
122                     props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
123                     props.put(propName,srcReference);
124                     h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); // #47609
125
}
126                 for (int i = 0; i < testFolders.length; i++) {
127                     if (!testFolders[i].exists()) {
128                         testFolders[i].mkdirs();
129                     }
130                     String JavaDoc propName;
131                     if (i == 0) {
132                         //Name the first test root test.src.dir to be compatible with NB 4.0
133
propName = "test.src.dir"; //NOI18N
134
}
135                     else {
136                         String JavaDoc name = testFolders[i].getName();
137                         propName = "test." + name + ".dir"; // NOI18N
138
}
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"; // NOI18N
144
}
145                     String JavaDoc testReference = refHelper.createForeignFileReference(testFolders[i], RubyProject.SOURCES_TYPE_RUBY);
146                     Element JavaDoc root = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE, "root"); // NOI18N
147
root.setAttribute("id", propName); // NOI18N
148
testRoots.appendChild(root);
149                     props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); // #47609
150
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 JavaDoc name,
165                                                   String JavaDoc srcRoot, String JavaDoc testRoot, String JavaDoc mainClass, String JavaDoc manifestFile, boolean isLibrary) throws IOException JavaDoc {
166         RakeProjectHelper h = ProjectGenerator.createProject(dirFO, RubyProjectType.TYPE);
167         Element JavaDoc data = h.getPrimaryConfigurationData(true);
168         Document JavaDoc doc = data.getOwnerDocument();
169         Element JavaDoc nameEl = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); // NOI18N
170
nameEl.appendChild(doc.createTextNode(name));
171         data.appendChild(nameEl);
172 // Element minant = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE, "minimum-ant-version"); // NOI18N
173
// minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION)); // NOI18N
174
// data.appendChild(minant);
175
EditableProperties ep = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
176         Element JavaDoc sourceRoots = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); //NOI18N
177
if (srcRoot != null) {
178             Element JavaDoc root = doc.createElementNS (RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
179
root.setAttribute ("id","src.dir"); //NOI18N
180
sourceRoots.appendChild(root);
181             ep.setProperty("src.dir", srcRoot); // NOI18N
182
}
183         data.appendChild (sourceRoots);
184         Element JavaDoc testRoots = doc.createElementNS(RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
185
if (testRoot != null) {
186             Element JavaDoc root = doc.createElementNS (RubyProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
187
root.setAttribute ("id","test.src.dir"); //NOI18N
188
testRoots.appendChild (root);
189             ep.setProperty("test.src.dir", testRoot); // NOI18N
190
}
191         data.appendChild (testRoots);
192         h.putPrimaryConfigurationData(data, true);
193 // ep.setProperty("dist.dir", "dist"); // NOI18N
194
// ep.setComment("dist.dir", new String[] {"# " + NbBundle.getMessage(RubyProjectGenerator.class, "COMMENT_dist.dir")}, false); // NOI18N
195
// ep.setProperty("dist.jar", "${dist.dir}/" + PropertyUtils.getUsablePropertyName(name) + ".jar"); // NOI18N
196
// ep.setProperty("javac.classpath", new String[0]); // NOI18N
197
// ep.setProperty("build.sysclasspath", "ignore"); // NOI18N
198
// ep.setComment("build.sysclasspath", new String[] {"# " + NbBundle.getMessage(RubyProjectGenerator.class, "COMMENT_build.sysclasspath")}, false); // NOI18N
199
// ep.setProperty("run.classpath", new String[] { // NOI18N
200
// "${javac.classpath}:", // NOI18N
201
// "${build.classes.dir}", // NOI18N
202
// });
203
// ep.setProperty("debug.classpath", new String[] { // NOI18N
204
// "${run.classpath}", // NOI18N
205
// });
206
// ep.setProperty("jar.compress", "false"); // NOI18N
207
if (!isLibrary) {
208             ep.setProperty(RubyProjectProperties.MAIN_CLASS, mainClass == null ? "" : mainClass); // NOI18N
209
}
210         
211 // ep.setProperty("javac.compilerargs", ""); // NOI18N
212
// ep.setComment("javac.compilerargs", new String[] {
213
// "# " + NbBundle.getMessage(RubyProjectGenerator.class, "COMMENT_javac.compilerargs"), // NOI18N
214
// }, false);
215
// SpecificationVersion sourceLevel = getDefaultSourceLevel();
216
// ep.setProperty("javac.source", sourceLevel.toString()); // NOI18N
217
// ep.setProperty("javac.target", sourceLevel.toString()); // NOI18N
218
// ep.setProperty("javac.deprecation", "false"); // NOI18N
219
// ep.setProperty("javac.test.classpath", new String[] { // NOI18N
220
// "${javac.classpath}:", // NOI18N
221
// "${build.classes.dir}:", // NOI18N
222
// "${libs.junit.classpath}", // NOI18N
223
// });
224
// ep.setProperty("run.test.classpath", new String[] { // NOI18N
225
// "${javac.test.classpath}:", // NOI18N
226
// "${build.test.classes.dir}", // NOI18N
227
// });
228
// ep.setProperty("debug.test.classpath", new String[] { // NOI18N
229
// "${run.test.classpath}", // NOI18N
230
// });
231
//
232
// ep.setProperty("build.generated.dir", "${build.dir}/generated"); // NOI18N
233
// ep.setProperty("meta.inf.dir", "${src.dir}/META-INF"); // NOI18N
234
//
235
// ep.setProperty("build.dir", "build"); // NOI18N
236
// ep.setComment("build.dir", new String[] {"# " + NbBundle.getMessage(RubyProjectGenerator.class, "COMMENT_build.dir")}, false); // NOI18N
237
// ep.setProperty("build.classes.dir", "${build.dir}/classes"); // NOI18N
238
// ep.setProperty("build.test.classes.dir", "${build.dir}/test/classes"); // NOI18N
239
// ep.setProperty("build.test.results.dir", "${build.dir}/test/results"); // NOI18N
240
// ep.setProperty("build.classes.excludes", "**/*.java,**/*.form"); // NOI18N
241
// ep.setProperty("dist.javadoc.dir", "${dist.dir}/javadoc"); // NOI18N
242
// ep.setProperty("platform.active", "default_platform"); // NOI18N
243
//
244
// ep.setProperty("run.jvmargs", ""); // NOI18N
245
// ep.setComment("run.jvmargs", new String[] {
246
// "# " + NbBundle.getMessage(RubyProjectGenerator.class, "COMMENT_run.jvmargs"), // NOI18N
247
// "# " + NbBundle.getMessage(RubyProjectGenerator.class, "COMMENT_run.jvmargs_2"), // NOI18N
248
// "# " + NbBundle.getMessage(RubyProjectGenerator.class, "COMMENT_run.jvmargs_3"), // NOI18N
249
// }, false);
250

251 // ep.setProperty(RubyProjectProperties.JAVADOC_PRIVATE, "false"); // NOI18N
252
// ep.setProperty(RubyProjectProperties.JAVADOC_NO_TREE, "false"); // NOI18N
253
// ep.setProperty(RubyProjectProperties.JAVADOC_USE, "true"); // NOI18N
254
// ep.setProperty(RubyProjectProperties.JAVADOC_NO_NAVBAR, "false"); // NOI18N
255
// ep.setProperty(RubyProjectProperties.JAVADOC_NO_INDEX, "false"); // NOI18N
256
// ep.setProperty(RubyProjectProperties.JAVADOC_SPLIT_INDEX, "true"); // NOI18N
257
// ep.setProperty(RubyProjectProperties.JAVADOC_AUTHOR, "false"); // NOI18N
258
// ep.setProperty(RubyProjectProperties.JAVADOC_VERSION, "false"); // NOI18N
259
// ep.setProperty(RubyProjectProperties.JAVADOC_WINDOW_TITLE, ""); // NOI18N
260
// ep.setProperty(RubyProjectProperties.JAVADOC_ENCODING, ""); // NOI18N
261
// ep.setProperty(RubyProjectProperties.JAVADOC_ADDITIONALPARAM, ""); // NOI18N
262

263 // if (manifestFile != null) {
264
// ep.setProperty("manifest.file", manifestFile); // NOI18N
265
// }
266
h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, ep);
267         return h;
268     }
269
270     private static FileObject createProjectDir (File JavaDoc dir) throws IOException JavaDoc {
271         Stack JavaDoc<String JavaDoc> stack = new Stack JavaDoc<String JavaDoc>();
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 JavaDoc dir) throws FileStateInvalidException {
289         File JavaDoc 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; // NOI18N
295
dirFO.getFileSystem().refresh(false);
296     }
297     
298
299     private static void createMainClass( String JavaDoc mainClassName, FileObject srcFolder, String JavaDoc templateName ) throws IOException JavaDoc {
300         
301         int lastDotIdx = mainClassName.lastIndexOf( '/' );
302         String JavaDoc 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; // Don't know the template
320
}
321                 
322         DataObject mt = DataObject.find( mainTemplate );
323         
324         FileObject pkgFolder = srcFolder;
325         if ( pName != null ) {
326             String JavaDoc fName = pName.replace( '.', '/' ); // NOI18N
327
pkgFolder = FileUtil.createFolder( srcFolder, fName );
328         }
329         DataFolder pDf = DataFolder.findFolder( pkgFolder );
330         // BEGIN SEMPLICE MODIFICATIONS
331
int extension = mName.lastIndexOf('.');
332         if (extension != -1) {
333             mName = mName.substring(0, extension);
334         }
335         // END SEMPLICE MODIFICATIONS
336
mt.createFromTemplate( pDf, mName );
337         
338     }
339 }
340
341
342
Popular Tags