Author: brett
Date: Wed Jan 18 02:17:41 2012
New Revision: 1232718

URL: http://svn.apache.org/viewvc?rev=1232718&view=rev
Log:
[NPANDAY-533] Refactor wix-maven-plugin to reduce duplication
Submitted by: Adrián Boimvaser

Modified:
    
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/AbstractWixMojo.java
    
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/CandleMojo.java
    
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/LightMojo.java

Modified: 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/AbstractWixMojo.java
URL: 
http://svn.apache.org/viewvc/incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/AbstractWixMojo.java?rev=1232718&r1=1232717&r2=1232718&view=diff
==============================================================================
--- 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/AbstractWixMojo.java
 (original)
+++ 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/AbstractWixMojo.java
 Wed Jan 18 02:17:41 2012
@@ -19,9 +19,15 @@ package npanday.plugin.wix;
  * under the License.
  */
 
-import java.io.File;
-
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.DefaultExecutor;
+import org.apache.commons.exec.ExecuteException;
 import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
 
 public abstract class AbstractWixMojo
     extends AbstractMojo
@@ -31,21 +37,74 @@ public abstract class AbstractWixMojo
     * @parameter
     */
     protected String[] extensions;
-    
+
     /**
-     * Agruments to pass to WiX executable as is
+     * Arguments to pass to WiX executable as is
      * @parameter expression="${arguments}"
      */
 
-     protected String arguments;
+    protected String arguments;
 
