Ejemplo n.º 1
0
    def build(self,
              input_files,
              output_file,
              packaging_module,
              include_version=True,
              src_prefix_path=""):
        """
    Build distributable version of x3dom
    
    @param src_prefix_path: Optional path that is used as prefix for all source files
    @type src_prefix_path: String
    """

        print "output file:", output_file
        print "input_files:", input_files

        version_out = ""

        if include_version == True:
            # find the VERSION file
            if os.path.isfile("VERSION"):
                version_file_name = "VERSION"
            elif os.path.isfile("src/VERSION"):
                version_file_name = "src/VERSION"
            else:
                print "FATAL: Cannot find any VERSION file"
                sys.exit(0)

            # parse file & generate version.js
            version_out = self.generate_version_file(version_file_name)

            # Add the version.js to the list of input files
            input_files.append((version_out, [version_out]))

        concatenated_file = ""
        in_len = 0
        out_len = 0

        # Merging files
        print "Packing Files"
        for (_, files) in input_files:
            for f in files:
                if f == version_out:
                    concatenated_file = self._mergeFile(concatenated_file, f)
                else:
                    concatenated_file = self._mergeFile(
                        concatenated_file,
                        self._prefixFilePath(f, src_prefix_path))
                """       
              #Single file?
              if filename[-3:] == ".js":
                  #Merge directly
                  concatenated_file = self._mergeFile(concatenated_file, filename)
              #Otherwise (folder)
              else:
                  #Open all files in folder and merge individually
                  print "Folder: ", filename
                  node_files = [f for f in os.listdir(filename) if isfile(join(filename,f)) and f[-3:]==".js"]
                  print ";".join(node_files)
                  for node_file in node_files:
                      concatenated_file = self._mergeFile(concatenated_file, join(filename,node_file))
            """

        print ""

        outpath = os.path.dirname(os.path.abspath(output_file))

        if not os.access(outpath, os.F_OK):
            print "Create Dir ", outpath
            os.mkdir(outpath)

        # Packaging
        print "Packaging"
        print self.VERSION_STRING
        print "  Algo    " + packaging_module
        print "  Output  " + os.path.abspath(output_file)

        # JSMIN
        if packaging_module == "jsmin":
            # Minifiy the concatenated files
            out_stream = StringIO()
            jsm = jsmin.JavascriptMinify()
            jsm.minify(StringIO(concatenated_file), out_stream)

            out_len = len(out_stream.getvalue())

            # Write the minified output file
            outfile = open(output_file, 'w')
            outfile.write(self.VERSION_STRING)
            outfile.write(out_stream.getvalue())
            outfile.close()

        # JSPACKER
        elif packaging_module == "jspacker":
            p = JavaScriptPacker()

            result = p.pack(concatenated_file,
                            compaction=True,
                            encoding=62,
                            fastDecode=False)
            out_len = len(result)
            outfile = open(output_file, 'w')
            outfile.write(self.VERSION_STRING)
            outfile.write(result)
            outfile.close()

        # ClosureCompiler
        elif packaging_module == "closure":

            # collect files
            files = []
            for (_, filesForComponent) in input_files:
                for f in filesForComponent:
                    if f == version_out:
                        files += ["--js=" + f]
                    else:
                        files += [
                            "--js=" + self._prefixFilePath(f, src_prefix_path)
                        ]
                    #concatenated_file = self._mergeFile(concatenated_file, _prefixFilePath(f, src_prefix_path))

            Popen([
                "java", "-jar", "tools/compiler.jar", "--js_output_file=" +
                output_file, "--summary_detail_level=3",
                "--warning_level=VERBOSE"
            ] + files)
            # Popen(["java", "-jar", "tools/compiler.jar", "--js_output_file=" + output_file] + files)

        # NONE
        elif packaging_module == 'none':
            outfile = open(output_file, 'w')
            outfile.write(self.VERSION_STRING)
            outfile.write(concatenated_file)
            outfile.close()

        # Output some stats
        in_len = len(concatenated_file)
        ratio = float(out_len) / float(in_len)
        print "  Packed  %s -> %s" % (in_len, out_len)
        print "  Ratio   %s" % (ratio)
Ejemplo n.º 2
0
 def output(self, _in, out, **kw):
     out.write(JavaScriptPacker().pack(_in.read(),
                                       compaction=False,
                                       encoding=62,
                                       fastDecode=True))