aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/resources/RSourceGenerator.java
blob: 2f2cff37ab012aaaafb2edf552c8e3c3883011e8 (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
// Copyright 2017 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.resources;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.android.SdkConstants;
import com.android.resources.ResourceType;
import com.google.common.collect.Iterables;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

/** Writes out an R.java source. */
public class RSourceGenerator {

  public static RSourceGenerator with(
      Path outputBasePath, FieldInitializers initializers, boolean finalFields) {
    return new RSourceGenerator(outputBasePath, initializers, finalFields);
  }

  private final Path outputBasePath;
  private final FieldInitializers values;
  private final boolean finalFields;

  private RSourceGenerator(
      Path outputBasePath, FieldInitializers initializers, boolean finalFields) {
    this.outputBasePath = outputBasePath;
    this.values = initializers;
    this.finalFields = finalFields;
  }

  /** Writes the java source with the writer values to the specified package and derived dir. */
  public void write(String packageName) throws IOException {
    writeSource(packageName, values);
  }

  /**
   * Writes the java source with the passed subset of the writer values.
   *
   * @param packageName The package and the dir to write the R java source to under the output path.
   * @param symbolsToWrite A map of ResourceType to resource name that will be written. If the map
   *     specifies a resource that does not exist in the writer values, it will be ignored.
   */
  public void write(String packageName, FieldInitializers symbolsToWrite) throws IOException {
    writeSource(packageName, values.filter(symbolsToWrite));
  }

  private void writeSource(
      String packageName,
      Iterable<Map.Entry<ResourceType, Map<String, FieldInitializer>>> initializersToWrite)
      throws IOException {
    String packageDir = packageName.replace('.', '/');
    Path packagePath = outputBasePath.resolve(packageDir);
    Path rJavaPath = packagePath.resolve(SdkConstants.FN_RESOURCE_CLASS);
    Files.createDirectories(rJavaPath.getParent());

    if (Iterables.isEmpty(initializersToWrite)) {
      return;
    }

    try (BufferedWriter writer = Files.newBufferedWriter(rJavaPath, UTF_8)) {
      writer.write("/* AUTO-GENERATED FILE.  DO NOT MODIFY.\n");
      writer.write(" *\n");
      writer.write(" * This class was automatically generated by the\n");
      writer.write(" * bazel tool from the resource data it found.  It\n");
      writer.write(" * should not be modified by hand.\n");
      writer.write(" */\n");
      writer.write(String.format("package %s;\n", packageName));
      writer.write("public final class R {\n");
      for (Map.Entry<ResourceType, Map<String, FieldInitializer>> entry : initializersToWrite) {
        writer.write(
            String.format("    public static final class %s {\n", entry.getKey().getName()));
        for (Map.Entry<String, FieldInitializer> fieldEntry : entry.getValue().entrySet()) {
          fieldEntry.getValue().writeInitSource(fieldEntry.getKey(), writer, finalFields);
        }
        writer.write("    }\n");
      }
      writer.write("}");
    }
  }
}