[Title] refactoring web-packaging command
authorTaeyoung Son <taeyoung2.son@samsung.com>
Thu, 9 May 2013 05:57:40 +0000 (14:57 +0900)
committerTaeyoung Son <taeyoung2.son@samsung.com>
Thu, 9 May 2013 06:25:48 +0000 (15:25 +0900)
[Desc.]
[Issue]

Change-Id: I83a32c98f21f72d2b1c3abe3cca11a82ca977fae

org.tizen.cli/META-INF/MANIFEST.MF
org.tizen.cli/src/org/tizen/cli/exec/wgt/Main.java
org.tizen.cli/test/src/org/tizen/cli/exec/wgt/MainTest.java

index e973423..5fb022c 100755 (executable)
@@ -12,7 +12,8 @@ Require-Bundle: org.tizen.web.common,
  org.tizen.web.zimlaunch,
  org.tizen.common.sdblib,
  org.tizen.common.verrari,
- org.tizen.common.verrari.realm
+ org.tizen.common.verrari.realm,
+ org.tizen.common.builder
 Bundle-ClassPath: .,
  lib/ant.jar,
  lib/commons-cli-1.2.jar
index 7538c9d..449a629 100755 (executable)
@@ -32,6 +32,7 @@ import static org.tizen.cli.exec.LaunchOptionConstants.OPT_EXCLUDE;
 import static org.tizen.cli.exec.LaunchOptionConstants.OPT_INCLUDE;
 import static org.tizen.cli.exec.LaunchOptionConstants.OPT_NOCHECK;
 import static org.tizen.cli.exec.LaunchOptionConstants.OPT_OVERWRITE;
+import static org.tizen.common.util.FilenameUtil.getRelativePath;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -45,7 +46,7 @@ import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
 import org.tizen.cli.exec.AbstractLauncher;
 import org.tizen.cli.exec.Help;
-import org.tizen.common.builder.BuildProcess;
+import org.tizen.common.FactoryWithArgument;
 import org.tizen.common.builder.Resource;
 import org.tizen.common.builder.ResourceLayer;
 import org.tizen.common.core.command.Executor;
@@ -55,11 +56,13 @@ import org.tizen.common.core.command.prompter.Option;
 import org.tizen.common.core.command.zip.ZipCommand;
 import org.tizen.common.file.FileHandler;
 import org.tizen.common.file.FileHandler.Attribute;
+import org.tizen.common.file.Filter;
+import org.tizen.common.file.SimpleFileFilter;
 import org.tizen.common.file.VirtualFileHandler;
+import org.tizen.common.file.filter.WildCardFilterFactory;
 import org.tizen.common.util.CollectionUtil;
 import org.tizen.common.util.FileUtil;
 import org.tizen.common.util.FilenameUtil;
