diff options
author | Raphael <raphael@google.com> | 2012-03-06 12:24:54 -0800 |
---|---|---|
committer | Raphael <raphael@google.com> | 2012-03-06 14:12:19 -0800 |
commit | e68e166503ab73e636500112311de611b58c6637 (patch) | |
tree | 72d5efd7f161ed8738b51d1410ae55eb2ab0af55 /lint | |
parent | 3bd2a615de043eb86648671f29376f32230701a2 (diff) | |
download | sdk-e68e166503ab73e636500112311de611b58c6637.tar.gz sdk-e68e166503ab73e636500112311de611b58c6637.tar.bz2 sdk-e68e166503ab73e636500112311de611b58c6637.zip |
Change lint error codes + Windows path fixes.
- Make returns codes be positive (1..5).
- Explain return codes in the usage help.
- Support either env var or system properties for bindir.
- Add support for a workdir property/envvar that is set by
lint.bat with the original invocation directory.
- Use workdir to solve relative input/output argument paths
(on windows we cd to the lint.bat location and record the
original path in workdir.)
Change-Id: I8b2583de761ce9c4ebba250460de6c60f1ea6a84
Diffstat (limited to 'lint')
-rwxr-xr-x | lint/cli/etc/lint.bat | 5 | ||||
-rw-r--r-- | lint/cli/src/com/android/tools/lint/Main.java | 143 | ||||
-rw-r--r-- | lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java | 33 |
3 files changed, 147 insertions, 34 deletions
diff --git a/lint/cli/etc/lint.bat b/lint/cli/etc/lint.bat index 41e52d8ee..1189ef110 100755 --- a/lint/cli/etc/lint.bat +++ b/lint/cli/etc/lint.bat @@ -20,6 +20,9 @@ rem Set up prog to be the path of this script, including following symlinks, rem and set up progdir to be the fully-qualified pathname of its directory. set prog=%~f0 +rem Grab current directory before we change it +set work_dir="%cd%" + rem Change current directory and drive to where the script is, to avoid rem issues with directories containing whitespaces. cd /d %~dp0 @@ -51,5 +54,5 @@ if debug NEQ "%1" goto NoDebug set jarpath=%frameworkdir%%jarfile% set javaextdirs=%frameworkdir% -call %java_exe% %java_debug% -Dcom.android.tools.lint.bindir=%prog_dir% -classpath "%jarpath%" com.android.tools.lint.Main %* +call %java_exe% %java_debug% -Dcom.android.tools.lint.bindir=%prog_dir% -Dcom.android.tools.lint.workdir=%work_dir% -classpath "%jarpath%" com.android.tools.lint.Main %* diff --git a/lint/cli/src/com/android/tools/lint/Main.java b/lint/cli/src/com/android/tools/lint/Main.java index 6cf28638e..1f2191ba3 100644 --- a/lint/cli/src/com/android/tools/lint/Main.java +++ b/lint/cli/src/com/android/tools/lint/Main.java @@ -21,6 +21,7 @@ import static com.android.tools.lint.client.api.IssueRegistry.PARSER_ERROR; import static com.android.tools.lint.detector.api.LintConstants.DOT_XML; import static com.android.tools.lint.detector.api.LintUtils.endsWith; +import com.android.annotations.Nullable; import com.android.tools.lint.checks.BuiltinIssueRegistry; import com.android.tools.lint.client.api.Configuration; import com.android.tools.lint.client.api.DefaultConfiguration; @@ -86,11 +87,13 @@ public class Main extends LintClient { private static final String VALUE_NONE = "none"; //$NON-NLS-1$ - private static final int ERRNO_ERRORS = -1; - private static final int ERRNO_USAGE = -2; - private static final int ERRNO_EXISTS = -3; - private static final int ERRNO_HELP = -4; - private static final int ERRNO_INVALIDARGS = -5; + private static final String PROP_WORK_DIR = "com.android.tools.lint.workdir"; //$NON-NLS-1$ + + private static final int ERRNO_ERRORS = 1; + private static final int ERRNO_USAGE = 2; + private static final int ERRNO_EXISTS = 3; + private static final int ERRNO_HELP = 4; + private static final int ERRNO_INVALIDARGS = 5; private List<Warning> mWarnings = new ArrayList<Warning>(); private Set<String> mSuppress = new HashSet<String>(); @@ -245,8 +248,7 @@ public class Main extends LintClient { System.err.println("Missing XML configuration file argument"); System.exit(ERRNO_INVALIDARGS); } - String filename = args[++index]; - File file = new File(filename); + File file = getInArgumentPath(args[++index]); if (!file.exists()) { System.err.println(file.getAbsolutePath() + " does not exist"); System.exit(ERRNO_INVALIDARGS); @@ -257,7 +259,7 @@ public class Main extends LintClient { System.err.println("Missing HTML output file name"); System.exit(ERRNO_INVALIDARGS); } - File output = new File(args[++index]); + File output = getOutArgumentPath(args[++index]); // Get an absolute path such that we can ask its parent directory for // write permission etc. output = output.getAbsoluteFile(); @@ -309,7 +311,7 @@ public class Main extends LintClient { System.err.println("Missing XML output file name"); System.exit(ERRNO_INVALIDARGS); } - File output = new File(args[++index]); + File output = getOutArgumentPath(args[++index]); if (output.exists()) { boolean delete = output.delete(); if (!delete) { @@ -416,7 +418,8 @@ public class Main extends LintClient { System.exit(ERRNO_INVALIDARGS); } else { String filename = arg; - File file = new File(filename); + File file = getInArgumentPath(filename); + if (!file.exists()) { System.err.println(String.format("%1$s does not exist.", filename)); System.exit(ERRNO_EXISTS); @@ -489,6 +492,81 @@ public class Main extends LintClient { System.exit(mFatal ? ERRNO_ERRORS : 0); } + /** + * Converts a relative or absolute command-line argument into an input file. + * + * @param filename The filename given as a command-line argument. + * @return A File matching filename, either absolute or relative to lint.workdir if defined. + */ + private File getInArgumentPath(String filename) { + File file = new File(filename); + + if (!file.isAbsolute()) { + File workDir = getLintWorkDir(); + if (workDir != null) { + File file2 = new File(workDir, filename); + if (file2.exists()) { + try { + file = file2.getCanonicalFile(); + } catch (IOException e) { + file = file2; + } + } + } + } + return file; + } + + /** + * Converts a relative or absolute command-line argument into an output file. + * <p/> + * The difference with {@code getInArgumentPath} is that we can't check whether the + * a relative path turned into an absolute compared to lint.workdir actually exists. + * + * @param filename The filename given as a command-line argument. + * @return A File matching filename, either absolute or relative to lint.workdir if defined. + */ + private File getOutArgumentPath(String filename) { + File file = new File(filename); + + if (!file.isAbsolute()) { + File workDir = getLintWorkDir(); + if (workDir != null) { + File file2 = new File(workDir, filename); + try { + file = file2.getCanonicalFile(); + } catch (IOException e) { + file = file2; + } + } + } + return file; + } + + + /** + * Returns the File corresponding to the system property or the environment variable + * for {@link #PROP_WORK_DIR}. + * This property is typically set by the SDK/tools/lint[.bat] wrapper. + * It denotes the path where the command-line client was originally invoked from + * and can be used to convert relative input/output paths. + * + * @return A new File corresponding to {@link #PROP_WORK_DIR} or null. + */ + @Nullable + private File getLintWorkDir() { + // First check the Java properties (e.g. set using "java -jar ... -Dname=value") + String path = System.getProperty(PROP_WORK_DIR); + if (path == null || path.length() == 0) { + // If not found, check environment variables. + path = System.getenv(PROP_WORK_DIR); + } + if (path != null && path.length() > 0) { + return new File(path); + } + return null; + } + private void printHelpTopicSuppress() { System.out.println(wrap(getSuppressHelp())); } @@ -556,26 +634,24 @@ public class Main extends LintClient { } private void printVersion() { - String binDir = System.getProperty("com.android.tools.lint.bindir"); //$NON-NLS-1$ - if (binDir != null && binDir.length() > 0) { - File file = new File(binDir, "source.properties"); //$NON-NLS-1$ - if (file.exists()) { - FileInputStream input = null; - try { - input = new FileInputStream(file); - Properties properties = new Properties(); - properties.load(input); - - String revision = properties.getProperty("Pkg.Revision"); //$NON-NLS-1$ - if (revision != null && revision.length() > 0) { - System.out.println(String.format("lint: version %1$s", revision)); - return; - } - } catch (IOException e) { - // Couldn't find or read the version info: just print out unknown below - } finally { - Closeables.closeQuietly(input); + File file = findResource("tools" + File.separator + //$NON-NLS-1$ + "source.properties"); //$NON-NLS-1$ + if (file.exists()) { + FileInputStream input = null; + try { + input = new FileInputStream(file); + Properties properties = new Properties(); + properties.load(input); + + String revision = properties.getProperty("Pkg.Revision"); //$NON-NLS-1$ + if (revision != null && revision.length() > 0) { + System.out.println(String.format("lint: version %1$s", revision)); + return; } + } catch (IOException e) { + // Couldn't find or read the version info: just print out unknown below + } finally { + Closeables.closeQuietly(input); } } @@ -765,6 +841,15 @@ public class Main extends LintClient { ARG_SIMPLEHTML + " <filename>", "Create a simple HTML report", ARG_XML + " <filename>", "Create an XML report instead.", }); + + out.println("\nExit Status:"); + printUsage(out, new String[] { + Integer.toString(ERRNO_ERRORS), "Lint errors detected.", + Integer.toString(ERRNO_USAGE), "Lint usage.", + Integer.toString(ERRNO_EXISTS), "Cannot clobber existing file.", + Integer.toString(ERRNO_HELP), "Lint help.", + Integer.toString(ERRNO_INVALIDARGS), "Invalid command-line argument.", + }); } private static void printUsage(PrintStream out, String[] args) { diff --git a/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java b/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java index 0e47ddebe..8d6367bc5 100644 --- a/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java +++ b/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java @@ -53,6 +53,9 @@ import javax.xml.parsers.DocumentBuilderFactory; */ @Beta public abstract class LintClient { + + private static final String PROP_BIN_DIR = "com.android.tools.lint.bindir"; //$NON-NLS-1$ + /** * Returns a configuration for use by the given project. The configuration * provides information about which issues are enabled, any customizations @@ -235,6 +238,28 @@ public abstract class LintClient { } /** + * Returns the File corresponding to the system property or the environment variable + * for {@link #PROP_BIN_DIR}. + * This property is typically set by the SDK/tools/lint[.bat] wrapper. + * It denotes the path of the wrapper on disk. + * + * @return A new File corresponding to {@link LintClient#PROP_BIN_DIR} or null. + */ + @Nullable + private File getLintBinDir() { + // First check the Java properties (e.g. set using "java -jar ... -Dname=value") + String path = System.getProperty(PROP_BIN_DIR); + if (path == null || path.length() == 0) { + // If not found, check environment variables. + path = System.getenv(PROP_BIN_DIR); + } + if (path != null && path.length() > 0) { + return new File(path); + } + return null; + } + + /** * Locates an SDK resource (relative to the SDK root directory). * <p> * TODO: Consider switching to a {@link URL} return type instead. @@ -246,13 +271,13 @@ public abstract class LintClient { */ @Nullable public File findResource(@NonNull String relativePath) { - String path = System.getProperty("com.android.tools.lint.bindir"); //$NON-NLS-1$ - if (path == null) { + File dir = getLintBinDir(); + if (dir == null) { throw new IllegalArgumentException("Lint must be invoked with the System property " - + "com.android.tools.lint.bindir pointing to the ANDROID_SDK tools directory"); + + PROP_BIN_DIR + " pointing to the ANDROID_SDK tools directory"); } - File top = new File(path).getParentFile(); + File top = dir.getParentFile(); File file = new File(top, relativePath); if (file.exists()) { return file; |