2

我正在编写一个小 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 的正确方法是什么,但保持干净的使用者语法?

提前致谢。

4

1 回答 1

2

多亏了这一点和上面彼得的评论(这意味着双重感谢彼得),我想出了一个解决方案。在我的想法中引入 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"
        }
    }
于 2013-08-30T23:15:56.663 回答