SourcesPlugin implements SmithyBuildPlugin { @Override public void execute(PluginContext context) { if (!context.getOriginalModel().isPresent()) { LOGGER.warning("No original model was provided, so the sources plugin cannot run"); return; } List<String> names; String projectionName = context.getProjectionName(); if (projectionName.equals("source")) { names = copySources(context); LOGGER.fine(() -> String.format("Copying source files to the sources of %s: %s", projectionName, names)); } else { LOGGER.fine(() -> String.format( "Creating the `%s` sources by extracting relevant components from the original model", projectionName)); names = ListUtils.of(PROJECTED_FILENAME); projectSources(context); } if (names.isEmpty()) { LOGGER.info(String.format("Skipping `%s` manifest because no Smithy sources found", projectionName)); } else { LOGGER.fine(() -> String.format("Writing `%s` manifest", projectionName)); String manifest = names.stream().map(name -> name.replace("\\", "/")).collect(Collectors.joining("\n")); context.getFileManifest().writeFile("manifest", manifest + "\n"); } }  @Override String getName(); @Override void execute(PluginContext context);  }
@Test public void copiesFilesForSourceProjection() throws URISyntaxException { Model model = Model.assembler() .addImport(getClass().getResource("sources/a.smithy")) .addImport(getClass().getResource("sources/b.smithy")) .addImport(getClass().getResource("sources/c/c.json")) .addImport(getClass().getResource("notsources/d.smithy")) .assemble() .unwrap(); MockManifest manifest = new MockManifest(); PluginContext context = PluginContext.builder() .fileManifest(manifest) .model(model) .originalModel(model) .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent())) .build(); new SourcesPlugin().execute(context); String manifestString = manifest.getFileString("manifest").get(); manifestString = manifestString.replace("\\", "/"); assertThat(manifestString, containsString("a.smithy\n")); assertThat(manifestString, containsString("b.smithy\n")); assertThat(manifestString, containsString("c/c.json\n")); assertThat(manifestString, not(containsString("d.json"))); assertThat(manifest.getFileString("a.smithy").get(), containsString("AString")); assertThat(manifest.getFileString("b.smithy").get(), containsString("BString")); assertThat(manifest.getFileString("c/c.json").get(), containsString("CString")); }
@Test public void copiesModelFromJarWithSourceProjection() throws URISyntaxException { Model model = Model.assembler() .addImport(getClass().getResource("sources/jar-import.jar")) .addImport(getClass().getResource("notsources/d.smithy")) .assemble() .unwrap(); MockManifest manifest = new MockManifest(); PluginContext context = PluginContext.builder() .fileManifest(manifest) .model(model) .originalModel(model) .sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").toURI()))) .build(); new SourcesPlugin().execute(context); String manifestString = manifest.getFileString("manifest").get(); manifestString = manifestString.replace("\\", "/"); assertThat(manifestString, containsString("jar-import/a.smithy\n")); assertThat(manifestString, containsString("jar-import/b/b.smithy\n")); assertThat(manifestString, containsString("jar-import/b/c/c.json\n")); assertThat(manifestString, not(containsString("jar-import/d.json"))); assertThat(manifest.getFileString("jar-import/a.smithy").get(), containsString("string A")); assertThat(manifest.getFileString("jar-import/b/b.smithy").get(), containsString("string B")); assertThat(manifest.getFileString("jar-import/b/c/c.json").get(), containsString("\"foo.baz#C\"")); }
@Test public void copiesModelFromJarWithNonSourceProjection() throws URISyntaxException { Model model = Model.assembler() .addImport(getClass().getResource("sources/jar-import.jar")) .addImport(getClass().getResource("notsources/d.smithy")) .assemble() .unwrap(); MockManifest manifest = new MockManifest(); ProjectionConfig projection = ProjectionConfig.builder().build(); PluginContext context = PluginContext.builder() .fileManifest(manifest) .projection("foo", projection) .model(model) .originalModel(model) .sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").toURI()))) .build(); new SourcesPlugin().execute(context); String manifestString = manifest.getFileString("manifest").get(); manifestString = manifestString.replace("\\", "/"); assertThat(manifestString, containsString("model.json")); assertThat(manifestString, not(containsString("jar-import"))); assertThat(manifest.getFileString("model.json").get(), containsString("\"foo.baz#A\"")); assertThat(manifest.getFileString("model.json").get(), containsString("\"foo.baz#B\"")); assertThat(manifest.getFileString("model.json").get(), containsString("\"foo.baz#C\"")); }
@Test public void copiesOnlyFilesFromSourcesForProjection() throws URISyntaxException { Model model = Model.assembler() .addImport(getClass().getResource("sources/a.smithy")) .addImport(getClass().getResource("sources/b.smithy")) .addImport(getClass().getResource("sources/c/c.json")) .addImport(getClass().getResource("notsources/d.smithy")) .assemble() .unwrap(); MockManifest manifest = new MockManifest(); ProjectionConfig projection = ProjectionConfig.builder().build(); PluginContext context = PluginContext.builder() .projection("foo", projection) .fileManifest(manifest) .model(model) .originalModel(model) .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent())) .build(); new SourcesPlugin().execute(context); String manifestString = manifest.getFileString("manifest").get(); String modelString = manifest.getFileString("model.json").get(); assertThat(manifestString, equalTo("model.json\n")); assertThat(modelString, containsString("AString")); assertThat(modelString, containsString("BString")); assertThat(modelString, containsString("CString")); assertThat(modelString, containsString("metadata-a")); assertThat(modelString, containsString("metadata-b")); assertThat(modelString, containsString("metadata-c")); assertThat(modelString, containsString("Atrait")); assertThat(modelString, containsString("Btrait")); assertThat(modelString, containsString("Ctrait")); assertThat(modelString, not(containsString("DString"))); assertThat(modelString, not(containsString("metadata-d"))); assertThat(modelString, not(containsString("Dtrait"))); }
@Test public void treatsNewlyAddedShapesAsNewSources() throws URISyntaxException { Model originalModel = Model.assembler().assemble().unwrap(); Model newModel = Model.assembler() .addShape(StringShape.builder().id("a.b#MyString").build()) .assemble() .unwrap(); MockManifest manifest = new MockManifest(); ProjectionConfig projection = ProjectionConfig.builder().build(); PluginContext context = PluginContext.builder() .projection("foo", projection) .fileManifest(manifest) .originalModel(originalModel) .model(newModel) .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent())) .build(); new SourcesPlugin().execute(context); String modelString = manifest.getFileString("model.json").get(); assertThat(modelString, containsString("MyString")); }
@Test public void doesNotAllowConflicts() throws URISyntaxException { Model model = Model.assembler() .addImport(getClass().getResource("sources/a.smithy")) .addImport(getClass().getResource("conflicting/a.smithy")) .assemble() .unwrap(); MockManifest manifest = new MockManifest(); PluginContext context = PluginContext.builder() .fileManifest(manifest) .model(model) .originalModel(model) .sources(ListUtils.of( Paths.get(getClass().getResource("sources/a.smithy").toURI()), Paths.get(getClass().getResource("conflicting/a.smithy").toURI()))) .build(); Assertions.assertThrows(SourcesConflictException.class, () -> new SourcesPlugin().execute(context)); }