This is an automated email from the ASF dual-hosted git repository. wusheng pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-skywalking-oal-tool.git
The following commit(s) were added to refs/heads/master by this push: new d8243d2 Finish the first file generation. d8243d2 is described below commit d8243d2c5ba309daf1cd4f81c05522dbcd9a79bb Author: Wu Sheng <wu.sh...@foxmail.com> AuthorDate: Sun Aug 5 22:49:23 2018 +0800 Finish the first file generation. --- .../java/org/apache/skywalking/oal/tool/Main.java | 16 +++++++++-- .../skywalking/oal/tool/output/FileGenerator.java | 31 +++++++++++++++++++++- .../skywalking/oal/tool/output/WriteWrapper.java} | 12 ++++++++- oal-parser/src/test/resources/oal_test.oal | 4 ++- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/oal-parser/src/main/java/org/apache/skywalking/oal/tool/Main.java b/oal-parser/src/main/java/org/apache/skywalking/oal/tool/Main.java index 19880c5..e454aa5 100644 --- a/oal-parser/src/main/java/org/apache/skywalking/oal/tool/Main.java +++ b/oal-parser/src/main/java/org/apache/skywalking/oal/tool/Main.java @@ -18,24 +18,30 @@ package org.apache.skywalking.oal.tool; +import freemarker.template.TemplateException; import java.io.File; +import java.io.IOException; +import java.util.List; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; +import org.apache.skywalking.oal.tool.output.FileGenerator; +import org.apache.skywalking.oal.tool.parser.AnalysisResult; +import org.apache.skywalking.oal.tool.parser.ScriptParser; /** * The main of SkyWalking OAL tool */ public class Main { - public static void main(String[] args) throws ParseException { + public static void main(String[] args) throws ParseException, IOException, TemplateException { Options options = new Options(); options.addRequiredOption("s", "script-filepath", true, "Need the absolute file path of OAL script."); options.addRequiredOption("o", "output", true, "Need the root output folder of the generated codes."); CommandLineParser parser = new DefaultParser(); - CommandLine line = parser.parse( options, args ); + CommandLine line = parser.parse(options, args); String scriptFilePath = line.getOptionValue("s"); String outputPath = line.getOptionValue("o"); @@ -44,5 +50,11 @@ public class Main { if (!scriptFile.exists()) { throw new IllegalArgumentException("OAL script file [" + scriptFilePath + "] doesn't exist"); } + + ScriptParser scriptParser = ScriptParser.createFromFile(scriptFilePath); + List<AnalysisResult> analysisResults = scriptParser.parse(); + + FileGenerator generator = new FileGenerator(analysisResults, outputPath); + generator.generate(); } } diff --git a/oal-parser/src/main/java/org/apache/skywalking/oal/tool/output/FileGenerator.java b/oal-parser/src/main/java/org/apache/skywalking/oal/tool/output/FileGenerator.java index 7d0fdc6..f7590a4 100644 --- a/oal-parser/src/main/java/org/apache/skywalking/oal/tool/output/FileGenerator.java +++ b/oal-parser/src/main/java/org/apache/skywalking/oal/tool/output/FileGenerator.java @@ -21,6 +21,8 @@ package org.apache.skywalking.oal.tool.output; import freemarker.template.Configuration; import freemarker.template.TemplateException; import freemarker.template.Version; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.List; @@ -42,8 +44,35 @@ public class FileGenerator { this.toDispatchers(); } - public void generate() { + public void generate() throws IOException, TemplateException { + for (AnalysisResult result : results) { + generate(result, "AggregateWorker.java", (writer) -> generateAggregateWorker(result, writer)); + } + + } + + private void generate(AnalysisResult result, String fileSuffix, + WriteWrapper writeWrapper) throws IOException, TemplateException { + File file = new File(outputPath, buildSubFolderName(result, fileSuffix)); + if (!file.exists()) { + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + file.createNewFile(); + } + FileWriter fileWriter = new FileWriter(file); + try { + writeWrapper.execute(fileWriter); + } finally { + fileWriter.close(); + } + } + private String buildSubFolderName(AnalysisResult result, String suffix) { + return "generated/" + + result.getSourceName().toLowerCase() + "/" + + result.getMetricName().toLowerCase() + "/" + + result.getMetricName() + suffix; } void generateAggregateWorker(AnalysisResult result, Writer output) throws IOException, TemplateException { diff --git a/oal-parser/src/test/resources/oal_test.oal b/oal-parser/src/main/java/org/apache/skywalking/oal/tool/output/WriteWrapper.java similarity index 74% copy from oal-parser/src/test/resources/oal_test.oal copy to oal-parser/src/main/java/org/apache/skywalking/oal/tool/output/WriteWrapper.java index 525d14d..3d5a0b7 100644 --- a/oal-parser/src/test/resources/oal_test.oal +++ b/oal-parser/src/main/java/org/apache/skywalking/oal/tool/output/WriteWrapper.java @@ -14,4 +14,14 @@ * See the License for the specific language governing permissions and * limitations under the License. * - */ \ No newline at end of file + */ + +package org.apache.skywalking.oal.tool.output; + +import freemarker.template.TemplateException; +import java.io.FileWriter; +import java.io.IOException; + +public interface WriteWrapper { + void execute(FileWriter fileWriter) throws IOException, TemplateException; +} diff --git a/oal-parser/src/test/resources/oal_test.oal b/oal-parser/src/test/resources/oal_test.oal index 525d14d..53d727a 100644 --- a/oal-parser/src/test/resources/oal_test.oal +++ b/oal-parser/src/test/resources/oal_test.oal @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. * - */ \ No newline at end of file + */ + +ServiceAvg = from(Service.latency).avg(); \ No newline at end of file