Skip to content

Commit

Permalink
create used config file on demand if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz-eitco-de committed Jun 18, 2024
1 parent 348a11b commit d3beafb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
93 changes: 66 additions & 27 deletions src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package de.eitco.cicd.dotnet;

import com.google.common.base.Strings;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -21,6 +24,8 @@ public record DotnetExecutor(
boolean ignoreResult
) {

public static final String DEFAULT_NUGET_CONFIG = "default.nuget.config";

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

return execute(defaultOptions().mergeIgnoreResult(ignoreResult), List.of(parameters), Set.of(), Map.of());
Expand Down Expand Up @@ -216,6 +221,13 @@ public void upsertNugetSource(String url, String sourceName, String username, St

Set<String> obfuscation = apiToken != null ? Set.of(apiToken) : Set.of();

File configFile = getConfigFile(configLocation);

if (configFile != null) {

enforceConfigFileExists(configFile);
}

List<String> updateParameters = getUpsertParameters(username, apiToken, configLocation, "nuget", "update", "source", sourceName, "--source", url);

int result = execute(defaultOptions().ignoreResult(), updateParameters, obfuscation, null);
Expand All @@ -227,59 +239,86 @@ public void upsertNugetSource(String url, String sourceName, String username, St
}
}

private static List<String> getUpsertParameters(String userName, String apiToken, NugetConfigLocation configLocation, String... firstParameters) {

List<String> parameters = new ArrayList<>(List.of(firstParameters));
private static void enforceConfigFileExists(File configFile) throws MojoExecutionException {

if (configLocation == NugetConfigLocation.PROJECT) {
if (configFile.exists()) {

parameters.add("--configfile");
parameters.add("NuGet.Config");
return;
}

if (configLocation == NugetConfigLocation.USER) {
try (InputStream resourceAsStream = DotnetExecutor.class.getClassLoader().getResourceAsStream(DEFAULT_NUGET_CONFIG)) {

parameters.add("--configfile");
FileUtils.forceMkdir(configFile.getParentFile());

if (SystemUtils.IS_OS_WINDOWS) {
byte[] bytes = resourceAsStream.readAllBytes();

parameters.add(System.getenv("appdata") + "\\NuGet\\NuGet.Config");
Files.write(configFile.toPath(), bytes);

} else {
} catch (IOException e) {

parameters.add("~/.nuget/NuGet/NuGet.Config");
}
throw new MojoExecutionException(e);
}

if (configLocation == NugetConfigLocation.SYSTEM) {
}

parameters.add("--configfile");
private static File getConfigFile(NugetConfigLocation configLocation) {

if (configLocation == null) {

return null;
}

switch (configLocation) {
case PROJECT:
return new File("nuget.config");
case USER:
if (SystemUtils.IS_OS_WINDOWS) {

if (SystemUtils.IS_OS_WINDOWS) {
return new File(System.getenv("appdata") + "\\NuGet\\NuGet.Config");

parameters.add(System.getenv("ProgramFiles(x86)") + "\\NuGet\\Config\\Nuget.Config");
} else {

return new File(System.getProperty("HOME") + "/.nuget/NuGet/NuGet.Config");
}
case SYSTEM:
if (SystemUtils.IS_OS_WINDOWS) {

} else {
return new File(System.getenv("ProgramFiles(x86)") + "\\NuGet\\Config\\Nuget.Config");

} else {

String customAppData = System.getenv("NUGET_COMMON_APPLICATION_DATA");
String customAppData = System.getenv("NUGET_COMMON_APPLICATION_DATA");

if (Strings.isNullOrEmpty(customAppData)) {
if (Strings.isNullOrEmpty(customAppData)) {

if (SystemUtils.IS_OS_LINUX) {
if (SystemUtils.IS_OS_LINUX) {

parameters.add("/etc/opt/NuGet/Config/NuGet.Config");
return new File("/etc/opt/NuGet/Config/NuGet.Config");

} else {

return new File("/Library/Application Support/NuGet.Config");
}
} else {

parameters.add("/Library/Application Support/NuGet.Config");
return new File(customAppData + "/NuGet/Config/NuGet.Config");
}
} else {

parameters.add(customAppData + "/NuGet/Config/NuGet.Config");
}
}

}
throw new IllegalStateException();
}

private static List<String> getUpsertParameters(String userName, String apiToken, NugetConfigLocation configLocation, String... firstParameters) {

List<String> parameters = new ArrayList<>(List.of(firstParameters));

File configFile = getConfigFile(configLocation);

if (configFile != null) {

parameters.add("--configfile");
parameters.add(configFile.getPath());
}

appendCredentials(userName, apiToken, parameters);
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/default.nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>

0 comments on commit d3beafb

Please sign in to comment.