Skip to content

Commit

Permalink
Revert "Refactor plugin scanning into lib" (#93116)
Browse files Browse the repository at this point in the history
This reverts the plugin scanning backport in 8.6.

Commits reverted: ef99ffd ba98f35 and 92a9cc1
  • Loading branch information
pgomulka committed Jan 23, 2023
1 parent e666c38 commit 0f87001
Show file tree
Hide file tree
Showing 29 changed files with 665 additions and 850 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ import java.nio.file.Files
import java.nio.file.Path
import java.util.stream.Collectors

import static org.elasticsearch.gradle.fixtures.TestClasspathUtils.setupNamedComponentScanner

class StablePluginBuildPluginFuncTest extends AbstractGradleFuncTest {

def setup() {
// underlaying TestClusterPlugin and StandaloneRestIntegTestTask are not cc compatible
configurationCacheCompatible = false
}

def "can build stable plugin properties"() {
given:
buildFile << """plugins {
id 'elasticsearch.stable-esplugin'
}
Expand All @@ -34,27 +38,8 @@ class StablePluginBuildPluginFuncTest extends AbstractGradleFuncTest {
name = 'myplugin'
description = 'test plugin'
}
repositories {
maven {
name = "local-test"
url = file("local-repo")
metadataSources {
artifact()
}
}
}
"""

// underlaying TestClusterPlugin and StandaloneRestIntegTestTask are not cc compatible
configurationCacheCompatible = false

def version = VersionProperties.elasticsearch
setupNamedComponentScanner(dir("local-repo/org/elasticsearch/elasticsearch-plugin-scanner/${version}/"), version)

}

def "can build stable plugin properties"() {
given:
when:
def result = gradleRunner(":pluginProperties").build()
def props = getPluginProperties()
Expand All @@ -77,16 +62,26 @@ class StablePluginBuildPluginFuncTest extends AbstractGradleFuncTest {
}

def "can scan and create named components file"() {
//THIS IS RUNNING A MOCK CONFIGURED IN setup()
given:
File jarFolder = new File(testProjectDir.root, "jars")
jarFolder.mkdirs()

buildFile << """
buildFile << """plugins {
id 'elasticsearch.stable-esplugin'
}
version = '1.2.3'
esplugin {
name = 'myplugin'
description = 'test plugin'
}
dependencies {
implementation files('${normalized(StableApiJarMocks.createPluginApiJar(jarFolder.toPath()).toAbsolutePath().toString())}')
implementation files('${normalized(StableApiJarMocks.createExtensibleApiJar(jarFolder.toPath()).toAbsolutePath().toString())}')
}
"""

file("src/main/java/org/acme/A.java") << """
Expand All @@ -100,16 +95,18 @@ class StablePluginBuildPluginFuncTest extends AbstractGradleFuncTest {
}
"""

when:
def result = gradleRunner(":assemble", "-i").build()

when:
def result = gradleRunner(":assemble").build()
Path namedComponents = file("build/generated-named-components/named_components.json").toPath();
def map = new JsonSlurper().parse(namedComponents.toFile())
then:
result.task(":assemble").outcome == TaskOutcome.SUCCESS
//we expect that a Fake namedcomponent scanner used in this test will be passed a filename to be created
File namedComponents = file("build/generated-named-components/named_components.json")
namedComponents.exists() == true

map == ["org.elasticsearch.plugin.scanner.test_classes.ExtensibleClass" : (["componentA" : "org.acme.A"]) ]
}


Map<String, String> getPluginProperties() {
Path propsFile = file("build/generated-descriptor/stable-plugin-descriptor.properties").toPath();
Properties rawProps = new Properties()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,54 @@

package org.elasticsearch.gradle.plugin;

import org.elasticsearch.gradle.LoggedExec;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.elasticsearch.gradle.plugin.scanner.ClassReaders;
import org.elasticsearch.gradle.plugin.scanner.NamedComponentScanner;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.CompileClasspath;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.process.ExecOperations;
import org.gradle.process.ExecResult;
import org.gradle.workers.WorkAction;
import org.gradle.workers.WorkParameters;
import org.gradle.workers.WorkerExecutor;
import org.objectweb.asm.ClassReader;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

public abstract class GenerateNamedComponentsTask extends DefaultTask {
private static final Logger LOGGER = Logging.getLogger(GenerateNamedComponentsTask.class);
private static final String NAMED_COMPONENTS_DIR = "generated-named-components/";
private static final String NAMED_COMPONENTS_FILE = "named_components.json";
private static final String NAMED_COMPONENTS_PATH = NAMED_COMPONENTS_DIR + NAMED_COMPONENTS_FILE;

private final WorkerExecutor workerExecutor;
private FileCollection pluginScannerClasspath;
private FileCollection classpath;
private ExecOperations execOperations;
private ProjectLayout projectLayout;

@Inject
public GenerateNamedComponentsTask(WorkerExecutor workerExecutor, ExecOperations execOperations, ProjectLayout projectLayout) {
public GenerateNamedComponentsTask(WorkerExecutor workerExecutor, ObjectFactory objectFactory, ProjectLayout projectLayout) {
this.workerExecutor = workerExecutor;
this.execOperations = execOperations;
this.projectLayout = projectLayout;

getOutputFile().convention(projectLayout.getBuildDirectory().file(NAMED_COMPONENTS_PATH));
getOutputFile().convention(projectLayout.getBuildDirectory().file("generated-named-components/" + NAMED_COMPONENTS_FILE));
}

@TaskAction
public void scanPluginClasses() {
File outputFile = projectLayout.getBuildDirectory().file(NAMED_COMPONENTS_PATH).get().getAsFile();

ExecResult execResult = LoggedExec.javaexec(execOperations, spec -> {
spec.classpath(pluginScannerClasspath.plus(getClasspath()).getAsPath());
spec.getMainClass().set("org.elasticsearch.plugin.scanner.NamedComponentScanner");
spec.args(outputFile);
spec.setErrorOutput(System.err);
spec.setStandardOutput(System.out);
workerExecutor.noIsolation().submit(GenerateNamedComponentsAction.class, params -> {
params.getClasspath().from(classpath);
params.getOutputFile().set(getOutputFile());
});
execResult.assertNormalExitValue();
}

@OutputFile
Expand All @@ -76,13 +70,36 @@ public void setClasspath(FileCollection classpath) {
this.classpath = classpath;
}

public void setPluginScannerClasspath(FileCollection pluginScannerClasspath) {
this.pluginScannerClasspath = pluginScannerClasspath;
public abstract static class GenerateNamedComponentsAction implements WorkAction<Parameters> {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

@Override
public void execute() {
Set<File> classpathFiles = getParameters().getClasspath().getFiles();

List<ClassReader> classReaders = ClassReaders.ofPaths(classpathFiles.stream().map(File::toPath));

NamedComponentScanner namedComponentScanner = new NamedComponentScanner();
Map<String, Map<String, String>> namedComponentsMap = namedComponentScanner.scanForNamedClasses(classReaders);
writeToFile(namedComponentsMap);
}

private void writeToFile(Map<String, Map<String, String>> namedComponentsMap) {
try {
String json = OBJECT_MAPPER.writeValueAsString(namedComponentsMap);
File file = getParameters().getOutputFile().getAsFile().get();
Path of = Path.of(file.getAbsolutePath());
Files.writeString(of, json);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public FileCollection getPluginScannerClasspath() {
return pluginScannerClasspath;
interface Parameters extends WorkParameters {

ConfigurableFileCollection getClasspath();

RegularFileProperty getOutputFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@

package org.elasticsearch.gradle.plugin;

import org.elasticsearch.gradle.VersionProperties;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
import org.gradle.api.plugins.JavaPlugin;
Expand All @@ -36,21 +33,12 @@ public void apply(Project project) {
});

final var pluginNamedComponents = project.getTasks().register("pluginNamedComponents", GenerateNamedComponentsTask.class, t -> {

SourceSet mainSourceSet = GradleUtils.getJavaSourceSets(project).findByName(SourceSet.MAIN_SOURCE_SET_NAME);
FileCollection dependencyJars = project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME);
FileCollection compiledPluginClasses = mainSourceSet.getOutput().getClassesDirs();
FileCollection classPath = dependencyJars.plus(compiledPluginClasses);
t.setClasspath(classPath);
});
Configuration pluginScannerConfig = project.getConfigurations().create("pluginScannerConfig");
DependencyHandler dependencyHandler = project.getDependencies();
pluginScannerConfig.defaultDependencies(
deps -> deps.add(
dependencyHandler.create("org.elasticsearch:elasticsearch-plugin-scanner:" + VersionProperties.getElasticsearch())
)
);
pluginNamedComponents.configure(t -> { t.setPluginScannerClasspath(pluginScannerConfig); });

final var pluginExtension = project.getExtensions().getByType(PluginPropertiesExtension.class);
pluginExtension.getBundleSpec().from(pluginNamedComponents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

package org.elasticsearch.plugin.scanner;
package org.elasticsearch.gradle.plugin.scanner;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@
* Side Public License, v 1.
*/

package org.elasticsearch.plugin.scanner;
package org.elasticsearch.gradle.plugin.scanner;

import org.elasticsearch.core.PathUtils;
import org.objectweb.asm.ClassReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -40,34 +34,17 @@ public class ClassReaders {
* This method must be used within a try-with-resources statement or similar
* control structure.
*/
public static List<ClassReader> ofDirWithJars(String path) {
if (path == null) {
public static List<ClassReader> ofDirWithJars(Path dir) {
if (dir == null) {
return Collections.emptyList();
}
Path dir = Paths.get(path);
try (var stream = Files.list(dir)) {
return ofPaths(stream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static List<ClassReader> ofPaths(Set<URL> classpathFiles) {
return ofPaths(classpathFiles.stream().map(ClassReaders::toPath));
}

private static Path toPath(URL url) {
try {
return PathUtils.get(url.toURI());
} catch (URISyntaxException e) {
throw new AssertionError(e);
}
}

/**
* This method must be used within a try-with-resources statement or similar
* control structure.
*/
public static List<ClassReader> ofPaths(Stream<Path> list) {
return list.filter(Files::exists).flatMap(p -> {
if (p.toString().endsWith(".jar")) {
Expand Down Expand Up @@ -106,18 +83,4 @@ private static List<ClassReader> classesInPath(Path root) {
}
}

public static List<ClassReader> ofClassPath() throws IOException {
String classpath = System.getProperty("java.class.path");
return ofClassPath(classpath);
}

public static List<ClassReader> ofClassPath(String classpath) {
if (classpath != null && classpath.equals("") == false) {// todo when do we set cp to "" ?
var classpathSeparator = System.getProperty("path.separator");

String[] pathelements = classpath.split(classpathSeparator);
return ofPaths(Arrays.stream(pathelements).map(Paths::get));
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

package org.elasticsearch.plugin.scanner;
package org.elasticsearch.gradle.plugin.scanner;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassReader;
Expand Down

0 comments on commit 0f87001

Please sign in to comment.