From 3a4f1390673ea29f96be1166a8f28fee4bd7444a Mon Sep 17 00:00:00 2001 From: nishadi Date: Mon, 22 May 2017 22:05:19 +0530 Subject: [PATCH 1/2] Add ability to compile files in a directory via gora-compiler --- .../gora/compiler/cli/GoraCompilerCLI.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java b/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java index c96ed57e4..39cd5a69a 100644 --- a/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java +++ b/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java @@ -46,18 +46,31 @@ public static void main(String[] args) { printHelp(); System.exit(1); } - File[] inputs = new File[args.length-1]; - for(int i = 0; i 0) + inputFiles = inputDir.listFiles(); + else { + LOG.error("Input directory must include at least one file."); printHelp(); System.exit(1); } - inputs[i] = inputFile; + } else { + inputFiles = new File[args.length - 1]; + for (int i = 0; i < inputFiles.length; i++) { + File inputFile = new File(args[i]); + if (!inputFile.isFile()) { + LOG.error("Input must be a file."); + printHelp(); + System.exit(1); + } + inputFiles[i] = inputFile; + } } try { - GoraCompiler.compileSchema(inputs, outputDir); + GoraCompiler.compileSchema(inputFiles, outputDir); LOG.info("Compiler executed SUCCESSFULL."); } catch (IOException e) { LOG.error("Error while compiling schema files. Check that the schemas are properly formatted."); From 1242a61db95dd10786a50caec1877544ad95e332 Mon Sep 17 00:00:00 2001 From: nishadi Date: Fri, 26 May 2017 20:10:17 +0530 Subject: [PATCH 2/2] Generate license to the compiled files --- .../gora/compiler/cli/GoraCompilerCLI.java | 39 +++++++++++++++++-- .../apache/gora/compiler/GoraCompiler.java | 18 ++++++++- .../gora/compiler/utils/LicenseHeaders.java | 13 +++++++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java b/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java index 39cd5a69a..ca4b0eefc 100644 --- a/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java +++ b/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java @@ -20,8 +20,9 @@ import java.io.File; import java.io.IOException; +import org.apache.commons.lang.ArrayUtils; import org.apache.gora.compiler.GoraCompiler; - +import org.apache.gora.compiler.utils.LicenseHeaders; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,6 +41,28 @@ public static void main(String[] args) { printHelp(); System.exit(1); } + // Setting the default license header to ASLv2 + LicenseHeaders licenseHeader = new LicenseHeaders("ASLv2"); + // Checking for user provided license + for (int i = 0; i < args.length; i++) { + if ("-license".equals(args[i])) { + if (i == args.length - 1) { + LOG.error("Must supply a valid license id."); + printHelp(); + System.exit(1); + } + if (licenseHeader.isValidLicense(args[i + 1])) { + licenseHeader.setLicenseName(args[i + 1]); + args = (String[]) ArrayUtils.removeElement(args, args[i + 1]); + args = (String[]) ArrayUtils.removeElement(args, args[i]); + } else { + LOG.error("Must supply a valid license id."); + printHelp(); + System.exit(1); + } + } + } + File outputDir = new File(args[args.length-1]); if(!outputDir.isDirectory()){ LOG.error("Must supply a directory for output"); @@ -70,7 +93,7 @@ public static void main(String[] args) { } } try { - GoraCompiler.compileSchema(inputFiles, outputDir); + GoraCompiler.compileSchema(inputFiles, outputDir, licenseHeader); LOG.info("Compiler executed SUCCESSFULL."); } catch (IOException e) { LOG.error("Error while compiling schema files. Check that the schemas are properly formatted."); @@ -80,6 +103,16 @@ public static void main(String[] args) { } private static void printHelp() { - LOG.info("Usage: gora-compiler ( -h | --help ) | ( [...] )"); + LOG.info("Usage: gora-compiler ( -h | --help ) | ( [...] [-license ])"); + LOG.error("License header options include;\n" + + "\t\t ASLv2 (Apache Software License v2.0) \n" + + "\t\t AGPLv3 (GNU Affero General Public License) \n" + + "\t\t CDDLv1 (Common Development and Distribution License v1.0) \n" + + "\t\t FDLv13 (GNU Free Documentation License v1.3) \n" + + "\t\t GPLv1 (GNU General Public License v1.0) \n" + + "\t\t GPLv2 (GNU General Public License v2.0) \n" + + "\t\t GPLv3 (GNU General Public License v3.0) \n" + + "\t\t LGPLv21 (GNU Lesser General Public License v2.1) \n" + + "\t\t LGPLv3 (GNU Lesser General Public License v2.1)"); } } diff --git a/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java b/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java index 469bec21a..b313bd4d8 100644 --- a/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java +++ b/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java @@ -19,6 +19,10 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -31,6 +35,7 @@ import org.apache.avro.Schema.Type; import org.apache.avro.SchemaNormalization; import org.apache.avro.compiler.specific.SpecificCompiler; +import org.apache.gora.compiler.utils.LicenseHeaders; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.node.JsonNodeFactory; @@ -57,7 +62,7 @@ public class GoraCompiler extends SpecificCompiler { GORA_HIDDEN_FIELD_NAMES.add(DIRTY_BYTES_FIELD_NAME); } - public static void compileSchema(File[] srcFiles, File dest) + public static void compileSchema(File[] srcFiles, File dest, LicenseHeaders licenseHeader) throws IOException { Schema.Parser parser = new Schema.Parser(); @@ -70,6 +75,13 @@ public static void compileSchema(File[] srcFiles, File dest) GoraCompiler compiler = new GoraCompiler(newSchema); compiler.setTemplateDir("/org/apache/gora/compiler/templates/"); compiler.compileToDestination(src, dest); + + //Adding the license to the compiled file + Path path = Paths.get(generateDestinationFileName(dest.toString(), newSchema)); + String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + content = licenseHeader.getLicense() + content.substring(content.indexOf("package")); + Files.write(path, content.getBytes(StandardCharsets.UTF_8)); + LOG.info("Compiled into: {}", dest.getAbsolutePath()); } } @@ -284,4 +296,8 @@ public static long fingerprint64(Schema schema) { return SchemaNormalization.parsingFingerprint64(schema); } + public static String generateDestinationFileName(String destDir, Schema schema) { + return destDir + File.separatorChar + schema.getNamespace().replace('.', File.separatorChar) + + File.separatorChar + schema.getName() + ".java"; + } } diff --git a/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java b/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java index 155e2d28d..54e9676ac 100644 --- a/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java +++ b/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java @@ -263,4 +263,17 @@ public String getLicense() { public String getLicenseName(){ return licenseName; } + + /** + * Validate a given license name against the supported license + * @param licenseName name of the license + * @return a booolean whether the license is supported or not + */ + public boolean isValidLicense(String licenseName) { + for (int i = 0; i < supportedLicenses.length; i++) { + if (supportedLicenses[i].equals(licenseName)) + return true; + } + return false; + } }