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

feat: add support for README partials #207

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"colorlog",
"jinja2",
"packaging",
"PyYAML",
"requests",
"protobuf",
]
Expand Down
20 changes: 20 additions & 0 deletions synthtool/gcp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import json
import os
import re
import yaml
from pathlib import Path

from synthtool.languages import node
Expand Down Expand Up @@ -62,6 +63,7 @@ def render(self, template_name: str, **kwargs) -> Path:
#
def _load_generic_metadata(self, metadata):
self._load_samples(metadata)
self._load_partials(metadata)

metadata["repo"] = {}
if os.path.exists("./.repo-metadata.json"):
Expand Down Expand Up @@ -111,6 +113,24 @@ def _read_quickstart(self, samples_dir):

return quickstart

#
# hand-crafted artisinal markdown can be provided in a .readme-partials.yml.
# The following fields are currently supported:
#
# introduction: a more thorough introduction than metadata["description"].
#
def _load_partials(self, metadata):
cwd_path = Path(os.getcwd())
partials_file = None
for file in [".readme-partials.yml", ".readme-partials.yaml"]:
if os.path.exists(cwd_path / file):
partials_file = cwd_path / file
break
if not partials_file:
return
with open(partials_file) as f:
metadata["partials"] = yaml.load(f, Loader=yaml.SafeLoader)


#
# parser to convert fooBar.js to Foo Bar.
Expand Down
4 changes: 4 additions & 0 deletions synthtool/gcp/templates/node_library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
[![npm version](https://img.shields.io/npm/v/{{ metadata['name'] }}.svg)](https://www.npmjs.org/package/{{ metadata['name'] }})
[![codecov](https://img.shields.io/codecov/c/github/{{ metadata['repo']['repo'] }}/master.svg?style=flat)](https://codecov.io/gh/{{ metadata['repo']['repo'] }})

{% if metadata['partials'] and metadata['partials']['introduction'] %}
{{ metadata['partials']['introduction'] }}
{% else %}
{{ metadata['description'] }}
{% endif %}
{% if metadata['deprecated'] %}
| :warning: Deprecated Module |
| --- |
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/.readme-partials.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
introduction: |-
[Cloud Storage](https://cloud.google.com/storage/docs) allows world-wide
storage and retrieval of any amount of data at any time. You can use Google
Cloud Storage for a range of scenarios including serving website content,
storing data for archival and disaster recovery, or distributing large data
objects to users via direct download.
15 changes: 15 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,18 @@ def test_hide_billing():
"README.md", metadata={"repo": {"requires_billing": False}}
).read_text()
assert "Enable billing for your project" not in result


def test_readme_partials():
cwd = os.getcwd()
os.chdir(FIXTURES)

common_templates = common.CommonTemplates()
metadata = {}
common_templates._load_partials(metadata)
# should have populated introduction from partial.
assert (
"objects to users via direct download" in metadata["partials"]["introduction"]
)

os.chdir(cwd)