aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDeployInfoAction.java
blob: 8fb0b4cc2c2e4731ead840135cf81e51ac78ab1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// 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.google.devtools.build.lib.rules.android;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.actions.ActionExecutionContext;
import com.google.devtools.build.lib.actions.ActionOwner;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction;
import com.google.devtools.build.lib.analysis.actions.ByteStringDeterministicWriter;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.rules.android.deployinfo.AndroidDeployInfoOuterClass;
import com.google.devtools.build.lib.rules.android.deployinfo.AndroidDeployInfoOuterClass.AndroidDeployInfo;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Writes AndroidDeployInfo proto message. This proto describes how
 * to deploy and launch an android_binary/android_test.
 */
@Immutable
public final class AndroidDeployInfoAction extends AbstractFileWriteAction {

  private static Iterable<Artifact> makeInputs(
      Artifact mergedManifest,
      Iterable<Artifact> additionalMergedManifests,
      Iterable<Artifact> apksToDeploy) {

    return ImmutableList.<Artifact>builder()
        .add(mergedManifest)
        .addAll(additionalMergedManifests)
        .addAll(apksToDeploy)
        .build();
  }

  private static final String GUID = "eda283ba-9000-4b80-8dc4-7939101c44ba";

  private final Artifact mergedManifest;
  private final ImmutableList<Artifact> additionalMergedManifests;
  private final ImmutableList<Artifact> apksToDeploy;

  AndroidDeployInfoAction(
      ActionOwner owner,
      Artifact outputFile,
      Artifact mergedManifest,
      ImmutableList<Artifact> additionalMergedManifests,
      ImmutableList<Artifact> apksToDeploy) {
    super(owner, makeInputs(mergedManifest, additionalMergedManifests, apksToDeploy),
        outputFile, false);
    this.mergedManifest = mergedManifest;
    this.additionalMergedManifests = additionalMergedManifests;
    this.apksToDeploy = apksToDeploy;
  }

  private ByteString getByteString() {
    AndroidDeployInfoOuterClass.AndroidDeployInfo.Builder builder =
        AndroidDeployInfoOuterClass.AndroidDeployInfo.newBuilder();
    builder.setMergedManifest(makeArtifactProto(mergedManifest));
    for (Artifact additionMergedManifest : additionalMergedManifests) {
      builder.addAdditionalMergedManifests(makeArtifactProto(additionMergedManifest));
    }
    for (Artifact apk : apksToDeploy) {
      builder.addApksToDeploy(makeArtifactProto(apk));
    }
    return builder.build().toByteString();
  }

  static void createDeployInfoAction(
      RuleContext ruleContext,
      Artifact deployInfo,
      Artifact mergedManifest,
      ImmutableList<Artifact> additionalMergedManifests,
      ImmutableList<Artifact> apksToDeploy) {
    Action action = new AndroidDeployInfoAction(ruleContext.getActionOwner(),
        deployInfo, mergedManifest, additionalMergedManifests, apksToDeploy);
    ruleContext.registerAction(action);
  }

  @Override
  public DeterministicWriter newDeterministicWriter(ActionExecutionContext ctx) throws IOException {
    return new ByteStringDeterministicWriter(getByteString());
  }

  @VisibleForTesting
  public AndroidDeployInfo getDeployInfo() throws InvalidProtocolBufferException {
    return AndroidDeployInfo.parseFrom(getByteString());
  }

  @Override
  protected String computeKey() {
    Fingerprint f = new Fingerprint()
        .addString(GUID);

    try (InputStream in = getByteString().newInput()) {
      byte[] buffer = new byte[512];
      int amountRead;
      while ((amountRead = in.read(buffer)) != -1) {
        f.addBytes(buffer, 0, amountRead);
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return f.hexDigestAndReset();
  }

  private static AndroidDeployInfoOuterClass.Artifact makeArtifactProto(Artifact artifact) {
    return AndroidDeployInfoOuterClass.Artifact.newBuilder()
        .setExecRootPath(artifact.getExecPathString())
        .build();
  }
}