Search in sources :

Example 1 with InvalidConfigurationException

use of no.entur.schema2proto.InvalidConfigurationException in project schema2proto by entur.

the class ModifyProtoMojo method execute.

public void execute() throws MojoExecutionException {
    if (configFile == null || !configFile.exists()) {
        throw new MojoExecutionException("Config file not found");
    }
    getLog().info(String.format("Modifying proto files from using config file %s. Output is defined in config file", configFile));
    try {
        ModifyProtoConfiguration configuration = ModifyProto.parseConfigurationFile(configFile, project.getBasedir());
        // Override based on maven parameter -DfailIfRemovedFields
        if (failIfRemovedFields != null) {
            configuration.failIfRemovedFields = failIfRemovedFields;
        }
        new ModifyProto().modifyProto(configuration);
    } catch (IOException e) {
        throw new MojoExecutionException("Error modifying proto files", e);
    } catch (InvalidConfigurationException e) {
        throw new MojoExecutionException("Invalid modify configuration file", e);
    } catch (InvalidProtobufException e) {
        throw new MojoExecutionException("Could not fully modify proto files", e);
    }
}
Also used : ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ModifyProto(no.entur.schema2proto.modifyproto.ModifyProto) InvalidProtobufException(no.entur.schema2proto.modifyproto.InvalidProtobufException) IOException(java.io.IOException) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException)

Example 2 with InvalidConfigurationException

use of no.entur.schema2proto.InvalidConfigurationException in project schema2proto by entur.

the class ModifyProto method parseConfigurationFile.

public static ModifyProtoConfiguration parseConfigurationFile(File configFile, File basedir) throws IOException, InvalidConfigurationException {
    ModifyProtoConfiguration configuration = new ModifyProtoConfiguration();
    try (InputStream in = Files.newInputStream(configFile.toPath())) {
        // Parse config file
        Constructor constructor = new Constructor(ModifyProtoConfigFile.class);
        TypeDescription customTypeDescription = new TypeDescription(ModifyProtoConfigFile.class);
        customTypeDescription.addPropertyParameters("newFields", NewField.class);
        customTypeDescription.addPropertyParameters("mergeFrom", MergeFrom.class);
        customTypeDescription.addPropertyParameters("valdiationRules", FieldOption.class);
        constructor.addTypeDescription(customTypeDescription);
        Yaml yaml = new Yaml(constructor);
        LOGGER.info("Using configFile {}", configFile);
        ModifyProtoConfigFile config = yaml.load(in);
        // Check config values
        if (config.outputDirectory == null) {
            throw new InvalidConfigurationException("No output directory");
        } else {
            configuration.outputDirectory = new File(basedir, config.outputDirectory);
            configuration.outputDirectory.mkdirs();
        }
        if (config.inputDirectory == null) {
            throw new InvalidConfigurationException("no input directory");
        } else {
            configuration.inputDirectory = new File(basedir, config.inputDirectory);
        }
        if (config.includes != null) {
            configuration.includes.addAll(config.includes.stream().filter(e -> StringUtils.trimToNull(e) != null).collect(Collectors.toList()));
        }
        if (config.excludes != null) {
            configuration.excludes.addAll(config.excludes.stream().filter(e -> StringUtils.trimToNull(e) != null).collect(Collectors.toList()));
        }
        if (config.mergeFrom != null) {
            configuration.mergeFrom = new ArrayList<>(config.mergeFrom);
        }
        if (config.newFields != null) {
            configuration.newFields = new ArrayList<>(config.newFields);
        }
        if (config.newEnumConstants != null) {
            configuration.newEnumConstants = new ArrayList<>(config.newEnumConstants);
        }
        if (config.fieldOptions != null) {
            configuration.fieldOptions = new ArrayList<>(config.fieldOptions);
        }
        configuration.includeBaseTypes = config.includeBaseTypes;
        if (config.protoLockFile != null) {
            configuration.protoLockFile = new File(basedir, config.protoLockFile);
        }
        configuration.failIfRemovedFields = config.failIfRemovedFields;
        if (config.customImportLocations != null) {
            configuration.customImportLocations = new ArrayList<>(config.customImportLocations.stream().filter(e -> StringUtils.trimToNull(e) != null).collect(Collectors.toList()));
        }
        configuration.basedir = basedir;
        configuration.includeGoPackageOptions = config.includeGoPackageOptions;
        configuration.goPackageSourcePrefix = config.goPackageSourcePrefix;
    }
    return configuration;
}
Also used : Schema(com.squareup.wire.schema.Schema) LoggerFactory(org.slf4j.LoggerFactory) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) MergeFrom(no.entur.schema2proto.modifyproto.config.MergeFrom) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) HashSet(java.util.HashSet) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Type(com.squareup.wire.schema.Type) GoPackageNameHelper.packageNameToGoPackageName(no.entur.schema2proto.generateproto.GoPackageNameHelper.packageNameToGoPackageName) Path(java.nio.file.Path) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException) MessageType(com.squareup.wire.schema.MessageType) FieldOption(no.entur.schema2proto.modifyproto.config.FieldOption) OptionReader(com.squareup.wire.schema.internal.parser.OptionReader) NewField(no.entur.schema2proto.modifyproto.config.NewField) Logger(org.slf4j.Logger) Files(java.nio.file.Files) BackwardsCompatibilityCheckException(no.entur.schema2proto.compatibility.BackwardsCompatibilityCheckException) Constructor(org.yaml.snakeyaml.constructor.Constructor) ProtoFile(com.squareup.wire.schema.ProtoFile) Collection(java.util.Collection) FileWriter(java.io.FileWriter) SyntaxReader(com.squareup.wire.schema.internal.parser.SyntaxReader) Set(java.util.Set) IdentifierSet(com.squareup.wire.schema.IdentifierSet) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) ProtolockBackwardsCompatibilityChecker(no.entur.schema2proto.compatibility.ProtolockBackwardsCompatibilityChecker) ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) EnumConstant(com.squareup.wire.schema.EnumConstant) NewEnumConstant(no.entur.schema2proto.modifyproto.config.NewEnumConstant) Objects(java.util.Objects) List(java.util.List) SchemaLoader(com.squareup.wire.schema.SchemaLoader) Writer(java.io.Writer) Optional(java.util.Optional) Field(com.squareup.wire.schema.Field) TypeDescription(org.yaml.snakeyaml.TypeDescription) Options(com.squareup.wire.schema.Options) EnumType(com.squareup.wire.schema.EnumType) InputStream(java.io.InputStream) ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) InputStream(java.io.InputStream) Constructor(org.yaml.snakeyaml.constructor.Constructor) TypeDescription(org.yaml.snakeyaml.TypeDescription) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException)

