001    /*
002     * Jpkg - Java library and tools for operating system package creation.
003     *
004     * Copyright (c) 2007-2008 Three Rings Design, Inc.
005     * All rights reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     * 1. Redistributions of source code must retain the above copyright
011     *    notice, this list of conditions and the following disclaimer.
012     * 2. Redistributions in binary form must reproduce the above copyright
013     *    notice, this list of conditions and the following disclaimer in the
014     *    documentation and/or other materials provided with the distribution.
015     * 3. Neither the name of the copyright owner nor the names of contributors
016     *    may be used to endorse or promote products derived from this software
017     *    without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
023     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
025     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
026     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
027     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
029     * POSSIBILITY OF SUCH DAMAGE.
030     */
031    package com.threerings.jpkg.ant.dpkg;
032    
033    import java.io.File;
034    
035    import org.apache.commons.io.FilenameUtils;
036    import org.apache.tools.ant.BuildException;
037    import org.apache.tools.ant.Project;
038    
039    import com.threerings.antidote.Validator;
040    import com.threerings.antidote.field.ListTask;
041    import com.threerings.antidote.property.FileProperty;
042    import com.threerings.antidote.property.StringProperty;
043    import com.threerings.jpkg.PackageBuilder;
044    import com.threerings.jpkg.ant.dpkg.dependencies.Conflict;
045    import com.threerings.jpkg.ant.dpkg.dependencies.Replacement;
046    import com.threerings.jpkg.ant.dpkg.dependencies.Require;
047    import com.threerings.jpkg.ant.dpkg.dependencies.conditions.EqualOrGreaterThan;
048    import com.threerings.jpkg.ant.dpkg.dependencies.conditions.EqualOrLesserThan;
049    import com.threerings.jpkg.ant.dpkg.dependencies.conditions.EqualTo;
050    import com.threerings.jpkg.ant.dpkg.dependencies.conditions.GreaterThan;
051    import com.threerings.jpkg.ant.dpkg.dependencies.conditions.LesserThan;
052    import com.threerings.jpkg.debian.DebianPackageBuilder;
053    import com.threerings.jpkg.debian.PackageInfo;
054    
055    public class Dpkg extends ListTask<Package>
056    {
057        /**
058         * All user supplied data will be assumed to be in this character encoding, e.g. maintainer
059         * script source.
060         */
061        public static final String CHAR_ENCODING = "UTF-8";
062    
063        // from Field
064        public String getFieldName ()
065        {
066            return "dpkg";
067        }
068    
069        @Override // from ListComponent
070        public String getChildFieldName ()
071        {
072            return "package";
073        }
074    
075        @Override // from Task
076        public void init ()
077        {
078            // register the dependency fields.
079            registerField(Require.class);
080            registerField(Conflict.class);
081            registerField(Replacement.class);
082    
083            // register the dependency condition fields.
084            registerField(EqualTo.class);
085            registerField(GreaterThan.class);
086            registerField(EqualOrGreaterThan.class);
087            registerField(LesserThan.class);
088            registerField(EqualOrLesserThan.class);
089        }
090    
091        @Override // from Task
092        public void execute ()
093        {
094            final Validator validator = new Validator();
095            validator.addValidation(this);
096            validator.validateAll();
097    
098            for (final Package pkg : getValidatedFieldList()) {
099                final File destination = new File(FilenameUtils.concat(_output.getValue().getAbsolutePath(), pkg.getFilename()));
100    
101                final PackageInfo info = pkg.createPackageInfo(_distribution.getValue(), _prefix.getValue());
102                final PackageBuilder builder = new DebianPackageBuilder(info);
103                log("Creating dpkg package " + destination.getAbsolutePath() + " from destroot " +
104                    pkg.getDestroot().getAbsolutePath(), Project.MSG_INFO);
105                try {
106                    builder.write(destination, pkg.getDestroot());
107    
108                } catch (final Exception e) {
109                    throw new BuildException(e);
110                }
111            }
112        }
113    
114        /**
115         * Ant setter field: output. The directory to create all packages in.
116         */
117        public void setOutput (String value)
118        {
119            _output.setValue(value);
120        }
121    
122        /**
123         * Ant setter field: prefix. The prefix, or root, of all defined packages.
124         */
125        public void setPrefix (String value)
126        {
127            _prefix.setValue(value);
128        }
129    
130        /**
131         * Ant setter field: distribution. The Apt distribution these packages will be apart of.
132         */
133        public void setDistribution (String value)
134        {
135            _distribution.setValue(value);
136        }
137    
138        /**
139         * Ant adder field: Add a &lt;package&gt; definition.
140         */
141        public void addPackage (Package pkg)
142        {
143            appendRequiresValidation(pkg);
144        }
145    
146        @Override // from BaseComponent
147        protected void validateField ()
148        {
149            if (noChildFieldsDefined()) {
150                return;
151            }
152    
153            switch (validateFieldList()) {
154                case ALL_INVALID:
155                case SOME_INVALID:
156                    return;
157    
158                case ALL_VALID:
159                    break;
160            }
161    
162            switch (validateProperties(_output, _prefix, _distribution)) {
163                case ALL_INVALID:
164                case SOME_INVALID:
165                    return;
166    
167                case ALL_VALID:
168                    break;
169            }
170        }
171    
172        /** Ant adder/setter fields. */
173        private final FileProperty _output = new FileProperty("output", this);
174        private final StringProperty _prefix = new StringProperty("prefix", this);
175        private final StringProperty _distribution = new StringProperty("distribution", this);
176    }