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