|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.gradle.transform; |
| 11 | + |
| 12 | +import org.gradle.api.Action; |
| 13 | +import org.gradle.api.artifacts.dsl.DependencyHandler; |
| 14 | +import org.gradle.api.artifacts.transform.InputArtifact; |
| 15 | +import org.gradle.api.artifacts.transform.TransformAction; |
| 16 | +import org.gradle.api.artifacts.transform.TransformOutputs; |
| 17 | +import org.gradle.api.artifacts.transform.TransformParameters; |
| 18 | +import org.gradle.api.artifacts.type.ArtifactTypeDefinition; |
| 19 | +import org.gradle.api.file.FileSystemLocation; |
| 20 | +import org.gradle.api.provider.Provider; |
| 21 | +import org.gradle.api.tasks.Input; |
| 22 | + |
| 23 | +import java.io.BufferedOutputStream; |
| 24 | +import java.io.File; |
| 25 | +import java.io.FileOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.Serializable; |
| 28 | +import java.io.UncheckedIOException; |
| 29 | +import java.nio.file.FileSystems; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.nio.file.PathMatcher; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Enumeration; |
| 34 | +import java.util.List; |
| 35 | +import java.util.zip.ZipEntry; |
| 36 | +import java.util.zip.ZipFile; |
| 37 | +import java.util.zip.ZipOutputStream; |
| 38 | + |
| 39 | +public abstract class FilteringJarTransform implements TransformAction<FilteringJarTransform.Parameters> { |
| 40 | + public static final String FILTERED_JAR_TYPE = "filtered-jar"; |
| 41 | + |
| 42 | + @InputArtifact |
| 43 | + public abstract Provider<FileSystemLocation> getInputArtifact(); |
| 44 | + |
| 45 | + @Override |
| 46 | + public void transform(TransformOutputs outputs) { |
| 47 | + File original = getInputArtifact().get().getAsFile(); |
| 48 | + File transformed = outputs.file(original.getName()); |
| 49 | + List<PathMatcher> excludes = createMatchers(getParameters().getExcludes()); |
| 50 | + |
| 51 | + try ( |
| 52 | + ZipFile input = new ZipFile(original); |
| 53 | + ZipOutputStream output = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(transformed))) |
| 54 | + ) { |
| 55 | + Enumeration<? extends ZipEntry> entries = input.entries(); |
| 56 | + while (entries.hasMoreElements()) { |
| 57 | + ZipEntry entry = entries.nextElement(); |
| 58 | + if (excludes.stream().noneMatch(e -> e.matches(Path.of(entry.getName())))) { |
| 59 | + output.putNextEntry(entry); |
| 60 | + input.getInputStream(entry).transferTo(output); |
| 61 | + output.closeEntry(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + output.flush(); |
| 66 | + output.finish(); |
| 67 | + } catch (IOException e) { |
| 68 | + throw new UncheckedIOException("Failed to patch archive", e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private List<PathMatcher> createMatchers(List<String> patterns) { |
| 73 | + return patterns.stream().map(p -> FileSystems.getDefault().getPathMatcher("glob:" + p)).toList(); |
| 74 | + } |
| 75 | + |
| 76 | + public static void registerTransform(DependencyHandler dependencyHandler, Action<Parameters> config) { |
| 77 | + dependencyHandler.registerTransform(FilteringJarTransform.class, spec -> { |
| 78 | + spec.getFrom().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE); |
| 79 | + spec.getTo().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FILTERED_JAR_TYPE); |
| 80 | + config.execute(spec.getParameters()); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + public abstract static class Parameters implements TransformParameters, Serializable { |
| 85 | + private List<String> excludes = new ArrayList<>(); |
| 86 | + |
| 87 | + @Input |
| 88 | + public List<String> getExcludes() { |
| 89 | + return excludes; |
| 90 | + } |
| 91 | + |
| 92 | + public void exclude(String exclude) { |
| 93 | + excludes.add(exclude); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments