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);
}
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations