diff --git a/packages/java-packages/codesnippet-maven-plugin/CHANGELOG.md b/packages/java-packages/codesnippet-maven-plugin/CHANGELOG.md index 1954edbb286..0e059c4270b 100644 --- a/packages/java-packages/codesnippet-maven-plugin/CHANGELOG.md +++ b/packages/java-packages/codesnippet-maven-plugin/CHANGELOG.md @@ -1,5 +1,11 @@ # Release History +## 1.0.0-beta.8 (2022-08-09) + +### Features Added + +- Added verbose exception indicating mismatch BEGIN and END codesnippet aliases to aid in troubleshooting snippet injection failures. + ## 1.0.0-beta.7 (2022-04-13) ### Bugs Fixed diff --git a/packages/java-packages/codesnippet-maven-plugin/README.md b/packages/java-packages/codesnippet-maven-plugin/README.md index 4fca5f11d92..b6e5e902078 100644 --- a/packages/java-packages/codesnippet-maven-plugin/README.md +++ b/packages/java-packages/codesnippet-maven-plugin/README.md @@ -12,7 +12,7 @@ First, reference the plugin in your maven project's `pom.xml` file. com.azure.tools codesnippet-maven-plugin - 1.0.0-beta.1 + 1.0.0-beta.8 **/src/samples/java/**/*.java ${project.basedir}/src/samples/java diff --git a/packages/java-packages/codesnippet-maven-plugin/pom.xml b/packages/java-packages/codesnippet-maven-plugin/pom.xml index c7df3b8bb8e..b107e4cabd6 100644 --- a/packages/java-packages/codesnippet-maven-plugin/pom.xml +++ b/packages/java-packages/codesnippet-maven-plugin/pom.xml @@ -6,7 +6,7 @@ com.azure.tools codesnippet-maven-plugin - 1.0.0-beta.7 + 1.0.0-beta.8 maven-plugin Codesnippet Maven Plugin diff --git a/packages/java-packages/codesnippet-maven-plugin/src/main/java/com/azure/tools/codesnippetplugin/implementation/SnippetDictionary.java b/packages/java-packages/codesnippet-maven-plugin/src/main/java/com/azure/tools/codesnippetplugin/implementation/SnippetDictionary.java index cafa9daf084..e72e1dad2e3 100644 --- a/packages/java-packages/codesnippet-maven-plugin/src/main/java/com/azure/tools/codesnippetplugin/implementation/SnippetDictionary.java +++ b/packages/java-packages/codesnippet-maven-plugin/src/main/java/com/azure/tools/codesnippetplugin/implementation/SnippetDictionary.java @@ -13,11 +13,30 @@ */ public final class SnippetDictionary { private final Map> snippetDictionary = new HashMap<>(); + private final List missingBeginTag = new ArrayList<>(); public boolean isActive() { return !snippetDictionary.isEmpty(); } + /** + * Gets all codesnippet aliases that are missing a corresponding end tag, aka all begin tags missing an end tag. + * + * @return All codesnippet aliases that are missing an end tag. + */ + public List getMissingEndTags() { + return new ArrayList<>(snippetDictionary.keySet()); + } + + /** + * Gets all codesnippet aliases that are missing a corresponding begin tag, aka all end tags missing a begin tag. + * + * @return All codesnippet aliases that are missing a begin tag. + */ + public List getMissingBeginTags() { + return missingBeginTag; + } + public void beginSnippet(String key) { if (!this.snippetDictionary.containsKey((key))) { this.snippetDictionary.put(key, new ArrayList<>()); @@ -31,9 +50,18 @@ public void processLine(String line) { } public List finalizeSnippet(String key) { - List value = this.snippetDictionary.get(key); - this.snippetDictionary.remove(key); + List value = null; + + // Check the dictionary for containing the key. + if (snippetDictionary.containsKey(key)) { + // If it does contain the key return the codesnippet for the key. + value = snippetDictionary.remove(key); + } else { + // Otherwise add the codesnippet to the list of keys that have missing begin tags. + missingBeginTag.add(key); + } + // Return no matter what, if begin tags are missing an exception will be thrown later. return value; } } diff --git a/packages/java-packages/codesnippet-maven-plugin/src/main/java/com/azure/tools/codesnippetplugin/implementation/SnippetReplacer.java b/packages/java-packages/codesnippet-maven-plugin/src/main/java/com/azure/tools/codesnippetplugin/implementation/SnippetReplacer.java index 376f65b7cbd..f822d3bd300 100644 --- a/packages/java-packages/codesnippet-maven-plugin/src/main/java/com/azure/tools/codesnippetplugin/implementation/SnippetReplacer.java +++ b/packages/java-packages/codesnippet-maven-plugin/src/main/java/com/azure/tools/codesnippetplugin/implementation/SnippetReplacer.java @@ -57,13 +57,13 @@ public final class SnippetReplacer { /** * The "verification" operation encapsulated by this function is as follows. - * + *

