Skip to content

Commit c8c5b98

Browse files
authored
Merge pull request #5 from neatc0der/release/v0.2
Release/v0.2
2 parents d7cd41e + 14b1226 commit c8c5b98

File tree

9 files changed

+97
-43
lines changed

9 files changed

+97
-43
lines changed

.build/mkdocs_markmap_build/common.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fnmatch
12
import os
23
import sys
34
from pathlib import Path
@@ -9,15 +10,15 @@
910
from github.GitRelease import GitRelease
1011
from github.Repository import Repository
1112

12-
from mkdocs_markmap.__meta__ import PACKAGE_NAME, PROJECT_NAME, PROJECT_VERSION, REPOSITORY
13+
from mkdocs_markmap.__meta__ import PACKAGE_NAME, PROJECT_NAME, PROJECT_VERSION, REPOSITORY_NAME
1314

1415

1516
PROJECT_PATH: Path = Path(__file__).parent.parent.parent.absolute()
1617
DIST_PATH: Path = PROJECT_PATH / 'dist'
1718
CHANGELOG_PATH: Path = PROJECT_PATH / 'changelog'
1819

19-
GZ_WILDCARD: str = f'{PROJECT_NAME}-{PROJECT_VERSION}*.gz'
20-
WHL_WILDCARD: str = f'{PACKAGE_NAME}-{PROJECT_VERSION}*.whl'
20+
GZ_WILDCARD: str = f'{PROJECT_NAME}-{{version}}*.gz'
21+
WHL_WILDCARD: str = f'{PACKAGE_NAME}-{{version}}*.whl'
2122

2223

2324
class GithubHandler(object):
@@ -28,23 +29,41 @@ def __init__(self, tag: str) -> None:
2829
print('environment variable "GITHUB_TOKEN" is not set')
2930
sys.exit(1)
3031
self.github: Github = Github(github_token)
31-
self.repository: Repository = self.github.get_repo(REPOSITORY)
32+
self.repository: Repository = self.github.get_repo(REPOSITORY_NAME)
3233

3334

3435
class AssetCollector(object):
36+
def __init__(self, tag: str = None) -> None:
37+
self.tag = tag
38+
39+
version = PROJECT_VERSION if tag is None else tag[1:]
40+
self.gz_wildcard = GZ_WILDCARD.format(version=version)
41+
self.whl_wildcard = WHL_WILDCARD.format(version=version)
42+
3543
def get_assets(self) -> List[str]:
36-
gz_assets: List[str] = list(str(p) for p in DIST_PATH.glob(GZ_WILDCARD))
37-
whl_assets: List[str] = list(str(p) for p in DIST_PATH.glob(WHL_WILDCARD))
44+
gz_assets: List[str] = list(str(p) for p in DIST_PATH.glob(self.gz_wildcard))
45+
whl_assets: List[str] = list(str(p) for p in DIST_PATH.glob(self.whl_wildcard))
46+
47+
help_text = ': run "inv build" first' if self.tag is None else ''
48+
49+
try:
50+
assert len(gz_assets) == 1, \
51+
f'release asset "{self.gz_wildcard}" is missing in {DIST_PATH}' + help_text
52+
assert len(whl_assets) == 1, \
53+
f'release asset "{self.whl_wildcard}" is missing in {DIST_PATH}' + help_text
3854

39-
assert len(gz_assets) == 1, \
40-
f'release asset "{GZ_WILDCARD}" is missing in {self.dist_path}: run "inv build" first'
41-
assert len(whl_assets) == 1, \
42-
f'release asset "{WHL_WILDCARD}" is missing in {self.dist_path}: run "inv build" first'
55+
except AssertionError as e:
56+
print(e)
57+
sys.exit(1)
4358

4459
return gz_assets + whl_assets
4560

4661

4762
class AssetDownloader(GithubHandler):
63+
def __init__(self, tag: str) -> None:
64+
super().__init__(tag)
65+
self._collector = AssetCollector(tag)
66+
4867
def get_assets_from_release(self) -> List[str]:
4968
try:
5069
release: GitRelease = next(r for r in self.repository.get_releases() if r.tag_name == self.tag)
@@ -53,10 +72,16 @@ def get_assets_from_release(self) -> List[str]:
5372
sys.exit(1)
5473

