Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue Jar tool source code #17514

Merged
merged 4 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 3rdparty/jvm/args4j/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

jvm_artifact(
group="args4j",
artifact="args4j",
version="2.33",
packages=["org.kohsuke.args4j.**"],
resolve="jar_tool_dev",
)
11 changes: 11 additions & 0 deletions 3rdparty/jvm/com/google/code/findbugs/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

jvm_artifact(
name="jsr305",
group="com.google.code.findbugs",
artifact="jsr305",
version="3.0.2",
packages=["javax.annotation.**"],
resolve="jar_tool_dev",
)
10 changes: 10 additions & 0 deletions 3rdparty/jvm/com/google/guava/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

jvm_artifact(
group="com.google.guava",
artifact="guava",
version="18.0",
packages=["com.google.common.**"],
resolve="jar_tool_dev",
)
1 change: 1 addition & 0 deletions pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ java_parser_dev = "src/python/pants/backend/java/dependency_inference/java_parse
# in `generate_scala_parser_lockfile_request`.
scala_parser_dev = "src/python/pants/backend/scala/dependency_inference/scala_parser.lock"
strip_jar_dev = "src/python/pants/jvm/strip_jar/strip_jar.lock"
jar_tool_dev = "src/python/pants/jvm/jar_tool/jar_tool.lock"

[scala]
version_for_resolve = { "scala_parser_dev" = "2.13.8" }
Expand Down
68 changes: 68 additions & 0 deletions src/java/org/pantsbuild/args4j/ArgfileOptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package org.pantsbuild.args4j;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;

/**
* An {@code OptionHandler} that can read an option value from an "argfile". An argfile is just a
* file containing an option's value as its contents. These can be useful to avoid command-line
* argument length limits. An argfile is specified on the command-line by prefixing the option
* argument value with a {@code @}, like so:
*
* <pre>-potentially-long-argument=@/path/to/argfile</pre>
*
* The path can be relative in which case it is taken as relative to the working directory of the
* associated java invocation.
*
* @param <T> The type of the underlying option value.
*/
public abstract class ArgfileOptionHandler<T> extends OptionHandler<T> {
private final OptionHandler<T> delegate;

/** @param delegate The {@code OptionHandler} to delegate final value parsing to. */
protected ArgfileOptionHandler(OptionHandler<T> delegate) {
super(delegate.owner, delegate.option, delegate.setter);
this.delegate = delegate;
}

@Override
public int parseArguments(final Parameters params) throws CmdLineException {
return delegate.parseArguments(
new Parameters() {
@Override
public String getParameter(int idx) throws CmdLineException {
String value = params.getParameter(idx);
if (!value.startsWith("@")) {
return value;
}

try {
return Files.toString(new File(value.substring(1)), Charsets.UTF_8).trim();
} catch (IOException e) {
throw new InvalidCmdLineArgumentException(
delegate.option,
value,
String.format("Failed to read argfile: %s", e.getMessage()));
}
}

@Override
public int size() {
return params.size();
}
});
}

@Override
public String getDefaultMetaVariable() {
return delegate.getDefaultMetaVariable();
}
}
6 changes: 6 additions & 0 deletions src/java/org/pantsbuild/args4j/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

java_sources(resolve="jar_tool_dev")

files(name="src", sources=["*.java"])
66 changes: 66 additions & 0 deletions src/java/org/pantsbuild/args4j/BooleanOptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package org.pantsbuild.args4j;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;

