001    /**
002     * Copyright (c) 2000-2013 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.util.ant;
016    
017    import aQute.lib.osgi.Analyzer;
018    import aQute.lib.osgi.Constants;
019    
020    import com.liferay.portal.kernel.util.OSDetector;
021    import com.liferay.portal.kernel.util.ReleaseInfo;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import java.io.File;
026    
027    import java.text.DateFormat;
028    import java.text.SimpleDateFormat;
029    
030    import java.util.Date;
031    import java.util.jar.Attributes;
032    import java.util.jar.Manifest;
033    
034    import org.apache.tools.ant.BuildException;
035    import org.apache.tools.ant.Project;
036    import org.apache.tools.ant.Task;
037    import org.apache.tools.ant.types.Path;
038    import org.apache.tools.ant.types.Reference;
039    
040    /**
041     * @author Raymond Aug??
042     */
043    public class ManifestHelperTask extends Task {
044    
045            @Override
046            public void execute() throws BuildException {
047                    try {
048                            doExecute();
049                    }
050                    catch (Exception e) {
051                            throw new BuildException(e);
052                    }
053            }
054    
055            public void setAnalyze(boolean analyze) {
056                    _analyze = analyze;
057            }
058    
059            public void setClasspathRef(Reference reference) {
060                    if (_path == null) {
061                            _path = new Path(getProject());
062                    }
063    
064                    Path path = _path.createPath();
065    
066                    path.setRefid(reference);
067            }
068    
069            public void setProjectDirPropertyName(String projectDirPropertyName) {
070                    _projectDirPropertyName = projectDirPropertyName;
071            }
072    
073            protected void doExecute() throws Exception {
074                    if (_projectDirPropertyName == null) {
075                            throw new BuildException(
076                                    "Attribute projectDirPropertyName must be set");
077                    }
078    
079                    Project project = getProject();
080    
081                    project.setProperty("build.revision", getBuildRevision());
082                    project.setProperty("build.time", getDateString(new Date()));
083                    project.setProperty(
084                            "release.info.build.date",
085                            String.valueOf(ReleaseInfo.getBuildDate()));
086                    project.setProperty(
087                            "release.info.build.number",
088                            String.valueOf(ReleaseInfo.getBuildNumber()));
089                    project.setProperty(
090                            "release.info.code.name", ReleaseInfo.getCodeName());
091                    project.setProperty(
092                            "release.info.release.info", ReleaseInfo.getReleaseInfo());
093                    project.setProperty(
094                            "release.info.server.info", ReleaseInfo.getServerInfo());
095                    project.setProperty("release.info.vendor", ReleaseInfo.getVendor());
096                    project.setProperty("release.info.version", ReleaseInfo.getVersion());
097    
098                    if (!_analyze) {
099                            return;
100                    }
101    
102                    Analyzer analyzer = new Analyzer();
103    
104                    analyzer.setBase(project.getBaseDir());
105    
106                    File classesDir = new File(project.getBaseDir(), "classes");
107    
108                    analyzer.setJar(classesDir);
109    
110                    File file = new File(project.getBaseDir(), "bnd.bnd");
111    
112                    if (file.exists()) {
113                            analyzer.setProperties(file);
114                    }
115                    else {
116                            analyzer.setProperty(Constants.EXPORT_PACKAGE, "*");
117                            analyzer.setProperty(
118                                    Constants.IMPORT_PACKAGE, "*;resolution:=optional");
119                    }
120    
121                    Manifest manifest = analyzer.calcManifest();
122    
123                    Attributes attributes = manifest.getMainAttributes();
124    
125                    project.setProperty(
126                            "export.packages", attributes.getValue(Constants.EXPORT_PACKAGE));
127                    project.setProperty(
128                            "import.packages", attributes.getValue(Constants.IMPORT_PACKAGE));
129    
130                    analyzer.close();
131            }
132    
133            protected String execute(String command) throws Exception {
134                    Runtime runtime = Runtime.getRuntime();
135    
136                    Process process = runtime.exec(command);
137    
138                    return StringUtil.read(process.getInputStream());
139            }
140    
141            protected String getBuildRevision() throws Exception {
142                    Project project = getProject();
143    
144                    File projectDir = new File(
145                            project.getBaseDir(), project.getProperty(_projectDirPropertyName));
146    
147                    File gitDir = new File(projectDir, ".git");
148    
149                    if (gitDir.exists()) {
150                            if (OSDetector.isWindows()) {
151                                    return execute("cmd /c git rev-parse HEAD");
152                            }
153                            else {
154                                    return execute("git rev-parse HEAD");
155                            }
156                    }
157    
158                    File svnDir = new File(projectDir, ".svn");
159    
160                    if (svnDir.exists()) {
161                            if (OSDetector.isWindows()) {
162                                    return execute("cmd /c svnversion .");
163                            }
164                            else {
165                                    return execute("svnversion .");
166                            }
167                    }
168    
169                    return StringPool.BLANK;
170            }
171    
172            protected String getDateString(Date date) {
173                    DateFormat dateFormat = new SimpleDateFormat(_PATTERN);
174    
175                    return dateFormat.format(date);
176            }
177    
178            private static final String _PATTERN = "EEE MMM d HH:mm:ss z yyyy";
179    
180            private boolean _analyze;
181            private Path _path;
182            private String _projectDirPropertyName;
183    
184    }