5574
DIST_PATH.mkdir(exist_ok=True)
56-
assets = []
5775

5876
http: PoolManager = PoolManager()
5977
for asset in release.get_assets():
78+
if not any(
79+
pattern
80+
for pattern in (self._collector.gz_wildcard, self._collector.whl_wildcard)
81+
if fnmatch.fnmatch(asset.browser_download_url, pattern)
82+
):
83+
continue
84+
6085
response: HTTPResponse = http.request('GET', asset.browser_download_url)
6186
if response.status != 200:
6287
print(f'Download of asset "{asset.name}" failed: {asset.browser_download_url} ({response.status})')
@@ -66,9 +91,7 @@ def get_assets_from_release(self) -> List[str]:
6691
with open(file_path, 'wb') as fp:
6792
fp.write(response.data)
6893

69-
assets.append(str(file_path))
70-
71-
return assets
94+
return self._collector.get_assets()
7295

7396

7497
class ChangelogLoader:

.build/mkdocs_markmap_build/distribution.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DistributionHandler(object):
1212
def __init__(self, tag: str) -> None:
1313
self._collector = AssetDownloader(tag)
1414

15-
def distribute(self):
15+
def distribute(self, dry_run: bool = True):
1616
assets: List[str] = self._collector.get_assets_from_release()
1717
os.environ['TWINE_USERNAME'] = '__token__'
1818

@@ -22,4 +22,8 @@ def distribute(self):
2222
print('environment variable "TWINE_PASSWORD" is not set')
2323
sys.exit(1)
2424

25-
dispatch(['upload', *assets])
25+
if dry_run:
26+
print('This is a dry run!')
27+
28+
else:
29+
dispatch(['upload', *assets])

.build/mkdocs_markmap_build/release.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def __init__(self, tag: str) -> None:
2121
self._changelog = ChangelogLoader()
2222
self._collector = AssetCollector()
2323

24-
def create(self, commit: str = None,dry_run: bool = True):
24+
def create(self, commit: str = None, dry_run: bool = True):
25+
release_message: str = self._changelog.get(self.tag)
2526
assets: List[str] = self._collector.get_assets()
2627
tagger: GitAuthor = None
2728

