// AAPT Package Ant Task // // Douglas Thrift // // $Id$ /* Copyright 2008 Douglas Thrift * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.douglasthrift.anttasks; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.UpToDate; import org.apache.tools.ant.types.DirSet; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.resources.FileResource; import org.apache.tools.ant.types.resources.Union; public class AAPTPackage extends CommandTask { private boolean force_; private boolean update_; private boolean make_; private boolean verbose_; private boolean extending_; private File manifest_; private Path include_; private File assets_; private File publicDefinitions_; private Path resource_; private File file_; private File java_; private ArrayList dirSets_ = new ArrayList(); public void setForce(boolean force) { force_ = force; } public void setUpdate(boolean update) { update_ = update; } public void setMake(boolean make) { make_ = make; } public void setVerbose(boolean verbose) { verbose_ = verbose; } public void setExtending(boolean extending) { extending_ = extending; } public void setManifest(File manifest) { manifest_ = manifest; } public void setInclude(Path include) { include_ = include; } public void setAssets(File assets) { assets_ = assets; } public void setPublicDefinitions(File publicDefinitions) { publicDefinitions_ = publicDefinitions; } public void setResource(Path resource) { resource_ = resource; } public void setFile(File file) { file_ = file; } public void setJava(File java) { java_ = java; } public void add(DirSet dirSet) { dirSets_.add(dirSet); } @Override public void execute() { if (isUpToDate()) return; command("aapt"); argument("package"); if (force_) argument("-f"); if (update_) argument("-u"); if (make_) argument("-m"); if (verbose_) argument("-v"); if (extending_) argument("-x"); if (manifest_ != null) arguments("-M", manifest_); if (include_ != null) for (Iterator iterator = include_.iterator(); iterator.hasNext();) arguments("-I", iterator.next().toString()); if (assets_ != null && assets_.exists()) arguments("-A", assets_); if (publicDefinitions_ != null) arguments("-P", publicDefinitions_); if (resource_ != null) for (String resource : resource_.list()) arguments("-S", resource); if (file_ != null) arguments("-F", file_); if (java_ != null) arguments("-J", java_); for (DirSet dirSet : dirSets_) for (Iterator iterator = dirSet.iterator(); iterator.hasNext();) argument(iterator.next().toString()); super.execute(); } private boolean isUpToDate() { Project project = getProject(); UpToDate upToDate = (UpToDate)project.createTask("uptodate"); Union union = upToDate.createSrcResources(); if (manifest_ != null) { FileSet fileSet = (FileSet)project.createDataType("fileset"); fileSet.setFile(manifest_); union.add(fileSet); } if (assets_ != null && assets_.exists()) { FileSet fileSet = (FileSet)project.createDataType("fileset"); fileSet.setDir(assets_); union.add(fileSet); } if (resource_ != null) union.add(resource_); union.addAll(dirSets_); // TODO: publicDefinitions_ if (file_ != null) { upToDate.setTargetFile(file_); if (!upToDate.eval()) return false; } if (java_ != null) { FileSet fileSet = (FileSet)project.createDataType("fileset"); fileSet.setDir(java_); fileSet.setIncludes("**/R.java"); if (fileSet.size() == 0) return false; for (Iterator iterator = fileSet.iterator(); iterator.hasNext();) { upToDate.setTargetFile(((FileResource)iterator.next()).getFile()); if (!upToDate.eval()) return false; } } return upToDate.eval(); } }