build.py -c closure: Show file name and line number of errors and warnings.

This commit is contained in:
Xavier Mamano
2011-12-05 20:11:00 +01:00
parent acaf67b01c
commit 7169eba788
4 changed files with 117 additions and 32 deletions

View File

@@ -1,25 +0,0 @@
import sys
import os
import tempfile
path = "../tools/closure-compiler.jar"
if not os.path.exists(path):
raise Exception("No closure-compiler.jar at %s; read README.txt!" % path)
def minimize(code):
ntf = tempfile.NamedTemporaryFile()
ntf.close()
open(ntf.name, "w").write(code)
ntf2 = tempfile.NamedTemporaryFile()
ntf2.close()
os.system(("java -jar %s --js %s --js_output_file %s" +
" --externs closure-compiler/Externs.js" +
" --jscomp_warning checkVars" +
" --jscomp_error checkRegExp" +
" --jscomp_error undefinedVars") % (path, ntf.name, ntf2.name))
data = open(ntf2.name).read()
os.unlink(ntf.name)
os.unlink(ntf2.name)
return data

View File

@@ -0,0 +1,71 @@
# Copyright 2010 The Closure Library Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utility to use the Closure Compiler CLI from Python."""
import distutils.version
import logging
import re
import subprocess
# Pulls a version number from the first line of 'java -version'
# See http://java.sun.com/j2se/versioning_naming.html to learn more about the
# command's output format.
_VERSION_REGEX = re.compile('"([0-9][.0-9]*)')
def _GetJavaVersion():
"""Returns the string for the current version of Java installed."""
proc = subprocess.Popen(['java', '-version'], stderr=subprocess.PIPE)
unused_stdoutdata, stderrdata = proc.communicate()
version_line = stderrdata.splitlines()[0]
return _VERSION_REGEX.search(version_line).group(1)
def Compile(compiler_jar_path, source_paths, flags=None):
"""Prepares command-line call to Closure Compiler.
Args:
compiler_jar_path: Path to the Closure compiler .jar file.
source_paths: Source paths to build, in order.
flags: A list of additional flags to pass on to Closure Compiler.
Returns:
The compiled source, as a string, or None if compilation failed.
"""
# User friendly version check.
if not (distutils.version.LooseVersion(_GetJavaVersion()) >=
distutils.version.LooseVersion('1.6')):
logging.error('Closure Compiler requires Java 1.6 or higher. '
'Please visit http://www.java.com/getjava')
return
args = ['java', '-jar', compiler_jar_path]
for path in source_paths:
args += ['--js', path]
if flags:
args += flags
logging.info('Compiling with the following command: %s', ' '.join(args))
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
stdoutdata, unused_stderrdata = proc.communicate()
if proc.returncode != 0:
return
return stdoutdata

View File

@@ -139,9 +139,14 @@ def undesired(filepath, excludes):
exclude = True
break
return exclude
def getNames (sourceDirectory, configFile = None):
return run(sourceDirectory, None, configFile, True)
def run (sourceDirectory, outputFilename = None, configFile = None):
def run (sourceDirectory, outputFilename = None, configFile = None,
returnAsListOfNames = False):
cfg = None
if configFile:
cfg = Config(configFile)
@@ -219,6 +224,16 @@ def run (sourceDirectory, outputFilename = None, configFile = None):
## Output the files in the determined order
result = []
# Return as a list of filenames
if returnAsListOfNames:
for fp in order:
fName = os.path.normpath(os.path.join(sourceDirectory, fp)).replace("\\","/")
print "Append: ", fName
result.append(fName)
print "\nTotal files: %d " % len(result)
return result
# Return as merged source code
for fp in order:
f = files[fp]
print "Exporting: ", f.filepath