Files
photocraft/scripts/engine_split/session_state.py
T
aiongg 62263bc7b8 Engine split step 6: rewrite only files a crate target compiles
compiled_files walks each crate's targets (standard paths and Cargo.toml target paths) through
mod lines, inline modules included. tools/analysis_ui.rs, which no module reaches, keeps main's text.
2026-11-23 03:38:36 +00:00

164 lines
7.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""Engine split, step 5: `Session` state.
The action recording and playback state left `ActionState` (`s.actions`) for the core type
`ActionRecording` (`s.recording`); the journal mark `recording` is called `target` there. Small
state types moved to `crates/engine-core/src/state.rs` (their old paths are re-exported), the
action-history bookkeeping to `action_history.rs`, and the stored-preferences document with the
methods that combine preferences, colour settings and presets to `prefs/saved.rs`.
`Clip::from_svg` is the free function `edit_cmds::clip_from_svg`, and the preset store is a session
slot read through `preset_store::PresetStoreExt` (`s.preset_store()`, `s.preset_store_mut()`).
python3 scripts/engine_split/session_state.py split
Rewrites every Rust file under `crates/` and `apps/`: the recording fields,
`Clip::from_svg` and the preset store field. The rest of the step (type moves, slots, hooks) is hand-written, its
own commit.
python3 scripts/engine_split/session_state.py branch --base <merge-base> --head <branch>
On a branch rebased onto the step. `--head` is the branch commit from before the rebase
(`ORIG_HEAD` right after it). Rewrites the files the branch changed, and lists the
branch's edits to definitions that moved (re-apply those in the new file).
Running either mode twice gives the same result. What it cannot do is printed at the end of
every run.
"""
import argparse, os, re, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from rust_source import compiled_files, edited_definitions, git # noqa: E402
FIELDS = ["record_tools", "recording", "take", "playing", "playback_stack", "playback_steps", "budget_shared", "batch_depth", "script_depth", "script_steps"]
RENAMED = {"recording": "target"}
RECORDING = re.compile(r"\.actions\.(" + "|".join(FIELDS) + r")\b")
FROM_SVG = re.compile(r"\bedit_cmds::Clip::from_svg\(")
BARE_FROM_SVG = re.compile(r"(?<![\w:])Clip::from_svg\(")
IMPORTS_FROM_SVG = re.compile(r"^\s*use [^;]*\bclip_from_svg\b", re.M)
# The preset store is a slot (`preset_store::PresetStoreExt`); `services.preset_store` is the UI's own.
PRESET_STORE = [
(re.compile(r"(?<!services)\.preset_store\.as_ref\(\)"), ".preset_store()"),
(re.compile(r"(?<!services)\.preset_store\.as_mut\(\)"), ".preset_store_mut()"),
(re.compile(r"(?<!services)\.preset_store\.(is_none|is_some)\(\)"), r".preset_store().\1()"),
]
# Definitions that moved in this step: (file it was in, item, new file).
MOVED = [
("cmd/file_cmds/open.rs", "struct BatchOpen", "state.rs"),
("cmd/edit_cmds.rs", "struct Clip", "state.rs"),
("cmd/smart_cmds.rs", "struct SmartLink", "state.rs"),
("cmd/channel_cmds/view.rs", "struct QuickMaskOptions", "state.rs"),
("cmd/edit_menu_cmds.rs", "struct EditState", "state.rs"),
("cmd/edit_menu_cmds.rs", "struct FadeSource", "state.rs"),
("cmd/edit_menu_cmds.rs", "enum FadeTarget", "state.rs"),
("cmd/edit_menu_cmds.rs", "struct CustomShape", "state.rs"),
("cmd/edit_menu_cmds.rs", "struct FindCursor", "state.rs"),
("cmd/analysis_cmds.rs", "struct AnalysisState", "state.rs"),
("cmd/analysis_cmds.rs", "struct LogRow", "state.rs"),
("cmd/analysis_cmds.rs", "struct DataPoints", "state.rs"),
("cmd/analysis_cmds.rs", "fn available", "state.rs"),
("cmd/automate_cmds.rs", "struct FileMenuState", "state.rs"),
("cmd/automate_cmds.rs", "struct ScriptEvents", "prefs.rs"),
("cmd/automate_cmds.rs", "struct ScriptBinding", "prefs.rs"),
("color_channels.rs", "struct Restriction", "state.rs"),
("color_channels.rs", "struct ColorChannels", "state.rs"),
("cmd/brush_cmds.rs", "struct LiveCommit", "state.rs"),
("cmd/pattern_cmds.rs", "struct PatternLibrary", "state.rs"),
("cmd/pattern_cmds.rs", "fn builtin", "state.rs"),
("link_watch.rs", "type Stamp", "state.rs"),
("link_watch.rs", "type LinkScan", "state.rs"),
("shortcut_sets.rs", "struct ShortcutSet", "prefs/shortcuts.rs"),
("cmd/actions_cmds.rs", "fn is_action_key", "prefs/shortcuts.rs"),
("cmd/actions_cmds.rs", "fn history_policy_in_use", "action_history.rs"),
("cmd/actions_cmds.rs", "struct ActionState", "actions_record.rs (the recording fields: ActionRecording)"),
("prefs.rs", "struct SavedPrefs", "prefs/saved.rs"),
("prefs.rs", "fn parse_prefs_json", "prefs/saved.rs"),
("prefs.rs", "fn parse_legacy_prefs", "prefs/saved.rs"),
("prefs.rs", "fn store_options", "prefs/saved.rs"),
("prefs.rs", "fn read_saved", "prefs/saved.rs"),
("prefs.rs", "fn prefs_to_json", "prefs/saved.rs"),
("prefs.rs", "fn prefs_value", "prefs/saved.rs"),
("prefs.rs", "fn load_prefs_json", "prefs/saved.rs"),
("prefs.rs", "fn saved_prefs", "prefs/saved.rs"),
("prefs.rs", "fn set_saved_prefs", "prefs/saved.rs"),
("prefs.rs", "fn prefs_view", "prefs/saved.rs"),
("prefs.rs", "fn set_pref", "prefs/saved.rs"),
]
SRC = "crates/engine-core/src/"
REPORT = []
def rewrite_text(text):
text = RECORDING.sub(lambda m: ".recording." + RENAMED.get(m.group(1), m.group(1)), text)
for rx, to in PRESET_STORE:
text = rx.sub(to, text)
text = FROM_SVG.sub("edit_cmds::clip_from_svg(", text)
return BARE_FROM_SVG.sub("clip_from_svg(", text)
def rewrite(path):
with open(path, encoding="utf-8") as f:
old = f.read()
new = rewrite_text(old)
if BARE_FROM_SVG.search(old) and not IMPORTS_FROM_SVG.search(new):
REPORT.append(f"{path}: `Clip::from_svg(` became `clip_from_svg(`; import `edit_cmds::clip_from_svg` there")
if new != old:
with open(path, "w", encoding="utf-8") as f:
f.write(new)
return True
return False
def split():
n = sum(rewrite(p) for p in sorted(compiled_files(["crates", "apps"])))
print(f"session_state split: {n} files rewritten")
def moved_edits(base, head):
new_files = {(old_file, item): new_file for old_file, item, new_file in MOVED}
for old_file, item in edited_definitions(base, head, list(new_files), ["crates/engine/src/", SRC]):
new_file = new_files[(old_file, item)]
REPORT.append(f"NOT DONE: the branch edits {old_file} near `{item}`; that definition is in {SRC}{new_file} now")
def branch(base, head):
changed = git("diff", "--name-only", f"{base}...{head}").split()
n = sum(rewrite(p) for p in changed if p.endswith(".rs") and os.path.exists(p))
moved_edits(base, head)
print(f"session_state branch: {n} files rewritten")
CANNOT_DO = """
What this script cannot do (the compiler points at each):
- a recording field reached through another binding (`let a = &mut s.actions; a.playing`) or set
in an `ActionState { .. }` literal: those fields are `s.recording.<field>` now;
- a file that now calls `preset_store()` / `preset_store_mut()` needs
`use photocraft_engine::preset_store::PresetStoreExt;` (`crate::preset_store::...` in core);
- an assignment to the old field (`s.preset_store = Some(store)`): attach a store with
`Session::attach_preset_store`, which keeps it in the preset store slot;
- edits to a moved definition: re-apply them in the new file named above;
- a new `Session` field: a plain core field when the pipeline reads it, else a slot its crate
registers (`Install::slot`, read through the crate's extension trait).
"""
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
sub = ap.add_subparsers(dest="mode", required=True)
sub.add_parser("split")
b = sub.add_parser("branch")
b.add_argument("--base", required=True)
b.add_argument("--head", required=True)
args = ap.parse_args()
os.chdir(git("rev-parse", "--show-toplevel").strip())
if args.mode == "split":
split()
else:
branch(args.base, args.head)
for line in REPORT:
print(line)
print(CANNOT_DO)
if __name__ == "__main__":
main()