gpt4 book ai didi

gradle - 在自定义gradle插件中创建SourceSet样式语法

转载 作者:行者123 更新时间:2023-12-03 03:16:32 26 4
gpt4 key购买 nike

我正在编写一个gradle插件,需要公开两个源位置的配置。目前,我是通过项目扩展名执行此操作的,消费者gradle文件的语法如下:

generator {
idl {
baseDir = "src"
include = "*.idl"
exclude = "**/.svn"
}

template {
baseDir = "src"
include = "*.template"
exclude = "**/.svn"
}
}

我的第一次尝试是通过在扩展类上具有两个属性来完成的,如下所示:
class GeneratorExtension {
public static final NAME = "generator"

private Project project

GeneratorExtension(Project project) {
this.project = project
}

@Input ConfigurableFileTree idl
@Input ConfigurableFileTree template

void idl(String dir, Closure closure) {
idl = project.fileTree(dir, closure)
}

void template(String dir, Closure closure) {
template = project.fileTree(dir, closure)
}
}

哪个将gradle文件的语法更改为:
generator {
idl (dir = "src") {
include = "*.idl"
exclude = "*"
}

template (dir = "src") {
include = "*.template"
exclude = ""
}
}

现在可以使用,但是并不能给我带来我所希望的干净可用性。我对gradle和groovy都是陌生的,看起来我可以通过使用DefaultNamedDomainObjectSet来完成我想要的事情,但是它引用了内部类,因此这就是我在这里的原因。

使用FileTree添加扩展名的正确方法是什么,但要保持干净的使用者语法?

提前致谢。

最佳答案

感谢this和Peter的上述评论(这意味着对Peter的双重感谢),我提出了一个解决方案。我的思想中引入了NamedDomainObjectContainer,从而为使用者提供了更好的语法。

我最终得到的是:

generator {
idl {
dir = 'src'
include = "*.idl"
exclude = "*"
}

templates {
java {
dir = 'src'
include = '*.template'
}

javascript {
dir = 'src'
include = '*.template'
}
}
}

和插件代码:
class GeneratorPlugin implements Plugin<Project> {
private GeneratorExtension extension
void apply(Project project) {
def templates = project.container(FileTreeContainer)
extension = project.extensions.create(GeneratorExtension.NAME, GeneratorExtension, project, templates)
applyTasks(project)
}

void applyTasks(final Project project) {
project.task('generateJava', type: GenerateJavaTask, group: 'Preprocess', description: 'Generate Java files based on provided IDL files and templates') { }
project.task('generateJavascript', type: GenerateJavascriptTask, group: 'Preprocess', description: 'Generate Javascript files based on provided IDL files and templates') { }
}
}

class GeneratorExtension {
public static final NAME = "generator"

private Project project
@Input FileTreeContainer idl
@Input NamedDomainObjectContainer<FileTreeContainer> templates

GeneratorExtension(Project project, NamedDomainObjectContainer<FileTreeContainer> templates) {
this.project = project
idl = new FileTreeContainer("idl")
this.templates = templates
}

void idl(Closure closure) {
project.configure(idl, closure)
idl.fileTree = project.fileTree(idl.dir) {
include idl.include
exclude idl.exclude
}
}

void templates(Closure closure) {
templates.configure(closure)
String foo = "hello"
}
}

关于gradle - 在自定义gradle插件中创建SourceSet样式语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18522158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com