use of org.jreleaser.util.Algorithm in project jreleaser by jreleaser.
the class Signer method collectArtifacts.
private static List<FilePair> collectArtifacts(JReleaserContext context, boolean forceSign, Function<FilePair, Boolean> validator) {
List<FilePair> files = new ArrayList<>();
Signing signing = context.getModel().getSigning();
Path signaturesDirectory = context.getSignaturesDirectory();
String extension = ".sig";
if (signing.getMode() != Signing.Mode.COSIGN) {
extension = signing.isArmored() ? ".asc" : ".sig";
}
if (signing.isFiles()) {
for (Artifact artifact : Artifacts.resolveFiles(context)) {
if (!artifact.isActive() || artifact.extraPropertyIsTrue(KEY_SKIP_SIGNING))
continue;
Path input = artifact.getEffectivePath(context);
Path output = signaturesDirectory.resolve(input.getFileName().toString().concat(extension));
FilePair pair = new FilePair(input, output);
if (!forceSign)
pair.setValid(validator.apply(pair));
files.add(pair);
}
}
if (signing.isArtifacts()) {
for (Distribution distribution : context.getModel().getActiveDistributions()) {
if (distribution.extraPropertyIsTrue(KEY_SKIP_SIGNING))
continue;
for (Artifact artifact : distribution.getArtifacts()) {
if (!artifact.isActive() || artifact.extraPropertyIsTrue(KEY_SKIP_SIGNING))
continue;
Path input = artifact.getEffectivePath(context, distribution);
Path output = signaturesDirectory.resolve(input.getFileName().toString().concat(extension));
FilePair pair = new FilePair(input, output);
if (!forceSign)
pair.setValid(validator.apply(pair));
files.add(pair);
}
}
}
if (signing.isChecksums()) {
for (Algorithm algorithm : context.getModel().getChecksum().getAlgorithms()) {
Path checksums = context.getChecksumsDirectory().resolve(context.getModel().getChecksum().getResolvedName(context, algorithm));
if (Files.exists(checksums)) {
Path output = signaturesDirectory.resolve(checksums.getFileName().toString().concat(extension));
FilePair pair = new FilePair(checksums, output);
if (!forceSign)
pair.setValid(validator.apply(pair));
files.add(pair);
}
}
}
return files;
}
use of org.jreleaser.util.Algorithm in project jreleaser by jreleaser.
the class AbstractReleaserBuilder method configureWith.
@Override
public ReleaserBuilder<R> configureWith(JReleaserContext context) {
this.context = context;
GitService service = context.getModel().getRelease().getGitService();
if (!service.resolveUploadAssetsEnabled(context.getModel().getProject())) {
return this;
}
List<Asset> assets = new ArrayList<>();
Checksum checksum = context.getModel().getChecksum();
if (service.isFiles()) {
for (Artifact artifact : Artifacts.resolveFiles(context)) {
if (!artifact.isActive() || artifact.extraPropertyIsTrue(KEY_SKIP_RELEASE))
continue;
Path path = artifact.getEffectivePath(context);
assets.add(Asset.file(Artifact.of(path, artifact.getExtraProperties())));
if (service.isChecksums() && isIndividual(context, artifact) && !artifact.extraPropertyIsTrue(KEY_SKIP_CHECKSUM)) {
for (Algorithm algorithm : checksum.getAlgorithms()) {
assets.add(Asset.checksum(Artifact.of(context.getChecksumsDirectory().resolve(path.getFileName() + "." + algorithm.formatted()))));
}
}
}
}
if (service.isArtifacts()) {
for (Distribution distribution : context.getModel().getActiveDistributions()) {
if (distribution.extraPropertyIsTrue(KEY_SKIP_RELEASE)) {
continue;
}
for (Artifact artifact : distribution.getArtifacts()) {
if (!artifact.isActive() || artifact.extraPropertyIsTrue(KEY_SKIP_RELEASE))
continue;
Path path = artifact.getEffectivePath(context, distribution);
assets.add(Asset.file(Artifact.of(path, artifact.getExtraProperties()), distribution));
if (service.isChecksums() && isIndividual(context, distribution, artifact)) {
for (Algorithm algorithm : checksum.getAlgorithms()) {
assets.add(Asset.checksum(Artifact.of(context.getChecksumsDirectory().resolve(distribution.getName()).resolve(path.getFileName() + "." + algorithm.formatted()))));
}
}
}
}
}
if (service.isChecksums()) {
for (Algorithm algorithm : checksum.getAlgorithms()) {
Path checksums = context.getChecksumsDirectory().resolve(checksum.getResolvedName(context, algorithm));
if (Files.exists(checksums)) {
assets.add(Asset.checksum(Artifact.of(checksums)));
}
}
}
Signing signing = context.getModel().getSigning();
if (signing.isEnabled() && service.isSignatures()) {
boolean signaturesAdded = false;
List<Asset> assetsCopy = new ArrayList<>(assets);
for (Asset asset : assetsCopy) {
if (asset.getArtifact().extraPropertyIsTrue(KEY_SKIP_SIGNING) || asset.getArtifact().extraPropertyIsTrue(KEY_SKIP_RELEASE_SIGNATURES))
continue;
Path signature = context.getSignaturesDirectory().resolve(asset.getFilename() + (signing.getSignatureExtension()));
if (Files.exists(signature)) {
assets.add(Asset.signature(Artifact.of(signature)));
signaturesAdded = true;
}
}
if (signaturesAdded && signing.getMode() == Signing.Mode.COSIGN) {
Path publicKeyFile = signing.getCosign().getResolvedPublicKeyFilePath(context);
assets.add(Asset.signature(Artifact.of(publicKeyFile)));
}
}
assets.forEach(this::addReleaseAsset);
return this;
}
use of org.jreleaser.util.Algorithm in project jreleaser by jreleaser.
the class AbstractArtifactUploader method collectArtifacts.
protected List<Artifact> collectArtifacts() {
List<Artifact> artifacts = new ArrayList<>();
List<String> keys = getUploader().resolveSkipKeys();
Checksum checksum = context.getModel().getChecksum();
boolean uploadChecksums = getUploader().isChecksums() && !(getUploader() instanceof Artifactory);
if (getUploader().isFiles()) {
for (Artifact artifact : Artifacts.resolveFiles(context)) {
if (!artifact.isActive())
continue;
Path path = artifact.getEffectivePath(context);
if (isSkip(artifact, keys))
continue;
if (Files.exists(path) && 0 != path.toFile().length()) {
artifacts.add(artifact);
if (uploadChecksums && isIndividual(context, artifact) && !artifact.extraPropertyIsTrue(KEY_SKIP_CHECKSUM)) {
for (Algorithm algorithm : checksum.getAlgorithms()) {
artifacts.add(Artifact.of(context.getChecksumsDirectory().resolve(path.getFileName() + "." + algorithm.formatted())));
}
}
}
}
}
if (getUploader().isArtifacts()) {
for (Distribution distribution : context.getModel().getActiveDistributions()) {
if (isSkip(distribution, keys))
continue;
for (Artifact artifact : distribution.getArtifacts()) {
if (!artifact.isActive())
continue;
Path path = artifact.getEffectivePath(context, distribution);
if (isSkip(artifact, keys))
continue;
if (Files.exists(path) && 0 != path.toFile().length()) {
String platform = artifact.getPlatform();
String platformReplaced = distribution.getPlatform().applyReplacements(platform);
if (isNotBlank(platformReplaced)) {
artifact.getExtraProperties().put("platformReplaced", platformReplaced);
}
artifacts.add(artifact);
if (uploadChecksums && isIndividual(context, distribution, artifact)) {
for (Algorithm algorithm : checksum.getAlgorithms()) {
artifacts.add(Artifact.of(context.getChecksumsDirectory().resolve(distribution.getName()).resolve(path.getFileName() + "." + algorithm.formatted())));
}
}
}
}
}
}
if (uploadChecksums) {
for (Algorithm algorithm : checksum.getAlgorithms()) {
Path checksums = context.getChecksumsDirectory().resolve(checksum.getResolvedName(context, algorithm));
if (Files.exists(checksums)) {
artifacts.add(Artifact.of(checksums));
}
}
}
Signing signing = context.getModel().getSigning();
if (getUploader().isSignatures() && signing.isEnabled()) {
String extension = signing.getSignatureExtension();
List<Artifact> signatures = new ArrayList<>();
for (Artifact artifact : artifacts) {
if (artifact.extraPropertyIsTrue(KEY_SKIP_SIGNING))
continue;
Path signaturePath = context.getSignaturesDirectory().resolve(artifact.getEffectivePath(context).getFileName() + extension);
if (Files.exists(signaturePath) && 0 != signaturePath.toFile().length()) {
signatures.add(Artifact.of(signaturePath, artifact.getExtraProperties()));
}
}
if (!signatures.isEmpty() && signing.getMode() == Signing.Mode.COSIGN) {
Path publicKeyFile = signing.getCosign().getResolvedPublicKeyFilePath(context);
signatures.add(Artifact.of(publicKeyFile));
}
artifacts.addAll(signatures);
}
return artifacts;
}
use of org.jreleaser.util.Algorithm in project jreleaser by jreleaser.
the class AbstractPackagerProcessor method verifyAndAddArtifacts.
protected boolean verifyAndAddArtifacts(Map<String, Object> props, Distribution distribution, List<Artifact> artifacts) throws PackagerProcessingException {
List<Artifact> activeArtifacts = artifacts.stream().filter(Artifact::isActive).collect(Collectors.toList());
if (activeArtifacts.size() == 0) {
// we can't proceed
context.getLogger().warn(RB.$("packager.no.matching.artifacts"), distribution.getName(), capitalize(packager.getType()));
return false;
}
int count = 0;
for (Artifact artifact : activeArtifacts) {
String artifactUrl = Artifacts.resolveDownloadUrl(context, packager.getType(), distribution, artifact);
if (isBlank(artifactUrl))
continue;
count++;
String platform = artifact.getPlatform();
String artifactPlatform = isNotBlank(platform) ? capitalize(platform) : "";
String platformReplaced = distribution.getPlatform().applyReplacements(platform);
String artifactPlatformReplaced = isNotBlank(platformReplaced) ? capitalize(platformReplaced) : "";
// add extra properties without clobbering existing keys
Map<String, Object> artifactProps = artifact.getResolvedExtraProperties(ARTIFACT + artifactPlatform);
artifactProps.keySet().stream().filter(k -> !props.containsKey(k)).forEach(k -> props.put(k, artifactProps.get(k)));
Path artifactPath = artifact.getEffectivePath(context, distribution);
long artifactSize = 0;
try {
artifactSize = Files.size(artifactPath);
} catch (IOException ignored) {
// this would be strange
context.getLogger().trace(ignored);
}
String artifactFile = artifact.getEffectivePath().getFileName().toString();
String artifactFileName = getFilename(artifactFile, FileType.getSupportedExtensions());
String artifactFileExtension = artifactFile.substring(artifactFileName.length());
String artifactFileFormat = artifactFileExtension.substring(1);
String artifactName = "";
String artifactVersion = "";
String projectVersion = context.getModel().getProject().getEffectiveVersion();
if (isNotBlank(projectVersion) && artifactFileName.contains(projectVersion)) {
artifactName = artifactFileName.substring(0, artifactFileName.indexOf(projectVersion));
if (artifactName.endsWith("-")) {
artifactName = artifactName.substring(0, artifactName.length() - 1);
}
artifactVersion = projectVersion;
}
projectVersion = context.getModel().getProject().getVersion();
if (isBlank(artifactName) && isNotBlank(projectVersion) && artifactFileName.contains(projectVersion)) {
artifactName = artifactFileName.substring(0, artifactFileName.indexOf(projectVersion));
if (artifactName.endsWith("-")) {
artifactName = artifactName.substring(0, artifactName.length() - 1);
}
artifactVersion = projectVersion;
}
String artifactOs = "";
String artifactArch = "";
if (isNotBlank(platform)) {
if (platform.contains("-")) {
String[] parts = platform.split("-");
artifactOs = parts[0];
artifactArch = parts[1];
}
}
safePut(props, ARTIFACT + artifactPlatform + NAME, artifactName);
safePut(props, ARTIFACT + artifactPlatform + VERSION, artifactVersion);
safePut(props, ARTIFACT + artifactPlatform + OS, artifactOs);
safePut(props, ARTIFACT + artifactPlatform + ARCH, artifactArch);
safePut(props, ARTIFACT + artifactPlatform + FILE, artifactFile);
safePut(props, ARTIFACT + artifactPlatform + SIZE, artifactSize);
safePut(props, ARTIFACT + artifactPlatform + FILE_NAME, artifactFileName);
safePut(props, ARTIFACT + artifactPlatform + FILE_EXTENSION, artifactFileExtension);
safePut(props, ARTIFACT + artifactPlatform + FILE_FORMAT, artifactFileFormat);
safePut(props, ARTIFACT + artifactPlatformReplaced + NAME, artifactName);
safePut(props, ARTIFACT + artifactPlatformReplaced + VERSION, artifactVersion);
safePut(props, ARTIFACT + artifactPlatformReplaced + OS, artifactOs);
safePut(props, ARTIFACT + artifactPlatformReplaced + ARCH, artifactArch);
safePut(props, ARTIFACT + artifactPlatformReplaced + FILE, artifactFile);
safePut(props, ARTIFACT + artifactPlatformReplaced + SIZE, artifactSize);
safePut(props, ARTIFACT + artifactPlatformReplaced + FILE_NAME, artifactFileName);
safePut(props, ARTIFACT + artifactPlatformReplaced + FILE_EXTENSION, artifactFileExtension);
safePut(props, ARTIFACT + artifactPlatformReplaced + FILE_FORMAT, artifactFileFormat);
for (Algorithm algorithm : context.getModel().getChecksum().getAlgorithms()) {
safePut(props, ARTIFACT + artifactPlatform + CHECKSUM + capitalize(algorithm.formatted()), artifact.getHash(algorithm));
safePut(props, ARTIFACT + artifactPlatformReplaced + CHECKSUM + capitalize(algorithm.formatted()), artifact.getHash(algorithm));
}
safePut(props, ARTIFACT + artifactPlatform + URL, artifactUrl);
safePut(props, ARTIFACT + artifactPlatformReplaced + URL, artifactUrl);
props.putAll(context.getModel().getUpload().resolveDownloadUrls(context, distribution, artifact, ARTIFACT + artifactPlatform));
props.putAll(context.getModel().getUpload().resolveDownloadUrls(context, distribution, artifact, ARTIFACT + artifactPlatformReplaced));
if (count == 1) {
props.putAll(context.getModel().getUpload().resolveDownloadUrls(context, distribution, artifact, DISTRIBUTION));
safePut(props, KEY_DISTRIBUTION_ARTIFACT, artifact);
safePut(props, KEY_DISTRIBUTION_URL, artifactUrl);
safePut(props, KEY_DISTRIBUTION_SIZE, artifactSize);
safePut(props, KEY_DISTRIBUTION_SHA_256, artifact.getHash(Algorithm.SHA_256));
for (Algorithm algorithm : context.getModel().getChecksum().getAlgorithms()) {
safePut(props, DISTRIBUTION + CHECKSUM + capitalize(algorithm.formatted()), artifact.getHash(algorithm));
}
safePut(props, KEY_DISTRIBUTION_ARTIFACT_PLATFORM, platform);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_PLATFORM_REPLACED, platformReplaced);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_NAME, artifactName);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_VERSION, artifactVersion);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_OS, artifactOs);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_ARCH, artifactArch);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_SIZE, artifactSize);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_FILE, artifactFile);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_FILE_NAME, artifactFileName);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_FILE_EXTENSION, artifactFileExtension);
safePut(props, KEY_DISTRIBUTION_ARTIFACT_FILE_FORMAT, artifactFileFormat);
safePut(props, KEY_ARTIFACT_PLATFORM, platform);
safePut(props, KEY_ARTIFACT_PLATFORM_REPLACED, platformReplaced);
safePut(props, KEY_ARTIFACT_NAME, artifactName);
safePut(props, KEY_ARTIFACT_VERSION, artifactVersion);
safePut(props, KEY_ARTIFACT_OS, artifactOs);
safePut(props, KEY_ARTIFACT_ARCH, artifactArch);
safePut(props, KEY_ARTIFACT_SIZE, artifactSize);
safePut(props, KEY_ARTIFACT_FILE, artifactFile);
safePut(props, KEY_ARTIFACT_FILE_NAME, artifactFileName);
safePut(props, KEY_ARTIFACT_FILE_EXTENSION, artifactFileExtension);
safePut(props, KEY_ARTIFACT_FILE_FORMAT, artifactFileFormat);
// add extra properties without clobbering existing keys
Map<String, Object> aprops = artifact.getResolvedExtraProperties();
Map<String, Object> bprops = new LinkedHashMap<>(aprops);
applyTemplates(aprops, bprops);
aprops.keySet().stream().filter(k -> !props.containsKey(k)).forEach(k -> props.put(k, aprops.get(k)));
}
}
return count > 0;
}
use of org.jreleaser.util.Algorithm in project jreleaser by jreleaser.
the class Checksum method collectAndWriteChecksums.
public static void collectAndWriteChecksums(JReleaserContext context) throws JReleaserException {
context.getLogger().info(RB.$("checksum.header"));
context.getLogger().increaseIndent();
context.getLogger().setPrefix("checksum");
Map<Algorithm, List<String>> checksums = new LinkedHashMap<>();
if (context.getModel().getChecksum().isFiles()) {
for (Artifact artifact : Artifacts.resolveFiles(context)) {
if (!artifact.isActive())
continue;
artifact.getEffectivePath(context);
if (artifact.extraPropertyIsTrue(KEY_SKIP_CHECKSUM))
continue;
for (Algorithm algorithm : context.getModel().getChecksum().getAlgorithms()) {
readHash(context, algorithm, artifact);
List<String> list = checksums.computeIfAbsent(algorithm, k -> new ArrayList<>());
list.add(artifact.getHash(algorithm) + " " + artifact.getEffectivePath(context).getFileName());
}
}
}
for (Distribution distribution : context.getModel().getActiveDistributions()) {
for (Artifact artifact : distribution.getArtifacts()) {
if (!artifact.isActive())
continue;
artifact.getEffectivePath(context, distribution);
for (Algorithm algorithm : context.getModel().getChecksum().getAlgorithms()) {
readHash(context, distribution, algorithm, artifact);
List<String> list = checksums.computeIfAbsent(algorithm, k -> new ArrayList<>());
list.add(artifact.getHash(algorithm) + " " + artifact.getEffectivePath(context, distribution).getFileName());
}
}
}
if (checksums.isEmpty()) {
context.getLogger().info(RB.$("checksum.not.enabled"));
context.getLogger().decreaseIndent();
context.getLogger().restorePrefix();
return;
}
for (Map.Entry<Algorithm, List<String>> entry : checksums.entrySet()) {
Algorithm algorithm = entry.getKey();
List<String> list = entry.getValue();
Path checksumsFilePath = context.getChecksumsDirectory().resolve(context.getModel().getChecksum().getResolvedName(context, algorithm));
String newContent = String.join(System.lineSeparator(), list) + System.lineSeparator();
try {
if (Files.exists(checksumsFilePath)) {
String oldContent = new String(Files.readAllBytes(checksumsFilePath));
if (newContent.equals(oldContent)) {
// no need to write down the same content
context.getLogger().info(RB.$("checksum.not.changed"));
context.getLogger().restorePrefix();
context.getLogger().decreaseIndent();
return;
}
}
} catch (IOException ignored) {
// OK
}
try {
if (isNotBlank(newContent)) {
Files.createDirectories(context.getChecksumsDirectory());
Files.write(checksumsFilePath, newContent.getBytes());
} else {
Files.deleteIfExists(checksumsFilePath);
}
} catch (IOException e) {
throw new JReleaserException(RB.$("ERROR_unexpected_error_checksum", checksumsFilePath.toAbsolutePath()), e);
}
}
context.getLogger().restorePrefix();
context.getLogger().decreaseIndent();
}
Aggregations