Skip to content

Instantly share code, notes, and snippets.

@azinneera
Last active December 14, 2021 06:25
Show Gist options
  • Save azinneera/2288815f83fe2e8b03cd762f7047c6fe to your computer and use it in GitHub Desktop.
Save azinneera/2288815f83fe2e8b03cd762f7047c6fe to your computer and use it in GitHub Desktop.
package io.asmaj.plugins.simple.codegen;
import io.ballerina.projects.plugins.CodeGenerator;
import io.ballerina.projects.plugins.CodeGeneratorContext;
import io.ballerina.projects.plugins.CompilerLifecycleContext;
import io.ballerina.projects.plugins.CompilerLifecycleEventContext;
import io.ballerina.projects.plugins.CompilerLifecycleListener;
import io.ballerina.projects.plugins.CompilerLifecycleTask;
import io.ballerina.projects.plugins.CompilerPlugin;
import io.ballerina.projects.plugins.CompilerPluginContext;
import io.ballerina.tools.text.TextDocument;
import io.ballerina.tools.text.TextDocuments;
import java.nio.charset.Charset;
/**
* A sample {@code CompilerPlugin} that generates a source file and a resource file.
*
* @since 2.0.0
*/
public class CodegenFunctionPlugin extends CompilerPlugin {
@Override
public void init(CompilerPluginContext pluginContext) {
pluginContext.addCodeGenerator(new DummySourceGenerator());
pluginContext.addCodeGenerator(new OpenApiSpecGenerator());
pluginContext.addCompilerLifecycleListener(new TestCompilerLifeCycleListener());
}
/**
* A sample {@code CodeGenerator} that creates a resource file.
*
* @since 2.0.0
*/
public static class OpenApiSpecGenerator extends CodeGenerator {
@Override
public void init(CodeGeneratorContext generatorContext) {
generatorContext.addSourceGeneratorTask(sourceGeneratorContext -> {
sourceGeneratorContext.addResourceFile("".getBytes(Charset.defaultCharset()), "openapi-spec.yaml");
});
}
}
/**
* A sample {@code CodeGenerator} that creates a source file with a function.
*
* @since 2.0.0
*/
public static class DummySourceGenerator extends CodeGenerator {
@Override
public void init(CodeGeneratorContext generatorContext) {
String content = "function dummyUtil() {}";
generatorContext.addSourceGeneratorTask(sourceGeneratorContext -> {
TextDocument textDocument = TextDocuments.from(content);
sourceGeneratorContext.addSourceFile(textDocument, "dummyfunc");
});
System.out.println("Before update:" + TestSingleton.getInstance().timestamp);
TestSingleton.getInstance().timestamp = System.currentTimeMillis();
System.out.println("After update in codegen task:" + TestSingleton.getInstance().timestamp);
}
}
private class TestCompilerLifeCycleListener extends CompilerLifecycleListener {
@Override
public void init(CompilerLifecycleContext lifecycleContext) {
lifecycleContext.addCodeGenerationCompletedTask(new CompletedTask());
}
}
private class CompletedTask implements CompilerLifecycleTask<CompilerLifecycleEventContext> {
@Override
public void perform(CompilerLifecycleEventContext compilerLifecycleEventContext) {
System.out.println("Compiler lifecycle event ctx:" + TestSingleton.getInstance().timestamp);
}
}
}
package io.asmaj.plugins.simple.codegen;
public class TestSingleton {
public long timestamp;
private static TestSingleton instance = new TestSingleton();
public static TestSingleton getInstance() {
return instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment