Skip to content

Commit

Permalink
Use FileUtils.ensureDirectory instead of Files.createDirectories in s…
Browse files Browse the repository at this point in the history
…tager plugin (#1011)
  • Loading branch information
romain-grecourt authored Jan 9, 2024
1 parent 818c396 commit f31ed63
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,8 @@

import io.helidon.build.common.Maps;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Copy an artifact to a given target location.
*/
Expand Down Expand Up @@ -54,7 +56,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
ctx.logInfo("Copying %s to %s", resolvedGav, resolveTarget);
Path artifact = ctx.resolve(resolvedGav);
Path targetFile = dir.resolve(resolveTarget);
Files.createDirectories(targetFile.getParent());
ensureDirectory(targetFile.getParent());
Files.copy(artifact, targetFile);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
import io.helidon.build.common.NetworkConnection;
import io.helidon.build.common.Strings;

import static io.helidon.build.common.FileUtils.ensureDirectory;
import static io.helidon.build.common.FileUtils.measuredSize;

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
private void download(StagingContext ctx, Path dir, Map<String, String> vars) throws IOException {
String path = resolveVar(target(), vars);
Path file = dir.resolve(path).normalize();
Files.createDirectories(file.getParent());
ensureDirectory(file.getParent());
URL url = new URL(resolveVar(this.url, vars));
try (BufferedInputStream bis = new BufferedInputStream(open(url, ctx))) {
try (OutputStream fos = Files.newOutputStream(file)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.List;
import java.util.Map;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Generate or copy a file to a given target location.
*/
Expand Down Expand Up @@ -68,7 +70,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
String resolvedSource = resolveVar(source, vars);
String resolvedContent = resolveVar(content, vars);
Path targetFile = dir.resolve(resolvedTarget).normalize();
Files.createDirectories(targetFile.getParent());
ensureDirectory(targetFile.getParent());
if (resolvedSource != null && !resolvedSource.isEmpty()) {
Path sourceFile = ctx.resolve(resolvedSource);
if (!Files.exists(sourceFile)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,14 +15,14 @@
*/
package io.helidon.build.maven.stager;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Generate a directory using a set of actions.
*/
Expand All @@ -39,8 +39,8 @@ public CompletionStage<Void> execute(StagingContext ctx, Path dir, Map<String, S
Path targetDir = dir.resolve(target());
ctx.logInfo("Staging %s", targetDir);
try {
Files.createDirectories(targetDir);
} catch (IOException ex) {
ensureDirectory(targetDir);
} catch (Throwable ex) {
return CompletableFuture.failedFuture(ex);
}
return super.execute(ctx, targetDir, vars);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,8 @@

import io.helidon.build.common.Strings;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Create a symlink.
*/
Expand Down Expand Up @@ -54,7 +56,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
Path link = dir.resolve(resolveVar(target(), vars));
Path linkTarget = link.getParent().relativize(dir.resolve(resolveVar(source, vars)));
ctx.logInfo("Creating symlink source: %s, target: %s", link, linkTarget);
Files.createDirectories(link.getParent());
ensureDirectory(link.getParent());
Files.createSymbolicLink(link, linkTarget);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.util.DecoratedCollection;

import static io.helidon.build.common.FileUtils.ensureDirectory;
import static java.util.stream.Collectors.toMap;

/**
Expand Down Expand Up @@ -74,7 +75,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
throw new IllegalStateException(sourceFile + " does not exist");
}
Path targetFile = dir.resolve(resolvedTarget).normalize();
Files.createDirectories(targetFile.getParent());
ensureDirectory(targetFile.getParent());
try (Reader reader = Files.newBufferedReader(sourceFile);
Writer writer = Files.newBufferedWriter(targetFile,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,11 @@
package io.helidon.build.maven.stager;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Unpack an artifact to a given target location.
*/
Expand Down Expand Up @@ -73,7 +74,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
Path artifact = ctx.resolve(resolvedGav);
Path targetDir = dir.resolve(resolvedTarget).normalize();
ctx.logInfo("Unpacking %s to %s", artifact, targetDir);
Files.createDirectories(targetDir);
ensureDirectory(targetDir);
ctx.unpack(artifact, targetDir, excludes, includes);
}
}

0 comments on commit f31ed63

Please sign in to comment.