Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Pig extension and a CLI command that generate Cachi2 lock files for Maven repository ZIPs #1304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions pig/src/main/java/org/jboss/pnc/bacon/pig/Cachi2Lockfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.jboss.pnc.bacon.pig;

import org.jboss.pnc.bacon.pig.impl.addons.cachi2.Cachi2LockfileGenerator;
import picocli.CommandLine;

import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;

@CommandLine.Command(
name = "cachi2lockfile",
description = "Generates a Cachi2 lock file for a given Maven repository ZIP file.")
public class Cachi2Lockfile implements Callable<Integer> {

@CommandLine.Parameters(description = "Comma-separated paths to Maven repositories (ZIPs or directories)")
private List<File> repositories = List.of();

@CommandLine.Option(
names = "--output",
description = "Target output file. If not provided, defaults to "
+ Cachi2LockfileGenerator.DEFAULT_OUTPUT_FILENAME)
private File output;

@CommandLine.Option(
names = "--maven-repository-url",
description = "Maven repository URL to record in the generated lock file. If not provided, org.jboss.pnc.bacon.pig.impl.utils.indy.Indy.getIndyUrl() will be used as the default one")
private String mavenRepoUrl;

@CommandLine.Option(
names = "--preferred-checksum-alg",
description = "Preferred checksum algorithm to record in the generated lock file. If not provided, the strongest available SHA version will be used")
private String preferredChecksumAlg;

@Override
public Integer call() {
if (repositories.isEmpty()) {
throw new IllegalArgumentException("Maven repository location was not provided");
}
var generator = Cachi2LockfileGenerator.newInstance();
if (output != null) {
generator.setOutputFile(output.toPath());
}
if (mavenRepoUrl != null) {
generator.setDefaultMavenRepositoryUrl(mavenRepoUrl);
}
if (preferredChecksumAlg != null) {
generator.setPreferredChecksumAlg(preferredChecksumAlg);
}
for (var path : repositories) {
if (!path.exists()) {
throw new IllegalArgumentException(path + " does not exist");
}
generator.addMavenRepository(path.toPath());
}
generator.generate();
return 0;
}
}
1 change: 1 addition & 0 deletions pig/src/main/java/org/jboss/pnc/bacon/pig/Pig.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
name = "pig",
description = "PiG tool",
subcommands = {
Cachi2Lockfile.class,
Pig.Configure.class,
Pig.Cancel.class,
Pig.Build.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.jboss.pnc.bacon.pig.impl.addons.camel.CamelRuntimeDependenciesToAlignTree;
import org.jboss.pnc.bacon.pig.impl.addons.camel.RuntimeDependenciesToAlignTree;
import org.jboss.pnc.bacon.pig.impl.addons.cachi2.Cachi2LockfileAddon;
import org.jboss.pnc.bacon.pig.impl.addons.microprofile.MicroProfileSmallRyeCommunityDepAnalyzer;
import org.jboss.pnc.bacon.pig.impl.addons.quarkus.QuarkusCommunityDepAnalyzer;
import org.jboss.pnc.bacon.pig.impl.addons.quarkus.QuarkusPostBuildAnalyzer;
Expand Down Expand Up @@ -73,6 +74,7 @@ public static List<AddOn> listAddOns(
releasePath,
extrasPath,
deliverables));
resultList.add(new Cachi2LockfileAddon(pigConfiguration, builds, releasePath, extrasPath));
return resultList;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package org.jboss.pnc.bacon.pig.impl.addons.cachi2;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static org.jboss.pnc.bacon.pig.impl.addons.cachi2.YamlUtil.initYamlMapper;

