Skip to content

Commit

Permalink
Create parent directories if it doesn't exists (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
hvvikram authored Feb 27, 2023
1 parent 5763130 commit 5922027
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -25,6 +26,14 @@ public JsonDependencyExporter(ExportDependenciesExtension exportDependenciesExte
this.exportDependenciesExtension = exportDependenciesExtension;
}

private static void createParent(Path path){
try{
Files.createDirectories(path.getParent());
} catch (IOException e) {
throw new ExporterException(e);
}
}

@Override
public void export(Set<ExternalDependency> dependencies) {
if (!exportDependenciesExtension.isEnabled()) {
Expand All @@ -38,10 +47,12 @@ public void export(Set<ExternalDependency> dependencies) {
.collect(Collectors.toSet());

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String file = exportDependenciesExtension.getFile();
Path path = Paths.get(file);
createParent(path);
try (BufferedWriter writer =
Files.newBufferedWriter(
Paths.get(exportDependenciesExtension.getFile()), StandardCharsets.UTF_8)) {
LOG.info("Exporting dependencies to JSON at " + exportDependenciesExtension.getFile());
Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
LOG.info("Exporting dependencies to JSON at " + file);
gson.toJson(dependencyModels, writer);
} catch (IOException e) {
throw new ExporterException(e);
Expand Down

0 comments on commit 5922027

Please sign in to comment.