Skip to content

Commit 0495f15

Browse files
aloubyanskyjomrazek
authored andcommitted
A Pig extension and a CLI command that generate Cachi2 lock files for Maven repository ZIPs to be able to prefetch Maven artifacts into Konflux for release purposes
1 parent 3d2bb92 commit 0495f15

19 files changed

+1192
-72
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.jboss.pnc.bacon.pig;
2+
3+
import org.jboss.pnc.bacon.pig.impl.addons.cachi2.Cachi2LockfileGenerator;
4+
import picocli.CommandLine;
5+
6+
import java.io.File;
7+
import java.util.List;
8+
import java.util.concurrent.Callable;
9+
10+
@CommandLine.Command(
11+
name = "cachi2lockfile",
12+
description = "Generates a Cachi2 lock file for a given Maven repository ZIP file.")
13+
public class Cachi2Lockfile implements Callable<Integer> {
14+
15+
@CommandLine.Parameters(description = "Comma-separated paths to Maven repositories (ZIPs or directories)")
16+
private List<File> repositories = List.of();
17+
18+
@CommandLine.Option(
19+
names = "--output",
20+
description = "Target output file. If not provided, defaults to "
21+
+ Cachi2LockfileGenerator.DEFAULT_OUTPUT_FILENAME)
22+
private File output;
23+
24+
@CommandLine.Option(
25+
names = "--maven-repository-url",
26+
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")
27+
private String mavenRepoUrl;
28+
29+
@CommandLine.Option(
30+
names = "--preferred-checksum-alg",
31+
description = "Preferred checksum algorithm to record in the generated lock file. If not provided, the strongest available SHA version will be used")
32+
private String preferredChecksumAlg;
33+
34+
@Override
35+
public Integer call() {
36+
if (repositories.isEmpty()) {
37+
throw new IllegalArgumentException("Maven repository location was not provided");
38+
}
39+
var generator = Cachi2LockfileGenerator.newInstance();
40+
if (output != null) {
41+
generator.setOutputFile(output.toPath());
42+
}
43+
if (mavenRepoUrl != null) {
44+
generator.setDefaultMavenRepositoryUrl(mavenRepoUrl);
45+
}
46+
if (preferredChecksumAlg != null) {
47+
generator.setPreferredChecksumAlg(preferredChecksumAlg);
48+
}
49+
for (var path : repositories) {
50+
if (!path.exists()) {
51+
throw new IllegalArgumentException(path + " does not exist");
52+
}
53+
generator.addMavenRepository(path.toPath());
54+
}
55+
generator.generate();
56+
return 0;
57+
}
58+
}

pig/src/main/java/org/jboss/pnc/bacon/pig/Pig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
name = "pig",
6060
description = "PiG tool",
6161
subcommands = {
62+
Cachi2Lockfile.class,
6263
Pig.Configure.class,
6364
Pig.Cancel.class,
6465
Pig.Build.class,

pig/src/main/java/org/jboss/pnc/bacon/pig/impl/addons/AddOnFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.jboss.pnc.bacon.pig.impl.addons.camel.CamelRuntimeDependenciesToAlignTree;
2121
import org.jboss.pnc.bacon.pig.impl.addons.camel.RuntimeDependenciesToAlignTree;
22+
import org.jboss.pnc.bacon.pig.impl.addons.cachi2.Cachi2LockfileAddon;
2223
import org.jboss.pnc.bacon.pig.impl.addons.microprofile.MicroProfileSmallRyeCommunityDepAnalyzer;
2324
import org.jboss.pnc.bacon.pig.impl.addons.quarkus.QuarkusCommunityDepAnalyzer;
2425
import org.jboss.pnc.bacon.pig.impl.addons.quarkus.QuarkusPostBuildAnalyzer;
@@ -73,6 +74,7 @@ public static List<AddOn> listAddOns(
7374
releasePath,
7475
extrasPath,
7576
deliverables));
77+
resultList.add(new Cachi2LockfileAddon(pigConfiguration, builds, releasePath, extrasPath));
7678
return resultList;
7779
}
7880

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package org.jboss.pnc.bacon.pig.impl.addons.cachi2;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
5+
import java.io.BufferedReader;
6+
import java.io.BufferedWriter;
7+
import java.io.IOException;
8+
import java.io.UncheckedIOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.TreeMap;
15+
16+
import static org.jboss.pnc.bacon.pig.impl.addons.cachi2.YamlUtil.initYamlMapper;
17+
18+
/**
19+
* Cachi2 lockfile
20+
*/
21+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
22+
public class Cachi2Lockfile {
23+
24+
/**
25+
* Reads a Cachi2 lockfile.
26+
*
27+
* @param lockfile Cachi2 lock file
28+
* @return Java object model representation of a Cachi2 lock file
29+
*/
30+
public static Cachi2Lockfile readFrom(Path lockfile) {
31+
try (BufferedReader reader = Files.newBufferedReader(lockfile)) {
32+
return initYamlMapper().readValue(reader, Cachi2Lockfile.class);
33+
} catch (IOException e) {
34+
throw new UncheckedIOException(e);
35+
}
36+
}
37+
38+
/**
39+
* Serializes an instance of {@link Cachi2Lockfile} to a YAML file.
40+
*
41+
* @param lockfile target YAML file
42+
*/
43+
public static void persistTo(Cachi2Lockfile lockfile, Path file) {
44+
var parentDir = file.getParent();
45+
if (parentDir != null) {
46+
try {
47+
Files.createDirectories(parentDir);
48+
} catch (IOException e) {
49+
throw new UncheckedIOException(e);
50+
}
51+
}
52+
try (BufferedWriter writer = Files.newBufferedWriter(file)) {
53+
initYamlMapper().writeValue(writer, lockfile);
54+
} catch (IOException e) {
55+
throw new UncheckedIOException(e);
56+
}
57+
}
58+
59+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
60+
public static class Cachi2Artifact {
61+
62+
private String type;
63+
private String filename;
64+
private Map<String, String> attributes = new TreeMap<>();
65+
private String checksum;
66+
67+
public String getType() {
68+
return type;
69+
}
70+
71+
public void setType(String type) {
72+
this.type = type;
73+
}
74+
75+
public String getFilename() {
76+
return filename;
77+
}
78+
79+
public void setFilename(String filename) {
80+
this.filename = filename;
81+
}
82+
83+
public Map<String, String> getAttributes() {
84+
return attributes;
85+
}
86+
87+
public void setAttributes(Map<String, String> attributes) {
88+
this.attributes = attributes;
89+
}
90+
91+
public String getChecksum() {
92+
return checksum;
93+
}
94+
95+
public void setChecksum(String checksum) {
96+
this.checksum = checksum;
97+
}
98+
99+
public void setGroupId(String groupId) {
100+
attributes.put("group_id", groupId);
101+
}
102+
103+
public void setArtifactId(String artifactId) {
104+
attributes.put("artifact_id", artifactId);
105+
}
106+
107+
public void setArtifactType(String type) {
108+
attributes.put("type", type);
109+
}
110+
111+
public void setClassifier(String classifier) {
112+
attributes.put("classifier", classifier);
113+
}
114+
115+
public void setVersion(String version) {
116+
attributes.put("version", version);
117+
}
118+
119+
public void setRepositoryUrl(String repositoryUrl) {
120+
attributes.put("repository_url", repositoryUrl);
121+
}
122+
}
123+
124+
private Map<String, String> metadata = new TreeMap<>();
125+
private List<Cachi2Artifact> artifacts = new ArrayList<>();
126+
127+
public Map<String, String> getMetadata() {
128+
return metadata;
129+
}
130+
131+
public void setMetadata(Map<String, String> metadata) {
132+
this.metadata = metadata;
133+
}
134+
135+
public List<Cachi2Artifact> getArtifacts() {
136+
return artifacts;
137+
}
138+
139+
public void addArtifact(Cachi2Artifact artifact) {
140+
artifacts.add(artifact);
141+
}
142+
143+
public void setArtifacts(List<Cachi2Artifact> content) {
144+
this.artifacts = content;
145+
}
146+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.jboss.pnc.bacon.pig.impl.addons.cachi2;
2+
3+
import org.jboss.pnc.bacon.pig.impl.PigContext;
4+
import org.jboss.pnc.bacon.pig.impl.addons.AddOn;
5+
import org.jboss.pnc.bacon.pig.impl.config.PigConfiguration;
6+
import org.jboss.pnc.bacon.pig.impl.pnc.PncBuild;
7+
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.util.Map;
11+
12+
/**
13+
* An add-on that generates Cachi2 lock files.
14+
*/
15+
public class Cachi2LockfileAddon extends AddOn {
16+
17+
/**
18+
* Output file name
19+
*/
20+
private static final String PARAM_FILENAME = "filename";
21+
22+
/**
23+
* Default repository URL for artifacts not recognized by PNC
24+
*/
25+
private static final String PARAM_DEFAULT_REPO_URL = "default-repository-url";
26+
27+
/**
28+
* Preferred checksum algorithm to record in the generated lock file
29+
*/
30+
private static final String PARAM_PREFERRED_CHECKSUM_ALG = "preferred-checksum-alg";
31+
32+
public Cachi2LockfileAddon(
33+
PigConfiguration pigConfiguration,
34+
Map<String, PncBuild> builds,
35+
String releasePath,
36+
String extrasPath) {
37+
super(pigConfiguration, builds, releasePath, extrasPath);
38+
}
39+
40+
@Override
41+
public String getName() {
42+
return "cachi2LockFile";
43+
}
44+
45+
@Override
46+
public void trigger() {
47+
var repoPath = PigContext.get().getRepositoryData().getRepositoryPath();
48+
if (!Files.exists(repoPath)) {
49+
throw new IllegalArgumentException(repoPath + " does not exist");
50+
}
51+
52+
Cachi2LockfileGenerator cachi2Lockfile = Cachi2LockfileGenerator.newInstance()
53+
.setOutputDirectory(Path.of(extrasPath))
54+
.addMavenRepository(repoPath);
55+
56+
setParams(cachi2Lockfile);
57+
58+
cachi2Lockfile.generate();
59+
}
60+
61+
/**
62+
* Set configured parameters on the generator.
63+
*
64+
* @param cachi2Lockfile lock file generator
65+
*/
66+
private void setParams(Cachi2LockfileGenerator cachi2Lockfile) {
67+
var params = getAddOnConfiguration();
68+
if (params != null) {
69+
setFilename(cachi2Lockfile, params);
70+
setDefaultRepositoryUrl(cachi2Lockfile, params);
71+
setPreferredChecksumAlg(cachi2Lockfile, params);
72+
}
73+
}
74+
75+
private void setFilename(Cachi2LockfileGenerator cachi2Lockfile, Map<String, ?> params) {
76+
var value = params.get(PARAM_FILENAME);
77+
if (value != null) {
78+
cachi2Lockfile.setOutputFileName(value.toString());
79+
}
80+
}
81+
82+
private void setDefaultRepositoryUrl(Cachi2LockfileGenerator cachi2Lockfile, Map<String, ?> params) {
83+
var value = params.get(PARAM_DEFAULT_REPO_URL);
84+
if (value != null) {
85+
cachi2Lockfile.setDefaultMavenRepositoryUrl(value.toString());
86+
}
87+
}
88+
89+
private void setPreferredChecksumAlg(Cachi2LockfileGenerator cachi2Lockfile, Map<String, ?> params) {
90+
var value = params.get(PARAM_PREFERRED_CHECKSUM_ALG);
91+
if (value != null) {
92+
cachi2Lockfile.setPreferredChecksumAlg(value.toString());
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)