KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > RailsProjectGenerator


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.railsprojects;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Stack JavaDoc;
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 JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45
46 /**
47  * Creates a RailsProject from scratch according to some initial configuration.
48  */

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); // NOI18N
53

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

66     public static RakeProjectHelper createProject(File JavaDoc dir, String JavaDoc name, boolean create) throws IOException JavaDoc {
67         FileObject dirFO = createProjectDir (dir);
68         
69         // Run Rails to generate the appliation skeleton
70
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         // TODO - open database.yml and edit in JDBC stuff?
81

82         RakeProjectHelper h = createProject(dirFO, name/*, "app", "test", mainClass, manifestFile, false*/); //NOI18N
83
Project p = ProjectManager.getDefault().findProject(dirFO);
84         ProjectManager.getDefault().saveProject(p);
85 // FileObject srcFolder = dirFO.createFolder("src"); // NOI18N
86
// dirFO.createFolder("test"); // NOI18N
87
// if ( mainClass != null ) {
88
// createMainClass( mainClass, srcFolder, "Templates/Ruby/main.rb" );
89
// }
90
// createMainClass( "Rakefile.rb", srcFolder, "Templates/Ruby/rakefile.rb" );
91

92         // Start the server
93
WebrickServer server = p.getLookup().lookup(WebrickServer.class);
94         if (server != null) {
95             server.ensureRunning();
96         }
97         return h;
98     }
99
100 // public static RakeProjectHelper createProject(final File dir, final String name,
101
// final File[] sourceFolders, final File[] testFolders, final String manifestFile) throws IOException {
102
// assert sourceFolders != null && testFolders != null: "Package roots can't be null"; //NOI18N
103
// final FileObject dirFO = createProjectDir (dir);
104
// // this constructor creates only java application type
105
// final RakeProjectHelper h = createProject(dirFO, name, null, null, null, manifestFile, false);
106
// final RailsProject p = (RailsProject) ProjectManager.getDefault().findProject(dirFO);
107
// final ReferenceHelper refHelper = p.getReferenceHelper();
108
// try {
109
// ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
110
// public Void run() throws Exception {
111
// Element data = h.getPrimaryConfigurationData(true);
112
// Document doc = data.getOwnerDocument();
113
// NodeList nl = data.getElementsByTagNameNS(RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
114
// assert nl.getLength() == 1;
115
// Element sourceRoots = (Element) nl.item(0);
116
// nl = data.getElementsByTagNameNS(RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
117
// assert nl.getLength() == 1;
118
// Element testRoots = (Element) nl.item(0);
119
// for (int i=0; i<sourceFolders.length; i++) {
120
// String propName;
121
// if (i == 0) {
122
// //Name the first src root src.dir to be compatible with NB 4.0
123
// propName = "src.dir"; //NOI18N
124
// }
125
// else {
126
// String name = sourceFolders[i].getName();
127
// propName = name + ".dir"; //NOI18N
128
// }
129
//
130
// int rootIndex = 1;
131
// EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
132
// while (props.containsKey(propName)) {
133
// rootIndex++;
134
// propName = name + rootIndex + ".dir"; //NOI18N
135
// }
136
// String srcReference = refHelper.createForeignFileReference(sourceFolders[i], RailsProject.SOURCES_TYPE_RUBY);
137
// Element root = doc.createElementNS (RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
138
// root.setAttribute ("id",propName); //NOI18N
139
// sourceRoots.appendChild(root);
140
// props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
141
// props.put(propName,srcReference);
142
// h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); // #47609
143
// }
144
// for (int i = 0; i < testFolders.length; i++) {
145
// if (!testFolders[i].exists()) {
146
// testFolders[i].mkdirs();
147
// }
148
// String propName;
149
// if (i == 0) {
150
// //Name the first test root test.src.dir to be compatible with NB 4.0
151
// propName = "test.src.dir"; //NOI18N
152
// }
153
// else {
154
// String name = testFolders[i].getName();
155
// propName = "test." + name + ".dir"; // NOI18N
156
// }
157
// int rootIndex = 1;
158
// EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
159
// while (props.containsKey(propName)) {
160
// rootIndex++;
161
// propName = "test." + name + rootIndex + ".dir"; // NOI18N
162
// }
163
// String testReference = refHelper.createForeignFileReference(testFolders[i], RailsProject.SOURCES_TYPE_RUBY);
164
// Element root = doc.createElementNS(RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE, "root"); // NOI18N
165
// root.setAttribute("id", propName); // NOI18N
166
// testRoots.appendChild(root);
167
// props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); // #47609
168
// props.put(propName, testReference);
169
// h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props);
170
// }
171
// h.putPrimaryConfigurationData(data,true);
172
// ProjectManager.getDefault().saveProject (p);
173
// return null;
174
// }
175
// });
176
// } catch (MutexException me ) {
177
// ErrorManager.getDefault().notify (me);
178
// }
179
// return h;
180
// }
181

