001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.ListUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.FileImpl;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.util.UniqueList;
025    
026    import java.io.File;
027    import java.io.FileInputStream;
028    import java.io.FilenameFilter;
029    
030    import java.util.ArrayList;
031    import java.util.Collections;
032    import java.util.HashMap;
033    import java.util.Iterator;
034    import java.util.List;
035    import java.util.Map;
036    import java.util.Properties;
037    
038    import org.apache.oro.io.GlobFilenameFilter;
039    import org.apache.tools.ant.DirectoryScanner;
040    
041    /**
042     * @author Alexander Chow
043     * @author Brian Wing Shun Chan
044     */
045    public class PluginsEnvironmentBuilder {
046    
047            public static void main(String[] args) throws Exception {
048                    try {
049                            File dir = new File(System.getProperty("plugins.env.dir"));
050    
051                            new PluginsEnvironmentBuilder(dir);
052                    }
053                    catch (Exception e) {
054                            e.printStackTrace();
055                    }
056            }
057    
058            public PluginsEnvironmentBuilder(File dir) throws Exception {
059                    DirectoryScanner ds = new DirectoryScanner();
060    
061                    ds.setBasedir(dir);
062                    ds.setIncludes(new String[] {"**\\liferay-plugin-package.properties"});
063    
064                    ds.scan();
065    
066                    String dirName = dir.getCanonicalPath();
067    
068                    String[] fileNames = ds.getIncludedFiles();
069    
070                    for (String fileName : fileNames) {
071                            setupProject(dirName, fileName);
072                    }
073            }
074    
075            protected void addClasspathEntry(StringBundler sb, String jar) {
076                    addClasspathEntry(sb, jar, null);
077            }
078    
079            protected void addClasspathEntry(
080                    StringBundler sb, String jar, Map<String, String> attributes) {
081    
082                    sb.append("\t<classpathentry kind=\"lib\" path=\"");
083                    sb.append(jar);
084    
085                    if ((attributes == null) || attributes.isEmpty()) {
086                            sb.append("\" />\n");
087    
088                            return;
089                    }
090    
091                    sb.append("\">\n\t\t<attributes>\n");
092    
093                    Iterator<Map.Entry<String, String>> itr =
094                            attributes.entrySet().iterator();
095    
096                    while (itr.hasNext()) {
097                            Map.Entry<String, String> entry = itr.next();
098    
099                            sb.append("\t\t\t<attribute name=\"");
100                            sb.append(entry.getKey());
101                            sb.append("\" value=\"");
102                            sb.append(entry.getValue());
103                            sb.append("\" />\n");
104                    }
105    
106                    sb.append("\t\t</attributes>\n\t</classpathentry>\n");
107            }
108    
109            protected void setupProject(String dirName, String fileName)
110                    throws Exception {
111    
112                    File propertiesFile = new File(dirName + "/" + fileName);
113    
114                    File libDir = new File(propertiesFile.getParent() + "/lib");
115    
116                    String libDirPath = StringUtil.replace(
117                            libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH);
118    
119                    if (libDirPath.contains("/themes/")) {
120                            return;
121                    }
122    
123                    File projectDir = new File(propertiesFile.getParent() + "/../..");
124    
125                    Properties properties = new Properties();
126    
127                    properties.load(new FileInputStream(propertiesFile));
128    
129                    String[] dependencyJars = StringUtil.split(
130                            properties.getProperty(
131                                    "portal-dependency-jars",
132                                    properties.getProperty("portal.dependency.jars")));
133    
134                    List<String> jars = ListUtil.toList(dependencyJars);
135    
136                    jars.add("commons-logging.jar");
137                    jars.add("log4j.jar");
138                    jars.add("util-bridges.jar");
139                    jars.add("util-java.jar");
140                    jars.add("util-taglib.jar");
141    
142                    Collections.sort(jars);
143    
144                    writeEclipseFiles(libDir, projectDir, dependencyJars);
145    
146                    List<String> ignores = ListUtil.fromFile(
147                            libDir.getCanonicalPath() + "/../.gitignore");
148    
149                    if (!libDirPath.contains("/ext/") && !ignores.contains("/lib")) {
150                            File gitignoreFile = new File(
151                                    libDir.getCanonicalPath() + "/.gitignore");
152    
153                            System.out.println("Updating " + gitignoreFile);
154    
155                            String[] gitIgnores = jars.toArray(new String[jars.size()]);
156    
157                            for (int i = 0; i < gitIgnores.length; i++) {
158                                    String gitIgnore = gitIgnores[i];
159    
160                                    if (Validator.isNotNull(gitIgnore) &&
161                                            !gitIgnore.startsWith("/")) {
162    
163                                            gitIgnores[i] = "/" + gitIgnore;
164                                    }
165                            }
166    
167                            _fileUtil.write(gitignoreFile, StringUtil.merge(gitIgnores, "\n"));
168                    }
169            }
170    
171            protected void writeClasspathFile(
172                            File libDir, String[] dependencyJars, String projectDirName,
173                            String projectName, boolean javaProject)
174                    throws Exception {
175    
176                    File classpathFile = new File(projectDirName + "/.classpath");
177    
178                    if (!javaProject) {
179                            classpathFile.delete();
180    
181                            return;
182                    }
183    
184                    List<String> globalJars = new UniqueList<String>();
185                    List<String> portalJars = new UniqueList<String>();
186    
187                    List<String> extGlobalJars = new UniqueList<String>();
188                    List<String> extPortalJars = new UniqueList<String>();
189    
190                    String libDirPath = StringUtil.replace(
191                            libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH);
192    
193                    if (libDirPath.contains("/ext/")) {
194                            FilenameFilter filenameFilter = new GlobFilenameFilter("*.jar");
195    
196                            for (String dirName : new String[] {"global", "portal"}) {
197                                    File file = new File(libDirPath + "/../ext-lib/" + dirName);
198    
199                                    List<String> jars = ListUtil.toList(file.list(filenameFilter));
200    
201                                    if (dirName.equals("global")) {
202                                            extGlobalJars.addAll(ListUtil.sort(jars));
203    
204                                            File dir = new File(PropsValues.LIFERAY_LIB_GLOBAL_DIR);
205    
206                                            String[] fileNames = dir.list(filenameFilter);
207    
208                                            globalJars.addAll(
209                                                    ListUtil.sort(ListUtil.toList(fileNames)));
210                                            globalJars.removeAll(extGlobalJars);
211                                    }
212                                    else if (dirName.equals("portal")) {
213                                            extPortalJars.addAll(ListUtil.sort(jars));
214    
215                                            File dir = new File(PropsValues.LIFERAY_LIB_PORTAL_DIR);
216    
217                                            String[] fileNames = dir.list(filenameFilter);
218    
219                                            portalJars.addAll(
220                                                    ListUtil.sort(ListUtil.toList(fileNames)));
221                                            portalJars.removeAll(extPortalJars);
222                                    }
223                            }
224                    }
225                    else {
226                            globalJars.add("portlet.jar");
227    
228                            portalJars.addAll(ListUtil.toList(dependencyJars));
229                            portalJars.add("commons-logging.jar");
230                            portalJars.add("log4j.jar");
231    
232                            Collections.sort(portalJars);
233                    }
234    
235                    String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar"));
236    
237                    List<String> customJars = null;
238    
239                    if (customJarsArray != null) {
240                            customJars = ListUtil.toList(customJarsArray);
241    
242                            for (String jar : portalJars) {
243                                    customJars.remove(jar);
244                            }
245    
246                            customJars.remove(projectName + "-service.jar");
247                            customJars.remove("util-bridges.jar");
248                            customJars.remove("util-java.jar");
249                            customJars.remove("util-taglib.jar");
250    
251                            Collections.sort(customJars);
252                    }
253                    else {
254                            customJars = new ArrayList<String>();
255                    }
256    
257                    StringBundler sb = new StringBundler();
258    
259                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
260                    sb.append("<classpath>\n");
261    
262                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
263                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
264                                    sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
265                                    sb.append("kind=\"src\" path=\"" + sourceDirName + "\" />\n");
266                            }
267                    }
268    
269                    sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n");
270                    sb.append("\t<classpathentry kind=\"con\" ");
271                    sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n");
272    
273                    if (_fileUtil.exists(projectDirName + "/test")) {
274                            sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
275                            sb.append("kind=\"src\" path=\"test\" />\n");
276    
277                            addClasspathEntry(sb, "/portal/lib/development/junit.jar");
278                            addClasspathEntry(sb, "/portal/lib/portal/commons-io.jar");
279                    }
280    
281                    addClasspathEntry(sb, "/portal/lib/development/activation.jar");
282                    addClasspathEntry(sb, "/portal/lib/development/annotations.jar");
283                    addClasspathEntry(sb, "/portal/lib/development/jsp-api.jar");
284                    addClasspathEntry(sb, "/portal/lib/development/mail.jar");
285                    addClasspathEntry(sb, "/portal/lib/development/servlet-api.jar");
286    
287                    Map<String, String> attributes = new HashMap<String, String>();
288    
289                    if (libDirPath.contains("/ext/")) {
290                            attributes.put("optional", "true");
291                    }
292    
293                    for (String jar : globalJars) {
294                            addClasspathEntry(sb, "/portal/lib/global/" + jar, attributes);
295                    }
296    
297                    for (String jar : portalJars) {
298                            addClasspathEntry(sb, "/portal/lib/portal/" + jar, attributes);
299                    }
300    
301                    addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
302                    addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
303                    addClasspathEntry(sb, "/portal/util-java/util-java.jar");
304                    addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
305    
306                    for (String jar : extGlobalJars) {
307                            addClasspathEntry(sb, "docroot/WEB-INF/ext-lib/global/" + jar);
308                    }
309    
310                    for (String jar : extPortalJars) {
311                            addClasspathEntry(sb, "docroot/WEB-INF/ext-lib/portal/" + jar);
312                    }
313    
314                    for (String jar : customJars) {
315                            if (libDirPath.contains("/tmp/WEB-INF/lib")) {
316                                    addClasspathEntry(sb, "tmp/WEB-INF/lib/" + jar);
317                            }
318                            else {
319                                    addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
320                            }
321                    }
322    
323                    sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
324                    sb.append("</classpath>");
325    
326                    System.out.println("Updating " + classpathFile);
327    
328                    String content = StringUtil.replace(
329                            sb.toString(), "\"/portal", "\"/portal-" + _BRANCH);
330    
331                    _fileUtil.write(classpathFile, content);
332            }
333    
334            protected void writeEclipseFiles(
335                            File libDir, File projectDir, String[] dependencyJars)
336                    throws Exception {
337    
338                    String projectDirName = projectDir.getCanonicalPath();
339    
340                    String projectName = StringUtil.extractLast(
341                            projectDirName, File.separatorChar);
342    
343                    boolean javaProject = false;
344    
345                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
346                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
347                                    javaProject = true;
348    
349                                    break;
350                            }
351                    }
352    
353                    if (!javaProject) {
354                            System.out.println(
355                                    "Eclipse Java project will not be used because a source " +
356                                            "folder does not exist");
357                    }
358    
359                    writeProjectFile(projectDirName, projectName, javaProject);
360    
361                    writeClasspathFile(
362                            libDir, dependencyJars, projectDirName, projectName, javaProject);
363    
364                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
365                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
366                                    List<String> gitIgnores = new ArrayList<String>();
367    
368                                    if (sourceDirName.endsWith("ext-impl/src")) {
369                                            gitIgnores.add("/classes");
370                                            gitIgnores.add("/ext-impl.jar");
371                                    }
372                                    else if (sourceDirName.endsWith("ext-service/src")) {
373                                            gitIgnores.add("/classes");
374                                            gitIgnores.add("/ext-service.jar");
375                                    }
376                                    else if (sourceDirName.endsWith("ext-util-bridges/src")) {
377                                            gitIgnores.add("/classes");
378                                            gitIgnores.add("/ext-util-bridges.jar");
379                                    }
380                                    else if (sourceDirName.endsWith("ext-util-java/src")) {
381                                            gitIgnores.add("/classes");
382                                            gitIgnores.add("/ext-util-java.jar");
383                                    }
384                                    else if (sourceDirName.endsWith("ext-util-taglib/src")) {
385                                            gitIgnores.add("/classes");
386                                            gitIgnores.add("/ext-util-taglib.jar");
387                                    }
388                                    else {
389                                            continue;
390                                    }
391    
392                                    String dirName = projectDirName + "/" + sourceDirName + "/../";
393    
394                                    if (gitIgnores.isEmpty()) {
395                                            _fileUtil.delete(dirName + ".gitignore");
396                                    }
397                                    else {
398                                            String gitIgnoresString = StringUtil.merge(
399                                                    gitIgnores, "\n");
400    
401                                            _fileUtil.write(dirName + ".gitignore", gitIgnoresString);
402                                    }
403                            }
404                    }
405    
406                    if (_fileUtil.exists(projectDirName + "/test")) {
407                            _fileUtil.write(
408                                    projectDirName + "/.gitignore", "/test-classes\n/test-results");
409                    }
410                    else {
411                            _fileUtil.delete(projectDirName + "/.gitignore");
412                    }
413            }
414    
415            protected void writeProjectFile(
416                            String projectDirName, String projectName, boolean javaProject)
417                    throws Exception {
418    
419                    StringBundler sb = new StringBundler(17);
420    
421                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
422                    sb.append("<projectDescription>\n");
423                    sb.append("\t<name>");
424                    sb.append(projectName);
425                    sb.append("-");
426                    sb.append(_BRANCH);
427                    sb.append("</name>\n");
428                    sb.append("\t<comment></comment>\n");
429                    sb.append("\t<projects></projects>\n");
430                    sb.append("\t<buildSpec>\n");
431    
432                    if (javaProject) {
433                            sb.append("\t\t<buildCommand>\n");
434                            sb.append("\t\t\t<name>org.eclipse.jdt.core.javabuilder</name>\n");
435                            sb.append("\t\t\t<arguments></arguments>\n");
436                            sb.append("\t\t</buildCommand>\n");
437                    }
438    
439                    sb.append("\t</buildSpec>\n");
440                    sb.append("\t<natures>\n");
441    
442                    if (javaProject) {
443                            sb.append("\t\t<nature>org.eclipse.jdt.core.javanature</nature>\n");
444                    }
445    
446                    sb.append("\t</natures>\n");
447                    sb.append("</projectDescription>");
448    
449                    File projectFile = new File(projectDirName + "/.project");
450    
451                    System.out.println("Updating " + projectFile);
452    
453                    _fileUtil.write(projectFile, sb.toString());
454            }
455    
456            private static final String _BRANCH = "trunk";
457    
458            private static final String[] _SOURCE_DIR_NAMES = new String[] {
459                    "docroot/WEB-INF/ext-impl/src", "docroot/WEB-INF/ext-service/src",
460                    "docroot/WEB-INF/ext-util-bridges/src",
461                    "docroot/WEB-INF/ext-util-java/src",
462                    "docroot/WEB-INF/ext-util-taglib/src", "docroot/WEB-INF/service",
463                    "docroot/WEB-INF/src"
464            };
465    
466            private static FileImpl _fileUtil = FileImpl.getInstance();
467    
468    }