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

Add --flatten flag to the AST command #1723

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Changes from 1 commit
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 @@ -16,12 +16,15 @@
package software.amazon.smithy.cli.commands;

import software.amazon.smithy.build.model.SmithyBuildConfig;
import software.amazon.smithy.cli.ArgumentReceiver;
import software.amazon.smithy.cli.Arguments;
import software.amazon.smithy.cli.Command;
import software.amazon.smithy.cli.HelpPrinter;
import software.amazon.smithy.cli.dependencies.DependencyResolver;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.ModelSerializer;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.model.validation.Severity;

final class AstCommand implements Command {
Expand All @@ -48,13 +51,33 @@ public String getSummary() {
public int execute(Arguments arguments, Env env) {
arguments.addReceiver(new ConfigOptions());
arguments.addReceiver(new BuildOptions());
arguments.addReceiver(new Options());

CommandAction action = HelpActionWrapper.fromCommand(
this, parentCommandName, new ClasspathAction(dependencyResolverFactory, this::runWithClassLoader));

return action.apply(arguments, env);
}

private static final class Options implements ArgumentReceiver {
static final String FLATTEN_OPTION = "--flatten";
private boolean flatten = false;

@Override
public boolean testOption(String name) {
if (FLATTEN_OPTION.equals(name)) {
flatten = true;
return true;
}
return false;
}

@Override
public void registerHelp(HelpPrinter printer) {
printer.option(FLATTEN_OPTION, null, "Flattens and removes mixins from the model.");
}
}

private int runWithClassLoader(SmithyBuildConfig config, Arguments arguments, Env env) {
Model model = new ModelBuilder()
.config(config)
Expand All @@ -67,6 +90,10 @@ private int runWithClassLoader(SmithyBuildConfig config, Arguments arguments, En
.build();

ModelSerializer serializer = ModelSerializer.builder().build();
Options options = arguments.getReceiver(Options.class);
if (options.flatten) {
model = ModelTransformer.create().flattenAndRemoveMixins(model);
}
env.stdout().println(Node.prettyPrintJson(serializer.serialize(model)));
return 0;
}
Expand Down