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