Skip to content

Commit

Permalink
feat: handle standardize START/END_EXCLUDE for snippets (#624)
Browse files Browse the repository at this point in the history
`[START_EXCLUDE]` and `[END_EXCLUDE]` region tags are standardized for snippets pulled into cloud.google.com.
  • Loading branch information
chingor13 authored Jun 11, 2020
1 parent d06f203 commit a0ee80e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 18 additions & 3 deletions synthtool/gcp/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

OPEN_SNIPPET_REGEX = r".*\[START ([a-z0-9_]+)\].*$"
CLOSE_SNIPPET_REGEX = r".*\[END ([a-z0-9_]+)\].*$"
OPEN_EXCLUDE_REGEX = r".*\[START_EXCLUDE\].*$"
CLOSE_EXCLUDE_REGEX = r".*\[END_EXCLUDE\].*$"


def _trim_leading_whitespace(lines: List[str]) -> List[str]:
Expand Down Expand Up @@ -65,23 +67,36 @@ def all_snippets_from_file(sample_file: str) -> Dict[str, str]:
snippet_lines = {} # type: Dict[str, List[str]]
open_snippets = set()
with open(sample_file) as f:
excluding = False
# Iterate over each line:
# - If the line matches an opening snippet tag, add that snippet tag to
# the set of open tags.
# - If the line matches a closing snippet tag, remove that snippet tag
# from the set of open tags.
# - Otherwise, add the line to each of the open snippets
# - If the line matches an opening exclude tag, record that we excluding
# content.
# - If the line matches a closing exclude tag, record that we are capturing
# content again.
# - Otherwise, if we are not excluding content, add the line to each of the
# open snippets
#
# This allows us to handle parsing nested or interleaved snippets.
# This allows us to handle parsing nested or interleaved snippets and ignore
# blocks of code in the snippets
for line in f:
open_match = re.match(pattern=OPEN_SNIPPET_REGEX, string=line)
close_match = re.match(pattern=CLOSE_SNIPPET_REGEX, string=line)
open_exclude_match = re.match(pattern=OPEN_EXCLUDE_REGEX, string=line)
close_exclude_match = re.match(pattern=CLOSE_EXCLUDE_REGEX, string=line)
if open_match:
open_snippets.add(open_match[1])
snippet_lines[open_match[1]] = []
elif close_match:
open_snippets.discard(close_match[1])
else:
elif open_exclude_match:
excluding = True
elif close_exclude_match:
excluding = False
elif not excluding:
for snippet in open_snippets:
snippet_lines[snippet].append(line)

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</dependency>
<!-- [START_EXCLUDE] -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- [END_EXCLUDE] -->
</dependencies>
<!-- [END monitoring_install_with_bom] -->
</project>

0 comments on commit a0ee80e

Please sign in to comment.