Skip to content

Commit

Permalink
Create the output folder for build-bundle when it doesn’t exist yet (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tutipeti authored Nov 18, 2020
1 parent a380c5b commit c975779
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.android.tools.build.bundletool.model.exceptions.InvalidCommandException;
import com.android.tools.build.bundletool.model.targeting.TargetingGenerator;
import com.android.tools.build.bundletool.model.utils.files.BufferedIo;
import com.android.tools.build.bundletool.model.utils.files.FileUtils;
import com.android.tools.build.bundletool.model.version.BundleToolVersion;
import com.android.tools.build.bundletool.validation.BundleModulesValidator;
import com.google.auto.value.AutoValue;
Expand All @@ -55,6 +56,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

Expand All @@ -72,6 +74,8 @@ public abstract class BuildBundleCommand {
Flag.mapCollector("metadata-file", ZipPath.class, Path.class);
private static final Flag<Boolean> UNCOMPRESSED_FLAG = Flag.booleanFlag("uncompressed");

private static final Logger logger = Logger.getLogger(BuildBundleCommand.class.getName());

public abstract Path getOutputPath();

public abstract boolean getOverwriteOutput();
Expand Down Expand Up @@ -252,6 +256,12 @@ public void execute() {
AppBundle.buildFromModules(
modulesWithTargeting.build(), bundleConfig, getBundleMetadata());

Path outputDirectory = getOutputPath().getParent();
if (Files.notExists(outputDirectory)) {
logger.info("Output directory '" + outputDirectory + "' does not exist, creating it.");
FileUtils.createDirectories(outputDirectory);
}

if (getOverwriteOutput()) {
Files.deleteIfExists(getOutputPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,21 @@ public void overwriteFlagSetOverritesOutput() throws Exception {
assertThat(Files.size(bundlePath)).isGreaterThan(0L);
}

@Test
public void allParentDirectoriesCreated() throws IOException {
Path outputPath = tmpDir.resolve("non-existing-dir").resolve("bundle.aab");

Path module = createSimpleBaseModule();
BuildBundleCommand.builder()
.setModulesPaths(ImmutableList.of(module))
.setOverwriteOutput(true)
.setOutputPath(outputPath)
.build()
.execute();

assertThat(Files.exists(outputPath)).isTrue();
}

private Path createSimpleBaseModule() throws IOException {
return new ZipBuilder()
.addFileWithProtoContent(
Expand Down

0 comments on commit c975779

Please sign in to comment.