/**
* An {@code OptionHandler} that parses boolean values but can also set them from presence alone.
*
* <p>The {@link org.kohsuke.args4j.spi.BooleanOptionHandler built-in args4j boolean parser} takes
* one argument which is parsed to a {@code boolean} option value. This handler can similarly take a
* value; although, the value is parsed as per {@link Boolean#parseBoolean(String)}. In addition
* this handler will treat no value as {@code true}. For example, the following all parse to {@code
* true}:
*
* <ul>
* <li>{@code -verbose}
* <li>{@code -verbose=true}
* <li>{@code -verbose=TRUE}
* </ul>
*
* And the following all parse to {@code false}:
*
* <ul>
* <li>{@code -verbose=false}
* <li>{@code -verbose=FALSE}
* </ul>
*
* Be careful! These are false (as per {@link Boolean#parseBoolean(String)}) too:
*
* <ul>
* <li>{@code -verbose=no}
* <li>{@code -verbose=yes}
* <li>{@code -verbose=0}
* <li>{@code -verbose=1}
* </ul>
*/
public class BooleanOptionHandler extends OptionHandler<Boolean> {

public BooleanOptionHandler(
CmdLineParser parser, OptionDef option, Setter<? super Boolean> setter) {
super(parser, option, setter);
}

@Override
public int parseArguments(Parameters params) throws CmdLineException {
if (params.size() == 0) {
setter.addValue(true);
return 0;
} else {
setter.addValue(Boolean.parseBoolean(params.getParameter(0)));
return 1;
}
}

@Override
public String getDefaultMetaVariable() {
return "BOOL";
}
}
82 changes: 82 additions & 0 deletions src/java/org/pantsbuild/args4j/CollectionOptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package org.pantsbuild.args4j;

import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.DelimitedOptionHandler;
import org.kohsuke.args4j.spi.OneArgumentOptionHandler;
import org.kohsuke.args4j.spi.Setter;

/**
* An {@code OptionHandler} that accepts a list of values to add to an underlying collection option
* value.
*
* <p>This handler interprets the raw option value as a comma-delimited list and passes the
* individual list values to a delegate {@link ItemParser} strategy to obtain collection members
* from.
*
* @param <T> The type of the underlying option values stored in the container.
*/
public class CollectionOptionHandler<T> extends DelimitedOptionHandler<T> {

/**
* A strategy for parsing an individual option value from a {@link String}.
*
* @param <T> The type of the underlying option value.
*/
public interface ItemParser<T> {

/** Parses {@link String Strings} to themselves. */
ItemParser<String> IDENTITY =
new ItemParser<String>() {
@Override
public String parse(String item) {
return item;
}
};

/**
* Converts an individual item from an option list into an instance of {@code T}.
*
* @param item An individual raw value to parse.
* @return The parsed value.
*/
T parse(String item);
}

/**
* @param parser The parser being used to parse this option.
* @param option Metadata describing the option being parsed.
* @param setter A helper that can add parsed values to the underlying collection option value.
* @param defaultMetaVariable The default meta variable to use to describe the individual items in
* the collection.
* @param itemParser The strategy for parsing individual option values with to populate the
* underlying collection option value with.
*/
public CollectionOptionHandler(
CmdLineParser parser,
OptionDef option,
Setter<? super T> setter,
final String defaultMetaVariable,
final ItemParser<T> itemParser) {

super(
parser,
option,
setter,
",",
new OneArgumentOptionHandler<T>(parser, option, setter) {
@Override
protected T parse(String argument) {
return itemParser.parse(argument);
}

@Override
public String getDefaultMetaVariable() {
return defaultMetaVariable;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package org.pantsbuild.args4j;

import javax.annotation.Nullable;
import org.kohsuke.args4j.OptionDef;

/**
* Indicates a problem parsing a command line argument.
*
* <p>Although args4j provides {@link org.kohsuke.args4j.CmdLineException} it is difficult to
* construct one. As such, this exception is useful for implementing {@link
* org.kohsuke.args4j.spi.OptionHandler OptionHandlers} that detect and reject malformed values.
*/
public class InvalidCmdLineArgumentException extends RuntimeException {

/**
* @param optionDef The {@code OptionDef} describing the option being parsed.
* @param optionValue The raw value of the option being parsed.
* @param message A message describing how the {@code optionValue} is invalid.
*/
public InvalidCmdLineArgumentException(
OptionDef optionDef, @Nullable Object optionValue, String message) {

super(
String.format(
"Invalid option value '%s' for the option with usage '%s': %s",
optionValue, optionDef.usage(), message));
}

/**
* @param optionName The name of the option being parsed.
* @param optionValue The raw value of the option being parsed.
* @param message A message describing how the {@code optionValue} is invalid.
*/
public InvalidCmdLineArgumentException(
String optionName, @Nullable Object optionValue, String message) {

super(
String.format(
"Invalid option value '%s' for option '%s': %s", optionValue, optionName, message));
}
}
Loading