View Javadoc

1   /*
2    * Copyright 2011 SOFTEC sa. All rights reserved.
3    *
4    * This source code is licensed under the Creative Commons
5    * Attribution-NonCommercial-NoDerivs 3.0 Luxembourg
6    * License.
7    *
8    * To view a copy of this license, visit
9    * http://creativecommons.org/licenses/by-nc-nd/3.0/lu/
10   * or send a letter to Creative Commons, 171 Second Street,
11   * Suite 300, San Francisco, California, 94105, USA.
12   */
13  
14  package org.codehaus.mojo.javascript;
15  
16  import org.apache.maven.plugin.MojoExecutionException;
17  import org.codehaus.plexus.util.DirectoryScanner;
18  import org.codehaus.plexus.util.FileUtils;
19  
20  import java.io.File;
21  import java.io.IOException;
22  
23  /**
24   * This goal strips and compress the source scripts in order to
25   * create the distribution package.
26   *
27   * @phase package
28   * @goal titanium-prepare-package
29   */
30  public class TitaniumPreparePackage extends AbstractCompressMojo {
31      /**
32       * <p>The package execution mode.</p>
33       * <p>Allow the execution of the package on an emulator/device.</p>
34       * <p>Values are:</p>
35       * <dl>
36       *   <dt>none</dt>
37       *   <dd>Do not execute. (Default value)</dd>
38       *   <dt>virtual</dt>
39       *   <dd>Execute on an emulator whose settings are specified in
40       *   {@link TitaniumPackageMojo#virtualDevice}.</dd>
41       *   <dt>device</dt>
42       *   <dd>Execute on a connected device.</dd>
43       * </dl>
44       *
45       * @parameter default-value="none" expression="${executeMode}"
46       */
47      protected String executeMode;
48  
49      /**
50       * The platform for which the code should be compiled.
51       * android, iphone, ipad, universal
52       *
53       * @parameter expression="${platform}"
54       * @required
55       */
56      protected String platform;
57  
58      /**
59       * Force compression even if not in "none" executeMode.
60       *
61       * @parameter default-value="false" expression="${forceCompress}"
62       */
63      protected boolean forceCompress;
64  
65      /**
66       * optional extension for the compressed artifact. Example "compressed"
67       *
68       * @parameter
69       */
70      private String scriptClassifier;
71  
72      /**
73       * The intput directory for the source javascript files.
74       *
75       * @parameter default-value="${project.build.outputDirectory}"
76       */
77      private File scriptsDirectory;
78  
79      /**
80       * The output directory of the compressed javascript files.
81       *
82       * @parameter default-value="${project.build.outputDirectory}"
83       */
84      private File compressedDirectory;
85  
86      /**
87       * The output directory of the compressed javascript files.
88       *
89       * @parameter default-value="${project.build.directory}/stripped"
90       */
91      private File strippedDirectory;
92  
93      /**
94       * The name of the script directory.
95       * <p>This parameter is optional it defaults to <code>platform-scripts</code></p>
96       * @parameter
97       */
98      private String scriptsDir;
99  
100     /**
101      * The name of the stripped directory.
102      * <p>This parameter is optional, it defaults to <code>platform-stripped</code></p>
103      * @parameter
104      */
105     private String strippedDirName;
106 
107     protected String getScriptsDir() {
108         if (scriptsDir == null) {
109             scriptsDir = platform + "-scripts";
110         }
111         return scriptsDir;
112     }
113 
114     protected String getStrippedDirName() {
115         if (strippedDirName == null) {
116             strippedDirName = platform + "-stripped";
117         }
118         return strippedDirName;
119     }
120     /**
121      * {@inheritDoc}
122      *
123      * @see org.codehaus.mojo.javascript.AbstractCompressMojo#getExtension()
124      */
125     public String getExtension()
126     {
127         return scriptClassifier;
128     }
129 
130     /**
131      * {@inheritDoc}
132      *
133      * @see org.codehaus.mojo.javascript.AbstractCompressMojo#getOutputDirectory()
134      */
135     protected File getOutputDirectory()
136     {
137         return new File(compressedDirectory, platform + File.separator + "Resources");
138     }
139 
140     /**
141      * {@inheritDoc}
142      *
143      * @see org.codehaus.mojo.javascript.AbstractCompressMojo#getOutputDirectory()
144      */
145     protected File getStrippedDirectory()
146     {
147         return new File(strippedDirectory, getStrippedDirName());
148     }
149 
150     /**
151      * {@inheritDoc}
152      *
153      * @see org.codehaus.mojo.javascript.AbstractCompressMojo#getSourceDirectory()
154      */
155     protected File getSourceDirectory()
156     {
157         return new File(scriptsDirectory, getScriptsDir());
158     }
159 
160     @Override
161     public void execute() throws MojoExecutionException {
162         if (!getSourceDirectory().exists()) {
163             getLog().info("Skipping prepare package as source directory doesn't exist");
164             return;
165         }
166         if (forceCompress || executeMode.equals("none")) {
167             super.execute();
168         } else {
169             getLog().info("Skipping compress and strip, performing simple scripts copy");
170             try {
171                 copy();
172             } catch (IOException ioe) {
173                 throw new MojoExecutionException("Error while copying scripts");
174             }
175         }
176     }
177 
178     protected void copy() throws IOException {
179         DirectoryScanner scanner = getDirectoryScanner();
180         scanner.scan();
181         String[] files = scanner.getIncludedFiles();
182 
183         getOutputDirectory().mkdirs();
184 
185         for (String file : files) {
186             File sourceFile = new File(scanner.getBasedir(), file);
187             File destFile = new File(getOutputDirectory(), file);
188             FileUtils.copyFile(sourceFile, destFile);
189         }
190     }
191 }