182     private static RakeProjectHelper createProject(FileObject dirFO, String JavaDoc name/*,
183                                                   String srcRoot, String testRoot, String mainClass, String manifestFile, boolean isLibrary*/
) throws IOException JavaDoc {
184         RakeProjectHelper h = ProjectGenerator.createProject(dirFO, RailsProjectType.TYPE);
185         Element JavaDoc data = h.getPrimaryConfigurationData(true);
186         Document JavaDoc doc = data.getOwnerDocument();
187         Element JavaDoc nameEl = doc.createElementNS(RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); // NOI18N
188
nameEl.appendChild(doc.createTextNode(name));
189         data.appendChild(nameEl);
190 // Element minant = doc.createElementNS(RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE, "minimum-ant-version"); // NOI18N
191
// minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION)); // NOI18N
192
// data.appendChild(minant);
193
EditableProperties ep = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
194 // Element sourceRoots = doc.createElementNS(RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); //NOI18N
195
// if (srcRoot != null) {
196
// Element root = doc.createElementNS (RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
197
// root.setAttribute ("id","src.dir"); //NOI18N
198
// sourceRoots.appendChild(root);
199
// ep.setProperty("src.dir", srcRoot); // NOI18N
200
// }
201
// data.appendChild (sourceRoots);
202
// Element testRoots = doc.createElementNS(RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
203
// if (testRoot != null) {
204
// Element root = doc.createElementNS (RailsProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
205
// root.setAttribute ("id","test.src.dir"); //NOI18N
206
// testRoots.appendChild (root);
207
// ep.setProperty("test.src.dir", testRoot); // NOI18N
208
// }
209
// data.appendChild (testRoots);
210
h.putPrimaryConfigurationData(data, true);
211 // ep.setProperty("dist.dir", "dist"); // NOI18N
212
// ep.setComment("dist.dir", new String[] {"# " + NbBundle.getMessage(RailsProjectGenerator.class, "COMMENT_dist.dir")}, false); // NOI18N
213
// ep.setProperty("dist.jar", "${dist.dir}/" + PropertyUtils.getUsablePropertyName(name) + ".jar"); // NOI18N
214
// ep.setProperty("javac.classpath", new String[0]); // NOI18N
215
// ep.setProperty("build.sysclasspath", "ignore"); // NOI18N
216
// ep.setComment("build.sysclasspath", new String[] {"# " + NbBundle.getMessage(RailsProjectGenerator.class, "COMMENT_build.sysclasspath")}, false); // NOI18N
217
// ep.setProperty("run.classpath", new String[] { // NOI18N
218
// "${javac.classpath}:", // NOI18N
219
// "${build.classes.dir}", // NOI18N
220
// });
221
// ep.setProperty("debug.classpath", new String[] { // NOI18N
222
// "${run.classpath}", // NOI18N
223
// });
224
// ep.setProperty("jar.compress", "false"); // NOI18N
225
// if (!isLibrary) {
226
// ep.setProperty(RailsProjectProperties.MAIN_CLASS, mainClass == null ? "" : mainClass); // NOI18N
227
// }
228

229 // ep.setProperty("javac.compilerargs", ""); // NOI18N
230
// ep.setComment("javac.compilerargs", new String[] {
231
// "# " + NbBundle.getMessage(RailsProjectGenerator.class, "COMMENT_javac.compilerargs"), // NOI18N
232
// }, false);
233
// SpecificationVersion sourceLevel = getDefaultSourceLevel();
234
// ep.setProperty("javac.source", sourceLevel.toString()); // NOI18N
235
// ep.setProperty("javac.target", sourceLevel.toString()); // NOI18N
236
// ep.setProperty("javac.deprecation", "false"); // NOI18N
237
// ep.setProperty("javac.test.classpath", new String[] { // NOI18N
238
// "${javac.classpath}:", // NOI18N
239
// "${build.classes.dir}:", // NOI18N
240
// "${libs.junit.classpath}", // NOI18N
241
// });
242
// ep.setProperty("run.test.classpath", new String[] { // NOI18N
243
// "${javac.test.classpath}:", // NOI18N
244
// "${build.test.classes.dir}", // NOI18N
245
// });
246
// ep.setProperty("debug.test.classpath", new String[] { // NOI18N
247
// "${run.test.classpath}", // NOI18N
248
// });
249
//
250
// ep.setProperty("build.generated.dir", "${build.dir}/generated"); // NOI18N
251
// ep.setProperty("meta.inf.dir", "${src.dir}/META-INF"); // NOI18N
252
//
253
// ep.setProperty("build.dir", "build"); // NOI18N
254
// ep.setComment("build.dir", new String[] {"# " + NbBundle.getMessage(RailsProjectGenerator.class, "COMMENT_build.dir")}, false); // NOI18N
255
// ep.setProperty("build.classes.dir", "${build.dir}/classes"); // NOI18N
256
// ep.setProperty("build.test.classes.dir", "${build.dir}/test/classes"); // NOI18N
257
// ep.setProperty("build.test.results.dir", "${build.dir}/test/results"); // NOI18N
258
// ep.setProperty("build.classes.excludes", "**/*.java,**/*.form"); // NOI18N
259
// ep.setProperty("dist.javadoc.dir", "${dist.dir}/javadoc"); // NOI18N
260
// ep.setProperty("platform.active", "default_platform"); // NOI18N
261
//
262
// ep.setProperty("run.jvmargs", ""); // NOI18N
263
// ep.setComment("run.jvmargs", new String[] {
264
// "# " + NbBundle.getMessage(RailsProjectGenerator.class, "COMMENT_run.jvmargs"), // NOI18N
265
// "# " + NbBundle.getMessage(RailsProjectGenerator.class, "COMMENT_run.jvmargs_2"), // NOI18N
266
// "# " + NbBundle.getMessage(RailsProjectGenerator.class, "COMMENT_run.jvmargs_3"), // NOI18N
267
// }, false);
268

269 // ep.setProperty(RailsProjectProperties.JAVADOC_PRIVATE, "false"); // NOI18N
270
// ep.setProperty(RailsProjectProperties.JAVADOC_NO_TREE, "false"); // NOI18N
271
// ep.setProperty(RailsProjectProperties.JAVADOC_USE, "true"); // NOI18N
272
// ep.setProperty(RailsProjectProperties.JAVADOC_NO_NAVBAR, "false"); // NOI18N
273
// ep.setProperty(RailsProjectProperties.JAVADOC_NO_INDEX, "false"); // NOI18N
274
// ep.setProperty(RailsProjectProperties.JAVADOC_SPLIT_INDEX, "true"); // NOI18N
275
// ep.setProperty(RailsProjectProperties.JAVADOC_AUTHOR, "false"); // NOI18N
276
// ep.setProperty(RailsProjectProperties.JAVADOC_VERSION, "false"); // NOI18N
277
// ep.setProperty(RailsProjectProperties.JAVADOC_WINDOW_TITLE, ""); // NOI18N
278
// ep.setProperty(RailsProjectProperties.JAVADOC_ENCODING, ""); // NOI18N
279
// ep.setProperty(RailsProjectProperties.JAVADOC_ADDITIONALPARAM, ""); // NOI18N
280

281 // if (manifestFile != null) {
282
// ep.setProperty("manifest.file", manifestFile); // NOI18N
283
// }
284
h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, ep);
285         return h;
286     }
287
288     private static FileObject createProjectDir (File JavaDoc dir) throws IOException JavaDoc {
289         Stack JavaDoc<String JavaDoc> stack = new Stack JavaDoc<String JavaDoc>();
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 JavaDoc dir) throws FileStateInvalidException {
307         File JavaDoc 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; // NOI18N
313
dirFO.getFileSystem().refresh(false);
314     }
315     
316
317     private static void createMainClass( String JavaDoc mainClassName, FileObject srcFolder, String JavaDoc templateName ) throws IOException JavaDoc {
318         
319         int lastDotIdx = mainClassName.lastIndexOf( '/' );
320         String JavaDoc 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; // Don't know the template
338
}
339                 
340         DataObject mt = DataObject.find( mainTemplate );
341         
342         FileObject pkgFolder = srcFolder;
343         if ( pName != null ) {
344             String JavaDoc fName = pName.replace( '.', '/' ); // NOI18N
345
pkgFolder = FileUtil.createFolder( srcFolder, fName );
346         }
347         DataFolder pDf = DataFolder.findFolder( pkgFolder );
348         // BEGIN SEMPLICE MODIFICATIONS
349
int extension = mName.lastIndexOf('.');
350         if (extension != -1) {
351             mName = mName.substring(0, extension);
352         }
353         // END SEMPLICE MODIFICATIONS
354
mt.createFromTemplate( pDf, mName );
355         
356     }
357 }
358
359
360
Popular Tags