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

时间:2013-08-29 22:46:37

标签: gradle

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

提前致谢。

1 个答案:

答案 0 :(得分:2)

感谢this和彼得在上面的评论(这意味着再次感谢彼得),我想出了一个解决方案。在我的思想中引入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"
        }
    }