@@ -41,104 +41,21 @@ def finalize(
4141 if self .marker ().exists ():
4242 return
4343
44- # Reflex is the parent directory of the scripts directory
45- # which is where this file is located
46- reflex_dir = pathlib .Path (__file__ ).parent .parent .absolute ()
47- current_working_directory = pathlib .Path .cwd ().absolute ()
48-
49- if importlib .util .find_spec (
50- "pre_commit"
51- ) and current_working_directory .is_relative_to (reflex_dir ):
52- import pre_commit .constants
53- import pre_commit .yaml
54-
55- patches = [
56- (
57- pre_commit .constants .__file__ ,
58- """from __future__ import annotations
59-
60- import importlib.metadata
61-
62- CONFIG_FILE = '.pre-commit-config.yaml'
63- MANIFEST_FILE = '.pre-commit-hooks.yaml'
64-
65- # Bump when modifying `empty_template`
66- LOCAL_REPO_VERSION = '1'
67-
68- VERSION = importlib.metadata.version('pre_commit')
69-
70- DEFAULT = 'default'""" ,
71- """from __future__ import annotations
72-
73- import importlib.metadata
74-
75- CONFIG_FILE = 'pyproject.toml'
76- MANIFEST_FILE = '.pre-commit-hooks.yaml'
77-
78- # Bump when modifying `empty_template`
79- LOCAL_REPO_VERSION = '1'
80-
81- VERSION = importlib.metadata.version('pre_commit')
82-
83- DEFAULT = 'default'""" ,
84- ),
85- (
86- pre_commit .yaml .__file__ ,
87- """from __future__ import annotations
88-
89- import functools
90- from typing import Any
91-
92- import yaml
93-
94- Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
95- yaml_compose = functools.partial(yaml.compose, Loader=Loader)
96- yaml_load = functools.partial(yaml.load, Loader=Loader)
97- Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
98-
99-
100- def yaml_dump(o: Any, **kwargs: Any) -> str:
101- # when python/mypy#1484 is solved, this can be `functools.partial`
102- return yaml.dump(
103- o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False,
104- **kwargs,
105- )""" ,
106- """from __future__ import annotations
107-
108- import functools
109- from typing import Any
110-
111- import yaml
112- import toml
113-
114- Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
115- yaml_compose = functools.partial(yaml.compose, Loader=Loader)
116- def yaml_load(stream):
117- try:
118- return toml.loads(stream).get("tool", {}).get("pre-commit", {})
119- except ValueError:
120- return yaml.load(stream, Loader=Loader)
121- Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
122-
123-
124- def yaml_dump(o: Any, **kwargs: Any) -> str:
125- # when python/mypy#1484 is solved, this can be `functools.partial`
126- return yaml.dump(
127- o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False,
128- **kwargs,
129- )""" ,
130- ),
131- ]
132-
133- for file , old , new in patches :
134- file_path = pathlib .Path (file )
135- file_content = file_path .read_text ()
136- if new not in file_content and old not in file_content :
137- raise RuntimeError (
138- f"Unexpected content in { file_path } . Did you update pre-commit without updating the patches?"
139- )
140- if old in file_content :
141- file_path .write_text (file_content .replace (old , new ))
44+ if importlib .util .find_spec ("pre_commit" ) and importlib .util .find_spec ("toml" ):
45+ import json
46+
47+ import toml
48+ import yaml
49+
50+ reflex_dir = pathlib .Path (__file__ ).parent .parent
51+ pre_commit_config = json .loads (
52+ json .dumps (
53+ toml .load (reflex_dir / "pyproject.toml" )["tool" ]["pre-commit" ]
54+ )
55+ )
56+ (reflex_dir / ".pre-commit-config.yaml" ).write_text (
57+ yaml .dump (pre_commit_config ), encoding = "utf-8"
58+ )
14259
14360 if not (pathlib .Path (self .root ) / "scripts" ).exists ():
14461 return
0 commit comments