Пример #1
0
def ReadSources(options, args):
    '''Reads all source specified on the command line, including sources
  included by --root options.
  '''
    def EnsureSourceLoaded(in_path, sources, path_rewriter):
        if in_path not in sources:
            out_path = path_rewriter.RewritePath(in_path)
            sources[in_path] = SourceWithPaths(source.GetFileContents(in_path),
                                               in_path, out_path)

    # Only read the actual source file if we will do a dependency analysis or
    # if we'll need it for the output.
    need_source_text = (len(options.roots) > 0
                        or options.mode in ('bundle', 'compressed_bundle'))
    path_rewriter = PathRewriter(options.prefix_map)
    sources = {}
    for root in options.roots:
        for name in treescan.ScanTreeForJsFiles(root):
            EnsureSourceLoaded(name, sources, path_rewriter)
    for path in args:
        if need_source_text:
            EnsureSourceLoaded(path, sources, path_rewriter)
        else:
            # Just add an empty representation of the source.
            sources[path] = SourceWithPaths('', path,
                                            path_rewriter.RewritePath(path))
    return sources
Пример #2
0
def ReadSources(roots=[],
                source_files=[],
                need_source_text=False,
                path_rewriter=PathRewriter(),
                exclude=[]):
    '''Reads all source specified on the command line, including sources
  included by --root options.
  '''
    def EnsureSourceLoaded(in_path, sources):
        if in_path not in sources:
            out_path = path_rewriter.RewritePath(in_path)
            sources[in_path] = SourceWithPaths(source.GetFileContents(in_path),
                                               in_path, out_path)

    # Only read the actual source file if we will do a dependency analysis or
    # the caller asks for it.
    need_source_text = need_source_text or len(roots) > 0
    sources = {}
    for root in roots:
        for name in treescan.ScanTreeForJsFiles(root):
            if any((r.search(name) for r in exclude)):
                continue
            EnsureSourceLoaded(name, sources)
    for path in source_files:
        if need_source_text:
            EnsureSourceLoaded(path, sources)
        else:
            # Just add an empty representation of the source.
            sources[path] = SourceWithPaths('', path,
                                            path_rewriter.RewritePath(path))
    return sources
Пример #3
0
def _GetRelativePathToSourceDict(root, prefix=''):
    """Scans a top root directory for .js sources.

  Args:
    root: str, Root directory.
    prefix: str, Prefix for returned paths.

  Returns:
    dict, A map of relative paths (with prefix, if given), to source.Source
      objects.
  """
    # Remember and restore the cwd when we're done. We work from the root so
    # that paths are relative from the root.
    start_wd = os.getcwd()
    os.chdir(root)

    path_to_source = {}
    for path in treescan.ScanTreeForJsFiles('.'):
        prefixed_path = _NormalizePathSeparators(os.path.join(prefix, path))
        path_to_source[prefixed_path] = source.Source(
            source.GetFileContents(path))

    os.chdir(start_wd)

    return path_to_source
Пример #4
0
def getSources(options_roots, args):
    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))
    return sources
Пример #5
0
def main():
    #    for pathJs in treescan.ScanTreeForJsFiles("E:/workspace/tools/closure2amd/test"):
    #        toAmd(pathJs)

    options, args = _GetOptionsParser().parse_args()
    filePath = options.filePath

    print "processing..."

    count = 0
    if os.path.isdir(filePath):
        for pathJs in treescan.ScanTreeForJsFiles(filePath):
            if toAmd(pathJs):
                count += 1
    else:
        toAmd(filePath)
        count += 1

    print "closure modules transfer to amd module : " + str(count)
Пример #6
0
  def run( self, options=None ):
    '''
    Performs the action.
    '''

    # we need to import some closure python classes here
    sys.path.append( config.CLOSURELIBRARY_PYTHON_PATH )
    import treescan

    # scan for .js files
    jsFilesGenerator = treescan.ScanTreeForJsFiles( config.SOFTWARE_PATH )

    # list of final .js files to compile    
    jsFiles = []

    # apply ignores
    for j in jsFilesGenerator:

      ignore = False

      for e in config.EXCLUDES_PATH:
        if j.find( e ) != -1:
          # ignore this guy
          ignore = True

      if options and options[0] == 'USE_INCLUDES':
        for i in config.INCLUDES_PATH:
          if j.find( i ) != -1:
            # force inclusion for this guy
            ignore = False

      if not ignore:
        # add this guy to the valid files
        jsFiles.append( j )

    # return filtered list
    return jsFiles
Пример #7
0
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)
Пример #8
0
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)
Пример #9
0
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)
Пример #10
0
def main():
    parser = optparse.OptionParser(description=__doc__)
    parser.usage = '%prog <tree_root>...'
    _, args = parser.parse_args()
    for root in args:
        print '\n'.join(treescan.ScanTreeForJsFiles(root))
Пример #11
0
    os.path.join(xtkDir, 'lib/closure-library/compiler-latest/compiler.jar'))
closureBuilder = os.path.normpath(
    os.path.join(xtkDir,
                 'lib/closure-library/closure/bin/build/closurebuilder.py'))

# grab the closure python utilities
sys.path.append(xtkDir + '/lib/closure-library/closure/bin/build')
import treescan
import jscompiler
import depstree

# ignore these files
excludePaths = ['lib', 'testing', 'deps']

# scan for .js files
jsFilesGenerator = treescan.ScanTreeForJsFiles(xtkDir)

# remove file if exists
if os.path.exists('temp_build.log'): os.remove('temp_build.log')
# start compilation time
start = time.clock()

# list of final .js files to compile
jsFiles = []

# apply ignores
for j in jsFilesGenerator:

    ignore = False

    for e in excludePaths: