diff --git a/tzk/builders.py b/tzk/builders.py index 5e67c76..000d682 100644 --- a/tzk/builders.py +++ b/tzk/builders.py @@ -13,7 +13,6 @@ information about the defined products without actually running any build steps. from collections.abc import Mapping from contextlib import contextmanager import functools -import json import os from pathlib import Path import re @@ -24,7 +23,7 @@ from typing import Callable, Dict, List, Optional, Set, Sequence, Tuple from tzk import git from tzk import tw -from tzk.util import BuildError, pushd +from tzk.util import alter_tiddlywiki_info, BuildError, pushd def tzk_builder(func): @@ -648,8 +647,22 @@ def editionify(target_folder: str, description: str) -> None: build_state['public_wiki_folder'], target_folder, ) - with (Path(target_folder) / "tiddlywiki.info").open("r") as f: - tinfo = json.load(f) - tinfo['description'] = description - with (Path(target_folder) / "tiddlywiki.info").open("w") as f: - json.dump(tinfo, f) + + def editor(tinfo): + tinfo['description'] = description + return tinfo + alter_tiddlywiki_info(Path(target_folder) / "tiddlywiki.info", editor) + + +@tzk_builder +def add_plugins(plugins: Sequence[str]) -> None: + """ + Add one or more plugins to the tiddlywiki.info file. + + :param plugins: A list of plugin names (e.g., "tiddlywiki/codemirror") to add. + """ + def editor(tinfo): + tinfo['plugins'].extend(plugins) + return tinfo + alter_tiddlywiki_info(Path(build_state['public_wiki_folder']) / "tiddlywiki.info", + editor) diff --git a/tzk/tw.py b/tzk/tw.py index c7de2f7..9d3b840 100644 --- a/tzk/tw.py +++ b/tzk/tw.py @@ -114,18 +114,28 @@ def _init_tw(wiki_name: str) -> None: os.environ['TIDDLYWIKI_EDITION_PATH'] = old_edition_path -def _add_filesystem_plugins(wiki_name: str) -> None: +def _restore_plugins(wiki_name: str) -> None: """ - Add the "tiddlywiki/filesystem" and "tiddlywiki/tiddlyweb" plugins - required for Node.js client-server operation to the new wiki's tiddlywiki.info. + Add the plugins from the edition's tiddlywiki.info to the new wiki's + tiddlywiki.info (for some reason, this is not done automatically). Also + add the two plugins required for client-server operation. """ - print("tzk: Adding filesystem plugins to tiddlywiki.info...") + print("tzk: Adding plugins to tiddlywiki.info...") + info_path = Path.cwd() / wiki_name / "tiddlywiki.info" + edition_path = Path(__file__).parent / "editions" / "tzk" / "tiddlywiki.info" + with info_path.open("r") as f: info_data = json.load(f) - info_data['plugins'] = ["tiddlywiki/filesystem", "tiddlywiki/tiddlyweb"] + with edition_path.open("r") as f: + edition_data = json.load(f) + + plugins = {"tiddlywiki/filesystem", "tiddlywiki/tiddlyweb"} + plugins = plugins.union(edition_data['plugins']) + info_data['plugins'] = sorted(plugins) + with info_path.open("w") as f: - json.dump(info_data, f) + json.dump(info_data, f, indent=4) def _init_gitignore() -> None: @@ -180,7 +190,7 @@ def install(wiki_name: str, tw_version_spec: str, author: Optional[str], else: _init_tw(wiki_name) - _add_filesystem_plugins(wiki_name) + _restore_plugins(wiki_name) _init_gitignore() _initial_commit() diff --git a/tzk/util.py b/tzk/util.py index 4e76f31..62dd65f 100644 --- a/tzk/util.py +++ b/tzk/util.py @@ -2,16 +2,29 @@ util.py - miscellaneous utility functions """ from contextlib import contextmanager +import json import os +from pathlib import Path import shutil import sys -from typing import NoReturn +from typing import Any, Callable, Dict, NoReturn class BuildError(Exception): pass +def alter_tiddlywiki_info( + info_path: Path, + edit_func: Callable[[Dict[str, Any]], Dict[str, Any]]) -> None: + "Change a tiddlywiki.info (or other json file) in an arbitrary manner." + with info_path.open("r") as f: + tinfo = json.load(f) + tinfo = edit_func(tinfo) + with info_path.open("w") as f: + json.dump(tinfo, f, indent=2) + + def fail(msg: str, exit_code: int = 1) -> NoReturn: "Print message to stderr and quit with exit code 1." print(msg, file=sys.stderr) @@ -54,4 +67,4 @@ def require_dependencies() -> None: if shutil.which("git") is None: fail("Git is not available. " "Please install Git and make it available on your PATH.\n" - "https://git-scm.com/book/en/v2/Getting-Started-Installing-Git") \ No newline at end of file + "https://git-scm.com/book/en/v2/Getting-Started-Installing-Git")