* 1. Scan under the target direction for all discovered code snippet DEFINITIONS 2. Examine all snippet CALLS, * finding where updates are needed. 3. Report all discovered snippets in need of update as well as all bad snippet * calls - * + *

* A "bad snippet call" is simply calling for a snippet whose ID has no definition. - * + *

* See {@link #updateCodesnippets(RootAndGlob, List, RootAndGlob, boolean, RootAndGlob, List, boolean, int, boolean, Log)} * for details on actually defining and calling snippets. */ @@ -405,6 +405,8 @@ private static List verifySnippets(Path file, Pattern beginReg static Map getAllSnippets(List snippetSources) throws IOException, MojoExecutionException { Map> codesnippets = new HashMap<>(); + Map> missingBeginTag = new HashMap<>(); + Map> missingEndTag = new HashMap<>(); for (Path samplePath : snippetSources) { List fileContent = Files.readAllLines(samplePath, StandardCharsets.UTF_8); @@ -432,9 +434,17 @@ static Map getAllSnippets(List snippetSources) snippetReader.processLine(line); } } + + if (!snippetReader.getMissingEndTags().isEmpty()) { + missingEndTag.put(samplePath.toString(), snippetReader.getMissingEndTags()); + } + + if (!snippetReader.getMissingBeginTags().isEmpty()) { + missingBeginTag.put(samplePath.toString(), snippetReader.getMissingBeginTags()); + } } - String potentialErrorMessage = createDuplicateCodesnippetErrorMessage(codesnippets); + String potentialErrorMessage = createInvalidSnippetsErrorMessage(codesnippets, missingEndTag, missingBeginTag); if (!potentialErrorMessage.isEmpty()) { throw new MojoExecutionException(potentialErrorMessage); } @@ -443,7 +453,8 @@ static Map getAllSnippets(List snippetSources) .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get(0))); } - private static String createDuplicateCodesnippetErrorMessage(Map> codesnippets) { + private static String createInvalidSnippetsErrorMessage(Map> codesnippets, + Map> missingEndTags, Map> missingBeginTags) { StringBuilder errorMessage = new StringBuilder(); for (Map.Entry> codesnippetsById : codesnippets.entrySet()) { @@ -470,6 +481,32 @@ private static String createDuplicateCodesnippetErrorMessage(Map> missingEndTag : missingEndTags.entrySet()) { + errorMessage.append("The following codesnippet aliases in file' ") + .append(missingEndTag.getKey()) + .append("' didn't have a matching END alias:") + .append(System.lineSeparator()); + + for (String alias : missingEndTag.getValue()) { + errorMessage.append(" - ").append(alias).append(System.lineSeparator()); + } + + errorMessage.append(System.lineSeparator()); + } + + for (Map.Entry> missingBeginTag : missingBeginTags.entrySet()) { + errorMessage.append("The following codesnippet aliases in file '") + .append(missingBeginTag.getKey()) + .append("' didn't have a matching BEGIN alias:") + .append(System.lineSeparator()); + + for (String alias : missingBeginTag.getValue()) { + errorMessage.append(" - ").append(alias).append(System.lineSeparator()); + } + + errorMessage.append(System.lineSeparator()); + } + return errorMessage.toString(); }