Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Implement "buck fetch"
Browse files Browse the repository at this point in the history
Summary:
Buck will cowardly refuse to download files at build time by default,
and we've not publicized the configuration setting that allows a user
to override this. Instead, we introduce "buck fetch", which downloads
all remote resources by executing the `BuildRule`.

In a follow up, I'll explore the option to cache downloadeds in order
to survive someone calling "buck clean". However, since we tell users
that "buck clean" is a bug, I consider this low priority.

Test Plan: buck test --all
  • Loading branch information
shs96c authored and sdwilsh committed Jan 20, 2015
1 parent 3acbe82 commit 3afdcbd
Show file tree
Hide file tree
Showing 18 changed files with 485 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/com/facebook/buck/cli/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ java_library(
'//src/com/facebook/buck/android/agent/util:util',
'//src/com/facebook/buck/apple/xcode:project_generator',
'//src/com/facebook/buck/apple:rules',
'//src/com/facebook/buck/event:event',
'//src/com/facebook/buck/command:command',
'//src/com/facebook/buck/event:event',
'//src/com/facebook/buck/event/listener:listener',
'//src/com/facebook/buck/file:downloader',
'//src/com/facebook/buck/file:file',
'//src/com/facebook/buck/graph:graph',
'//src/com/facebook/buck/io:io',
'//src/com/facebook/buck/java:autodeps',
Expand Down
4 changes: 4 additions & 0 deletions src/com/facebook/buck/cli/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public enum Command {
"deletes any generated files",
CleanCommand.class,
false),
FETCH(
"downloads remote resources to your local machine",
FetchCommand.class,
false),
INSTALL(
"builds and installs an APK",
InstallCommand.class,
Expand Down
133 changes: 133 additions & 0 deletions src/com/facebook/buck/cli/FetchCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2015-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.facebook.buck.command.Build;
import com.facebook.buck.file.Downloader;
import com.facebook.buck.file.HttpDownloader;
import com.facebook.buck.file.RemoteFileDescription;
import com.facebook.buck.json.BuildFileParseException;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.model.BuildTargetException;
import com.facebook.buck.rules.ActionGraph;
import com.facebook.buck.rules.BuildEvent;
import com.facebook.buck.rules.Description;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.rules.TargetGraphToActionGraph;
import com.facebook.buck.step.TargetDevice;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;

import java.io.IOException;
import java.net.Proxy;

public class FetchCommand extends AbstractCommandRunner<BuildCommandOptions> {

public FetchCommand(CommandRunnerParams params) {
super(params);
}

@Override
BuildCommandOptions createOptions(BuckConfig buckConfig) {
return new BuildCommandOptions(buckConfig);
}

@Override
int runCommandWithOptionsInternal(BuildCommandOptions options)
throws IOException, InterruptedException {

ImmutableSet<BuildTarget> buildTargets =
getBuildTargets(options.getArgumentsFormattedAsBuildTargets());

if (buildTargets.isEmpty()) {
console.printBuildFailure("Must specify at least one build target to fetch.");
return 1;
}

// Post the build started event, setting it to the Parser recorded start time if appropriate.
if (getParser().getParseStartTime().isPresent()) {
getBuckEventBus().post(
BuildEvent.started(buildTargets),
getParser().getParseStartTime().get());
} else {
getBuckEventBus().post(BuildEvent.started(buildTargets));
}

FetchTargetNodeToBuildRuleTransformer ruleGenerator = createFetchTransformer(options);
TargetGraphToActionGraph transformer = new TargetGraphToActionGraph(
getBuckEventBus(),
ruleGenerator);

ActionGraph actionGraph;
try {
TargetGraph targetGraph = getParser().buildTargetGraphForBuildTargets(
buildTargets,
options.getDefaultIncludes(),
getBuckEventBus(),
console,
environment,
options.getEnableProfiling());

actionGraph = transformer.apply(targetGraph);
buildTargets = ruleGenerator.getDownloadableTargets();
} catch (BuildTargetException | BuildFileParseException e) {
console.printBuildFailureWithoutStacktrace(e);
return 1;
}

int exitCode;
try (Build build = options.createBuild(
options.getBuckConfig(),
actionGraph,
getProjectFilesystem(),
getAndroidDirectoryResolver(),
getBuildEngine(),
getArtifactCache(),
console,
getBuckEventBus(),
Optional.<TargetDevice>absent(),
getCommandRunnerParams().getPlatform(),
getCommandRunnerParams().getEnvironment(),
getCommandRunnerParams().getObjectMapper(),
getCommandRunnerParams().getClock())) {
exitCode = build.executeAndPrintFailuresToConsole(
buildTargets,
options.isKeepGoing(),
console,
options.getPathToBuildReport());
}

getBuckEventBus().post(BuildEvent.finished(buildTargets, exitCode));

return exitCode;
}

private FetchTargetNodeToBuildRuleTransformer createFetchTransformer(
BuildCommandOptions options) {
Optional<String> defaultMavenRepo = options.getBuckConfig().getValue("download", "maven_repo");
Downloader downloader = new HttpDownloader(Optional.<Proxy>absent(), defaultMavenRepo);
Description<?> description = new RemoteFileDescription(downloader);
return new FetchTargetNodeToBuildRuleTransformer(
ImmutableSet.<Description<?>>of(description)
);
}

@Override
String getUsageIntro() {
return "fetch remote resources";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2015-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.facebook.buck.model.BuildTarget;
import com.facebook.buck.parser.NoSuchBuildTargetException;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.Description;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.rules.TargetNode;
import com.facebook.buck.rules.TargetNodeToBuildRuleTransformer;
import com.google.common.collect.ImmutableSet;

class FetchTargetNodeToBuildRuleTransformer implements TargetNodeToBuildRuleTransformer {

private final ImmutableSet<Description<?>> descriptions;
// TODO(user): Allow the TargetToActionGraph to be stateless.
private final ImmutableSet.Builder<BuildTarget> downloadableTargets;
private final BuildTargetNodeToBuildRuleTransformer delegate;

public FetchTargetNodeToBuildRuleTransformer(
ImmutableSet<Description<?>> descriptions) {
this.descriptions = descriptions;

this.downloadableTargets = ImmutableSet.builder();
this.delegate = new BuildTargetNodeToBuildRuleTransformer();
}

@Override
public <T> BuildRule transform(
TargetGraph targetGraph,
BuildRuleResolver ruleResolver,
TargetNode<T> targetNode) throws NoSuchBuildTargetException {
TargetNode<?> node = substituteTargetNodeIfNecessary(targetNode);
return delegate.transform(targetGraph, ruleResolver, node);
}

public ImmutableSet<BuildTarget> getDownloadableTargets() {
return downloadableTargets.build();
}

private TargetNode<?> substituteTargetNodeIfNecessary(TargetNode<?> node) {
for (Description<?> description : descriptions) {
if (node.getDescription().getBuildRuleType().equals(description.getBuildRuleType())) {
downloadableTargets.add(node.getBuildTarget());
return node.amend(description);
}
}
return node;
}
}
7 changes: 5 additions & 2 deletions src/com/facebook/buck/file/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ java_library(
'Downloader.java',
'DownloadEvent.java',
'DownloadProgressEvent.java',
'ExplodingDownloader.java',
'HttpDownloader.java',
'MavenUrlDecoder.java',
],
Expand All @@ -24,18 +25,20 @@ java_library(
],
deps = [
':downloader',
'//third-party/java/guava:guava',
'//third-party/java/jsr:jsr305',
'//src/com/facebook/buck/event:event',
'//src/com/facebook/buck/io:io',
'//src/com/facebook/buck/model:model',
'//src/com/facebook/buck/rules:build_rule',
'//src/com/facebook/buck/rules:rules',
'//src/com/facebook/buck/step/fs:fs',
'//src/com/facebook/buck/step:step',
'//src/com/facebook/buck/util:exceptions',
'//third-party/java/guava:guava',
'//third-party/java/infer-annotations:infer-annotations',
'//third-party/java/jsr:jsr305',
],
visibility = [
'//src/com/facebook/buck/cli:cli',
'//src/com/facebook/buck/rules:types',
],
)
3 changes: 2 additions & 1 deletion src/com/facebook/buck/file/DownloadStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public DownloadStep(Downloader downloader, URI url, HashCode sha1, Path output)
public int execute(ExecutionContext context) throws InterruptedException {
BuckEventBus eventBus = context.getBuckEventBus();
try {
downloader.fetch(eventBus, url, output);
Path resolved = context.getProjectFilesystem().resolve(output);
downloader.fetch(eventBus, url, resolved);

HashCode readHash = Files.asByteSource(output.toFile()).hash(Hashing.sha1());
if (!sha1.equals(readHash)) {
Expand Down
32 changes: 32 additions & 0 deletions src/com/facebook/buck/file/ExplodingDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2015-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.file;

import com.facebook.buck.event.BuckEventBus;
import com.facebook.buck.util.HumanReadableException;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;

public class ExplodingDownloader implements Downloader {
@Override
public void fetch(BuckEventBus eventBus, URI uri, Path output) throws IOException {
throw new HumanReadableException(
"Downloading files at runtime is disabled, please run 'buck fetch' before your build");
}
}
11 changes: 1 addition & 10 deletions src/com/facebook/buck/file/RemoteFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.facebook.buck.step.Step;
import com.facebook.buck.step.fs.CopyStep;
import com.facebook.buck.step.fs.MakeCleanDirectoryStep;
import com.facebook.buck.util.HumanReadableException;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand All @@ -41,7 +40,6 @@
* build.
*/
public class RemoteFile extends AbstractBuildRule {
private final boolean isBuildTimeDownloadingOk;
private final URI uri;
private final HashCode sha1;
private final Path output;
Expand All @@ -50,13 +48,12 @@ public class RemoteFile extends AbstractBuildRule {
public RemoteFile(
BuildRuleParams params,
SourcePathResolver resolver,
boolean isBuildTimeDownloadingOk,
Downloader downloader,
URI uri,
HashCode sha1,
String out) {
super(params, resolver);
this.isBuildTimeDownloadingOk = isBuildTimeDownloadingOk;

this.uri = uri;
this.sha1 = sha1;
this.downloader = downloader;
Expand All @@ -74,7 +71,6 @@ protected ImmutableCollection<Path> getInputsToCompareToOutput() {
@Override
protected RuleKey.Builder appendDetailsToRuleKey(RuleKey.Builder builder) {
return builder
.setReflectively("donwnload-at-runtime", isBuildTimeDownloadingOk)
.setReflectively("sha1", sha1.toString())
.setReflectively("out", output.toString())
.setReflectively("url", uri.toString());
Expand All @@ -83,11 +79,6 @@ protected RuleKey.Builder appendDetailsToRuleKey(RuleKey.Builder builder) {
@Override
public ImmutableList<Step> getBuildSteps(
BuildContext context, BuildableContext buildableContext) {
if (!isBuildTimeDownloadingOk) {
throw new HumanReadableException(
"Downloading files at runtime is disabled, please run 'buck fetch' before your build");
}

ImmutableList.Builder<Step> steps = ImmutableList.builder();

Path tempFile = BuildTargets.getBinPath(
Expand Down
7 changes: 1 addition & 6 deletions src/com/facebook/buck/file/RemoteFileDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@
public class RemoteFileDescription implements Description<RemoteFileDescription.Arg> {

public static final BuildRuleType TYPE = new BuildRuleType("remote_file");
private final boolean isBuildTimeDownloadingOk;
private final Downloader downloader;

public RemoteFileDescription(
boolean isBuildTimeDownloadingOk,
Downloader downloader) {
this.isBuildTimeDownloadingOk = isBuildTimeDownloadingOk;
public RemoteFileDescription(Downloader downloader) {
this.downloader = downloader;
}

Expand All @@ -63,7 +59,6 @@ public <A extends Arg> BuildRule createBuildRule(
return new RemoteFile(
params,
new SourcePathResolver(resolver),
isBuildTimeDownloadingOk,
downloader,
args.url,
sha1,
Expand Down
Loading

0 comments on commit 3afdcbd

Please sign in to comment.