Xtext Headless模式代码生成

来源:互联网 发布:河北软件学院 编辑:程序博客网 时间:2024/05/16 05:13

Xtext

Xtext是eclipse下用于构建DSL的工具。其使用非常方便,除了支持开发DSL以外,还能得到eclipse语法高亮、错误提示等功能。http://www.eclipse.org/Xtext/index.html

建议先读读官方文档里面的最开始的几个tutorial,对Xtext的使用有个大概的了解。http://www.eclipse.org/Xtext/documentation.html#FirstFiveMinutes


Headless模式下代码生成

一般情况下,我们编写Xtext生成的DSL项目的时候,更多的时候使用在IDE环境下编写。这样虽然方便了我们的编辑,但是在项目构建等使用场景,IDE就显得有些笨拙,不容易自动化。我假设大家已经读完了Xtext文档中的例子,我们来看看如何通过命令行的方式生成DSL对应的java代码。


1.创建Xtext工程


2.写入工程名称,以及对应的DSL后缀名


3.运行mwe2,生成一堆乱七八糟的东西


4.download antlr,此时在console中会提示你下载download antlr,输入y,回车


5.编写DSL Generator,因为我们这里涂个方便,就直接使用模板中自带的,去掉注释部分就可以用


6.写main函数,用于接收一个DSL文件,生成greetings.txt

package org.kiwi.text.hello.generator;

import java.util.List;


import org.eclipse.emf.common.util.URI;

import org.eclipse.emf.ecore.resource.Resource;

import org.eclipse.emf.ecore.resource.ResourceSet;

import org.eclipse.xtext.generator.IGenerator;

import org.eclipse.xtext.generator.JavaIoFileSystemAccess;

import org.eclipse.xtext.util.CancelIndicator;

import org.eclipse.xtext.validation.CheckMode;

import org.eclipse.xtext.validation.IResourceValidator;

import org.eclipse.xtext.validation.Issue;

import org.kiwi.text.hello.MyDslStandaloneSetupGenerated;


import com.google.inject.Inject;

import com.google.inject.Injector;

import com.google.inject.Provider;


public class Main {

        public static void main(String[] args) {

               if (args.length==0) {

                        System.err.println("Aborting: no path to EMF resource provided!");

                       return;

                }

                Injector injector =new MyDslStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();

                Main main = injector.getInstance(Main.class);

                main.runGenerator(args[0]);

        }        

        @Inject 

        private Provider<ResourceSet> resourceSetProvider;

        @Inject

        private IResourceValidator validator;

        @Inject

        private IGenerator generator;

        @Inject 

        private JavaIoFileSystemAccess fileAccess;


        protected void runGenerator(String string) {

               // load the resource

                ResourceSet set =resourceSetProvider.get();

                Resource resource = set.getResource(URI.createURI(string),true);

                

               // validate the resource

                List<Issue> list =validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);

               if (!list.isEmpty()) {

                       for (Issue issue : list) {

                                System.err.println(issue);

                        }

                       return;

                }

                

               // configure and start the generator

               fileAccess.setOutputPath(".");

               generator.doGenerate(resource,fileAccess);

                

                System.out.println("Code generation finished.");

        }

}

7.配置java headless


8.Export runnable jar




注意:要选择“Package required libraries into generted JAR”

8.验证

创建文件hello.mydsl,内容如下:

Hello Jack!
Hello Lucy!
Hello Nigel!


执行代码生成:

java -jar hello-dsl.jar hello.mydsl


得到greetings.txt:

People to greet: Jack, Lucy, Nigel


更多

这里只是展示了如何使用headless模式,没有展示如何生成java代码。可以参考Xtext官方文档如何生成JVM语言。


参考资料

http://xtextcasts.org/episodes/12-building-with-ant?view=comments#comment_32

0 0