001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ruby.railsprojects;
043:
044: import java.io.BufferedReader;
045: import java.io.File;
046: import java.io.FileReader;
047: import java.io.IOException;
048: import java.net.MalformedURLException;
049: import java.net.URL;
050: import java.util.List;
051: import java.util.regex.Matcher;
052: import java.util.regex.Pattern;
053: import org.netbeans.api.ruby.platform.RubyInstallation;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.api.ruby.platform.RubyPlatform;
056: import org.netbeans.modules.ruby.platform.gems.GemManager;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.util.Exceptions;
060:
061: /**
062: * Miscellaneous utilities for the Rails project module.
063: * @author Jiri Rechtacek
064: */
065: public class RailsProjectUtil {
066:
067: private RailsProjectUtil() {
068: }
069:
070: /** Get the version string out of a ruby version.rb file */
071: public static String getVersionString(File versionFile) {
072: try {
073: Pattern VERSION_ELEMENT = Pattern
074: .compile("\\s*[A-Z]+\\s*=\\s*(\\d+)\\s*");
075: BufferedReader br = new BufferedReader(new FileReader(
076: versionFile));
077: StringBuilder sb = new StringBuilder();
078: int major = 0;
079: int minor = 0;
080: int tiny = 0;
081: for (int line = 0; line < 10; line++) {
082: String s = br.readLine();
083: if (s == null) {
084: break;
085: }
086:
087: if (s.indexOf("MAJOR") != -1) {
088: Matcher m = VERSION_ELEMENT.matcher(s);
089: if (m.matches()) {
090: major = Integer.parseInt(m.group(1));
091: }
092: } else if (s.indexOf("MINOR") != -1) {
093: Matcher m = VERSION_ELEMENT.matcher(s);
094: if (m.matches()) {
095: minor = Integer.parseInt(m.group(1));
096: }
097: } else if (s.indexOf("TINY") != -1) {
098: Matcher m = VERSION_ELEMENT.matcher(s);
099: if (m.matches()) {
100: tiny = Integer.parseInt(m.group(1));
101: }
102: }
103: }
104: br.close();
105:
106: return major + "." + minor + "." + tiny;
107: } catch (IOException ioe) {
108: Exceptions.printStackTrace(ioe);
109: }
110:
111: return null;
112: }
113:
114: public static String getRailsVersion(Project project) {
115: GemManager gemManager = RubyPlatform.gemManagerFor(project);
116: // Add in the builtins first (since they provide some more specific
117: // UI configuration for known generators (labelling the arguments etc.)
118: String railsVersion = gemManager.getVersion("rails"); // NOI18N
119:
120: FileObject railsPlugin = project.getProjectDirectory()
121: .getFileObject("vendor/rails/railties"); // NOI18N
122: if (railsPlugin != null) {
123: FileObject versionFo = railsPlugin
124: .getFileObject("lib/rails/version.rb"); // NOI18N
125: if (versionFo != null) {
126: File versionFile = FileUtil.toFile(versionFo);
127: String version = RailsProjectUtil
128: .getVersionString(versionFile);
129: if (version != null) {
130: railsVersion = version;
131: }
132: }
133: }
134:
135: return railsVersion;
136: }
137:
138: /**
139: * Returns the property value evaluated by RailsProject's PropertyEvaluator.
140: *
141: * @param p project
142: * @param value of property
143: * @return evaluated value of given property or null if the property not set or
144: * if the project doesn't provide RakeProjectHelper
145: */
146: public static Object getEvaluatedProperty(Project p, String value) {
147: if (value == null) {
148: return null;
149: }
150: RailsProject j2seprj = p.getLookup().lookup(RailsProject.class);
151: if (j2seprj != null) {
152: return j2seprj.evaluator().evaluate(value);
153: } else {
154: return null;
155: }
156: }
157:
158: public static void getAllScripts(String prefix,
159: FileObject sourcesRoot, List<String> result) {
160: FileObject children[] = sourcesRoot.getChildren();
161: if (!"".equals(prefix)) {
162: prefix += "/";
163: //prefix += ".";
164: }
165: for (int i = 0; i < children.length; i++) {
166: if (children[i].isData()) {
167: if (children[i].getMIMEType().equals(
168: RubyInstallation.RUBY_MIME_TYPE)) {
169: result.add(prefix + children[i].getNameExt());
170: }
171: }
172: if (children[i].isFolder()) {
173: getAllScripts(prefix + children[i].getNameExt(),
174: children[i], result);
175: }
176: }
177: }
178:
179: /**
180: * Creates an URL of a classpath or sourcepath root
181: * For the existing directory it returns the URL obtained from {@link File#toUri()}
182: * For archive file it returns an URL of the root of the archive file
183: * For non existing directory it fixes the ending '/'
184: * @param root the file of a root
185: * @param offset a path relative to the root file or null (eg. src/ for jar:file:///lib.jar!/src/)"
186: * @return an URL of the root
187: * @throws MalformedURLException if the URL cannot be created
188: */
189: public static URL getRootURL(File root, String offset)
190: throws MalformedURLException {
191: URL url = root.toURI().toURL();
192: if (FileUtil.isArchiveFile(url)) {
193: url = FileUtil.getArchiveRoot(url);
194: } else if (!root.exists()) {
195: url = new URL(url.toExternalForm() + "/"); // NOI18N
196: }
197: if (offset != null) {
198: assert offset.endsWith("/"); //NOI18N
199: url = new URL(url.toExternalForm() + offset); // NOI18N
200: }
201: return url;
202: }
203: }
|