Skip to content
This repository was archived by the owner on Jun 23, 2024. It is now read-only.

Commit 74dd004

Browse files
committed
Show versions diff of modules
1 parent 7107b29 commit 74dd004

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

Diff for: sync/cli/Main.py

+10
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ def sync(cls) -> int:
253253
index = Index(root_folder=root_folder, config=config)
254254
index(version=cls._args.index_version, to_file=True)
255255

256+
if cls._args.diff_file:
257+
markdown_text = sync.get_versions_diff()
258+
if markdown_text is not None:
259+
if isinstance(cls._args.diff_file, str):
260+
diff_file = Path(cls._args.diff_file)
261+
diff_file.write_text(markdown_text)
262+
263+
else:
264+
print(markdown_text)
265+
256266
if cls._args.push:
257267
sync.push_by_git(cls._args.git_branch)
258268

Diff for: sync/cli/Parameters.py

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from argparse import (
33
ArgumentParser as ArgumentParserBase,
44
RawDescriptionHelpFormatter,
5+
Action,
56
_HelpAction
67
)
78
from pathlib import Path
@@ -33,6 +34,14 @@ def __init__(self, *args, **kwargs):
3334
Parameters.add_parser_help(self)
3435

3536

37+
class BoolOrStrAction(Action):
38+
def __call__(self, parser, namespace, values, option_string=None):
39+
if values is None or values == "" or values.isspace():
40+
values = True
41+
42+
setattr(namespace, self.dest, values)
43+
44+
3645
class Parameters:
3746
_root_folder: Path
3847
_github_token: Optional[str]
@@ -293,6 +302,14 @@ def configure_parser_sync(cls, sub_parsers):
293302
default=Index.latest_version,
294303
help="Version of the index file ({0}), default: {1}.".format(ModulesJson.filename(), "%(default)s")
295304
)
305+
p.add_argument(
306+
"--diff",
307+
dest="diff_file",
308+
metavar="FILE",
309+
action=BoolOrStrAction,
310+
nargs="?",
311+
help="Save versions diff of modules (GitHub flavored Markdown)."
312+
)
296313
p.add_argument(
297314
"--force",
298315
action="store_true",

Diff for: sync/core/Sync.py

+32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from concurrent.futures import ThreadPoolExecutor
44
from datetime import datetime
55

6+
from tabulate import tabulate
7+
68
from .Config import Config
79
from .Pull import Pull
810
from ..model import (
@@ -29,6 +31,8 @@ def __init__(self, root_folder, config, tracks=None):
2931
else:
3032
self._tracks = tracks
3133

34+
self._updated_diff = list()
35+
3236
def _update_jsons(self, track, force):
3337
module_folder = self._modules_folder.joinpath(track.id)
3438

@@ -88,6 +92,15 @@ def _update_jsons(self, track, force):
8892
update_json.write(update_json_file)
8993
track.write(track_json_file)
9094

95+
if len(update_json.versions) >= 2:
96+
self._updated_diff.append(
97+
(update_json.versions[-2], online_module)
98+
)
99+
else:
100+
self._updated_diff.append(
101+
(None, online_module)
102+
)
103+
91104
return online_module
92105

93106
@staticmethod
@@ -153,3 +166,22 @@ def push_by_git(self, branch):
153166
subprocess.run(["git", "add", "."], cwd=self._root_folder.as_posix())
154167
subprocess.run(["git", "commit", "-m", msg], cwd=self._root_folder.as_posix())
155168
subprocess.run(["git", "push", "-u", "origin", branch], cwd=self._root_folder.as_posix())
169+
170+
def get_versions_diff(self):
171+
headers = ["id", "name", "version"]
172+
table = []
173+
174+
if len(self._updated_diff) == 0:
175+
return None
176+
177+
for last, new in self._updated_diff:
178+
version = new.version_display
179+
if last is not None:
180+
version = f"{last.version_display} -> {version}"
181+
182+
table.append(
183+
[new.id, new.name, version]
184+
)
185+
186+
markdown_text = tabulate(table, headers, tablefmt="github")
187+
return markdown_text

Diff for: sync/core/Sync.pyi

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from datetime import date
22
from pathlib import Path
3-
from typing import Optional, List
3+
from typing import Optional, List, Tuple
44

55
from .Pull import Pull
66
from ..model import (
77
ConfigJson,
88
TrackJson,
9-
OnlineModule
9+
OnlineModule,
10+
VersionItem
1011
)
1112
from ..track import BaseTracks
1213
from ..utils import Log
@@ -21,6 +22,7 @@ class Sync:
2122
_modules_folder: Path
2223
_config: ConfigJson
2324
_tracks: BaseTracks
25+
_updated_diff: List[Tuple[Optional[VersionItem], OnlineModule]]
2426

2527
def __init__(self, root_folder: Path, config: ConfigJson, tracks: Optional[BaseTracks] = ...): ...
2628
def _update_jsons(self, track: TrackJson, force: bool) -> Optional[OnlineModule]: ...
@@ -30,3 +32,4 @@ class Sync:
3032
def create_local_tracks(self) -> BaseTracks: ...
3133
def update(self, module_ids: Optional[List[str]] = ..., force: bool = ..., **kwargs): ...
3234
def push_by_git(self, branch: str): ...
35+
def get_versions_diff(self) -> Optional[str]: ...

0 commit comments

Comments
 (0)