001package io.ebean.enhance.ant;
002
003import io.ebean.enhance.Transformer;
004import org.apache.tools.ant.BuildException;
005import org.apache.tools.ant.Task;
006
007import java.io.File;
008
009/**
010 * An ANT task that can enhance entity beans etc for use by Ebean.
011 * <p>
012 * You can use this ANT task as part of your build process to enhance entity
013 * beans etc.
014 * </p>
015 * <p>
016 * The parameters are:
017 * <ul>
018 * <li> <b>classSource</b> This is the root directory where the .class files
019 * are found. </li>
020 * <li> <b>packages</b> A comma delimited list of packages that is searched for
021 * classes that need to be enhanced. If the package ends with ** or * then all
022 * subpackages are also searched. </li>
023 * <li> <b>transformArgs</b> Arguments passed to the transformer. Typically a
024 * debug level in the form of debug=1 etc. </li>
025 * </ul>
026 * </p>
027 *
028 * <pre class="code">
029 *
030 *       &lt;taskdef name=&quot;ebeanEnhance&quot; classname=&quot;AntEnhanceTask&quot; classpath=&quot;bin&quot; /&gt;
031 *
032 *   &lt;target name=&quot;enhance&quot; depends=&quot;compile&quot;&gt;
033 *       &lt;ebeanEnhance
034 *            classSource=&quot;${bin.dir}&quot;
035 *            packages=&quot;com.avaje.ebean.meta.**, com.acme.myapp.entity.**&quot;
036 *            transformArgs=&quot;debug=1&quot; /&gt;
037 *   &lt;/target&gt;
038 *
039 * </pre>
040 */
041public class AntEnhanceTask extends Task {
042
043  private String classpath;
044
045  private String classSource;
046
047  private String transformArgs;
048
049  private String packages;
050
051  @Override
052  public void execute() throws BuildException {
053
054//    StringBuilder extraClassPath = new StringBuilder();
055//    extraClassPath.append(classSource);
056//    if (classpath != null)
057//    {
058//      if (!extraClassPath.toString().endsWith(";"))
059//      {
060//        extraClassPath.append(";");
061//      }
062//      extraClassPath.append(classpath);
063//    }
064    Transformer t = new Transformer(null, transformArgs);//extraClassPath.toString(),
065
066    ClassLoader cl = AntEnhanceTask.class.getClassLoader();
067    OfflineFileTransform ft = new OfflineFileTransform(t, cl, classSource);
068
069    ft.process(packages);
070  }
071
072  /**
073   * the classpath used to search for e.g. inerited classes
074   */
075  public String getClasspath() {
076    return classpath;
077  }
078
079  /**
080   * the classpath used to search for e.g. inerited classes
081   */
082  public void setClasspath(String classpath) {
083    this.classpath = classpath;
084  }
085
086  /**
087   * Set the directory holding the class files we want to transform.
088   */
089  public void setClassSource(String source) {
090    this.classSource = source;
091  }
092
093  /**
094   * Set the arguments passed to the transformer.
095   */
096  public void setTransformArgs(String transformArgs) {
097    this.transformArgs = transformArgs;
098  }
099
100  /**
101   * Set the package name to search for classes to transform.
102   * <p>
103   * If the package name ends in "/**" then this recursively transforms all
104   * sub packages as well.
105   * </p>
106   */
107  public void setPackages(String packages) {
108    this.packages = packages;
109  }
110
111}