aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/UnvalidatedAndroidData.java
blob: ec3a2bcd7192925f5d56799263f5a59c5c025440 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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.android;

import com.android.builder.core.VariantType;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.android.aapt2.CompiledResources;
import com.google.devtools.build.android.aapt2.ResourceCompiler;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

/**
 * Android data that has yet to be merged and validated, the primary data for the Processor.
 *
 * <p>The life cycle of AndroidData goes: {@link UnvalidatedAndroidData} -> {@link
 * MergedAndroidData} -> {@link DensityFilteredAndroidData} -> {@link DependencyAndroidData}
 */
class UnvalidatedAndroidData extends UnvalidatedAndroidDirectories {

  private static final Pattern VALID_REGEX = Pattern.compile(".*:.*:.+");

  public static final String EXPECTED_FORMAT = "resources[#resources]:assets[#assets]:manifest";

  public static UnvalidatedAndroidData valueOf(String text) {
    return valueOf(text, FileSystems.getDefault());
  }

  @VisibleForTesting
  static UnvalidatedAndroidData valueOf(String text, FileSystem fileSystem) {
    if (!VALID_REGEX.matcher(text).find()) {
      throw new IllegalArgumentException(text + " is not in the format '" + EXPECTED_FORMAT + "'");
    }
    String[] parts = text.split(":");
    return new UnvalidatedAndroidData(
        splitPaths(parts[0], fileSystem),
        splitPaths(parts[1], fileSystem),
        exists(fileSystem.getPath(parts[2])));
  }

  private final Path manifest;

  public UnvalidatedAndroidData(
      ImmutableList<Path> resourceDirs, ImmutableList<Path> assetDirs, Path manifest) {
    super(resourceDirs, assetDirs);
    this.manifest = manifest;
  }

  public Path getManifest() {
    return manifest;
  }

  @Override
  public String toString() {
    return String.format("UnvalidatedAndroidData(%s, %s, %s)", resourceDirs, assetDirs, manifest);
  }

  @Override
  public int hashCode() {
    return Objects.hash(resourceDirs, assetDirs, manifest);
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (!(obj instanceof UnvalidatedAndroidData)) {
      return false;
    }
    UnvalidatedAndroidData other = (UnvalidatedAndroidData) obj;
    return Objects.equals(other.resourceDirs, resourceDirs)
        && Objects.equals(other.assetDirs, assetDirs)
        && Objects.equals(other.manifest, manifest);
  }

  public CompiledResources compile(ResourceCompiler compiler, Path workingDirectory)
      throws IOException, ExecutionException, InterruptedException {
    for (Path resourceDir : resourceDirs) {
      compiler.queueDirectoryForCompilation(resourceDir);
    }
    return archiveCompiledResources(
        compiler.getCompiledArtifacts(),
        workingDirectory,
        workingDirectory.resolve("compiled.zip"));
  }

  protected CompiledResources archiveCompiledResources(
      List<Path> resources, Path workingDirectory, Path output) throws IOException {
    return CompiledResources.from(
        AndroidResourceOutputs.archiveCompiledResources(
            output, workingDirectory, workingDirectory, resources),
        manifest,
        assetDirs);
  }

  /* Processes the resources for databinding annotations if dataBindingOut is defined. */
  public UnvalidatedAndroidData processDataBindings(
      @Nullable Path dataBindingOut, String packagePath, Path dataBindingWorkingDirectory)
      throws IOException {

    if (dataBindingOut == null) {
      return this;
    }

    Preconditions.checkNotNull(manifest);
    Preconditions.checkNotNull(packagePath);

    final List<Path> processed = new ArrayList<>();
    final Path metadataWorkingDirectory =
        Files.createDirectory(dataBindingWorkingDirectory.resolve("metadata"));
    final Path databindingResourceRoot = dataBindingWorkingDirectory.resolve("resources");
    for (Path resource : resourceDirs) {
      processed.add(
          AndroidResourceProcessor.processDataBindings(
              databindingResourceRoot,
              resource,
              metadataWorkingDirectory,
              VariantType.LIBRARY,
              packagePath,
              manifest,
              false));
    }

    AndroidResourceOutputs.archiveDirectory(metadataWorkingDirectory, dataBindingOut);

    return new UnvalidatedAndroidData(ImmutableList.copyOf(processed), assetDirs, manifest) {
      @Override
      protected CompiledResources archiveCompiledResources(
          List<Path> resources, Path workingDirectory, Path output) throws IOException {
        // Update the archiving to ensure that the resources are correctly placed.
        return CompiledResources.from(
            AndroidResourceOutputs.archiveCompiledResources(
                output, databindingResourceRoot, workingDirectory, resources),
            manifest,
            assetDirs);
      }
    };
  }
}