def tzjs_dump_dependencies(env, options, input_js): """ Lists all the dependencies of the .js file. We attempt to retain some kind of order with the leaves of the dependency tree at the top of the list. """ # The set of files to be injected injects = inject_js_from_options(options) LOG.info("files to inject:") _ = [ LOG.info(" - %s" % i) for i in injects ] # Do a full parse with a correct context, and extract the # javascript includes context = context_from_options(options, input_js[0]) deps = render_js_extract_includes(context, options, env_load_templates(env, input_js), injects) # TODO : Do we need this find_dependencies stage? It doesn't pick # up any javascript tags. for i in input_js: deps += find_dependencies(i, options.templatedirs, env) # Write dependency data # LOG.info("deps are: %s" % deps) output_dependency_info(options.dependency_file, options.output, deps) return 0
def html_generate(env, options, input_js, input_html): """ Generate html based on the templates and build mode. """ # - dev, canvas_dev: # render top-level js files into a temporary file # collect the .js files that need to be included # setup includes, startup code and the js render result into variables # render html template # # - release, canvas: # need to know name of output js file # setup startup code to point to .tzjs or .js file # render html template # Load templates (using default html template if not specified) Profiler.start('load_templates') template_html = load_html_template(env, input_html) if template_html is None: LOG.error("failed to load file %s from template dirs" % input_html[0]) exit(1) # Get context if len(input_js) > 0: title = input_js[0] elif options.codefile: title = options.codefile elif len(input_html) > 0: title = input_html[0] else: title = "Unknown" title = splitext(basename(title))[0] context = context_from_options(options, title) Profiler.stop('load_templates') Profiler.start('code_gen') # In development modes, render the JS code that needs embedding rendered_js = "" inc_js = [] if options.mode in [ 'plugin-debug', 'canvas-debug' ]: inject_js = inject_js_from_options(options) Profiler.start('load_js_templates') templates_js = env_load_templates(env, input_js) Profiler.stop('load_js_templates') (rendered_js, inc_js) = render_js(context, options, templates_js, inject_js) # Add the HTML and JS code into the tz_* variables default_add_code(options, context, rendered_js, inc_js) Profiler.stop('code_gen') Profiler.start('html_render') # Render the template and write it out try: res = template_html.render(context) except Exception, e: raise ToolsException("Error in '%s': %s %s" \ % (input_html, e.__class__.__name__, str(e)))
def tzjs_generate(env, options, input_js): # The set of files to be injected Profiler.start('find_inject_code') inject_js = inject_js_from_options(options) Profiler.stop('find_inject_code') if 0 < len(inject_js): LOG.info("Files to inject:") for i in inject_js: LOG.info(" - '%s'" % i) # Create a context and render the template Profiler.start('load_templates') context = context_from_options(options, input_js[0]) templates_js = env_load_templates(env, input_js) Profiler.stop('load_templates') Profiler.start('render_js') (rendered_js, inc_js) = render_js(context, options, templates_js, inject_js) Profiler.stop('render_js') if 0 != len(inc_js): raise ToolsException("internal error") # If required, remove all calls to 'debug.*' methods BEFORE # compacting if options.stripdebug: strip_path = "strip-debug" if options.stripdebugpath: strip_path = normpath(abspath(options.stripdebugpath)) LOG.info("Stripping debug method calls ...") # Check we can actually run strip debug, with the given path p = subprocess.Popen('%s -h' % strip_path, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) p.communicate() if p.returncode != 0: raise ToolsException( \ "\n\tstrip-debug tool could not be found, check it's on your path\n" "\tor supply the path with --strip-debug <path>. To run maketzjs\n" "\twithout stripping debug code run with --no-strip-debug." ) Profiler.start('strip_debug') strip_debug_flags = "-Ddebug=false" # Add the default flags first, in case the custom flags # override them. if options.verbose: strip_debug_flags += " -v" for s in options.stripnamespaces: strip_debug_flags += " --namespace %s" % s for v in options.stripvars: strip_debug_flags += " -D %s" % v if options.ignoreerrors: strip_debug_flags += " --ignore-errors" # Launch the strip command and pass in the full script via # streams. strip_cmd = "%s %s" % (strip_path, strip_debug_flags) LOG.info("Strip cmd: %s" % strip_cmd) p = subprocess.Popen(strip_cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stripped_js, err) = p.communicate(rendered_js) strip_retval = p.wait() if 0 != strip_retval: with NamedTemporaryFile(delete = False) as t: t.write(rendered_js) raise ToolsException( \ "strip-debug tool exited with code %d and stderr:\n\n%s\n" "The (merged) input probably contains a syntax error. It has " "been written to:\n %s\nfor inspection." \ % (strip_retval, err, t.name)) if not err is None and len(err) > 0: print "error output from strip-debug tool:" print "%s" % err rendered_js = stripped_js Profiler.stop('strip_debug') # If required, compact the JS via a temporary file, otherwise just # write out directly to the output file. if options.yui or options.closure or options.uglifyjs: Profiler.start('compact') with NamedTemporaryFile(delete = False) as t: LOG.info("Writing temp JS to '%s'" % t.name) t.write(rendered_js) LOG.info("Compacting temp JS to '%s'" % options.output) tzjs_compact(options, t.name, options.output) remove(t.name) Profiler.stop('compact') else: LOG.info("Writing JS to '%s'" % options.output) Profiler.start('write_out') try: with open(options.output, 'wb') as f: f.write(rendered_js) LOG.info("Succeeded") except IOError: raise ToolsException("failed to write file: %s" % options.output) Profiler.stop('write_out') return 0
def tzjs_generate(env, options, input_js): # The set of files to be injected Profiler.start('find_inject_code') inject_js = inject_js_from_options(options) Profiler.stop('find_inject_code') if 0 < len(inject_js): LOG.info("Files to inject:") for i in inject_js: LOG.info(" - '%s'", i) # Create a context and render the template Profiler.start('load_templates') context = context_from_options(options, input_js[0]) templates_js = env_load_templates(env, input_js) Profiler.stop('load_templates') Profiler.start('render_js') (rendered_js, inc_js) = render_js(context, options, templates_js, inject_js) rendered_js = rendered_js.encode('utf-8') Profiler.stop('render_js') if 0 != len(inc_js): raise ToolsException("internal error") # If required, remove all calls to 'debug.*' methods BEFORE # compacting # TODO: We write and read the files too many times. Better to # write once to a temporary, keep track of the name and invoke # each external command on files, creating subsequent temporaries # as required. if options.stripdebug: strip_path = "strip-debug" if options.stripdebugpath: strip_path = normpath(abspath(options.stripdebugpath)) LOG.info("Stripping debug method calls ...") # Check we can actually run strip debug, with the given path p = subprocess.Popen('%s -h' % strip_path, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) p.communicate() if p.returncode != 0: raise ToolsException( \ "\n\tstrip-debug tool could not be found, check it's on your path\n" "\tor supply the path with --strip-debug <path>. To run maketzjs\n" "\twithout stripping debug code run with --no-strip-debug." ) Profiler.start('strip_debug') strip_debug_flags = "-Ddebug=false" # Add the default flags first, in case the custom flags # override them. if options.verbose: strip_debug_flags += " -v" for s in options.stripnamespaces: strip_debug_flags += " --namespace %s" % s for v in options.stripvars: strip_debug_flags += " -D %s" % v if options.ignoreerrors: strip_debug_flags += " --ignore-errors" # Launch the strip command and pass in the full script via # streams. with NamedTemporaryFile(delete = False) as t: LOG.info("Writing temp JS to '%s'", t.name) t.write(rendered_js) with NamedTemporaryFile(delete = False) as tmp_out: pass strip_cmd = "%s %s -o %s %s" % (strip_path, strip_debug_flags, tmp_out.name, t.name) LOG.info("Strip cmd: %s", strip_cmd) strip_retval = subprocess.call(strip_cmd, shell=True) if 0 != strip_retval: raise ToolsException( \ "strip-debug tool exited with code %d\n" "The (merged) input probably contains a syntax error:\n" " %s" % (strip_retval, t.name)) rendered_js = read_file_utf8(tmp_out.name).encode('utf-8') remove(tmp_out.name) remove(t.name) Profiler.stop('strip_debug') # If required, compact the JS via a temporary file, otherwise just # write out directly to the output file. if options.mode != 'webworker-debug' and (options.yui or options.closure or options.uglifyjs): Profiler.start('compact') with NamedTemporaryFile(delete = False) as t: LOG.info("Writing temp JS to '%s'", t.name) t.write(rendered_js) LOG.info("Compacting temp JS to '%s'", options.output) tzjs_compact(options, t.name, options.output) remove(t.name) Profiler.stop('compact') else: LOG.info("Writing JS to '%s'", options.output) Profiler.start('write_out') try: with open(options.output, 'wb') as f: f.write(rendered_js) LOG.info("Succeeded") except IOError: raise ToolsException("failed to write file: %s" % options.output) Profiler.stop('write_out') return 0
def html_generate(env, options, input_js, input_html): """ Generate html based on the templates and build mode. """ # - dev, canvas_dev: # render top-level js files into a temporary file # collect the .js files that need to be included # setup includes, startup code and the js render result into variables # render html template # # - release, canvas: # need to know name of output js file # setup startup code to point to .tzjs or .js file # render html template # Load templates (using default html template if not specified) Profiler.start('load_templates') template_html = load_html_template(env, input_html) if template_html is None: LOG.error("failed to load file %s from template dirs", input_html[0]) exit(1) # Get context if len(input_js) > 0: title = input_js[0] elif options.codefile: title = options.codefile elif len(input_html) > 0: title = input_html[0] else: title = "Unknown" title = splitext(basename(title))[0] context = context_from_options(options, title) Profiler.stop('load_templates') Profiler.start('code_gen') # In development modes, render the JS code that needs embedding rendered_js = "" inc_js = [] if options.mode in ['plugin-debug', 'canvas-debug']: inject_js = inject_js_from_options(options) Profiler.start('load_js_templates') templates_js = env_load_templates(env, input_js) Profiler.stop('load_js_templates') (rendered_js, inc_js) = render_js(context, options, templates_js, inject_js) # Add the HTML and JS code into the tz_* variables default_add_code(options, context, rendered_js, inc_js) Profiler.stop('code_gen') Profiler.start('html_render') # Render the template and write it out try: res = template_html.render(context) except Exception, e: raise ToolsException("Error in '%s': %s %s" \ % (input_html, e.__class__.__name__, str(e)))
def tzjs_generate(env, options, input_js): # The set of files to be injected Profiler.start('find_inject_code') inject_js = inject_js_from_options(options) Profiler.stop('find_inject_code') if 0 < len(inject_js): LOG.info("Files to inject:") for i in inject_js: LOG.info(" - '%s'", i) # Create a context and render the template Profiler.start('load_templates') context = context_from_options(options, input_js[0]) templates_js = env_load_templates(env, input_js) Profiler.stop('load_templates') Profiler.start('render_js') (rendered_js, inc_js) = render_js(context, options, templates_js, inject_js) rendered_js = rendered_js.encode('utf-8') Profiler.stop('render_js') if 0 != len(inc_js): raise ToolsException("internal error") # If required, remove all calls to 'debug.*' methods BEFORE # compacting # TODO: We write and read the files too many times. Better to # write once to a temporary, keep track of the name and invoke # each external command on files, creating subsequent temporaries # as required. if options.stripdebug: strip_path = "strip-debug" if options.stripdebugpath: strip_path = normpath(abspath(options.stripdebugpath)) LOG.info("Stripping debug method calls ...") # Check we can actually run strip debug, with the given path p = subprocess.Popen('%s -h' % strip_path, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) p.communicate() if p.returncode != 0: raise ToolsException( \ "\n\tstrip-debug tool could not be found, check it's on your path\n" "\tor supply the path with --strip-debug <path>. To run maketzjs\n" "\twithout stripping debug code run with --no-strip-debug." ) Profiler.start('strip_debug') strip_debug_flags = "-Ddebug=false" # Add the default flags first, in case the custom flags # override them. if options.verbose: strip_debug_flags += " -v" for s in options.stripnamespaces: strip_debug_flags += " --namespace %s" % s for v in options.stripvars: strip_debug_flags += " -D %s" % v if options.ignoreerrors: strip_debug_flags += " --ignore-errors" # Launch the strip command and pass in the full script via # streams. with NamedTemporaryFile(delete=False) as t: LOG.info("Writing temp JS to '%s'", t.name) t.write(rendered_js) with NamedTemporaryFile(delete=False) as tmp_out: pass strip_cmd = "%s %s -o %s %s" % (strip_path, strip_debug_flags, tmp_out.name, t.name) LOG.info("Strip cmd: %s", strip_cmd) strip_retval = subprocess.call(strip_cmd, shell=True) if 0 != strip_retval: raise ToolsException( \ "strip-debug tool exited with code %d\n" "The (merged) input probably contains a syntax error:\n" " %s" % (strip_retval, t.name)) rendered_js = read_file_utf8(tmp_out.name).encode('utf-8') remove(tmp_out.name) remove(t.name) Profiler.stop('strip_debug') # If required, compact the JS via a temporary file, otherwise just # write out directly to the output file. if options.mode != 'webworker-debug' and (options.yui or options.closure or options.uglifyjs): Profiler.start('compact') with NamedTemporaryFile(delete=False) as t: LOG.info("Writing temp JS to '%s'", t.name) t.write(rendered_js) LOG.info("Compacting temp JS to '%s'", options.output) tzjs_compact(options, t.name, options.output) remove(t.name) Profiler.stop('compact') else: LOG.info("Writing JS to '%s'", options.output) Profiler.start('write_out') try: with open(options.output, 'wb') as f: f.write(rendered_js) LOG.info("Succeeded") except IOError: raise ToolsException("failed to write file: %s" % options.output) Profiler.stop('write_out') return 0