🚚 migrate#

stewbeet migrate [minimal|basic] [--dry-run] [--yes]: turn an existing datapack or resource pack into a StewBeet project.

MIGRATION_TEMPLATES: tuple[str, ...] = ('minimal', 'basic')[source]#

Templates a pack can be migrated onto. The extensive one ships example content that would mix with the pack’s.

migrate_command() → None[source]#

Parse the command line, then migrate the packs found in the working directory.

select_packs(
working_dir: Path,
) → tuple[FoundPack | None, FoundPack | None] | None[source]#

The datapack and the resource pack to migrate, or None after telling the user why there is nothing to migrate.

download_template(template_name: str) → bytes | None[source]#

The template archive matching the installed StewBeet version, or None after reporting why it could not be fetched.

run_migration(
working_dir: Path,
template: ZipFile,
datapack: FoundPack | None,
resource_pack: FoundPack | None,
*,
dry_run: bool,
assume_yes: bool,
) → MigrationPlan | None[source]#

Show the plan, then apply it unless this is a dry run or the user declines.

Returns:

The plan when it could run (applied, unless dry_run), None when it was refused or cancelled.

packs#

Find the datapacks and resource packs a folder holds, before stewbeet migrate turns it into a project.

SEARCH_DEPTH: int = 2[source]#

How many folders below the working directory a pack is looked for.

SKIPPED_FOLDERS: frozenset[str] = frozenset({'__pycache__', 'build', 'node_modules'})[source]#

Folders never searched, on top of hidden ones: they hold outputs and dependencies, not sources.

PLAIN_PACK_KEYS: frozenset[str] = frozenset({'description', 'pack_format'})[source]#

Keys of the “pack” object that beet.yml and StewBeet already produce, so a pack.mcmeta holding only these can go.

class FoundPack(
root: Path,
has_data: bool,
has_assets: bool,
mcmeta: JsonDict,
)[source]#

Bases: object

A folder holding a pack.mcmeta next to a data/ folder, an assets/ folder, or both.

root: Path[source]#
has_data: bool[source]#
has_assets: bool[source]#
mcmeta: JsonDict[source]#
static read(
folder: Path,
) → FoundPack | None[source]#

The pack in folder, or None when it holds no pack.mcmeta or neither data/ nor assets/.

property is_plain: bool[source]#

Whether pack.mcmeta holds nothing beyond a description and a pack format.

>>> FoundPack(Path("."), True, False, {"pack": {"pack_format": 61, "description": "Hi"}}).is_plain
True
>>> FoundPack(Path("."), True, False, {"pack": {"pack_format": 61}, "overlays": {}}).is_plain
False
property description_yaml: str | None[source]#

The description as a YAML value for beet.yml, None when pack.mcmeta has none. JSON is valid YAML, which keeps a text component description intact.

>>> FoundPack(Path("."), True, False, {"pack": {"description": "Épée"}}).description_yaml
'"Épée"'
property kind: str[source]#

What the pack holds, as the terminal names it.

>>> FoundPack(Path("."), True, True, {}).kind
'datapack and resource pack'
property namespaces: list[str][source]#

Namespaces under data/, without minecraft, which a pack only uses to tag its own functions.

find_packs(
folder: Path,
depth: int = 2,
) → list[FoundPack][source]#

Every pack in folder and up to depth levels below it, without looking inside a pack.

plan#

Everything stewbeet migrate will do, worked out before anything on disk changes.

A dry run prints the plan, a real run prints it and then applies it, so --dry-run shows exactly what happens.

TEMPLATE_ICONS: tuple[str, ...] = ('src/pack.png', 'assets/pack.png')[source]#

Where a template may keep its pack.png. The migrated icon goes wherever the chosen template has it.

RESOURCE_PACK_CONFIG: str = '\n# Loads src/assets/, where `stewbeet migrate` moved your resource pack\nresource_pack:\n    name: "resource_pack"\n    load: ["src"]\n'[source]#

Appended to a template’s beet.yml that only builds a datapack, when a resource pack is migrated.

class Move(source: pathlib.Path, destination: pathlib.Path)[source]#

Bases: object

source: Path[source]#
destination: Path[source]#
class Extract(member: str)[source]#

Bases: object

A template file written as it is in the template.

