Gradle plugin for custom language

2020-06-27 00:54发布

问题:

I have a custom language (let's say it is MyLang but it can be any language) and I would like to make a plugin for it. The plugin needs to

  • be able to recognize the sourcesets for the language given with DSL
  • be able to compile them using an executable (the compiler)

I was able to create a plugin with a compile task (empty yet) and annotate a function with @LanguageType setting the language name to "mylang".

How can I modify the plugin to be possible to add sourcesets from build.gradle files using DSL like sourceSets { mylang { ... } }?

How can I modify the build task to be able to build the files of the source set?

  class MylangBuildPlugin implements Plugin<Project> {
    static final String COMPILE_TASK_NAME = 'compileMylang'

    void apply(Project project) {
        project.getPluginManager().apply(ComponentModelBasePlugin.class);
        createCompileTask(project)
    }

    @LanguageType
    void registerLanguage(LanguageTypeBuilder<BaseLanguageSourceSet> builder) {
        builder.setLanguageName("mylang");
        builder.defaultImplementation(BaseLanguageSourceSet.class);
    }


    private void createCompileTask(Project project) {
        project.task(COMPILE_TASK_NAME) {
        }
    }
  }