Refactor CommandLineBuildTargetNormalizer to use pass-from-above.
Summary:
`CommandLineBuildTargetNormalizer` is constructed lazily by and made available through
`AbstractCommandOptions`.
This diff also deletes `BuildTargetNormalizer`, which apparently was unused.
This reduces the number of `Function` objects that need to be created.
This will make it simpler to add a normalize() method that takes a
single string, which will be done in a follow-up diff that uses it.
Test Plan: Sandcastle builds.
diff --git a/src/com/facebook/buck/cli/AbstractCommandOptions.java b/src/com/facebook/buck/cli/AbstractCommandOptions.java
index e01aab6..fa5fca2 100644
--- a/src/com/facebook/buck/cli/AbstractCommandOptions.java
+++ b/src/com/facebook/buck/cli/AbstractCommandOptions.java
@@ -34,6 +34,8 @@
import java.util.Properties;
import java.util.StringTokenizer;
+import javax.annotation.Nullable;
+
public abstract class AbstractCommandOptions {
@VisibleForTesting static final String HELP_LONG_ARG = "--help";
@@ -62,6 +64,9 @@
private final BuckConfig buckConfig;
+ @Nullable // Lazily loaded via getCommandLineBuildTargetNormalizer().
+ private CommandLineBuildTargetNormalizer commandLineBuildTargetNormalizer;
+
AbstractCommandOptions(BuckConfig buckConfig) {
this.buckConfig = Preconditions.checkNotNull(buckConfig);
}
@@ -83,6 +88,13 @@
return help;
}
+ protected CommandLineBuildTargetNormalizer getCommandLineBuildTargetNormalizer() {
+ if (commandLineBuildTargetNormalizer == null) {
+ commandLineBuildTargetNormalizer = new CommandLineBuildTargetNormalizer(buckConfig);
+ }
+ return commandLineBuildTargetNormalizer;
+ }
+
/** @return androidSdkDir */
public static Optional<File> findAndroidSdkDir() {
Optional<File> androidSdkDir = findDirectoryByPropertiesThenEnvironmentVariable(
diff --git a/src/com/facebook/buck/cli/AuditCommandOptions.java b/src/com/facebook/buck/cli/AuditCommandOptions.java
index b5f38df..fdcd1b1 100644
--- a/src/com/facebook/buck/cli/AuditCommandOptions.java
+++ b/src/com/facebook/buck/cli/AuditCommandOptions.java
@@ -48,7 +48,7 @@
}
public List<String> getArgumentsFormattedAsBuildTargets() {
- return CommandLineBuildTargetNormalizer.normalizeAll(getBuckConfig(), getArguments());
+ return getCommandLineBuildTargetNormalizer().normalizeAll(getArguments());
}
public boolean shouldGenerateDotOutput() {
diff --git a/src/com/facebook/buck/cli/BuildCommandOptions.java b/src/com/facebook/buck/cli/BuildCommandOptions.java
index 488391c..ee9b29d 100644
--- a/src/com/facebook/buck/cli/BuildCommandOptions.java
+++ b/src/com/facebook/buck/cli/BuildCommandOptions.java
@@ -101,7 +101,7 @@
}
public List<String> getArgumentsFormattedAsBuildTargets() {
- return CommandLineBuildTargetNormalizer.normalizeAll(getBuckConfig(), getArguments());
+ return getCommandLineBuildTargetNormalizer().normalizeAll(getArguments());
}
public boolean isCodeCoverageEnabled() {
diff --git a/src/com/facebook/buck/cli/BuildTargetNormalizer.java b/src/com/facebook/buck/cli/BuildTargetNormalizer.java
deleted file mode 100644
index bc5e815..0000000
--- a/src/com/facebook/buck/cli/BuildTargetNormalizer.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright 2012-present Facebook, Inc.
- *
- * 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.
- */
-
-package com.facebook.buck.cli;
-
-import com.google.common.collect.Lists;
-
-import java.util.List;
-
-public class BuildTargetNormalizer {
-
- private BuildTargetNormalizer() {}
-
- public static List<String> getArgumentsFormattedAsBuildTargets(List<String> arguments) {
- return Lists.transform(arguments, CommandLineBuildTargetNormalizer.normalize);
- }
-
-}
diff --git a/src/com/facebook/buck/cli/CommandLineBuildTargetNormalizer.java b/src/com/facebook/buck/cli/CommandLineBuildTargetNormalizer.java
index f9fd284..6b4e7d0 100644
--- a/src/com/facebook/buck/cli/CommandLineBuildTargetNormalizer.java
+++ b/src/com/facebook/buck/cli/CommandLineBuildTargetNormalizer.java
@@ -16,6 +16,7 @@
package com.facebook.buck.cli;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
@@ -58,10 +59,31 @@
*/
class CommandLineBuildTargetNormalizer {
- /** Utility class: do not instantiate. */
- private CommandLineBuildTargetNormalizer() {}
+ private final Function<String, String> normalizer;
- public static String normalizeBuildTargetIdentifier(final String buildTargetFromCommandLine) {
+ CommandLineBuildTargetNormalizer(final BuckConfig buckConfig) {
+ Preconditions.checkNotNull(buckConfig);
+ this.normalizer = new Function<String, String>() {
+ @Override
+ public String apply(String arg) {
+ String aliasValue = buckConfig.getBuildTargetForAlias(arg);
+ if (aliasValue != null) {
+ return aliasValue;
+ } else {
+ return normalizeBuildTargetIdentifier(arg);
+ }
+ }
+ };
+ }
+
+ public List<String> normalizeAll(List<String> arguments) {
+ // When transforming command-line arguments, first check to see whether it is an alias in the
+ // BuckConfig. If so, return the value associated with the alias. Otherwise, try normalize().
+ return Lists.transform(arguments, normalizer);
+ }
+
+ @VisibleForTesting
+ static String normalizeBuildTargetIdentifier(final String buildTargetFromCommandLine) {
Preconditions.checkNotNull(buildTargetFromCommandLine);
// Build rules in the root are weird, but they do happen. Special-case them.
@@ -98,29 +120,4 @@
return target;
}
-
- public static List<String> normalizeAll(final BuckConfig buckConfig, List<String> arguments) {
- // When transforming command-line arguments, first check to see whether it is an alias in the
- // BuckConfig. If so, return the value associated with the alias. Otherwise, try normalize().
- return Lists.transform(arguments, new Function<String, String>() {
- @Override
- public String apply(String arg) {
- String aliasValue = buckConfig.getBuildTargetForAlias(arg);
- if (aliasValue != null) {
- return aliasValue;
- } else {
- return CommandLineBuildTargetNormalizer.normalize.apply(arg);
- }
- }
- });
- }
-
- public static final Function<String, String> normalize = new Function<String, String>() {
-
- @Override
- public String apply(String target) {
- return normalizeBuildTargetIdentifier(target);
- }
-
- };
}
diff --git a/src/com/facebook/buck/cli/UninstallCommandOptions.java b/src/com/facebook/buck/cli/UninstallCommandOptions.java
index 85cdc74..2ed7b10 100644
--- a/src/com/facebook/buck/cli/UninstallCommandOptions.java
+++ b/src/com/facebook/buck/cli/UninstallCommandOptions.java
@@ -73,6 +73,6 @@
}
public List<String> getArgumentsFormattedAsBuildTargets() {
- return CommandLineBuildTargetNormalizer.normalizeAll(getBuckConfig(), getArguments());
+ return getCommandLineBuildTargetNormalizer().normalizeAll(getArguments());
}
}