Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

package com.erggroup.mass.ant;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.taskdefs.Echo;
import org.apache.tools.ant.types.FileSet;

public class SandboxElement implements JatsElement
{
        private Jats parent;
        private Project project;
        private String name;
        private String version;
        private String includes; //= "**/*";
        private String excludes;
        private Boolean verbose;
        private boolean runtime = false;
        private static Collection sandboxed = new ArrayList();

        public String getElementType()
        {
                return "sandbox";
        }

        public void execute(Jats parent)
        {
                this.parent = parent;
                this.project = parent.getProject();

                if (name == null || version == null)
                {
                        throw new BuildException("Both \'name\' and \'version\' must be specified for <sandbox/>");
                }

                if ( isVerbose() )
                {
                        this.doEcho("Updating package \'" + name + "\' version \'" + version + "\'." );
                }

                Copy copyTask = new Copy();
                copyTask.setProject(project);
                copyTask.setTaskName("sandbox");
                copyTask.setTodir(parent.getTodir());
                copyTask.setIncludeEmptyDirs(false);

                FileSet set = getFileSet();
                set.setProject(project);

                if (set != null)
                {
                        copyTask.addFileset(set);
                }

                copyTask.perform();

                sandboxed.add(this);

                PackageRef packageRef = new PackageRef();
                packageRef.setProject(project);
                copyTask.setTaskName("sandbox");
                packageRef.setPackage(name);
                packageRef.setVersion(version);
                packageRef.setVerbose( "false" );
                packageRef.execute();
        }

        private FileSet getFileSet()
        {
                if (name == null || version == null)
                {
                        throw new BuildException("Either 'name' or 'version' was not specifies for <jats> <sandbox>");
                }

                FileSet fileSet = null;

                File projectNameDir = new File(parent.getFromdir(), name);
                File projectVersionDir = new File(projectNameDir, version);
                if (projectVersionDir.exists() == false || projectVersionDir.isDirectory() == false)
                {
                        throw new BuildException("Directory " + projectVersionDir.toString() +
                                                         " for package \'" + name
                                                         + "\' version \'" + version
                                                         + "\' does not exist");
                }
                else
                {
                        validatePackageDescriptionFile(projectVersionDir);

                        // Setup the file set
                        fileSet = new FileSet();
                        fileSet.setProject(project);
                        fileSet.setDir(projectVersionDir);
                        if ( includes == null )
                        {
                                includes = parent.getIncludes();
                        }
                        fileSet.setIncludes(includes);
                        fileSet.setExcludes("descpkg");
                        if (excludes != null)
                        {
                                fileSet.setExcludes(excludes);
                        }
                }

                return fileSet;
        }

        private void doEcho(String message)
        {
                Echo echo = new Echo();
                echo.setTaskName(this.getElementType());
                echo.setProject(this.project);
                echo.setMessage(message);
                echo.execute();
        }

        private void validatePackageDescriptionFile(File location)
        {
                // Look for the descpkg file and determine whether it contains the correct info.
                //
                File descpkg = new File(location, "descpkg");
                if (descpkg.exists() == false || descpkg.isFile() == false)
                {
                        throw new BuildException("The \'descpkg\' file for package \'" + name
                                                                   + "\' version \'" + version
                                                                   + "\' does not exist");
                }
                else
                {
                        // Read in the file, and determine whether in contain
                        // the correct 'version' string.
                        //
                        try
                        {
                                FileReader reader = new FileReader(descpkg);

                                BufferedReader buffer = new BufferedReader(reader);
                                try
                                {
                                        String line = buffer.readLine();
                                        String strippedLine = line.trim();
                                        /** @todo currently there is an issue with some descpkg file
                                          * not having the correct format (i.e. 1.2.2 cr instead of 1.2.2.cr)
                                          */
                                        if (line == null /*|| strippedLine.endsWith(version) == false*/)
                                        {
                                                throw new BuildException("The \'descpkg\' file for package \'" + name
                                                                                   + "\' version \'" + version
                                                                                   + "\' does contain the correct version info.");
                                        }
                                }
                                catch (IOException ex)
                                {
                                        throw new BuildException("The \'descpkg\' file for package \'" + name
                                                                                   + "\' version \'" + version
                                                                                   + "\' could not be read.");
                                }
                        }
                        catch (FileNotFoundException ex)
                        {
                                throw new BuildException("The \'descpkg\' file for package \'" + name
                                                                   + "\' version \'" + version
                                                                   + "\' does not exist");
                        }
                }
        }

        public void setName(String name)
        {
                this.name = name;
        }

        public void setVersion(String version)
        {
                this.version = version;
        }

        public void setIncludes(String includes)
        {
                this.includes = includes;
        }

        public void setExcludes(String excludes)
        {
                this.excludes = excludes;
        }

        public void setRuntime( String newValue )
        {
                this.runtime = ( "true".equalsIgnoreCase(newValue) );
        }

        static Collection getSandboxElements()
        {
                return sandboxed;
        }

        public String getName()
        {
                return name;
        }

        public String getVersion()
        {
                return version;
        }

        public boolean isRuntime()
        {
                return runtime;
        }

        public void setVerbose( String newValue )
        {
                this.verbose = new Boolean(!( "false".equalsIgnoreCase(newValue) ));
        }

        public boolean isVerbose()
        {
                if ( verbose == null )
                {
                        return parent.isVerbose();
                }
                return verbose.booleanValue();
        }
}