-import org.tizen.web.builder.JavaScriptMinifier;
 import org.tizen.web.common.WebConstant;
 
 /**
@@ -71,16 +74,10 @@ public class
 Main
 extends AbstractLauncher
 {
-    private static final String OPT_NAME_MINIFY = "minify";
-    private static final String OPT_DESC_MINIFY = "Minify resources(js | css)";
+    protected SimpleFileFilter filter = new SimpleFileFilter(true);
+    protected FactoryWithArgument<Filter, String> filterFactory = new WildCardFilterFactory();
 
-    //for build framework(org.tizen.common.builder)
-    protected static final String RESOURCE_LAYER_MINIFY = "minify";
-    protected static final String RESOURCE_LAYER_MINIFY_JS = RESOURCE_LAYER_MINIFY+".js";
     protected static final String RESOURCE_LAYER_START = "start";
-    protected static final String RESOURCE_LAYER_OPTIMIZE = "optimize";
-    protected static final String RESOURCE_LAYER_END = "end";
-
 
     /**
      * Entry point for cli main
@@ -116,7 +113,6 @@ extends AbstractLauncher
         opts.addOption( OptionBuilder.hasArg().withLongOpt( OPT_INCLUDE ).withDescription( DESC_INCLUDE ).create( OPT_INCLUDE.substring( 0, 1 ) ) );
         opts.addOption( OptionBuilder.hasArg().withLongOpt( OPT_EXCLUDE ).withDescription( DESC_EXCLUDE ).create( OPT_EXCLUDE.substring( 0, 1 ) ) );
 
-        opts.addOption( OptionBuilder.hasArg().withLongOpt( OPT_NAME_MINIFY ).withDescription( OPT_DESC_MINIFY ).create( OPT_NAME_MINIFY.substring( 0, 1 ) ) );
         return opts;
     }
 
@@ -211,31 +207,34 @@ extends AbstractLauncher
             return;
         }
 
-        BuildProcess buildProcess = new BuildProcess();
-
         ResourceLayer startLayer = new ResourceLayer("start", new VirtualFileHandler());
-        addBuilders(cmdLine, buildProcess, startLayer);
-
         Resource[] resources = getResources(baseDir, startLayer, includes, excludes);
-        if (buildProcess.getLastBuilder() != null) {
-            logger.debug("start build process");
-            buildProcess.build(resources);
-        }
-        zipFiles(wgtPath, baseDir, resources, includes, excludes);
+        zipFiles(wgtPath, baseDir, resources);
     }
 
     private Resource[] getResources(String baseDir, ResourceLayer resourceLayer, String[] includes, String[] excludes) throws IOException {
         List<File> files = FileUtil.findFiles(new File(baseDir), ".*", true);
         List<Resource> resources = new ArrayList<Resource>();
         FileHandler fh = resourceLayer.getFileHandler();
+        
+        setIncludes(includes);
+        setExcludes(excludes);
+
         for (File file : files) {
-            Resource resource = new Resource(resourceLayer, file.getPath());
+            String filePath = file.getPath();
+            if ( !FilenameUtil.equals(filePath, baseDir)
+                    && !filter.accept(baseDir, getRelativePath(baseDir, filePath))) {
+                logger.debug("Ignore {}", filePath);
+                continue;
+            }
+
+            Resource resource = new Resource(resourceLayer, filePath);
 
             String dir = null;
             if (file.isFile()) {
                 dir = file.getParent();
             } else {
-                dir = file.getPath();
+                dir = filePath;
             }
 
             if (fh instanceof VirtualFileHandler) {
@@ -249,6 +248,48 @@ extends AbstractLauncher
     }
 
     /**
+     * Set including file name patterns
+     * 
+     * @param includes including file name patterns
+     */
+    public
+    void
+    setIncludes(
+        final String[] includes
+    )
+    {
+        filter.clearIncludes();
+        if ( null != includes )
+        {
+            for ( final String include : includes )
+            {
+                filter.addIncludes( filterFactory.create( include ) );
+            }
+        }
+    }
+    
+    /**
+     * Set excluding file name patterns
+     * 
+     * @param excludes excluding file name patterns
+     */
+    public
+    void
+    setExcludes(
+        final String[] excludes
+    )
+    {
+        filter.clearExcludes();
+        if ( null != excludes )
+        {
+            for ( final String exclude : excludes )
+            {
+                filter.addExcludes( filterFactory.create( exclude ) );
+            }
+        }
+    }
+
+    /**
      * Archive <code>baseDir</code> to <code>wgtPath</code> including
      * <code>includes</code> and excluding <code>excludes</code>
      * 
@@ -257,38 +298,13 @@ extends AbstractLauncher
      * @param includes includes file pattern
      * @param excludes excludes file pattern
      */
-    private void zipFiles(final String wgtPath, final String baseDir, final Resource[] resources, final String[] includes, final String[] excludes) {
+    private void zipFiles(final String wgtPath, final String baseDir, final Resource[] resources) {
         final ZipCommand command = new ZipCommand(baseDir, resources, wgtPath);
-        command.setIncludes(includes);
-        command.setExcludes(excludes);
 
         final Executor executor = getExecutor();
         executor.execute(command);
     }
 
-    private void addBuilders(CommandLine cmdLine, BuildProcess buildProcess, ResourceLayer parentLayer ) {
-        if (buildProcess == null | cmdLine == null) {
-            return;
-        }
-        /*
-         * 1. optimization builder
-         */
-        String[] mins = cmdLine.getOptionValues(OPT_NAME_MINIFY);
-        if (mins != null) {
-            ResourceLayer optimizeLayer = new ResourceLayer(RESOURCE_LAYER_OPTIMIZE, parentLayer, parentLayer.getFileHandler());
-            ResourceLayer minifyLayer = new ResourceLayer(RESOURCE_LAYER_MINIFY, optimizeLayer, parentLayer.getFileHandler());
-            for (String minify : mins) {
-                if ("js".equals(minify)||"javascript".equals(minify)) {
-                    ResourceLayer jsMinLayer = new ResourceLayer(RESOURCE_LAYER_MINIFY_JS, minifyLayer,parentLayer.getFileHandler());
-                    JavaScriptMinifier jsMin = new JavaScriptMinifier(jsMinLayer);
-                    buildProcess.addBuilder(jsMin);
-                } else if ("css".equals(minify)) {
-                    throw new UnsupportedOperationException("css minification could not support yet.");
-                }
-            }
-        }
-    }
-
     /**
      * Check if <code>path</code> exists
      * 
@@ -353,12 +369,10 @@ extends AbstractLauncher
     void
     zipFiles(
             final String wgtPath,
-            final String baseDir,
-            final String[] includes,
-            final String[] excludes
+            final String baseDir
             )
     {
-        zipFiles(wgtPath, baseDir, null, includes, excludes);
+        zipFiles(wgtPath, baseDir, null);
     }
 
     /* (non-Javadoc)
index a62f6f2..9fa7ec6 100755 (executable)
@@ -92,7 +92,7 @@ extends AbstractMainTest
                        }
                };
                
-               main.zipFiles( "/test.zip", "/test", null, null );
+               main.zipFiles( "/test.zip", "/test");
                
                assertTrue( 0 < size( getBytes( fileHandler.read( "/test.zip" ) ) ) );