use of no.entur.schema2proto.modifyproto.config.MergeFrom in project schema2proto by entur.
the class ModifyProtoTest method testMergeProto.
@Test
public void testMergeProto() throws IOException, InvalidProtobufException, InvalidConfigurationException {
File expected = new File("src/test/resources/modify/expected/nopackagename").getCanonicalFile();
File source = new File("src/test/resources/modify/input/nopackagename").getCanonicalFile();
File mergefrom = new File("src/test/resources/modify/mergefrom/nopackagename").getCanonicalFile();
MergeFrom m = new MergeFrom();
m.sourceFolder = mergefrom;
m.protoFile = "mergefrom.proto";
ModifyProtoConfiguration configuration = new ModifyProtoConfiguration();
configuration.inputDirectory = source;
configuration.mergeFrom = Collections.singletonList(m);
modifyProto(configuration);
compareExpectedAndGenerated(expected, "mergefrom.proto", generatedRootFolder, "simple.proto");
}
use of no.entur.schema2proto.modifyproto.config.MergeFrom 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.modifyproto.config.MergeFrom in project schema2proto by entur.
the class ModifyProto method modifyProto.
public void modifyProto(ModifyProtoConfiguration configuration) throws IOException, InvalidProtobufException, InvalidConfigurationException {
SchemaLoader schemaLoader = new SchemaLoader();
// Collect source proto files (but not dependencies). Used to know which files should be written to .proto and which that should remain a dependency.
Collection<File> protoFiles = FileUtils.listFiles(configuration.inputDirectory, new String[] { "proto" }, true);
List<String> protosLoaded = protoFiles.stream().map(e -> configuration.inputDirectory.toURI().relativize(e.toURI()).getPath()).collect(Collectors.toList());
for (String importRootFolder : configuration.customImportLocations) {
schemaLoader.addSource(new File(configuration.basedir, importRootFolder).toPath());
}
schemaLoader.addSource(configuration.inputDirectory);
for (Path s : schemaLoader.sources()) {
LOGGER.info("Linking proto from path {}", s);
}
for (String s : schemaLoader.protos()) {
LOGGER.info("Linking proto {}", s);
}
Schema schema = schemaLoader.load();
// First run initial pruning, then look at the results and add referenced types from xsd.base_type
IdentifierSet.Builder initialIdentifierSet = new IdentifierSet.Builder();
initialIdentifierSet.exclude(configuration.excludes);
initialIdentifierSet.include(configuration.includes);
IdentifierSet finalIterationIdentifiers;
if (configuration.includeBaseTypes) {
finalIterationIdentifiers = followOneMoreLevel(initialIdentifierSet, schema);
} else {
finalIterationIdentifiers = initialIdentifierSet.build();
}
Schema prunedSchema = schema.prune(finalIterationIdentifiers);
for (String s : finalIterationIdentifiers.unusedExcludes()) {
LOGGER.warn("Unused exclude: {} (already excluded elsewhere or explicitly included?)", s);
}
for (String s : finalIterationIdentifiers.unusedIncludes()) {
LOGGER.warn("Unused include: {} (already included elsewhere or explicitly excluded?) ", s);
}
for (NewField newField : configuration.newFields) {
addField(newField, prunedSchema);
}
for (NewEnumConstant newEnumValue : configuration.newEnumConstants) {
addEnumConstant(newEnumValue, prunedSchema);
}
for (MergeFrom mergeFrom : configuration.mergeFrom) {
mergeFromFile(mergeFrom, prunedSchema, configuration);
}
for (FieldOption fieldOption : configuration.fieldOptions) {
addFieldOption(fieldOption, prunedSchema);
}
Set<Boolean> possibleIncompatibilitiesDetected = new HashSet<>();
if (configuration.protoLockFile != null) {
try {
ProtolockBackwardsCompatibilityChecker backwardsCompatibilityChecker = new ProtolockBackwardsCompatibilityChecker();
backwardsCompatibilityChecker.init(configuration.protoLockFile);
ImmutableList<ProtoFile> files = prunedSchema.protoFiles();
files.stream().forEach(file -> possibleIncompatibilitiesDetected.add(backwardsCompatibilityChecker.resolveBackwardIncompatibilities(file)));
} catch (FileNotFoundException e) {
throw new InvalidConfigurationException("Could not find proto.lock file, check configuration");
}
}
if (configuration.includeGoPackageOptions) {
includeGoPackageNameOptions(prunedSchema.protoFiles(), configuration.goPackageSourcePrefix);
}
Set<String> emptyImportLocations = protosLoaded.stream().map(prunedSchema::protoFile).filter(Objects::nonNull).filter(this::isEmptyFile).map(p -> p.location().getPath()).collect(Collectors.toSet());
protosLoaded.stream().map(prunedSchema::protoFile).filter(Objects::nonNull).filter(p -> !isEmptyFile(p)).forEach(file -> {
file.imports().removeIf(emptyImportLocations::contains);
file.publicImports().removeIf(emptyImportLocations::contains);
File outputFile = new File(configuration.outputDirectory, file.location().getPath());
outputFile.getParentFile().mkdirs();
try (Writer writer = new FileWriter(outputFile)) {
writer.write(file.toSchema());
} catch (IOException e) {
throw new RuntimeException(e);
}
LOGGER.info("Wrote file {}", outputFile.getPath());
});
if (configuration.failIfRemovedFields && possibleIncompatibilitiesDetected.contains(Boolean.TRUE)) {
throw new BackwardsCompatibilityCheckException("Backwards incompatibilities detected. Check warnings messages above. To ignore warnings, rerun with -DfailIfRemovedFields=false");
}
}
Aggregations