member: str[source]#
class Skip(member: str, reason: str)[source]#

Bases: object

A template file left out.

member: str[source]#
reason: str[source]#
class Delete(path: pathlib.Path, reason: str)[source]#

Bases: object

path: Path[source]#
reason: str[source]#
class RemoveFolder(path: Path)[source]#

Bases: object

A pack folder that the moves leave empty.

path: Path[source]#
class WriteConfig(text: str, changes: tuple[str, ...])[source]#

Bases: object

The template’s beet.yml, filled in from the migrated packs.

text: str[source]#
changes: tuple[str, ...][source]#
class MigrationPlan(
working_dir: pathlib.Path,
steps: tuple[stewbeet.core.migrate.plan.Move | stewbeet.core.migrate.plan.Extract | stewbeet.core.migrate.plan.Skip | stewbeet.core.migrate.plan.Delete | stewbeet.core.migrate.plan.RemoveFolder | stewbeet.core.migrate.plan.WriteConfig, ...],
warnings: tuple[str, ...],
)[source]#

Bases: object

working_dir: Path[source]#
steps: tuple[Move | Extract | Skip | Delete | RemoveFolder | WriteConfig, ...][source]#

In the order they are applied.

warnings: tuple[str, ...][source]#
describe() → list[str][source]#

One line per step, the way the terminal shows the plan.

conflicts() → list[str][source]#

Why the plan cannot run, empty when it can. Nothing is written unless this is empty.

build_plan(
working_dir: Path,
template: ZipFile,
datapack: FoundPack | None,
resource_pack: FoundPack | None,
) → MigrationPlan[source]#

Everything moving these packs into the template takes, without touching the disk.

Parameters:
  • datapack – The pack whose data/ is migrated, possibly the same folder as resource_pack.

  • resource_pack – The pack whose assets/ is migrated.

plan_template_files(
working_dir: Path,
template: ZipFile,
datapack: FoundPack | None,
resource_pack: FoundPack | None,
replaced_icon: str | None,
) → list[Move | Extract | Skip | Delete | RemoveFolder | WriteConfig][source]#

Which template files are written, and why the others are not. The template never writes over a file.

plan_config(
text: str,
working_dir: Path,
datapack: FoundPack | None,
resource_pack: FoundPack | None,
) → tuple[WriteConfig, list[str]][source]#

The template’s beet.yml with the migrated project’s own values, and what is still left to fill in by hand.

fill_identity(
text: str,
main_pack: FoundPack | None,
namespaces: list[str],
) → tuple[str, list[str], list[str]][source]#

Replace the template’s name and id with the pack’s, for the templates that set them.

Returns:

The new config text, the changes made, and the warnings about what could not be filled in.

plan_moves(
working_dir: Path,
packs: list[FoundPack],
icon: Path | None,
icon_destination: Path,
) → list[Move | Extract | Skip | Delete | RemoveFolder | WriteConfig][source]#

data/ and assets/ into src/, and the icon to where the template keeps its own.

plan_mcmeta(
working_dir: Path,
packs: list[FoundPack],
) → tuple[list[Move | Extract | Skip | Delete | RemoveFolder | WriteConfig], list[str]][source]#

A plain pack.mcmeta is folded into beet.yml, a richer one moves to src/ (at most one can).

plan_cleanup(
working_dir: Path,
packs: list[FoundPack],
pack_steps: list[Move | Extract | Skip | Delete | RemoveFolder | WriteConfig],
) → list[Move | Extract | Skip | Delete | RemoveFolder | WriteConfig][source]#

The pack folders, and their parents, that nothing is left in once the files have moved. Deepest first.

has_config_key(text: str, key: str) → bool[source]#

Whether a top-level key is set in a YAML config.

>>> has_config_key('id: "a"\n  name: "b"', "name")
False
set_config_key(text: str, key: str, value: str) → str[source]#

Set a top-level key of a YAML config, keeping every comment. A missing key is appended.

Parameters:

value – Already a YAML value, quotes included.

>>> set_config_key('id: "a"\nname: "b"\n', "id", '"c"')
'id: "c"\nname: "b"\n'
>>> set_config_key('output: "build"\n', "description", '"Hi"')
'output: "build"\n\ndescription: "Hi"\n'