Skip to content

Commit

Permalink
Make PackagedResources an @autovalue.
Browse files Browse the repository at this point in the history
RELNOTES: None.
PiperOrigin-RevId: 261260032
  • Loading branch information
Googler authored and copybara-github committed Aug 2, 2019
1 parent 7b7d5b9 commit 09ec893
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.devtools.common.options.OptionsParser;
import com.google.devtools.common.options.ShellQuotedParamsFilePreProcessor;
import com.google.devtools.common.options.TriState;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -187,16 +188,31 @@ public static void main(String[] args) throws Exception {
.debug(aaptConfigOptions.debug)
.includeGeneratedLocales(aaptConfigOptions.generatePseudoLocale)
.includeOnlyConfigs(aaptConfigOptions.resourceConfigs)
.link(compiled)
.copyPackageTo(options.packagePath)
.copyProguardTo(options.proguardOutput)
.copyMainDexProguardTo(options.mainDexProguardOutput)
.createSourceJar(options.srcJarOutput)
.copyRTxtTo(options.rOutput);
.link(compiled);
profiler.recordEndOf("link");

copy(packagedResources.apk(), options.packagePath);
if (options.proguardOutput != null) {
copy(packagedResources.proguardConfig(), options.proguardOutput);
}
if (options.mainDexProguardOutput != null) {
copy(packagedResources.mainDexProguard(), options.mainDexProguardOutput);
}
if (options.srcJarOutput != null) {
AndroidResourceOutputs.createSrcJar(
packagedResources.javaSourceDirectory(), options.srcJarOutput, /* staticIds= */ false);
}
if (options.rOutput != null) {
copy(packagedResources.rTxt(), options.rOutput);
}
if (options.resourcesOutput != null) {
packagedResources.asArchive().writeTo(options.resourcesOutput, /* compress= */ false);
}
}
}

private static void copy(Path from, Path out) throws IOException {
Files.createDirectories(out.getParent());
Files.copy(from, out);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ java_library(
"//src/tools/android/java/com/google/devtools/build/android/ziputils:ziputils_lib",
"//third_party:android_common_25_0_0",
"//third_party:asm",
"//third_party:auto_value",
"//third_party:guava",
"//third_party:jsr305",
"//third_party/java/android_databinding:exec",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,31 @@
// limitations under the License.
package com.google.devtools.build.android.aapt2;

import com.google.devtools.build.android.AndroidResourceOutputs;
import com.google.auto.value.AutoValue;
import com.google.devtools.build.android.ResourcesZip;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.annotation.Nullable;

/** Represents the packaged, flattened resources. */
public class PackagedResources {
@AutoValue
public abstract class PackagedResources {

private final Path apk;
private final Path proto;
private final Path rTxt;
private final Path proguardConfig;
private final Path mainDexProguard;
private final Path javaSourceDirectory;
private final Path resourceIds;
private final Path attributes;
private final Path packages;
public abstract Path apk();

private PackagedResources(
Path apk,
Path proto,
Path rTxt,
Path proguardConfig,
Path mainDexProguard,
Path javaSourceDirectory,
Path resourceIds,
Path attributes,
Path packages) {
this.apk = apk;
this.proto = proto;
this.rTxt = rTxt;
this.proguardConfig = proguardConfig;
this.mainDexProguard = mainDexProguard;
this.javaSourceDirectory = javaSourceDirectory;
this.resourceIds = resourceIds;
this.attributes = attributes;
this.packages = packages;
}
public abstract Path proto();

public abstract Path rTxt();

public abstract Path proguardConfig();

public abstract Path mainDexProguard();

public abstract Path javaSourceDirectory();

abstract Path resourceIds();

public abstract Path attributes();

public abstract Path packages();

public static PackagedResources of(
Path outPath,
Expand All @@ -63,9 +48,8 @@ public static PackagedResources of(
Path javaSourceDirectory,
Path resourceIds,
Path attributes,
Path packages)
throws IOException {
return new PackagedResources(
Path packages) {
return new AutoValue_PackagedResources(
outPath,
protoPath,
rTxt,
Expand All @@ -77,95 +61,7 @@ public static PackagedResources of(
packages);
}

public PackagedResources copyPackageTo(Path packagePath) throws IOException {
return of(
copy(apk, packagePath),
proto,
rTxt,
proguardConfig,
mainDexProguard,
javaSourceDirectory,
resourceIds,
attributes,
packages);
}

public PackagedResources copyRTxtTo(Path rOutput) throws IOException {
if (rOutput == null) {
return this;
}
return new PackagedResources(
apk,
proto,
copy(rTxt, rOutput),
proguardConfig,
mainDexProguard,
javaSourceDirectory,
resourceIds,
attributes,
packages);
}

private Path copy(Path from, Path out) throws IOException {
Files.createDirectories(out.getParent());
Files.copy(from, out);
return out;
}

public PackagedResources copyProguardTo(Path proguardOut) throws IOException {
if (proguardOut == null) {
return this;
}
return of(
apk,
proto,
rTxt,
copy(proguardConfig, proguardOut),
mainDexProguard,
javaSourceDirectory,
resourceIds,
attributes,
packages);
}

public PackagedResources copyMainDexProguardTo(Path mainDexProguardOut) throws IOException {
if (mainDexProguardOut == null) {
return this;
}
return of(
apk,
proto,
rTxt,
proguardConfig,
copy(mainDexProguard, mainDexProguardOut),
javaSourceDirectory,
resourceIds,
attributes,
packages);
}

public PackagedResources createSourceJar(@Nullable Path sourceJarPath) throws IOException {
if (sourceJarPath == null) {
return this;
}
AndroidResourceOutputs.createSrcJar(javaSourceDirectory, sourceJarPath, false);
return of(
apk,
proto,
rTxt,
proguardConfig,
mainDexProguard,
sourceJarPath,
resourceIds,
attributes,
packages);
}

public ResourcesZip asArchive() {
return ResourcesZip.fromApkWithProto(proto, attributes, resourceIds, packages);
}

public Path getApk() {
return apk;
return ResourcesZip.fromApkWithProto(proto(), attributes(), resourceIds(), packages());
}
}

0 comments on commit 09ec893

Please sign in to comment.