Skip to content

Commit

Permalink
delete cache entry for artifact when installed to local repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz-eitco-de committed Jul 1, 2024
1 parent a5c4ea2 commit 0e0aeff
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
50 changes: 49 additions & 1 deletion src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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*(?<directory>.*)\\s*");

public int execute(String... parameters) throws MojoExecutionException {

Expand Down Expand Up @@ -80,7 +84,7 @@ private int execute(ExecutionOptions executionOptions, List<String> parameters,
builder.inheritIO();
}

builder.environment().put("DOTNET_CLI_TELEMETRY_OPTOUT", "TRUE");
optOut(builder);

environment.forEach((key, value) -> builder.environment().put(key, value));

Expand All @@ -104,6 +108,10 @@ private int execute(ExecutionOptions executionOptions, List<String> parameters,
}
}

private static void optOut(ProcessBuilder builder) {
builder.environment().put("DOTNET_CLI_TELEMETRY_OPTOUT", "TRUE");
}

private static String presentCommand(List<String> command, Set<String> obfuscation) {

return command.stream().map(x -> obfuscation.contains(x) ? "****" : x).collect(Collectors.joining(" "));
Expand Down Expand Up @@ -357,6 +365,46 @@ private static void appendCredentials(String username, String apiToken, List<Str
public void clean() throws MojoExecutionException {

retry(1, defaultOptions().ignoreResult(), List.of("clean"), Set.of(), Map.of("Version", version));
}

public String getLocalArtifactCache() throws MojoExecutionException {

ProcessBuilder builder = new ProcessBuilder();
optOut(builder);

builder.command(buildCommand(List.of("nuget", "locals", "global-packages", "--list"), Map.of()));

try {

Process process = builder.start();

int exitCode = process.waitFor();

if (exitCode != 0) {

String errorOut = IOUtils.toString(process.getErrorStream(), StandardCharsets.UTF_8);
String stdOut = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);
log.error(stdOut + "\n" + errorOut);

throw new MojoExecutionException("Cannot get nuget cache for global packages - process exited with code " + exitCode);
}

String stdOut = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);

Matcher matcher = LOCALS_PATTERN.matcher(stdOut);

if (!matcher.matches()) {

throw new MojoExecutionException("Cannot get nuget cache for global packages - output does not match expected pattern: " + stdOut);
}

return matcher.group("directory");


} catch (IOException | InterruptedException e) {

throw new MojoExecutionException(e);
}
}

}
33 changes: 32 additions & 1 deletion src/main/java/de/eitco/cicd/dotnet/InstallMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.eitco.cicd.dotnet;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -12,10 +13,13 @@
@Mojo(name = "install", defaultPhase = LifecyclePhase.INSTALL)
public class InstallMojo extends AbstractDotnetMojo {

public static final String NUPKG_SUFFIX = ".nupkg";
private static File globalsCacheDirectory;

@Override
public void execute() throws MojoExecutionException {

File[] files = targetDirectory.listFiles(file -> file.getName().endsWith(".nupkg"));
File[] files = targetDirectory.listFiles(file -> file.getName().endsWith(NUPKG_SUFFIX));

if (files == null) {

Expand All @@ -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());
}

}

0 comments on commit 0e0aeff

Please sign in to comment.