001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.FileUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.PropertiesUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.util.InitUtil;
024    
025    import java.io.File;
026    
027    import java.util.Arrays;
028    import java.util.Properties;
029    import java.util.Set;
030    import java.util.TreeSet;
031    
032    import org.apache.tools.ant.DirectoryScanner;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class PluginsSummaryBuilder {
038    
039            public static void main(String[] args) {
040                    InitUtil.initWithSpring();
041    
042                    File pluginsDir = new File(System.getProperty("plugins.dir"));
043    
044                    new PluginsSummaryBuilder(pluginsDir);
045            }
046    
047            public PluginsSummaryBuilder(File pluginsDir) {
048                    try {
049                            _pluginsDir = pluginsDir;
050    
051                            _createPluginsSummary();
052                    }
053                    catch (Exception e) {
054                            e.printStackTrace();
055                    }
056            }
057    
058            private void _createPluginsSummary() throws Exception {
059                    StringBundler sb = new StringBundler();
060    
061                    sb.append("<plugins-summary>\n");
062    
063                    DirectoryScanner directoryScanner = new DirectoryScanner();
064    
065                    directoryScanner.setBasedir(_pluginsDir);
066                    directoryScanner.setExcludes(
067                            new String[] {"**\\tmp\\**", "**\\tools\\**"});
068                    directoryScanner.setIncludes(
069                            new String[] {
070                                    "**\\liferay-plugin-package.properties"
071                            });
072    
073                    directoryScanner.scan();
074    
075                    String[] fileNames = directoryScanner.getIncludedFiles();
076    
077                    Arrays.sort(fileNames);
078    
079                    for (String fileName : fileNames) {
080                            fileName = StringUtil.replace(
081                                    fileName, StringPool.BACK_SLASH, StringPool.SLASH);
082    
083                            _createPluginSummary(fileName, sb);
084                    }
085    
086                    for (String author : _distinctAuthors) {
087                            sb.append("\t<author>");
088                            sb.append(author);
089                            sb.append("</author>\n");
090                    }
091    
092                    for (String license : _distinctLicenses) {
093                            sb.append("\t<license>");
094                            sb.append(license);
095                            sb.append("</license>\n");
096                    }
097    
098                    sb.append("</plugins-summary>");
099    
100                    FileUtil.write(_pluginsDir + "/summary.xml", sb.toString());
101            }
102    
103            private void _createPluginSummary(String fileName, StringBundler sb)
104                    throws Exception {
105    
106                    String content = FileUtil.read(fileName);
107    
108                    int x = fileName.indexOf(StringPool.SLASH);
109    
110                    String type = fileName.substring(0, x);
111    
112                    if (type.endsWith("s")) {
113                            type = type.substring(0, type.length() - 1);
114                    }
115    
116                    x = fileName.indexOf(StringPool.SLASH, x) + 1;
117    
118                    int y = fileName.indexOf(StringPool.SLASH, x);
119    
120                    String artifactId = fileName.substring(x, y);
121    
122                    Properties properties = PropertiesUtil.load(content);
123    
124                    String name = _readProperty(properties, "name");
125                    String tags = _readProperty(properties, "tags");
126                    String shortDescription = _readProperty(
127                            properties, "short-description");
128                    String longDescription = _readProperty(properties, "long-description");
129                    String changeLog = _readProperty(properties, "change-log");
130                    String pageURL = _readProperty(properties, "page-url");
131                    String author = _readProperty(properties, "author");
132                    String licenses = _readProperty(properties, "licenses");
133                    String liferayVersions = _readProperty(properties, "liferay-versions");
134    
135                    _distinctAuthors.add(author);
136                    _distinctLicenses.add(licenses);
137    
138                    sb.append("\t<plugin>\n");
139    
140                    _writeElement(sb, "artifact-id", artifactId, 2);
141                    _writeElement(sb, "name", name, 2);
142                    _writeElement(sb, "type", type, 2);
143                    _writeElement(sb, "tags", tags, 2);
144                    _writeElement(sb, "short-description", shortDescription, 2);
145                    _writeElement(sb, "long-description", longDescription, 2);
146                    _writeElement(sb, "change-log", changeLog, 2);
147                    _writeElement(sb, "page-url", pageURL, 2);
148                    _writeElement(sb, "author", author, 2);
149                    _writeElement(sb, "licenses", licenses, 2);
150                    _writeElement(sb, "liferay-versions", liferayVersions, 2);
151    
152                    sb.append("\t\t<releng>\n");
153                    sb.append(_readReleng(fileName));
154                    sb.append("\t\t</releng>\n");
155                    sb.append("\t</plugin>\n");
156            }
157    
158            private String _readProperty(Properties properties, String key) {
159                    return GetterUtil.getString(properties.getProperty(key));
160            }
161    
162            private String _readReleng(String fileName) throws Exception {
163                    int x = fileName.indexOf("WEB-INF");
164    
165                    String relativeWebInfDirName = fileName.substring(0, x + 8);
166    
167                    String fullWebInfDirName =
168                            _pluginsDir + StringPool.SLASH + relativeWebInfDirName;
169    
170                    String relengPropertiesFileName =
171                            fullWebInfDirName + "liferay-releng.properties";
172    
173                    Properties relengProperties = null;
174    
175                    if (FileUtil.exists(relengPropertiesFileName)) {
176                            String relengPropertiesContent = FileUtil.read(
177                                    relengPropertiesFileName);
178    
179                            relengProperties = PropertiesUtil.load(relengPropertiesContent);
180                    }
181                    else {
182                            relengProperties = new Properties();
183                    }
184    
185                    String relengPropertiesContent = _updateRelengPropertiesFile(
186                            relengPropertiesFileName, relengProperties);
187    
188                    relengProperties = PropertiesUtil.load(relengPropertiesContent);
189    
190                    StringBundler sb = new StringBundler();
191    
192                    _writeElement(sb, "bundle", relengProperties, 3);
193                    _writeElement(sb, "category", relengProperties, 3);
194                    _writeElement(sb, "demo-url", relengProperties, 3);
195                    _writeElement(sb, "dependent-apps", relengProperties, 3);
196    
197                    if (FileUtil.exists(fullWebInfDirName + "releng/icons/90x90.png")) {
198                            _writeElement(
199                                    sb, "icon", relativeWebInfDirName + "releng/icons/90x90.png",
200                                    3);
201                    }
202    
203                    _writeElement(sb, "labs", relengProperties, 3);
204                    _writeElement(sb, "marketplace", relengProperties, 3);
205                    _writeElement(sb, "public", relengProperties, 3);
206    
207                    String fullScreenshotsDirName =
208                            fullWebInfDirName + "releng/screenshots/";
209                    String relativeScreenshotsDirName =
210                            relativeWebInfDirName + "releng/screenshots/";
211    
212                    if (FileUtil.exists(fullScreenshotsDirName)) {
213                            String[] screenshotsFileNames = FileUtil.listFiles(
214                                    fullScreenshotsDirName);
215    
216                            Arrays.sort(screenshotsFileNames);
217    
218                            for (String screenshotsFileName : screenshotsFileNames) {
219                                    if (screenshotsFileName.equals("Thumbs.db") ||
220                                            screenshotsFileName.endsWith(".png")) {
221    
222                                            FileUtil.delete(
223                                                    fullScreenshotsDirName + screenshotsFileName);
224                                    }
225    
226                                    if (!screenshotsFileName.endsWith(".jpg")) {
227                                            continue;
228                                    }
229    
230                                    _writeElement(
231                                            sb, "screenshot",
232                                            relativeScreenshotsDirName + screenshotsFileName, 3);
233                            }
234                    }
235    
236                    _writeElement(sb, "supported", relengProperties, 3);
237    
238                    return sb.toString();
239            }
240    
241            private String _updateRelengPropertiesFile(
242                            String relengPropertiesFileName, Properties relengProperties)
243                    throws Exception {
244    
245                    StringBundler sb = new StringBundler();
246    
247                    _writeProperty(sb, relengProperties, "bundle", "false");
248                    _writeProperty(sb, relengProperties, "category", "");
249                    _writeProperty(sb, relengProperties, "demo-url", "");
250                    _writeProperty(sb, relengProperties, "dependent-apps", "");
251                    _writeProperty(sb, relengProperties, "labs", "true");
252                    _writeProperty(sb, relengProperties, "marketplace", "false");
253                    _writeProperty(sb, relengProperties, "public", "true");
254                    _writeProperty(sb, relengProperties, "supported", "false");
255    
256                    String relengPropertiesContent = sb.toString();
257    
258                    FileUtil.write(relengPropertiesFileName, relengPropertiesContent);
259    
260                    return relengPropertiesContent;
261            }
262    
263            private void _writeElement(
264                    StringBundler sb, String name, Properties properties, int tabsCount) {
265    
266                    _writeElement(sb, name, _readProperty(properties, name), tabsCount);
267            }
268    
269            private void _writeElement(
270                    StringBundler sb, String name, String value, int tabsCount) {
271    
272                    for (int i = 0; i < tabsCount; i++) {
273                            sb.append("\t");
274                    }
275    
276                    sb.append("<");
277                    sb.append(name);
278                    sb.append(">");
279                    sb.append(value);
280                    sb.append("</");
281                    sb.append(name);
282                    sb.append(">\n");
283            }
284    
285            private void _writeProperty(
286                    StringBundler sb, Properties properties, String key,
287                    String defaultValue) {
288    
289                    String value = GetterUtil.getString(
290                            properties.getProperty(key), defaultValue);
291    
292                    if (sb.length() > 0) {
293                            sb.append(StringPool.NEW_LINE);
294                    }
295    
296                    sb.append(key);
297                    sb.append(StringPool.EQUAL);
298                    sb.append(value);
299            }
300    
301            private Set<String> _distinctAuthors = new TreeSet<String>();
302            private Set<String> _distinctLicenses = new TreeSet<String>();
303            private File _pluginsDir;
304    
305    }