From 0e0aeff541cdb2a190ff3ae688f2b7664ae54b41 Mon Sep 17 00:00:00 2001 From: jschwarz Date: Mon, 1 Jul 2024 12:55:47 +0200 Subject: [PATCH] delete cache entry for artifact when installed to local repository --- .../de/eitco/cicd/dotnet/DotnetExecutor.java | 50 ++++++++++++++++++- .../de/eitco/cicd/dotnet/InstallMojo.java | 33 +++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java b/src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java index a68549f..24cf9c4 100644 --- a/src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java +++ b/src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java @@ -2,6 +2,7 @@ import com.google.common.base.Strings; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.SystemUtils; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; @@ -12,6 +13,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; public record DotnetExecutor( @@ -26,6 +29,7 @@ public record DotnetExecutor( ) { public static final String DEFAULT_NUGET_CONFIG = "default.nuget.config"; + public static final Pattern LOCALS_PATTERN = Pattern.compile("\\s*global-packages:\\s*(?.*)\\s*"); public int execute(String... parameters) throws MojoExecutionException { @@ -80,7 +84,7 @@ private int execute(ExecutionOptions executionOptions, List parameters, builder.inheritIO(); } - builder.environment().put("DOTNET_CLI_TELEMETRY_OPTOUT", "TRUE"); + optOut(builder); environment.forEach((key, value) -> builder.environment().put(key, value)); @@ -104,6 +108,10 @@ private int execute(ExecutionOptions executionOptions, List parameters, } } + private static void optOut(ProcessBuilder builder) { + builder.environment().put("DOTNET_CLI_TELEMETRY_OPTOUT", "TRUE"); + } + private static String presentCommand(List command, Set obfuscation) { return command.stream().map(x -> obfuscation.contains(x) ? "****" : x).collect(Collectors.joining(" ")); @@ -357,6 +365,46 @@ private static void appendCredentials(String username, String apiToken, List file.getName().endsWith(".nupkg")); + File[] files = targetDirectory.listFiles(file -> file.getName().endsWith(NUPKG_SUFFIX)); if (files == null) { @@ -39,10 +43,37 @@ public void execute() throws MojoExecutionException { getLog().info("installing " + targetFile.getAbsolutePath()); Files.copy(file.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + getLog().debug("get package id for " + file.getName() + " project version is " + projectVersion); + String packageId = file.getName().substring(0, file.getName().length() - NUPKG_SUFFIX.length() - projectVersion.length() - 1); + + File cacheDirectory = getGlobalsCacheDirectory(); + + File cacheEntry = new File(new File(cacheDirectory, packageId), projectVersion); + getLog().debug("cache entry for " + file.getName() + " is " + cacheEntry.getAbsolutePath()); + + if (cacheEntry.isDirectory()) { + + getLog().debug("deleting cache entry " + cacheEntry.getAbsolutePath()); + + FileUtils.deleteDirectory(cacheEntry); + } + } catch (IOException e) { throw new MojoExecutionException(e); } } } + + private File getGlobalsCacheDirectory() throws MojoExecutionException { + + if (globalsCacheDirectory != null) { + + return globalsCacheDirectory; + } + + return globalsCacheDirectory = new File(newExecutor().getLocalArtifactCache()); + } + }