-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional module name to plugin properties (#86016)
In preparation for modularizing Elasticsearch server, each plugin needs a way to declare itself as modular. When modularized, the main class of a plugin must be found in a named module. This commit adds a module name property to the plugin properties template and gradle support for setting it, as well as automatically inferring the module name when a module-info.java exists in the source a plugin.
- Loading branch information
Showing
12 changed files
with
427 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
build-tools/src/main/java/org/elasticsearch/gradle/plugin/GeneratePluginPropertiesTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.plugin; | ||
|
||
import groovy.text.SimpleTemplateEngine; | ||
import groovy.text.Template; | ||
|
||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.InvalidUserDataException; | ||
import org.gradle.api.file.ConfigurableFileCollection; | ||
import org.gradle.api.file.ProjectLayout; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.provider.ListProperty; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.InputFiles; | ||
import org.gradle.api.tasks.Optional; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.tree.ClassNode; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.UncheckedIOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
|
||
public abstract class GeneratePluginPropertiesTask extends DefaultTask { | ||
|
||
private static final String PROPERTIES_FILENAME = "plugin-descriptor.properties"; | ||
|
||
@Inject | ||
public GeneratePluginPropertiesTask(ProjectLayout projectLayout) { | ||
setDescription("Generate " + PROPERTIES_FILENAME); | ||
getOutputFile().convention(projectLayout.getBuildDirectory().file("generated-descriptor/" + PROPERTIES_FILENAME)); | ||
} | ||
|
||
@Input | ||
public abstract Property<String> getPluginName(); | ||
|
||
@Input | ||
public abstract Property<String> getPluginDescription(); | ||
|
||
@Input | ||
public abstract Property<String> getPluginVersion(); | ||
|
||
@Input | ||
public abstract Property<String> getElasticsearchVersion(); | ||
|
||
@Input | ||
public abstract Property<String> getJavaVersion(); | ||
|
||
@Optional | ||
@Input | ||
public abstract Property<String> getClassname(); | ||
|
||
@Input | ||
public abstract ListProperty<String> getExtendedPlugins(); | ||
|
||
@Input | ||
public abstract Property<Boolean> getHasNativeController(); | ||
|
||
@Input | ||
public abstract Property<Boolean> getRequiresKeystore(); | ||
|
||
@Input | ||
public abstract Property<PluginType> getPluginType(); | ||
|
||
@Input | ||
public abstract Property<String> getJavaOpts(); | ||
|
||
@Input | ||
public abstract Property<Boolean> getIsLicensed(); | ||
|
||
@InputFiles | ||
public abstract ConfigurableFileCollection getModuleInfoFile(); | ||
|
||
@OutputFile | ||
public abstract RegularFileProperty getOutputFile(); | ||
|
||
@TaskAction | ||
public void generatePropertiesFile() throws IOException { | ||
PluginType pluginType = getPluginType().get(); | ||
String classname = getClassname().getOrElse(""); | ||
if (pluginType.equals(PluginType.BOOTSTRAP) == false && classname.isEmpty()) { | ||
throw new InvalidUserDataException("classname is a required setting for esplugin"); | ||
} | ||
|
||
Map<String, Object> props = new HashMap<>(); | ||
props.put("name", getPluginName().get()); | ||
props.put("description", getPluginDescription().get()); | ||
props.put("version", getPluginVersion().get()); | ||
props.put("elasticsearchVersion", getElasticsearchVersion().get()); | ||
props.put("javaVersion", getJavaVersion().get()); | ||
props.put("classname", classname); | ||
props.put("extendedPlugins", String.join(",", getExtendedPlugins().get())); | ||
props.put("hasNativeController", getHasNativeController().get()); | ||
props.put("requiresKeystore", getRequiresKeystore().get()); | ||
props.put("type", pluginType.toString()); | ||
props.put("javaOpts", getJavaOpts().get()); | ||
props.put("licensed", getIsLicensed().get()); | ||
props.put("modulename", findModuleName()); | ||
|
||
SimpleTemplateEngine engine = new SimpleTemplateEngine(); | ||
Path outputFile = getOutputFile().get().getAsFile().toPath(); | ||
Files.createDirectories(outputFile.getParent()); | ||
try ( | ||
var inputStream = GeneratePluginPropertiesTask.class.getResourceAsStream("/" + PROPERTIES_FILENAME); | ||
var reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
var writer = Files.newBufferedWriter(outputFile, StandardCharsets.UTF_8) | ||
) { | ||
Template template = engine.createTemplate(reader); | ||
template.make(props).writeTo(writer); | ||
} | ||
} | ||
|
||
private String findModuleName() { | ||
if (getModuleInfoFile().isEmpty()) { | ||
return ""; | ||
} | ||
Path moduleInfoSource = getModuleInfoFile().getSingleFile().toPath(); | ||
ClassNode visitor = new ClassNode(); | ||
try (var inputStream = Files.newInputStream(moduleInfoSource)) { | ||
new ClassReader(inputStream).accept(visitor, 0); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
return visitor.module.name; | ||
} | ||
} |
Oops, something went wrong.