Xtext Headless模式代码生成

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 {

publicstatic 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 IResourceValidatorvalidator;

@Inject

private IGeneratorgenerator;

@Inject

private JavaIoFileSystemAccessfileAccess;

protectedvoid 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

时间: 2024-07-30 10:21:19

Xtext Headless模式代码生成的相关文章

Chrome headless 模式

我们在通过Selenium运行自动化测试时,必须要启动浏览器,浏览器的启动与关闭必然会影响执行效率,而且还会干扰你做其它事情(本机运行的话). 那能不能把自动化测试的运行放在后台?当然可以! htmlunit 项目可以模拟浏览器运行,是一个没有界面的浏览器,运行速度快. PhantomJS 是一个基于webkit的JavaScript API.它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaScript代码.任何你可以在基于webkit浏览器做的事情,它都能做

chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式

WebDriverException: Message: unknown error: Chrome failed to start: crashed 1. 背景在使用selenium + chrome浏览器渲染模式爬取数据时,如果并发任务过多,或者爬虫的运行时间很长,那么很容易出现浏览器崩溃的现象,如下: 这一般是资源消耗过大造成的(据说chrome浏览器有内存泄漏的情况).那如何解决这个问题呢? 这种情况下,我们首先就会想到使用无界面的浏览器PhantomJS,但是PhantomJS现在年久

chrome headless Manual 模式渲染网页

可以看看这个里面的介绍,写得很好.https://duo.com/blog/driving-headless-chrome-with-python from selenium import webdriver from selenium.webdriver.chrome.options import Options` chrome_options = Options() chrome_options.add_argument("--headless") ##需要加上,否则会弹出真实浏览

selenium(六)Headless Chrome/Firefox--PhantomJS停止支持后,使用无界面模式。

简介: 以前都用PhantomJS来进行无界面模式的自动化测试,或者爬取某些动态页面. 但是最近selenium更新以后,'Selenium support for PhantomJS has been deprecated, please use headless '提示不支持PhantomJs,请使用headless模式. 好吧,我们还是继续使用firefox chrome的headless模式吧. 一:版本确认 1.windows下 selenium  3.9.0 我使用这个版本的sele

【转】利用 selenium 的 webdrive 驱动 headless chrome

1.参考 使用 headless chrome进行测试 2.概念 Headless模式解决了什么问题: 自动化工具例如 selenium 利用有头浏览器进行测试,面临效率和稳定性的影响,所以出现了 Headless Browser, 3年前,无头浏览器 PhantomJS 已经如火如荼出现了,紧跟着 NightmareJS 也成为一名巨星.无头浏览器带来巨大便利性:页面爬虫.自动化测试.WebAutomation... 用过PhantomJS的都知道,它的环境是运行在一个封闭的沙盒里面,在环境内

反爬虫——使用chrome headless时一些需要注意的细节

以前我们介绍过chrome headless的用法(https://www.cnblogs.com/apocelipes/p/9264673.html). 今天我们要稍微提一下其中一个细节. 反爬和window.navigator对象 navigator对象,一个对大家来说既熟悉又陌生的名词,熟悉是因为在学BOM对象的时候或多或少都见过甚至在代码中使用过,陌生是因为对于navigator对象来说虽然各大浏览器都有实现却一直没有一个统一的标准,所以在不同浏览器上的navigator对象也可能是不一

Headless Chrome入门

原文地址:Getting Started with Headless Chrome  By EricBidelman  Engineer @ Google working on web tooling: Headless Chrome, Puppeteer, Lighthouse Headless Chrome在Chrome59中发布,用于在headless环境中运行Chrome浏览器,也就是在非Chrome环境中运行Chrome.它将Chromium和Blink渲染引擎提供的所有现代Web平台

Python 爬虫杂记 - Chrome Headless

Chrome Headless使用 测试 Chrome 版本: 62.0.3202.89(正式版本)(64 位) Python环境:python2.7 注: Headless模式需要59版本及以上! Chrome的安装与配置不在此赘述, 不过需要注意的是: 版本号与驱动的映射关系! 版本号与驱动的映射关系!! 版本号与驱动的映射关系!!! Chrome与Chromedriver的映射关系表: Chromedriver下载链接:http://chromedriver.storage.googlea

ThoughtWorks 2017技术雷达

前言: ThoughtWorks人酷爱技术.我们对技术进行构建.研究. 测试.开源.记述,并始终致力于对其进行改进-以求造福 大众.我们的使命是支持卓越软件并掀起IT革命.我们创建 并分享ThoughtWorks技术雷达就是为了支持这一使命.由 ThoughtWorks中一群资深技术领导组成的ThoughtWorks 技术顾问委员会(TAB)创建了该雷达.他们定期开会讨论 Thoughtworks的全球技术战略以及对行业有重大影响的 技术趋势. 这个雷达以独特的形式记录技术顾问委员会的讨论结果,