Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate upgraded models in MigrateCommand #1579

Merged
merged 1 commit into from
May 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.model.validation.Severity;
import software.amazon.smithy.model.validation.ValidatedResultException;
import software.amazon.smithy.utils.IoUtils;
import software.amazon.smithy.utils.Pair;
import software.amazon.smithy.utils.SimpleParser;
import software.amazon.smithy.utils.StringUtils;

Expand Down Expand Up @@ -171,8 +173,31 @@ private int run(Arguments arguments, Env env) {
smithyBuild.build(resultConsumer, resultConsumer);
Model finalizedModel = resultConsumer.getResult().getModel();

for (Path modelFile : resolveModelFiles(finalizedModel, models)) {
writeMigratedFile(finalizedModel, modelFile);
List<Path> resolvedModelFiles = resolveModelFiles(finalizedModel, models);

// Validate upgraded models before writing
ModelAssembler assembler = ModelBuilder.createModelAssembler(classLoader);
smithyBuildConfig.getImports().forEach(assembler::addImport);

List<Pair<Path, String>> upgradedModels = new ArrayList<>();
for (Path modelFilePath : resolvedModelFiles) {
String upgradedModelString = upgradeFile(finalizedModel, modelFilePath);
upgradedModels.add(Pair.of(modelFilePath, upgradedModelString));
// Replace existing models with upgraded models for a Smithy IDL model file
assembler.addUnparsedModel(modelFilePath.toAbsolutePath().toString(), upgradedModelString);
}


try {
assembler.assemble().validate();
} catch (ValidatedResultException e) {
throw new RuntimeException("Upgraded Smithy models are invalid. "
+ "Please report the following errors to Smithy team.\n"
+ e.getMessage());
}

for (Pair<Path, String> upgradedModel : upgradedModels) {
writeMigratedFile(upgradedModel.right, upgradedModel.left);
}

return 0;
Expand Down Expand Up @@ -204,9 +229,9 @@ private List<Path> resolveModelFiles(Model model, List<String> modelFilesOrDirec
.collect(Collectors.toList());
}

private void writeMigratedFile(Model completeModel, Path filePath) {
private void writeMigratedFile(String upgradeFileString, Path filePath) {
try {
Files.write(filePath, upgradeFile(completeModel, filePath).getBytes(StandardCharsets.UTF_8));
Files.write(filePath, upgradeFileString.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new CliError(format("Unable to write migrated model file to %s: %s", filePath, e));
}
Expand Down