001/*
002 * Copyright 2007-2018 The jdeb developers.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.vafer.jdeb.ant;
018
019import java.io.File;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collection;
023
024import org.apache.tools.ant.BuildException;
025import org.apache.tools.ant.Project;
026import org.apache.tools.ant.taskdefs.MatchingTask;
027import org.apache.tools.ant.taskdefs.Tar;
028import org.apache.tools.ant.types.FileSet;
029import org.vafer.jdeb.Console;
030import org.vafer.jdeb.DataProducer;
031import org.vafer.jdeb.DebMaker;
032import org.vafer.jdeb.PackagingException;
033import org.vafer.jdeb.producers.DataProducerFileSet;
034
035/**
036 * AntTask for creating debian archives.
037 */
038public class DebAntTask extends MatchingTask {
039
040    /** The Debian package produced */
041    private File deb;
042
043    /** The directory containing the control files to build the package */
044    private File control;
045
046    /** The file containing the PGP keys */
047    private File keyring;
048
049    /** The key to use in the keyring */
050    private String key;
051
052    /** The passphrase for the key to sign the changes file */
053    private String passphrase;
054
055    /** The file to read the changes from */
056    private File changesIn;
057
058    /** The file where to write the changes to */
059    private File changesOut;
060
061    /** The file where to write the changes of the changes input to */
062    private File changesSave;
063
064    /** The compression method used for the data file (none, gzip, bzip2 or xz) */
065    private String compression = "gzip";
066
067    /**
068     * The digest algorithm to use.
069     *
070     * @see org.bouncycastle.bcpg.HashAlgorithmTags
071     */
072    private String digest = "SHA1";
073
074    /** Trigger the verbose mode detailing all operations */
075    private boolean verbose;
076
077    private Collection<Link> links = new ArrayList<Link>();
078
079    private Collection<DataProducer> dataProducers = new ArrayList<DataProducer>();
080    private Collection<DataProducer> conffilesProducers = new ArrayList<DataProducer>();
081
082
083    public void setDestfile( File deb ) {
084        this.deb = deb;
085    }
086
087    public void setControl( File control ) {
088        this.control = control;
089    }
090
091    public void setChangesIn( File changes ) {
092        this.changesIn = changes;
093    }
094
095    public void setChangesOut( File changes ) {
096        this.changesOut = changes;
097    }
098
099    public void setChangesSave( File changes ) {
100        this.changesSave = changes;
101    }
102
103    public void setKeyring( File keyring ) {
104        this.keyring = keyring;
105    }
106
107    public void setKey( String key ) {
108        this.key = key;
109    }
110
111    public void setPassphrase( String passphrase ) {
112        this.passphrase = passphrase;
113    }
114
115    public void setCompression( String compression ) {
116        this.compression = compression;
117    }
118
119    public void setVerbose( boolean verbose ) {
120        this.verbose = verbose;
121    }
122
123    public void addFileSet( FileSet fileset ) {
124        dataProducers.add(new DataProducerFileSet(fileset));
125    }
126
127    public void addTarFileSet( Tar.TarFileSet fileset ) {
128        dataProducers.add(new DataProducerFileSet(fileset));
129    }
130
131    public void addData( Data data ) {
132        dataProducers.add(data);
133    }
134
135    public void addLink( Link link ) {
136        links.add(link);
137    }
138
139    public void setDigest(String digest) {
140        this.digest = digest;
141    }
142
143    public void execute() {
144        // add the data producers for the links
145        for (Link link : links) {
146            dataProducers.add(link.toDataProducer());
147        }
148
149        // validate the type of the <data> elements
150        for (DataProducer dataProducer : dataProducers) {
151            if (dataProducer instanceof Data) {
152                Data data = (Data) dataProducer;
153                if (data.getType() == null) {
154                    throw new BuildException("The type of the data element wasn't specified (expected 'file', 'directory' or 'archive')");
155                } else if (!Arrays.asList("file", "directory", "archive").contains(data.getType().toLowerCase())) {
156                    throw new BuildException("The type '" + data.getType() + "' of the data element is unknown (expected 'file', 'directory' or 'archive')");
157                }
158                if (data.getConffile() != null && data.getConffile()) {
159                    conffilesProducers.add(dataProducer);
160                }
161            }
162        }
163
164        Console console = new TaskConsole(this, verbose);
165
166        DebMaker debMaker = new DebMaker(console, dataProducers, conffilesProducers);
167        debMaker.setDeb(deb);
168        debMaker.setControl(control);
169        debMaker.setChangesIn(changesIn);
170        debMaker.setChangesOut(changesOut);
171        debMaker.setChangesSave(changesSave);
172        debMaker.setKeyring(keyring);
173        debMaker.setKey(key);
174        debMaker.setPassphrase(passphrase);
175        debMaker.setCompression(compression);
176        debMaker.setDigest(digest);
177
178        try {
179            debMaker.validate();
180            debMaker.makeDeb();
181
182        } catch (PackagingException e) {
183            log("Failed to create the Debian package " + deb, e, Project.MSG_ERR);
184            throw new BuildException("Failed to create the Debian package " + deb, e);
185        }
186    }
187}