diff options
author | 2017-05-16 20:18:46 +0200 | |
---|---|---|
committer | 2017-05-17 15:20:55 +0200 | |
commit | e04253eb9e673634751b29029a5aca0f19e2c753 (patch) | |
tree | 001922687265215be3c0bd800fe9bc783e477aaf /src/tools/android/java/com/google/devtools/build/android/resources/RSourceGenerator.java | |
parent | ba0e2b2930bbda02bfb4a81eb579716709a14809 (diff) |
Use one writer for generating R.java source files
RELNOTES: None
PiperOrigin-RevId: 156205544
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/resources/RSourceGenerator.java')
-rw-r--r-- | src/tools/android/java/com/google/devtools/build/android/resources/RSourceGenerator.java | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/resources/RSourceGenerator.java b/src/tools/android/java/com/google/devtools/build/android/resources/RSourceGenerator.java new file mode 100644 index 0000000000..e2d19b1c73 --- /dev/null +++ b/src/tools/android/java/com/google/devtools/build/android/resources/RSourceGenerator.java @@ -0,0 +1,100 @@ +// 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.Collection; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +/** 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 { + write(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, Map<ResourceType, Set<String>> symbolsToWrite) + throws IOException { + write(packageName, values.filter(symbolsToWrite)); + } + + private void write( + String packageName, + Iterable<Entry<ResourceType, Collection<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 (Entry<ResourceType, Collection<FieldInitializer>> entry : initializersToWrite) { + writer.write( + String.format(" public static final class %s {\n", entry.getKey().getName())); + for (FieldInitializer field : entry.getValue()) { + field.writeInitSource(writer, finalFields); + } + writer.write(" }\n"); + } + writer.write("}"); + } + } +} |