-     /**
-      * @parameter expression="${wix.home}" default-value="${env.WIX}"
-      */
-     private File wixHome;
-     
-     public String getWixPath( String name )
-     {
+    /**
+     * @parameter expression="${wix.home}" default-value="${env.WIX}"
+     */
+    private File wixHome;
+
+    /**
+     * Suppress schema validation of documents (performance boost)
+     *
+     * @parameter expression="${suppressSchemaValidation}"
+     */
+    private boolean suppressSchemaValidation;
+
+    public void execute()
+        throws MojoExecutionException
+    {
+        try
+        {
+            CommandLine commandLine = new CommandLine( getWixPath( 
getCommand() ) );
+
+            if ( extensions != null )
+            {
+                for ( String ext : extensions )
+                {
+                    commandLine.addArgument( "-ext " + ext );
+                }
+            }
+
+            if ( suppressSchemaValidation )
+            {
+                commandLine.addArgument( "-ss" );
+            }
+
+            if ( arguments != null )
+            {
+                commandLine.addArgument( arguments );
+            }
+
+            commandLine.addArguments( getArguments().toArray( new String[0] ) 
);
+
+            getLog().info( "Executing " + commandLine );
+
+            DefaultExecutor executor = new DefaultExecutor();
+            int exitValue = executor.execute( commandLine );
+            if ( exitValue != 0 )
+            {
+                throw new MojoExecutionException( "Problem executing " + 
getCommand() + ", return code " + exitValue );
+            }
+        }
+        catch ( ExecuteException e )
+        {
+            throw new MojoExecutionException( "Problem executing " + 
getCommand(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "Problem executing " + 
getCommand(), e );
+        }
+    }
+
+    private String getWixPath( String name )
+    {
          if ( wixHome != null )
          {
              return new File( new File( wixHome, "bin" ), name 
).getAbsolutePath();
@@ -53,4 +112,8 @@ public abstract class AbstractWixMojo
          return name;
      }
 
+    public abstract String getCommand();
+
+    public abstract List<String> getArguments()
+        throws MojoExecutionException;
 }

Modified: 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/CandleMojo.java
URL: 
http://svn.apache.org/viewvc/incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/CandleMojo.java?rev=1232718&r1=1232717&r2=1232718&view=diff
==============================================================================
--- 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/CandleMojo.java
 (original)
+++ 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/CandleMojo.java
 Wed Jan 18 02:17:41 2012
@@ -19,20 +19,17 @@ package npanday.plugin.wix;
  * under the License.
  */
 
-import org.apache.commons.exec.CommandLine;
-import org.apache.commons.exec.DefaultExecutor;
-import org.apache.commons.exec.ExecuteException;
-import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 
 import java.io.File;
-import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Goal which executes WiX candle to create a .wixobj file.
  *
  * @goal candle
- * 
+ *
  * @phase package
  */
 public class CandleMojo
@@ -44,7 +41,7 @@ public class CandleMojo
      * @required
      */
     private File[] sourceFiles;
-    
+
     /**
      * Definitions to be passed on before pre Compilation
      * @parameter expression="${definitions}"
@@ -63,70 +60,51 @@ public class CandleMojo
      */
     private File outputDirectory;
 
-    public void execute()
+    @Override
+    public String getCommand()
+    {
+        return "candle";
+    }
+
+    @Override
+    public List<String> getArguments()
         throws MojoExecutionException
     {
-        String paths = "";
-        for (int x = 0; x < sourceFiles.length; x++) {
-          File f = sourceFiles[x];
-          if ( !f.exists() )
-          {
-             throw new MojoExecutionException( "Source file does not exist " + 
sourceFiles[x] );
-          } else {
-            paths = paths + sourceFiles[x].getAbsolutePath() + " ";
-          }
-        }
+        List<String> arguments = new ArrayList<String>();
 
-        try {
-          CommandLine commandLine = new CommandLine( getWixPath( "candle" ) );
-          commandLine.addArgument( "-nologo" );
-          commandLine.addArgument( "-sw" ); 
-          
-          if(definitions.length>0)
-          {
-            for (int x = 0; x < definitions.length; x++) 
+        arguments.add( "-nologo" );
+        arguments.add( "-sw" );
+
+        if(definitions.length>0)
+        {
+            for ( String definition : definitions )
             {
-                commandLine.addArgument( "-d" + definitions[x] );
+                arguments.add( "-d" + definition );
             }
-          }
-          
-          if(outputDirectory != null)
-          {
-            if (!outputDirectory.exists()) 
+        }
+
+        if(outputDirectory != null)
+        {
+            if (!outputDirectory.exists())
             {
               outputDirectory.mkdir();
             }
-            commandLine.addArgument( "-out" );
-            commandLine.addArgument( outputDirectory.getAbsolutePath() + "\\" 
);
-          }
-          if ( arch != null ) {
-            commandLine.addArgument( "-arch" );
-            commandLine.addArgument( arch );
-          }
-          if ( extensions != null ) {
-            for ( String ext : extensions ) {
-              commandLine.addArgument( "-ext" );
-              commandLine.addArgument( ext );
-            }
-          }
+            arguments.add( "-out" );
+            arguments.add( outputDirectory.getAbsolutePath() + "\\" );
+        }
 
-          if ( arguments != null ) {
-            commandLine.addArguments( arguments );
-          }
-
-          commandLine.addArguments( paths );
-          
-          DefaultExecutor executor = new DefaultExecutor();
-          int exitValue = executor.execute(commandLine);
-          
-          if ( exitValue != 0 ) {
-              throw new MojoExecutionException( "Problem executing candle, 
return code " + exitValue );
-          }
-         
-        } catch (ExecuteException e) {
-          throw new MojoExecutionException( "Problem executing candle", e );
-        } catch (IOException e ) {
-          throw new MojoExecutionException( "Problem executing candle", e );
+        if ( arch != null ) {
+            arguments.add( "-arch " + arch );
+        }
+
+        for ( File sourceFile : sourceFiles )
+        {
+            if ( !sourceFile.exists() )
+            {
+                throw new MojoExecutionException( "Source file does not exist 
" + sourceFile );
+            }
+            arguments.add( sourceFile.getAbsolutePath() );
         }
+        return arguments;
     }
 }

Modified: 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/LightMojo.java
URL: 
http://svn.apache.org/viewvc/incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/LightMojo.java?rev=1232718&r1=1232717&r2=1232718&view=diff
==============================================================================
--- 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/LightMojo.java
 (original)
+++ 
incubator/npanday/trunk/plugins/wix-maven-plugin/src/main/java/npanday/plugin/wix/LightMojo.java
 Wed Jan 18 02:17:41 2012
@@ -19,20 +19,18 @@ package npanday.plugin.wix;
  * under the License.
  */
 
-import org.apache.commons.exec.CommandLine;
-import org.apache.commons.exec.DefaultExecutor;
-import org.apache.commons.exec.ExecuteException;
-import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.codehaus.plexus.util.StringUtils;
 
 import java.io.File;
-import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Goal which executes WiX light to create a .msi file.
  *
  * @goal light
- * 
+ *
  * @phase package
  */
 public class LightMojo
@@ -44,7 +42,7 @@ public class LightMojo
      * @required
      */
     private File[] objectFiles;
-    
+
     /**
      * Output file
      * @parameter expression="${outputFile}"
@@ -63,70 +61,63 @@ public class LightMojo
      */
     private File outputDirectory;
 
-    public void execute()
+
+    /**
+     * Localized string cultures to load from .wxl files and libraries.
+     *
+     * @parameter expression="${cultures}"
+     */
+    private String[] cultures;
+
+    @Override
+    public String getCommand()
+    {
+        return "light";
+    }
+
+    @Override
+    public List<String> getArguments()
         throws MojoExecutionException
     {
+        List<String> arguments = new ArrayList<String>();
 
-        String paths = "";
-        for (int x = 0; x < objectFiles.length; x++) {
-          File f = objectFiles[x];
-          if ( !f.exists() )
-          {
-             throw new MojoExecutionException( "Object file does not exist " + 
objectFiles[x] );
-          } else {
-            paths = paths + objectFiles[x].getAbsolutePath() + " ";
-          }
+        for ( File objectFile : objectFiles )
+        {
+            if ( !objectFile.exists() )
+            {
+                throw new MojoExecutionException( "Object file does not exist 
" + objectFile );
+            }
+            arguments.add( objectFile.getAbsolutePath() );
         }
 
         if(localizationFiles.length > 0)
         {
-          paths += "-loc ";
-          for (int x = 0; x < localizationFiles.length; x++) {
-            File f = localizationFiles[x];
-            if ( !f.exists() )
+            arguments.add( "-loc" );
+            for ( File localizationFile : localizationFiles )
             {
-               throw new MojoExecutionException( "Localization file does not 
exist " + objectFiles[x] );
-            } else {
-               paths = paths + localizationFiles[x].getAbsolutePath() + " ";
+                if ( !localizationFile.exists() )
+                {
+                    throw new MojoExecutionException( "Localization file does 
not exist " + localizationFile );
+                }
+                arguments.add( localizationFile.getAbsolutePath() );
             }
-          }
         }
-        
-        try {
-          CommandLine commandLine = new CommandLine( getWixPath( "light" ) );
-          commandLine.addArguments( paths );
-
-          if (outputFile != null) {
-            commandLine.addArgument( "-o" );
-            commandLine.addArgument( outputFile.getAbsolutePath() );
-          }
-          else if (outputDirectory != null) {
-            commandLine.addArgument( "-out" );
-            commandLine.addArgument( outputDirectory.getAbsolutePath() + "\\" 
);
-          }
-
-          if ( extensions != null ) {
-            for ( String ext : extensions ) {
-              commandLine.addArgument( "-ext" );
-              commandLine.addArgument( ext );
-            }
-          }
 
-          if ( arguments != null ) {
-            commandLine.addArguments( arguments );
-          }
-
-          DefaultExecutor executor = new DefaultExecutor();
-          int exitValue = executor.execute(commandLine);
-          
-          if ( exitValue != 0 ) {
-              throw new MojoExecutionException( "Problem executing light, 
return code " + exitValue );
-          }
-         
-        } catch (ExecuteException e) {
-         throw new MojoExecutionException( "Problem executing light", e );
-        } catch (IOException e ) {
-          throw new MojoExecutionException( "Problem executing light", e );
+        if (outputFile != null) {
+            arguments.add( "-o" );
+            arguments.add( outputFile.getAbsolutePath() );
+        }
+        else if (outputDirectory != null) {
+            arguments.add( "-out" );
+            arguments.add( outputDirectory.getAbsolutePath() + "\\" );
         }
+
+        if ( cultures != null )
+        {
+            String commaDelimitedCultures = StringUtils.join( cultures, "," );
+            arguments.add( "-cultures:" + commaDelimitedCultures );
+        }
+
+        return arguments;
     }
 }


Reply via email to