Example 3 with InvalidConfigurationException

use of no.entur.schema2proto.InvalidConfigurationException in project schema2proto by entur.

the class GenerateProtoMojo method execute.

public void execute() throws MojoExecutionException {
    try {
        if (configFile == null || !configFile.exists()) {
            throw new MojoExecutionException("Config file not found");
        }
        if (xsdFile == null || !xsdFile.exists()) {
            throw new MojoExecutionException("XSD file not found");
        }
        getLog().info(String.format("Generating proto files from %s using config file %s. Output is defined in config file", xsdFile, configFile));
        Schema2ProtoConfiguration configuration = new Schema2ProtoConfiguration();
        configuration.xsdFile = xsdFile;
        Schema2Proto.parseConfigurationFileIntoConfiguration(configuration, new FileInputStream(configFile));
        // Override based on maven parameter -DfailIfRemovedFields
        if (failIfRemovedFields != null) {
            configuration.failIfRemovedFields = failIfRemovedFields;
        }
        Schema2Proto.parseAndSerialize(configuration);
    } catch (MojoExecutionException | InvalidConfigurationException | IOException e) {
        throw new MojoExecutionException("Error generating proto files", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Schema2ProtoConfiguration(no.entur.schema2proto.generateproto.Schema2ProtoConfiguration) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException)

Example 4 with InvalidConfigurationException

use of no.entur.schema2proto.InvalidConfigurationException in project schema2proto by entur.

the class Schema2Proto method parseCommandLineOptions.

@SuppressWarnings("java:S3776")
private static Schema2ProtoConfiguration parseCommandLineOptions(CommandLine cmd) throws InvalidConfigurationException {
    Schema2ProtoConfiguration configuration = new Schema2ProtoConfiguration();
    String[] args = cmd.getArgs();
    if (args.length != 1) {
        throw new InvalidConfigurationException("Missing xsd file argument");
    } else {
        configuration.xsdFile = new File(args[0]);
    }
    if (!cmd.hasOption(OPTION_OUTPUT_DIRECTORY)) {
        throw new InvalidConfigurationException(OPTION_OUTPUT_DIRECTORY);
    } else {
        configuration.outputDirectory = new File(cmd.getOptionValue(OPTION_OUTPUT_DIRECTORY));
    }
    configuration.outputFilename = cmd.getOptionValue(OPTION_OUTPUT_FILENAME);
    if (cmd.hasOption(OPTION_PACKAGE)) {
        configuration.defaultProtoPackage = cmd.getOptionValue(OPTION_PACKAGE);
    }
    if (cmd.hasOption(OPTION_FORCE_PACKAGE)) {
        configuration.forceProtoPackage = cmd.getOptionValue(OPTION_FORCE_PACKAGE);
    }
    configuration.customTypeMappings = parseRegexMap(cmd, OPTION_CUSTOM_TYPE_MAPPINGS);
    configuration.customTypeReplacements = parseRegexMap(cmd, OPTION_CUSTOM_TYPE_REPLACINGS);
    configuration.customNameMappings = parseRegexMap(cmd, OPTION_CUSTOM_NAME_MAPPINGS);
    SortedMap<String, Object> options = new TreeMap<>();
    if (cmd.hasOption(OPTION_OPTIONS)) {
        for (String mapping : cmd.getOptionValue(OPTION_OPTIONS).split(",")) {
            int colon = mapping.indexOf(':');
            if (colon > -1) {
                String optionName = mapping.substring(0, colon);
                String parameter = mapping.substring(colon + 1);
                if (parameter.equals("true")) {
                    options.put(optionName, true);
                } else if (parameter.equals("false")) {
                    options.put(optionName, false);
                } else {
                    options.put(optionName, parameter);
                }
            } else {
                LOGGER.error("{} is not a option, use optionName:optionValue", mapping);
            }
        }
    }
    configuration.options = options;
    configuration.customImports = parseCommaSeparatedStringValues(cmd, OPTION_CUSTOM_IMPORTS);
    configuration.customImportLocations = parseCommaSeparatedStringValues(cmd, OPTION_CUSTOM_IMPORT_LOCATIONS);
    if (cmd.hasOption(OPTION_IGNORE_OUTPUT_FIELDS)) {
        for (String ignoreOutputField : cmd.getOptionValue(OPTION_IGNORE_OUTPUT_FIELDS).split(",")) {
            String[] split = StringUtils.split(ignoreOutputField, "/");
            configuration.ignoreOutputFields.add(new FieldPath(split[0], split[1], split[2]));
        }
    }
    if (cmd.hasOption(OPTION_INCLUDE_MESSAGE_DOCS)) {
        configuration.includeMessageDocs = Boolean.parseBoolean(cmd.getOptionValue(OPTION_INCLUDE_MESSAGE_DOCS));
    }
    if (cmd.hasOption(OPTION_INCLUDE_FIELD_DOCS)) {
        configuration.includeFieldDocs = Boolean.parseBoolean(cmd.getOptionValue(OPTION_INCLUDE_FIELD_DOCS));
    }
    if (cmd.hasOption(OPTION_INCLUDE_SOURCE_LOCATION_IN_DOC)) {
        configuration.includeSourceLocationInDoc = Boolean.parseBoolean(cmd.getOptionValue(OPTION_INCLUDE_SOURCE_LOCATION_IN_DOC));
    }
    if (cmd.hasOption(OPTION_INHERITANCE_TO_COMPOSITION)) {
        configuration.inheritanceToComposition = Boolean.parseBoolean(cmd.getOptionValue(OPTION_INHERITANCE_TO_COMPOSITION));
    }
    if (cmd.hasOption(OPTION_INCLUDE_VALIDATION_RULES)) {
        configuration.includeValidationRules = Boolean.parseBoolean(cmd.getOptionValue(OPTION_INCLUDE_VALIDATION_RULES));
    }
    if (cmd.hasOption(OPTION_INCLUDE_SKIP_EMPTY_TYPE_INHERITANCE)) {
        configuration.skipEmptyTypeInheritance = Boolean.parseBoolean(cmd.getOptionValue(OPTION_INCLUDE_SKIP_EMPTY_TYPE_INHERITANCE));
    }
    if (cmd.hasOption(OPTION_INCLUDE_XSD_OPTIONS)) {
        configuration.includeXsdOptions = Boolean.parseBoolean(cmd.getOptionValue(OPTION_INCLUDE_XSD_OPTIONS));
    }
    if (cmd.hasOption(OPTION_PROTOLOCK_FILENAME)) {
        configuration.protoLockFile = new File(cmd.getOptionValue(OPTION_PROTOLOCK_FILENAME));
    }
    if (cmd.hasOption(OPTION_DERIVATION_BY_SUBSUMPTION)) {
        configuration.derivationBySubsumption = Boolean.parseBoolean(cmd.getOptionValue(OPTION_DERIVATION_BY_SUBSUMPTION));
    }
    if (cmd.hasOption(OPTION_INCLUDE_GO_PACKAGE_OPTIONS)) {
        configuration.includeGoPackageOptions = Boolean.parseBoolean(cmd.getOptionValue(OPTION_INCLUDE_GO_PACKAGE_OPTIONS));
    }
    if (cmd.hasOption(OPTION_GO_PACKAGE_SOURCE_PREFIX)) {
        configuration.goPackageSourcePrefix = cmd.getOptionValue(OPTION_GO_PACKAGE_SOURCE_PREFIX);
    }
    return configuration;
}
Also used : TreeMap(java.util.TreeMap) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException)

Example 5 with InvalidConfigurationException

use of no.entur.schema2proto.InvalidConfigurationException in project schema2proto by entur.

the class Schema2Proto method parseConfigurationFileIntoConfiguration.

public static void parseConfigurationFileIntoConfiguration(Schema2ProtoConfiguration configuration, InputStream in) throws InvalidConfigurationException {
    Yaml yaml = new Yaml();
    Schema2ProtoConfigFile configFile = yaml.loadAs(in, Schema2ProtoConfigFile.class);
    if (configFile.outputDirectory == null) {
        throw new InvalidConfigurationException(OPTION_OUTPUT_DIRECTORY);
    } else {
        configuration.outputDirectory = new File(configFile.outputDirectory);
    }
    configuration.outputFilename = configFile.outputFilename;
    SortedMap<Pattern, String> customTypeMappings = new TreeMap<>(Comparator.comparing(Pattern::pattern));
    if (configFile.customTypeMappings != null) {
        customTypeMappings.putAll(parseRegexpKeyValue(configFile.customTypeMappings));
    }
    SortedMap<Pattern, String> customTypeReplacements = new TreeMap<>(Comparator.comparing(Pattern::pattern));
    if (configFile.customTypeReplacements != null) {
        customTypeReplacements.putAll(parseRegexpKeyValue(configFile.customTypeReplacements));
    }
    SortedMap<Pattern, String> customNameMappings = new TreeMap<>(Comparator.comparing(Pattern::pattern));
    if (configFile.customNameMappings != null) {
        customNameMappings.putAll(parseRegexpKeyValue(configFile.customNameMappings));
    }
    configuration.customTypeMappings.putAll(customTypeMappings);
    configuration.customTypeReplacements.putAll(customTypeReplacements);
    configuration.customNameMappings.putAll(customNameMappings);
    configuration.defaultProtoPackage = configFile.defaultProtoPackage;
    configuration.forceProtoPackage = configFile.forceProtoPackage;
    configuration.includeMessageDocs = configFile.includeMessageDocs;
    configuration.includeFieldDocs = configFile.includeFieldDocs;
    configuration.includeSourceLocationInDoc = configFile.includeSourceLocationInDoc;
    configuration.inheritanceToComposition = configFile.inheritanceToComposition;
    configuration.includeValidationRules = configFile.includeValidationRules;
    configuration.skipEmptyTypeInheritance = configFile.skipEmptyTypeInheritance;
    configuration.includeXsdOptions = configFile.includeXsdOptions;
    configuration.derivationBySubsumption = configFile.derivationBySubsumption;
    configuration.includeGoPackageOptions = configFile.includeGoPackageOptions;
    configuration.goPackageSourcePrefix = configFile.goPackageSourcePrefix;
    Map<String, Object> options = configFile.options;
    if (configFile.options != null) {
        configuration.options.putAll(options);
    }
    if (configFile.customImports != null) {
        configuration.customImports.addAll(configFile.customImports);
    }
    if (configFile.customImportLocations != null) {
        configuration.customImportLocations.addAll(configFile.customImportLocations);
    }
    if (configFile.ignoreOutputFields != null) {
        for (String ignoreOutputField : configFile.ignoreOutputFields) {
            String[] split = StringUtils.split(ignoreOutputField, "/");
            configuration.ignoreOutputFields.add(new FieldPath(split[0], split[1], split[2]));
        }
    }
    if (configFile.protoLockFile != null) {
        configuration.protoLockFile = new File(configFile.protoLockFile);
    }
    configuration.failIfRemovedFields = configFile.failIfRemovedFields;
}
Also used : Pattern(java.util.regex.Pattern) TreeMap(java.util.TreeMap) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException)

Aggregations

InvalidConfigurationException (no.entur.schema2proto.InvalidConfigurationException)7 ProtoFile (com.squareup.wire.schema.ProtoFile)5 File (java.io.File)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)3 ImmutableList (com.google.common.collect.ImmutableList)2 EnumConstant (com.squareup.wire.schema.EnumConstant)2 EnumType (com.squareup.wire.schema.EnumType)2 Field (com.squareup.wire.schema.Field)2 IdentifierSet (com.squareup.wire.schema.IdentifierSet)2 Location (com.squareup.wire.schema.Location)2 MessageType (com.squareup.wire.schema.MessageType)2 Options (com.squareup.wire.schema.Options)2 Schema (com.squareup.wire.schema.Schema)2 SchemaLoader (com.squareup.wire.schema.SchemaLoader)2 Type (com.squareup.wire.schema.Type)2 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)2 OptionReader (com.squareup.wire.schema.internal.parser.OptionReader)2 SyntaxReader (com.squareup.wire.schema.internal.parser.SyntaxReader)2 FileNotFoundException (java.io.FileNotFoundException)2