/**
* Cachi2 lockfile
*/
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Cachi2Lockfile {

/**
* Reads a Cachi2 lockfile.
*
* @param lockfile Cachi2 lock file
* @return Java object model representation of a Cachi2 lock file
*/
public static Cachi2Lockfile readFrom(Path lockfile) {
try (BufferedReader reader = Files.newBufferedReader(lockfile)) {
return initYamlMapper().readValue(reader, Cachi2Lockfile.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Serializes an instance of {@link Cachi2Lockfile} to a YAML file.
*
* @param lockfile target YAML file
*/
public static void persistTo(Cachi2Lockfile lockfile, Path file) {
var parentDir = file.getParent();
if (parentDir != null) {
try {
Files.createDirectories(parentDir);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
try (BufferedWriter writer = Files.newBufferedWriter(file)) {
initYamlMapper().writeValue(writer, lockfile);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public static class Cachi2Artifact {

private String type;
private String filename;
private Map<String, String> attributes = new TreeMap<>();
private String checksum;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public Map<String, String> getAttributes() {
return attributes;
}

public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}

public String getChecksum() {
return checksum;
}

public void setChecksum(String checksum) {
this.checksum = checksum;
}

public void setGroupId(String groupId) {
attributes.put("group_id", groupId);
}

public void setArtifactId(String artifactId) {
attributes.put("artifact_id", artifactId);
}

public void setArtifactType(String type) {
attributes.put("type", type);
}

public void setClassifier(String classifier) {
attributes.put("classifier", classifier);
}

public void setVersion(String version) {
attributes.put("version", version);
}

public void setRepositoryUrl(String repositoryUrl) {
attributes.put("repository_url", repositoryUrl);
}
}

private Map<String, String> metadata = new TreeMap<>();
private List<Cachi2Artifact> artifacts = new ArrayList<>();

public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

public List<Cachi2Artifact> getArtifacts() {
return artifacts;
}

public void addArtifact(Cachi2Artifact artifact) {
artifacts.add(artifact);
}

public void setArtifacts(List<Cachi2Artifact> content) {
this.artifacts = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.jboss.pnc.bacon.pig.impl.addons.cachi2;

import org.jboss.pnc.bacon.pig.impl.PigContext;
import org.jboss.pnc.bacon.pig.impl.addons.AddOn;
import org.jboss.pnc.bacon.pig.impl.config.PigConfiguration;
import org.jboss.pnc.bacon.pig.impl.pnc.PncBuild;

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

/**
* An add-on that generates Cachi2 lock files.
*/
public class Cachi2LockfileAddon extends AddOn {

/**
* Output file name
*/
private static final String PARAM_FILENAME = "filename";

/**
* Default repository URL for artifacts not recognized by PNC
*/
private static final String PARAM_DEFAULT_REPO_URL = "default-repository-url";

/**
* Preferred checksum algorithm to record in the generated lock file
*/
private static final String PARAM_PREFERRED_CHECKSUM_ALG = "preferred-checksum-alg";

public Cachi2LockfileAddon(
PigConfiguration pigConfiguration,
Map<String, PncBuild> builds,
String releasePath,
String extrasPath) {
super(pigConfiguration, builds, releasePath, extrasPath);
}

@Override
public String getName() {
return "cachi2LockFile";
}

@Override
public void trigger() {
var repoPath = PigContext.get().getRepositoryData().getRepositoryPath();
if (!Files.exists(repoPath)) {
throw new IllegalArgumentException(repoPath + " does not exist");
}

Cachi2LockfileGenerator cachi2Lockfile = Cachi2LockfileGenerator.newInstance()
.setOutputDirectory(Path.of(extrasPath))
.addMavenRepository(repoPath);

setParams(cachi2Lockfile);

cachi2Lockfile.generate();
}

/**
* Set configured parameters on the generator.
*
* @param cachi2Lockfile lock file generator
*/
private void setParams(Cachi2LockfileGenerator cachi2Lockfile) {
var params = getAddOnConfiguration();
if (params != null) {
setFilename(cachi2Lockfile, params);
setDefaultRepositoryUrl(cachi2Lockfile, params);
setPreferredChecksumAlg(cachi2Lockfile, params);
}
}

private void setFilename(Cachi2LockfileGenerator cachi2Lockfile, Map<String, ?> params) {
var value = params.get(PARAM_FILENAME);
if (value != null) {
cachi2Lockfile.setOutputFileName(value.toString());
}
}

private void setDefaultRepositoryUrl(Cachi2LockfileGenerator cachi2Lockfile, Map<String, ?> params) {
var value = params.get(PARAM_DEFAULT_REPO_URL);
if (value != null) {
cachi2Lockfile.setDefaultMavenRepositoryUrl(value.toString());
}
}

private void setPreferredChecksumAlg(Cachi2LockfileGenerator cachi2Lockfile, Map<String, ?> params) {
var value = params.get(PARAM_PREFERRED_CHECKSUM_ALG);
if (value != null) {
cachi2Lockfile.setPreferredChecksumAlg(value.toString());
}
}
}
Loading