def compile(compiler_jar_path, deps, inputs, compiler_flags, root, update_build_version=True): input = list(inputs)[0] # Make sure a .jar is specified. if not compiler_jar_path: logging.error('--compiler_jar flag must be specified if --output is ' '"compiled"') return None compiled_source = jscompiler.Compile( compiler_jar_path, deps, compiler_flags) if compiled_source is None: logging.error('JavaScript compilation failed.') return None else: logging.info('JavaScript compilation succeeded.') file_name_no_suffix = os.path.splitext(os.path.basename(input))[0] min_js = os.path.dirname(input) + "/" + file_name_no_suffix + ".c.min.js" timestamp, build_path = version.updater.get_build_version(input, update_build_version) dir = root.replace("\\", "/") + version.updater.BASE_BUILD_DIR + build_path + "/" min_js_new = dir + file_name_no_suffix + "." + timestamp + ".c.min.js" if not os.path.exists(dir): os.makedirs(dir) svn.try_lock(min_js_new) out = open(min_js_new, "w") out.write(compiled_source) logging.info('min js : ' + min_js_new) return min_js_new, min_js
def main(): logging.basicConfig(format=(sys.argv[0] + ': %(message)s'), level=logging.INFO) options, args = _GetOptionsParser().parse_args() # Make our output pipe. if options.output_file: out = open(options.output_file, 'w') else: out = sys.stdout sources = set() logging.info('Scanning paths...') for path in options.roots: for js_path in treescan.ScanTreeForJsFiles(path): sources.add(_PathSource(js_path)) # Add scripts specified on the command line. for js_path in args: sources.add(_PathSource(js_path)) logging.info('%s sources scanned.', len(sources)) # Though deps output doesn't need to query the tree, we still build it # to validate dependencies. logging.info('Building dependency tree..') tree = depstree.DepsTree(sources) input_namespaces = set() inputs = options.inputs or [] for input_path in inputs: js_input = _GetInputByPath(input_path, sources) if not js_input: logging.error('No source matched input %s', input_path) sys.exit(1) input_namespaces.update(js_input.provides) input_namespaces.update(options.namespaces) if not input_namespaces: logging.error('No namespaces found. At least one namespace must be ' 'specified with the --namespace or --input flags.') sys.exit(2) # The Closure Library base file must go first. base = _GetClosureBaseFile(sources) deps = [base] + tree.GetDependencies(input_namespaces) output_mode = options.output_mode if output_mode == 'list': out.writelines([js_source.GetPath() + '\n' for js_source in deps]) elif output_mode == 'script': for js_source in deps: src = js_source.GetSource() if js_source.is_goog_module: src = _WrapGoogModuleSource(src) out.write(src + '\n') elif output_mode == 'compiled': logging.warning("""\ Closure Compiler now natively understands and orders Closure dependencies and is prefererred over using this script for performing JavaScript compilation. Please migrate your codebase. See: https://github.com/google/closure-compiler/wiki/Manage-Closure-Dependencies """) # Make sure a .jar is specified. if not options.compiler_jar: logging.error( '--compiler_jar flag must be specified if --output is ' '"compiled"') sys.exit(2) # Will throw an error if the compilation fails. compiled_source = jscompiler.Compile( options.compiler_jar, [js_source.GetPath() for js_source in deps], jvm_flags=options.jvm_flags, compiler_flags=options.compiler_flags) logging.info('JavaScript compilation succeeded.') out.write(compiled_source) else: logging.error('Invalid value for --output flag.') sys.exit(2)
def main(): logging.basicConfig(format=(sys.argv[0] + ': %(message)s'), level=logging.INFO) options, args = _GetOptionsParser().parse_args() # Make our output pipe. if options.output_file: out = open(options.output_file, 'w') else: out = sys.stdout sources = set() logging.info('Scanning paths...') for path in options.roots: for js_path in treescan.ScanTreeForJsFiles(path): sources.add(_PathSource(js_path)) # Add scripts specified on the command line. for path in args: sources.add(source.Source(_PathSource(path))) logging.info('%s sources scanned.', len(sources)) # Though deps output doesn't need to query the tree, we still build it # to validate dependencies. logging.info('Building dependency tree..') tree = depstree.DepsTree(sources) input_namespaces = set() inputs = options.inputs or [] for input_path in inputs: js_input = _GetInputByPath(input_path, sources) if not js_input: logging.error('No source matched input %s', input_path) sys.exit(1) input_namespaces.update(js_input.provides) input_namespaces.update(options.namespaces) if not input_namespaces: logging.error('No namespaces found. At least one namespace must be ' 'specified with the --namespace or --input flags.') sys.exit(2) # The Closure Library base file must go first. base = _GetClosureBaseFile(sources) deps = [base] + tree.GetDependencies(input_namespaces) output_mode = options.output_mode if output_mode == 'list': out.writelines([js_source.GetPath() + '\n' for js_source in deps]) elif output_mode == 'script': out.writelines([js_source.GetSource() for js_source in deps]) elif output_mode == 'compiled': # Make sure a .jar is specified. if not options.compiler_jar: logging.error( '--compiler_jar flag must be specified if --output is ' '"compiled"') sys.exit(2) compiled_source = jscompiler.Compile( options.compiler_jar, [js_source.GetPath() for js_source in deps], options.compiler_flags) if compiled_source is None: logging.error('JavaScript compilation failed.') sys.exit(1) else: logging.info('JavaScript compilation succeeded.') out.write(compiled_source) else: logging.error('Invalid value for --output flag.') sys.exit(2)
def make( myIncludes=[], inputAry=[], namespaceAry=[], rootAry=[], output_mode='list', compiler_jar=None, compiler_flags=[], jvm_flags=[], output_file='', baseFileName='base.js', ): logging.basicConfig(format=sys.argv[0] + ': %(message)s', level=logging.INFO) if output_mode: out = open(output_file, 'w') else: out = sys.stdout sources = set() logging.info('Scanning paths...') for path in rootAry: for js_path in treescan.ScanTreeForJsFiles(path): sources.add(_PathSource(js_path)) logging.info('sources added by rootPath scan: %s', len(sources)) # Add scripts specified on the command line. # logging.info('Adding myIncludes...') # for js_path in myIncludes: # sources.add(_PathSource(js_path)) # logging.info('sources added by myIncludes: %s', len(myIncludes)) logging.info('sources added total: %s', len(sources)) # Though deps output doesn't need to query the tree, we still build it # to validate dependencies. logging.info('Building dependency tree..') tree = depstree.DepsTree(sources) input_namespaces = set() inputs = inputAry or [] for input_path in inputs: js_input = _GetInputByPath(input_path, sources) if not js_input: logging.error('No source matched input %s', input_path) sys.exit(1) input_namespaces.update(js_input.provides) input_namespaces.update(namespaceAry) if not namespaceAry: logging.error( 'No namespaces found. At least one namespace must be specified with the --namespace or --input flags.' ) sys.exit(2) # The Closure Library base file must go first. base = _GetClosureBaseFile(sources, baseFileName) logging.info('Using base file %s', base) deps = [base] + tree.GetDependencies(input_namespaces) # Add scripts specified on the command line. logging.info('Adding myIncludes...') myIncludesPathSource = [] for js_path in myIncludes: myIncludesPathSource.append(_PathSource(js_path)) newDeps = [] newDeps.extend(myIncludesPathSource) newDeps.extend(deps) #for theFile in myIncludes: # deps.insert(0,_PathSource(theFile)) #deps.insert(0, _PathSource(theFile)) logging.info('sources added by myIncludes: %s', len(myIncludes)) if output_mode == 'list': out.writelines([js_source.GetPath() + '\n' for js_source in newDeps]) # out.write('test \n') out.close() exists = os.path.exists(output_file) if exists: logging.info('Wrote source list to %s', output_file) else: logging.error('failed to write source list to %s', output_file) elif output_mode == 'script': for js_source in newDeps: src = js_source.GetSource() if js_source.is_goog_module: src = _WrapGoogModuleSource(src) out.write(src + '\n') exists = os.path.exists(output_file) if exists: logging.info('Wrote source script to %s', output_file) else: logging.error('failed to write source script to %s', output_file) elif output_mode == 'compiled': logging.warning("""\ Closure Compiler now natively understands and orders Closure dependencies and is prefererred over using this script for performing JavaScript compilation. Please migrate your codebase. See: https://github.com/google/closure-compiler/wiki/Manage-Closure-Dependencies """) # Make sure a .jar is specified. if not compiler_jar: logging.error( '--compiler_jar flag must be specified if --output is "compiled"' ) sys.exit(2) # Will throw an error if the compilation fails. compiled_source = jscompiler.Compile( compiler_jar, [js_source.GetPath() for js_source in newDeps], jvm_flags=jvm_flags, compiler_flags=compiler_flags) logging.info('JavaScript compilation succeeded.') out.write(compiled_source) out.close() exists = os.path.exists(output_file) if exists: logging.info('Wrote compiled script to %s', output_file) else: logging.error('failed to write compiled script to %s', output_file) else: logging.error('Invalid value for --output flag.') sys.exit(2)
package.append(experimentalFiles) makeSourceFiles(package, buildPath + "Total.export.js") outputFilename = "TotalWeb.min.js" # Setup compiler flags compilerFlags = [ "--externs", buildPath + "externs.js", "--language_in", "ECMASCRIPT5" ] if advanced: compilerFlags.extend(["--compilation_level", "ADVANCED_OPTIMIZATIONS"]) # To Debug #compilerFlags.extend([ "--formatting", "pretty_print"]) else: compilerFlags.extend(["--compilation_level", "SIMPLE_OPTIMIZATIONS"]) # To Debug #compilerFlags.extend([ "--formatting", "pretty_print"]) print "Building minized file." minimized, error = jscompiler.Compile(buildPath + 'compiler.jar', sourceFiles, compilerFlags) print error print "Writing to %s." % outputFilename licence = file(buildPath + "licence.txt", "r+").read() if not os.path.exists(buildPath + "generated"): os.mkdir(buildPath + "generated") file(buildPath + "generated/" + outputFilename, "w").write(licence + minimized) raw_input("Press a key to finish.")
package.append(astroFiles) package.append(experimentalFiles) makeSourceFiles(package, "Total.export.js") outputFilename = "TotalWeb.min.js" # Setup compiler flags compilerFlags = ["--externs", "externs.js", "--language_in", "ECMASCRIPT5"] if advanced: compilerFlags.extend(["--compilation_level", "ADVANCED_OPTIMIZATIONS"]) # To Debug #compilerFlags.extend([ "--formatting", "pretty_print"]) else: compilerFlags.extend([ "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--formatting", "pretty_print" ]) print "Building minized file." minimized, error = jscompiler.Compile('compiler.jar', sourceFiles, compilerFlags) print error print "Writing to %s." % outputFilename licence = file("licence.txt", "r+").read() if not os.path.exists("generated"): os.mkdir("generated") file("generated/" + outputFilename, "w").write(licence + minimized) raw_input("Press a key to finish.")