ProcessFilesForPackages.java revision 6543
1690N/A/*
1690N/A * CDDL HEADER START
1690N/A *
1690N/A * The contents of this file are subject to the terms of the
1690N/A * Common Development and Distribution License, Version 1.0 only
1690N/A * (the "License"). You may not use this file except in compliance
1690N/A * with the License.
1690N/A *
1690N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
1690N/A * or http://forgerock.org/license/CDDLv1.0.html.
1690N/A * See the License for the specific language governing permissions
1690N/A * and limitations under the License.
1690N/A *
1690N/A * When distributing Covered Code, include this CDDL HEADER in each
1690N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
1690N/A * If applicable, add the following below this CDDL HEADER, with the
1690N/A * fields enclosed by brackets "[]" replaced with your own identifying
1690N/A * information:
1690N/A * Portions Copyright [yyyy] [name of copyright owner]
1690N/A *
1690N/A * CDDL HEADER END
1690N/A *
1690N/A *
1690N/A * Copyright 2013 ForgeRock AS
4122N/A */
1690N/Apackage org.opends.build.tools;
1690N/A
1690N/Aimport java.io.File;
1690N/Aimport java.io.FileFilter;
2601N/Aimport java.net.URI;
2601N/Aimport java.util.Collections;
2601N/Aimport java.util.LinkedList;
2601N/Aimport java.util.List;
2601N/A
2601N/Aimport org.apache.tools.ant.BuildException;
2601N/Aimport org.apache.tools.ant.Task;
2601N/A
2601N/A/**
2601N/A * A utility class for the packaging process. It is used by the build.xml to
3352N/A * perform the package construction.
4134N/A */
2601N/Apublic class ProcessFilesForPackages extends Task
1690N/A{
1690N/A /** The source directory name */
2601N/A private String sourceDirName;
3902N/A
2601N/A /** Files contained in the package */
2601N/A private final List<File> files = new LinkedList<File>();
2601N/A
3352N/A /** Files which should be excluded from the package */
2601N/A private final List<File> excludedFiles = new LinkedList<File>();
2601N/A
2601N/A /** Package documentation files */
2601N/A private final List<File> docFiles = new LinkedList<File>();
1690N/A
2601N/A /** The end-of-line character for this platform */
2601N/A private static String EOL = System.getProperty("line.separator");
2601N/A
2601N/A /**
2601N/A * Returns the source directory name.
2601N/A *
1690N/A * @return The source directory name.
1690N/A */
1690N/A public String getSourceDirName()
2439N/A {
1690N/A return sourceDirName;
2601N/A }
2601N/A
2601N/A /**
2601N/A * Sets the source directory name.
2601N/A *
2601N/A * @param sourceDirName
2601N/A * The source directory name.
2601N/A */
1690N/A public void setSourceDirName(String sourceDirName)
1690N/A {
1690N/A this.sourceDirName = sourceDirName;
1690N/A }
2601N/A
2601N/A @Override()
2601N/A public void execute()
2601N/A {
2601N/A try
1690N/A {
2601N/A // Process the filtering of the files contained in the given directory.
2601N/A filterFiles(new File(getSourceDirName()));
2601N/A files.removeAll(docFiles);
2601N/A // Sorts the list.
2601N/A Collections.sort(files);
2601N/A Collections.sort(docFiles);
2601N/A // Returns the properties which can be used fill data.
2601N/A getProject().setNewProperty("listRPMFiles", formatAsFileList(files));
2601N/A getProject().setNewProperty("listRPMDocFiles",
2601N/A formatAsDocList(docFiles));
2601N/A getProject().setNewProperty("excludedRPMFiles",
2601N/A formatAsExcludedList(excludedFiles));
2601N/A getProject().setNewProperty("installRpmFiles", getInstallationFiles());
2601N/A }
2601N/A catch (Exception e)
2601N/A {
2601N/A throw new BuildException(
2601N/A "An error occurs during filtering package files", e);
2601N/A }
1690N/A }
1690N/A
1690N/A /**
2601N/A * Returns the installation files for the RPM package.
2601N/A *
2601N/A * @return A string containing the installation files for the RPM package.
2601N/A */
2601N/A private String getInstallationFiles()
1690N/A {
1690N/A return new StringBuilder("cp -rf \"").append(sourceDirName).append("\"/* .")
1690N/A .append(EOL).toString();
1690N/A }
1690N/A /**
1690N/A * Formats the file list to be supported by RPM.
1690N/A *
1690N/A * @param fileList
1690N/A * The list of files to be processed.
1690N/A * @return A string containing the list of files which should be excluded in
1690N/A * the RPM source.
1690N/A */
1690N/A private String formatAsExcludedList(final List<File> fileList)
2601N/A {
1690N/A final StringBuilder sb = new StringBuilder();
1690N/A for (final File f : fileList)
1690N/A {
1690N/A sb.append(relativeToSourceDirName(f)).append(EOL);
1690N/A }
1690N/A return sb.toString();
1690N/A }
1690N/A
1690N/A /**
1690N/A * Formats the file list to be supported by RPM.
1690N/A *
2601N/A * @param fileList
2601N/A * The list of files to be processed.
1690N/A * @return A string containing the list of files which can be included in the
2601N/A * RPM source.
1690N/A */
3902N/A private String formatAsFileList(final List<File> fileList)
3902N/A {
3902N/A final StringBuilder sb = new StringBuilder();
3902N/A sb.append("%dir %{_prefix}").append(EOL);
1690N/A for (final File f : fileList)
3902N/A {
3902N/A if (f.isDirectory())
3902N/A {
3352N/A sb.append("%dir %{_prefix}");
3902N/A }
2601N/A else
2601N/A {
2601N/A sb.append("%{_prefix}");
2601N/A }
2601N/A sb.append(relativeToSourceDirName(f)).append(EOL);
2601N/A }
2601N/A return sb.toString();
2601N/A }
3352N/A
2601N/A /**
2601N/A * Formats the document list to be supported by RPM.
2601N/A *
2601N/A * @param fileList
2601N/A * The list of files to be processed.
2601N/A * @return A string containing the formatted list doc.
2601N/A */
2601N/A private String formatAsDocList(final List<File> fileList)
2601N/A {
2601N/A final StringBuilder sb = new StringBuilder();
2601N/A for (final File f : fileList)
2601N/A {
2601N/A // FIXME The folder needs to be copied as well.
2601N/A if (!f.isDirectory())
2601N/A {
2601N/A sb.append(
2601N/A relativeToSourceDirName(f)).append(EOL);
2601N/A }
2601N/A }
2601N/A return sb.toString();
2601N/A }
2601N/A
2601N/A /**
2601N/A * Sorts all the files contained in selected directory and fills the
2601N/A * appropriate lists.
2601N/A *
2601N/A * @param dir
2601N/A * The directory where to analyze the files.
2601N/A * @throws Exception
2601N/A * If an exception occurs during the process.
2601N/A */
2601N/A private void filterFiles(final File dir) throws Exception
2601N/A {
2601N/A final ExcludeFileFilter exFilter = new ExcludeFileFilter();
2601N/A final DocFileFilter docFilter = new DocFileFilter();
2601N/A
2601N/A // The spaces in path can generate errors. (see OPENDJ-1063)
2601N/A final File fdir =
2601N/A new File(new URI("file:///"
2601N/A + dir.getAbsolutePath().replaceAll(" ", "%20")));
2601N/A
2601N/A for (final File f : fdir.listFiles())
2601N/A {
2601N/A if (f.isDirectory())
2601N/A {
2601N/A filterFiles(f);
2601N/A }
2601N/A // Completes the excluded files list.
2601N/A if (exFilter.accept(f))
2601N/A {
2601N/A excludedFiles.add(f);
2601N/A }
2601N/A else
2601N/A {
2601N/A files.add(f);
2601N/A }
2601N/A // Completes the document file list.
2601N/A if (docFilter.accept(f))
2601N/A {
2601N/A docFiles.add(f);
2601N/A }
2601N/A }
2601N/A }
2601N/A
2601N/A /**
2601N/A * Returns the relative path to given source directory name.
2601N/A *
2601N/A * @param f
2601N/A * The file we want the relative path.
2601N/A * @return A string representing the relative path to the given source
2601N/A * directory name.
2601N/A */
2601N/A private String relativeToSourceDirName(File f)
2601N/A {
2601N/A return f.getAbsolutePath().replace(getSourceDirName(), "");
2601N/A }
2601N/A
2601N/A /**
2601N/A * A file filter which excludes all files that belong to other OS.
3853N/A */
2601N/A static final class ExcludeFileFilter implements FileFilter
2601N/A {
2601N/A /** {@inheritDoc} */
2601N/A public boolean accept(File file)
2601N/A {
2601N/A final String fileName = file.getName().toLowerCase();
2601N/A return (
2601N/A fileName.equals("bat")
2601N/A || fileName.endsWith(".app")
2601N/A || fileName.endsWith(".bat")
2601N/A || fileName.endsWith(".exe")
2601N/A || file.getAbsolutePath().contains(".app"));
2601N/A }
2601N/A }
2601N/A
2601N/A /**
2601N/A * A file filter which accepts only documentation files.
2601N/A */
2601N/A static final class DocFileFilter implements FileFilter
2601N/A {
2601N/A /** {@inheritDoc} */
2601N/A public boolean accept(File file)
2601N/A {
2601N/A final String fileName = file.getName().toLowerCase();
2601N/A if (file.isDirectory())
2601N/A {
2601N/A return fileName.equals("legal-notices");
2601N/A }
2601N/A return (
2601N/A !file.getAbsolutePath().contains("/template/")
2601N/A && !file.getAbsolutePath().contains("/legals/")
2601N/A && (fileName.endsWith(".doc")
2601N/A || fileName.endsWith(".txt")
2601N/A || fileName.contains("example-plugin")
2601N/A || fileName.equals("readme")
2601N/A || fileName.equals("opends.license")));
2601N/A }
2601N/A }
2601N/A}
2601N/A