@@ -37,7 +38,7 @@ def create(self, commit: str = None,dry_run: bool = True):
3738
parameters: Dict[str, str] = {
3839
'object': commit,
3940
'release_name': self.tag,
40-
'release_message': self._changelog.get(self.tag),
41+
'release_message': release_message,
4142
'tag': self.tag,
4243
'tag_message': f'Release version {self.tag}',
4344
'type': 'commit',
@@ -59,12 +60,19 @@ def create(self, commit: str = None,dry_run: bool = True):
5960
pprint(parameters, width=120)
6061

6162
else:
62-
release: GitRelease = self.repository.create_git_tag_and_release(**parameters)
63+
release: GitRelease = self.repository.create_git_tag_and_release(**parameters, draft=True)
6364
print(f'Release "{self.tag}" created: {release.html_url}')
6465
for asset in assets:
6566
release_asset: GitReleaseAsset = release.upload_asset(asset)
6667
print(f'Release asset "{release_asset.name}" uploaded: {release_asset.url}')
6768

69+
release.update_release(
70+
name=self.tag,
71+
message=release_message,
72+
draft=True,
73+
)
74+
print('Release published')
75+
6876
def delete(self):
6977
try:
7078
next(t for t in self.repository.get_tags() if t.name == self.tag)

.github/workflows/distribute.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Distribution Workflow
2+
3+
on:
4+
release:
5+
types:
6+
- released
7+
8+
jobs:
9+
build:
10+
name: Distribute
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Prepare Python
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: 3.8
20+
21+
- name: Install Dependencies
22+
run: |
23+
python3 -m pip install --upgrade pip
24+
pip install -r requirements/build.txt
25+
python3 setup.py install
26+
27+
- name: Distribute Package to PyPI
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
TWINE_PASSWORD: ${{ secrets.TWINE_TOKEN }}
31+
run: inv distribute --tag ${GITHUB_REF#refs/*/} --no-dry-run

.github/workflows/release.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,3 @@ jobs:
3131
env:
3232
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3333
run: inv release --no-dry-run
34-
35-
- name: Distribute Package to PyPI
36-
env:
37-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38-
TWINE_PASSWORD: ${{ secrets.TWINE_TOKEN }}
39-
run: inv distribute

mkdocs_markmap/__meta__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PACKAGE_NAME = 'mkdocs_markmap'
22
PROJECT_NAME = PACKAGE_NAME.replace('_', '-')
3-
PROJECT_VERSION = '0.1'
3+
PROJECT_VERSION = '0.2'
44

55
OWNER = 'neatc0der'
6-
REPOSITORY = f'{OWNER}/{PROJECT_NAME}'
6+
REPOSITORY_NAME = f'{OWNER}/{PROJECT_NAME}'

mkdocs_markmap/extension.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from markdown.preprocessors import Preprocessor
88

99

10-
INC_SYNTAX = re.compile(r'\{!\s*(?P<path>.+?)\s*!\}')
10+
INCLUDE_SYNTAX = re.compile(r'\{!\s*(?P<path>.+?)\s*!\}')
1111

1212

1313
class MarkmapPreprocessor(Preprocessor):
@@ -25,7 +25,7 @@ def run(self, lines: List[str]) -> List[str]:
2525
done: bool = False
2626
while not done:
2727
for loc, line in enumerate(lines):
28-
match: Optional[re.Match[AnyStr]] = INC_SYNTAX.search(line)
28+
match: Optional[re.Match[AnyStr]] = INCLUDE_SYNTAX.search(line)
2929
if match is None:
3030
continue
3131

@@ -42,10 +42,10 @@ def run(self, lines: List[str]) -> List[str]:
4242

4343
except Exception as e:
4444
print('Warning: could not include file {}. Ignoring statement. Error: {}'.format(path, e))
45-
lines[loc] = INC_SYNTAX.sub('',line)
45+
lines[loc] = INCLUDE_SYNTAX.sub('',line)
4646
break
4747

48-
line_split: List[str] = INC_SYNTAX.split(line)
48+
line_split: List[str] = INCLUDE_SYNTAX.split(line)
4949
if len(markmap) == 0:
5050
markmap.append('')
5151
else:
@@ -69,7 +69,7 @@ def run(self, lines: List[str]) -> List[str]:
6969

7070
# todo: replace this functionality with an "include"
7171
class MarkmapExtension(Extension):
72-
def __init__(self, configs: Dict[str, str] = {}):
72+
def __init__(self, **configs: Dict[str, str]):
7373
self.config: Dict[str, str] = {
7474
'base_path': ['docs', 'Default location from which to evaluate relative paths for the include statement.'],
7575
'encoding': ['utf-8', 'Encoding of the files used by the include statement.'],

setup.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from setuptools import setup, find_packages
33
from typing import List
44

5-
from mkdocs_markmap.__meta__ import PROJECT_NAME, PROJECT_VERSION
5+
from mkdocs_markmap.__meta__ import PROJECT_NAME, PROJECT_VERSION, REPOSITORY_NAME
66

77

88
def readme() -> str:
@@ -23,20 +23,14 @@ def get_requirements(filename: str, base_dir: str = 'requirements') -> List[str]
2323
return install_requires
2424

2525

26-
long_description = (
27-
"This is a mkdocs plugin that introduces support for markmap."
28-
"Please follow the instruction in reame to enable this plugin."
29-
)
30-
31-
3226
setup(
3327
name=PROJECT_NAME,
3428
version=PROJECT_VERSION,
3529
description='MkDocs plugin and extension to creates mindmaps from markdown using markmap',
36-
long_description=long_description,
37-
long_description_content_type='text/plain',
30+
long_description=readme(),
31+
long_description_content_type='text/markdown',
3832
keywords='mkdocs python markdown mermaid',
39-
url='https://github.com/neac0der/mkdocs-markmap',
33+
url=f'https://github.com/{REPOSITORY_NAME}',
4034
author='neatc0der',
4135
author_email='',
4236
license='MIT',

tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ def distribute(c, tag=TARGET_TAG, dry_run=True):
4545
print(f'distribute release: {tag}')
4646

4747
handler: DistributionHandler = DistributionHandler(tag)
48-
handler.distribute()
48+
handler.distribute(dry_run=dry_run)

0 commit comments

Comments
 (0)