Skip to content

Commit

Permalink
Move Tool-related methods from BuckConfig to ToolConfig
Browse files Browse the repository at this point in the history
Summary: Reducing dependencies of `config:config`.

Test Plan: CI

Reviewed By: ttsugriy

fbshipit-source-id: d83daae
  • Loading branch information
Sergey Tyurin authored and facebook-github-bot committed Oct 4, 2017
1 parent 29096c8 commit 1e49235
Show file tree
Hide file tree
Showing 22 changed files with 129 additions and 58 deletions.
4 changes: 3 additions & 1 deletion src/com/facebook/buck/android/AndroidBinaryDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import com.facebook.buck.rules.macros.ExecutableMacroExpander;
import com.facebook.buck.rules.macros.LocationMacroExpander;
import com.facebook.buck.rules.macros.MacroHandler;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.MoreCollectors;
import com.facebook.buck.util.RichStream;
Expand Down Expand Up @@ -481,7 +482,8 @@ private Optional<RedexOptions> getRedexOptions(
return Optional.empty();
}

Optional<Tool> redexBinary = buckConfig.getTool(SECTION, CONFIG_PARAM_REDEX, resolver);
Optional<Tool> redexBinary =
buckConfig.getView(ToolConfig.class).getTool(SECTION, CONFIG_PARAM_REDEX, resolver);
if (!redexBinary.isPresent()) {
throw new HumanReadableException(
"Requested running ReDex for %s but the path to the tool"
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/android/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ java_immutables_library(
"//src/com/facebook/buck/rules/macros:types",
"//src/com/facebook/buck/rules/query:query",
"//src/com/facebook/buck/rules/query:types",
"//src/com/facebook/buck/rules/tool/config:config",
"//src/com/facebook/buck/shell:rules",
"//src/com/facebook/buck/shell:steps",
"//src/com/facebook/buck/step:step",
Expand Down
2 changes: 0 additions & 2 deletions src/com/facebook/buck/config/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ java_immutables_library(
"//src/com/facebook/buck/model:model",
"//src/com/facebook/buck/parser:rule_pattern",
"//src/com/facebook/buck/randomizedtrial:randomizedtrial",
"//src/com/facebook/buck/rules:build_rule",
"//src/com/facebook/buck/rules:interfaces",
"//src/com/facebook/buck/rules:relative_cell_name",
"//src/com/facebook/buck/rules:rule_key",
"//src/com/facebook/buck/rules:source_path",
"//src/com/facebook/buck/util:exceptions",
Expand Down
49 changes: 3 additions & 46 deletions src/com/facebook/buck/config/BuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@
import com.facebook.buck.parser.BuildTargetParseException;
import com.facebook.buck.parser.BuildTargetParser;
import com.facebook.buck.parser.BuildTargetPatternParser;
import com.facebook.buck.rules.BinaryBuildRuleToolProvider;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.CellPathResolver;
import com.facebook.buck.rules.ConstantToolProvider;
import com.facebook.buck.rules.DefaultBuildTargetSourcePath;
import com.facebook.buck.rules.HashedFileTool;
import com.facebook.buck.rules.PathSourcePath;
import com.facebook.buck.rules.RuleKeyDiagnosticsMode;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.Tool;
import com.facebook.buck.rules.ToolProvider;
import com.facebook.buck.util.Ansi;
import com.facebook.buck.util.AnsiEnvironmentChecking;
import com.facebook.buck.util.HumanReadableException;
Expand Down Expand Up @@ -323,7 +317,7 @@ public Optional<BuildTarget> getMaybeBuildTarget(String section, String field) {
/** @return the parsed BuildTarget in the given section and field. */
public BuildTarget getRequiredBuildTarget(String section, String field) {
Optional<BuildTarget> target = getBuildTarget(section, field);
return required(section, field, target);
return getOrThrow(section, field, target);
}

public <T extends Enum<T>> Optional<T> getEnum(String section, String field, Class<T> clazz) {
Expand Down Expand Up @@ -365,43 +359,6 @@ public SourcePath getSourcePath(Path path) {
"Failed to transform Path %s to Source Path because path was not found.", path)));
}

/**
* @return a {@link Tool} identified by a @{link BuildTarget} or {@link Path} reference by the
* given section:field, if set.
*/
public Optional<ToolProvider> getToolProvider(String section, String field) {
Optional<String> value = getValue(section, field);
if (!value.isPresent()) {
return Optional.empty();
}
Optional<BuildTarget> target = getMaybeBuildTarget(section, field);
if (target.isPresent()) {
return Optional.of(
new BinaryBuildRuleToolProvider(target.get(), String.format("[%s] %s", section, field)));
} else {
return Optional.of(
new ConstantToolProvider(
new HashedFileTool(
() ->
checkPathExistsAndResolve(
value.get(),
String.format("Overridden %s:%s path not found: ", section, field)))));
}
}

public Optional<Tool> getTool(String section, String field, BuildRuleResolver resolver) {
Optional<ToolProvider> provider = getToolProvider(section, field);
if (!provider.isPresent()) {
return Optional.empty();
}
return Optional.of(provider.get().resolve(resolver));
}

public Tool getRequiredTool(String section, String field, BuildRuleResolver resolver) {
Optional<Tool> path = getTool(section, field, resolver);
return required(section, field, path);
}

/**
* In a {@link BuckConfig}, an alias can either refer to a fully-qualified build target, or an
* alias defined earlier in the {@code alias} section. The mapping produced by this method
Expand Down Expand Up @@ -664,7 +621,7 @@ public ImmutableMap<String, String> getMap(String section, String field) {
return config.getMap(section, field);
}

private <T> T required(String section, String field, Optional<T> value) {
public <T> T getOrThrow(String section, String field, Optional<T> value) {
if (!value.isPresent()) {
throw new HumanReadableException(
String.format(".buckconfig: %s:%s must be set", section, field));
Expand Down Expand Up @@ -728,7 +685,7 @@ public Optional<Path> getPath(String sectionName, String name) {

public Path getRequiredPath(String section, String field) {
Optional<Path> path = getPath(section, field);
return required(section, field, path);
return getOrThrow(section, field, path);
}

public String getClientId() {
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/cxx/toolchain/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ java_immutables_library(
"//src/com/facebook/buck/rules:source_path",
"//src/com/facebook/buck/rules/coercer:coercer",
"//src/com/facebook/buck/rules/keys:keys",
"//src/com/facebook/buck/rules/tool/config:config",
"//src/com/facebook/buck/shell:steps",
"//src/com/facebook/buck/step:step",
"//src/com/facebook/buck/step/fs:fs",
Expand Down
9 changes: 6 additions & 3 deletions src/com/facebook/buck/cxx/toolchain/CxxBuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.facebook.buck.rules.BuildRuleType;
import com.facebook.buck.rules.RuleScheduleInfo;
import com.facebook.buck.rules.ToolProvider;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.facebook.buck.util.environment.Platform;
import com.facebook.buck.util.immutables.BuckStyleImmutable;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -133,7 +134,8 @@ public Optional<ImmutableList<String>> getFlags(String field) {
* Constructs the appropriate Archiver for the specified platform.
*/
public Optional<ArchiverProvider> getArchiverProvider(Platform defaultPlatform) {
Optional<ToolProvider> toolProvider = delegate.getToolProvider(cxxSection, "ar");
Optional<ToolProvider> toolProvider =
delegate.getView(ToolConfig.class).getToolProvider(cxxSection, "ar");
return toolProvider.map(
archiver -> {
Optional<Platform> archiverPlatform =
Expand Down Expand Up @@ -200,7 +202,8 @@ public Optional<CompilerProvider> getCompilerProvider(String field) {
}

public Optional<LinkerProvider> getLinkerProvider(String field, LinkerProvider.Type defaultType) {
Optional<ToolProvider> toolProvider = delegate.getToolProvider(cxxSection, field);
Optional<ToolProvider> toolProvider =
delegate.getView(ToolConfig.class).getToolProvider(cxxSection, field);
if (!toolProvider.isPresent()) {
return Optional.empty();
}
Expand Down Expand Up @@ -265,7 +268,7 @@ public int getDebugPathSanitizerLimit() {
}

public Optional<ToolProvider> getToolProvider(String name) {
return delegate.getToolProvider(cxxSection, name);
return delegate.getView(ToolConfig.class).getToolProvider(cxxSection, name);
}

public boolean isUniqueLibraryNameEnabled() {
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/go/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ java_immutables_library(
"//src/com/facebook/buck/rules/args:args",
"//src/com/facebook/buck/rules/coercer:interface",
"//src/com/facebook/buck/rules/keys:keys",
"//src/com/facebook/buck/rules/tool/config:config",
"//src/com/facebook/buck/shell:steps",
"//src/com/facebook/buck/step:step",
"//src/com/facebook/buck/step/fs:fs",
Expand Down
3 changes: 2 additions & 1 deletion src/com/facebook/buck/go/GoBuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.facebook.buck.rules.CommandTool;
import com.facebook.buck.rules.HashedFileTool;
import com.facebook.buck.rules.Tool;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.MoreCollectors;
import com.facebook.buck.util.ProcessExecutor;
Expand Down Expand Up @@ -151,7 +152,7 @@ ImmutableList<Path> getVendorPaths() {
}

Optional<Tool> getGoTestMainGenerator(BuildRuleResolver resolver) {
return delegate.getTool(SECTION, "test_main_gen", resolver);
return delegate.getView(ToolConfig.class).getTool(SECTION, "test_main_gen", resolver);
}

ImmutableList<Path> getAssemblerIncludeDirs() {
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/haskell/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ java_immutables_library(
"//src/com/facebook/buck/rules/keys:keys",
"//src/com/facebook/buck/rules/query:query",
"//src/com/facebook/buck/rules/query:types",
"//src/com/facebook/buck/rules/tool/config:config",
"//src/com/facebook/buck/shell:rules",
"//src/com/facebook/buck/shell:steps",
"//src/com/facebook/buck/step:step",
Expand Down
2 changes: 2 additions & 0 deletions src/com/facebook/buck/haskell/HaskellBuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.facebook.buck.io.ExecutableFinder;
import com.facebook.buck.rules.SystemToolProvider;
import com.facebook.buck.rules.ToolProvider;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.facebook.buck.util.RichStream;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -55,6 +56,7 @@ private Optional<ImmutableList<String>> getFlags(String section, String field) {

private ToolProvider getTool(String section, String configName, String systemName) {
return delegate
.getView(ToolConfig.class)
.getToolProvider(section, configName)
.orElseGet(
() ->
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/js/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ java_immutables_library(
"//src/com/facebook/buck/rules/keys:keys",
"//src/com/facebook/buck/rules/query:query",
"//src/com/facebook/buck/rules/query:types",
"//src/com/facebook/buck/rules/tool/config:config",
"//src/com/facebook/buck/shell:rules",
"//src/com/facebook/buck/shell:worker",
"//src/com/facebook/buck/shell:worker_tool",
Expand Down
5 changes: 4 additions & 1 deletion src/com/facebook/buck/js/ReactNativeBuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.Tool;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.facebook.buck.util.HumanReadableException;
import java.util.Optional;

Expand All @@ -40,7 +41,9 @@ public ReactNativeBuckConfig(BuckConfig delegate) {
* @return Tool for the react native javascript packager.
*/
public Tool getPackager(BuildRuleResolver resolver) {
return delegate.getRequiredTool("react-native", "packager_worker", resolver);
return delegate
.getView(ToolConfig.class)
.getRequiredTool("react-native", "packager_worker", resolver);
}

public SourcePath getPackagerSourcePath() {
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/jvm/scala/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ java_immutables_library(
"//src/com/facebook/buck/rules/args:args",
"//src/com/facebook/buck/rules/coercer:interface",
"//src/com/facebook/buck/rules/macros:macros",
"//src/com/facebook/buck/rules/tool/config:config",
"//src/com/facebook/buck/shell:steps",
"//src/com/facebook/buck/step:step",
"//src/com/facebook/buck/util:exceptions",
Expand Down
4 changes: 3 additions & 1 deletion src/com/facebook/buck/jvm/scala/ScalaBuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.buck.rules.CommandTool;
import com.facebook.buck.rules.HashedFileTool;
import com.facebook.buck.rules.Tool;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.facebook.buck.util.HumanReadableException;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -72,7 +73,8 @@ public ImmutableList<String> getCompilerFlags() {
}

private Tool findScalac(BuildRuleResolver resolver) {
Optional<Tool> configScalac = delegate.getTool(SECTION, "compiler", resolver);
Optional<Tool> configScalac =
delegate.getView(ToolConfig.class).getTool(SECTION, "compiler", resolver);
if (configScalac.isPresent()) {
return configScalac.get();
}
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/lua/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ java_immutables_library(
"//src/com/facebook/buck/rules/coercer:coercer",
"//src/com/facebook/buck/rules/keys:keys",
"//src/com/facebook/buck/rules/macros:types",
"//src/com/facebook/buck/rules/tool/config:config",
"//src/com/facebook/buck/shell:steps",
"//src/com/facebook/buck/step:step",
"//src/com/facebook/buck/step/fs:fs",
Expand Down
3 changes: 3 additions & 0 deletions src/com/facebook/buck/lua/LuaBuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.buck.io.ExecutableFinder;
import com.facebook.buck.rules.ErrorToolProvider;
import com.facebook.buck.rules.SystemToolProvider;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.facebook.buck.util.RichStream;
import com.google.common.collect.ImmutableList;
import java.nio.file.Paths;
Expand All @@ -43,6 +44,7 @@ private LuaPlatform getPlatform(String section, CxxPlatform cxxPlatform) {
return LuaPlatform.builder()
.setLua(
delegate
.getView(ToolConfig.class)
.getToolProvider(section, "lua")
.orElseGet(
() ->
Expand All @@ -62,6 +64,7 @@ private LuaPlatform getPlatform(String section, CxxPlatform cxxPlatform) {
.orElse(LuaPlatform.PackageStyle.INPLACE))
.setPackager(
delegate
.getView(ToolConfig.class)
.getToolProvider(section, "packager")
.orElseGet(
() -> ErrorToolProvider.from("no packager set in '%s.packager'", section)))
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/python/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_library(
"//src/com/facebook/buck/rules:command_tool",
"//src/com/facebook/buck/rules:source_path",
"//src/com/facebook/buck/rules/args:args",
"//src/com/facebook/buck/rules/tool/config:config",
"//src/com/facebook/buck/util:exceptions",
"//src/com/facebook/buck/util:packaged_resource",
"//src/com/facebook/buck/util:process_executor",
Expand Down
6 changes: 4 additions & 2 deletions src/com/facebook/buck/python/PythonBuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.Tool;
import com.facebook.buck.rules.VersionedTool;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.PackagedResource;
import com.facebook.buck.util.ProcessExecutor;
Expand Down Expand Up @@ -193,7 +194,8 @@ public Tool getPexTool(BuildRuleResolver resolver) {
}

private Tool getRawPexTool(BuildRuleResolver resolver) {
Optional<Tool> executable = delegate.getTool(SECTION, "path_to_pex", resolver);
Optional<Tool> executable =
delegate.getView(ToolConfig.class).getTool(SECTION, "path_to_pex", resolver);
if (executable.isPresent()) {
return executable.get();
}
Expand All @@ -210,7 +212,7 @@ public Optional<BuildTarget> getPexExecutorTarget() {
}

public Optional<Tool> getPexExecutor(BuildRuleResolver resolver) {
return delegate.getTool(SECTION, "path_to_pex_executer", resolver);
return delegate.getView(ToolConfig.class).getTool(SECTION, "path_to_pex_executer", resolver);
}

public NativeLinkStrategy getNativeLinkStrategy() {
Expand Down
Loading

0 comments on commit 1e49235

Please sign in to comment.