Xpandについて勉強しようと思います。
以下簡単なサンプル作成手順を示します。
Xpandプロジェクトの作成

EMFメタモデルを利用する。

テンプレートを作成する。

metamodelとモデルを作成

private void generate(BuildSpecification buildSpec, Object monitor)
throws CoreException {
// get project root folder as absolute file system path
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resource = root.findMember(new Path(buildSpec.getProject()
.getName()));
String containerName = resource.getLocation().toPortableString();
// configure outlets
OutputImpl output = new OutputImpl();
Outlet outlet = new Outlet(containerName);
outlet.setOverwrite(true);
output.addOutlet(outlet);
// create execution context
Map globalVarsMap = new HashMap();
XpandExecutionContextImpl execCtx = new XpandExecutionContextImpl(
output, null, globalVarsMap, null, null);
EmfRegistryMetaModel metamodel = new EmfRegistryMetaModel() {
@Override
protected EPackage[] allPackages() {
return new EPackage[] { BuildspecificationPackage.eINSTANCE,
EcorePackage.eINSTANCE };
}
};
execCtx.registerMetaModel(metamodel);
// generate
XpandFacade facade = XpandFacade.create(execCtx);
String templatePath = "template::Sample::main";
// String templatePath = "template::BuildTemplate::main";
facade.evaluate(templatePath, buildSpec);
resource.refreshLocal(IResource.DEPTH_INFINITE, null);
}
private BuildSpecification createModel(Object project, String srcDirName,
String binDirName) {
BuildSpecification buildSpecification = BuildspecificationFactory.eINSTANCE
.createBuildSpecification();
Project projectSpecification = BuildspecificationFactory.eINSTANCE
.createProject();
projectSpecification.setName("test");
projectSpecification.setBinaryFolder(binDirName);
projectSpecification.setSourceFolder(srcDirName);
buildSpecification.setProject(projectSpecification);
return buildSpecification;
}
テンプレートの中身
«IMPORT buildspecification» «EXTENSION template::GeneratorExtensions» «DEFINE main FOR BuildSpecification» «EXPAND project FOR this.project» «ENDDEFINE» «DEFINE project FOR Project» «FILE "hogebuild.xml"-»«ENDFILE» «ENDDEFINE» «DEFINE compileTarget FOR Project-» sampleです。 «EXPAND compileTarget FOR this» «EXPAND jarTarget FOR this»«ENDDEFINE» «DEFINE jarTarget FOR Project-» «ENDDEFINE»
Xtendを使って、Xpandのテンプレートの中から色々呼び出せるように拡張
import buildspecification; String distributionDir(Project this): "dist"; String fullyQualifiedJarFileName(Project this): this.distributionDir() + "/" + this.name + ".jar";
上記プログラムを起動するとテンプレートのFILEで定義したファイル名で保存される。
めんどいならActivator内にうまい具合に起動するようにしてしまばよい。
Xpandの公式サイト
0 件のコメント:
コメントを投稿