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            protected void doExecute() throws Exception {
070                    Project project = getProject();
071    
072                    project.setProperty("build.revision", getBuildRevision());
073                    project.setProperty("build.time", getDateString(new Date()));
074                    project.setProperty(
075                            "release.info.build.date",
076                            String.valueOf(ReleaseInfo.getBuildDate()));
077                    project.setProperty(
078                            "release.info.build.number",
079                            String.valueOf(ReleaseInfo.getBuildNumber()));
080                    project.setProperty(
081                            "release.info.code.name", ReleaseInfo.getCodeName());
082                    project.setProperty(
083                            "release.info.parent.build.number",
084                            String.valueOf(ReleaseInfo.getParentBuildNumber()));
085                    project.setProperty(
086                            "release.info.release.info", ReleaseInfo.getReleaseInfo());
087                    project.setProperty(
088                            "release.info.server.info", ReleaseInfo.getServerInfo());
089                    project.setProperty("release.info.vendor", ReleaseInfo.getVendor());
090                    project.setProperty("release.info.version", ReleaseInfo.getVersion());
091    
092                    if (!_analyze) {
093                            return;
094                    }
095    
096                    Analyzer analyzer = new Analyzer();
097    
098                    analyzer.setBase(project.getBaseDir());
099    
100                    File classesDir = new File(project.getBaseDir(), "classes");
101    
102                    analyzer.setJar(classesDir);
103    
104                    File file = new File(project.getBaseDir(), "bnd.bnd");
105    
106                    if (file.exists()) {
107                            analyzer.setProperties(file);
108                    }
109                    else {
110                            analyzer.setProperty(Constants.EXPORT_PACKAGE, "*");
111                            analyzer.setProperty(
112                                    Constants.IMPORT_PACKAGE, "*;resolution:=optional");
113                    }
114    
115                    Manifest manifest = analyzer.calcManifest();
116    
117                    Attributes attributes = manifest.getMainAttributes();
118    
119                    project.setProperty(
120                            "export.packages", attributes.getValue(Constants.EXPORT_PACKAGE));
121                    project.setProperty(
122                            "import.packages", attributes.getValue(Constants.IMPORT_PACKAGE));
123            }
124    
125            protected String execute(String command) throws Exception {
126                    Runtime runtime = Runtime.getRuntime();
127    
128                    Process process = runtime.exec(command);
129    
130                    return StringUtil.read(process.getInputStream());
131            }
132    
133            protected String getBuildRevision() throws Exception {
134                    Project project = getProject();
135    
136                    File projectDir = new File(
137                            project.getBaseDir(), project.getProperty("project.dir"));
138    
139                    File gitDir = new File(projectDir, ".git");
140    
141                    if (gitDir.exists()) {
142                            if (OSDetector.isWindows()) {
143                                    return execute("cmd /c git rev-parse HEAD");
144                            }
145                            else {
146                                    return execute("git rev-parse HEAD");
147                            }
148                    }
149    
150                    File svnDir = new File(projectDir, ".svn");
151    
152                    if (svnDir.exists()) {
153                            if (OSDetector.isWindows()) {
154                                    return execute("cmd /c svnversion .");
155                            }
156                            else {
157                                    return execute("svnversion .");
158                            }
159                    }
160    
161                    return StringPool.BLANK;
162            }
163    
164            protected String getDateString(Date date) {
165                    DateFormat dateFormat = new SimpleDateFormat(_PATTERN);
166    
167                    return dateFormat.format(date);
168            }
169    
170            private static final String _PATTERN = "EEE MMM d HH:mm:ss z yyyy";
171    
172            private boolean _analyze;
173            private Path _path;
174    
175    }