@@ -58,8 +58,9 @@ class SetupInfo:
5858 benchmarks_root : Union [str , None ]
5959 test_framework : str
6060 ignore_paths : list [str ]
61- formatter : str
61+ formatter : Union [ str , list [ str ]]
6262 git_remote : str
63+ enable_telemetry : bool
6364
6465
6566class DependencyManager (Enum ):
@@ -93,7 +94,9 @@ def init_codeflash() -> None:
9394 if should_modify :
9495 setup_info : SetupInfo = collect_setup_info ()
9596 git_remote = setup_info .git_remote
96- configure_pyproject_toml (setup_info )
97+ configured = configure_pyproject_toml (setup_info )
98+ if not configured :
99+ apologize_and_exit ()
97100
98101 install_github_app (git_remote )
99102
@@ -156,30 +159,30 @@ def ask_run_end_to_end_test(args: Namespace) -> None:
156159 run_end_to_end_test (args , bubble_sort_path , bubble_sort_test_path )
157160
158161
159- def is_valid_pyproject_toml (pyproject_toml_path : Path ) -> tuple [dict [str , Any ] | None , str ]: # noqa: PLR0911
162+ def is_valid_pyproject_toml (pyproject_toml_path : Path ) -> tuple [bool , dict [str , Any ] | None , str ]: # noqa: PLR0911
160163 if not pyproject_toml_path .exists ():
161- return None , f"Configuration file not found: { pyproject_toml_path } "
164+ return False , None , f"Configuration file not found: { pyproject_toml_path } "
162165
163166 try :
164167 config , _ = parse_config_file (pyproject_toml_path )
165168 except Exception as e :
166- return None , f"Failed to parse configuration: { e } "
169+ return False , None , f"Failed to parse configuration: { e } "
167170
168171 module_root = config .get ("module_root" )
169172 if not module_root :
170- return None , "Missing required field: 'module_root'"
173+ return False , config , "Missing required field: 'module_root'"
171174
172175 if not Path (module_root ).is_dir ():
173- return None , f"Invalid 'module_root': directory does not exist at { module_root } "
176+ return False , config , f"Invalid 'module_root': directory does not exist at { module_root } "
174177
175178 tests_root = config .get ("tests_root" )
176179 if not tests_root :
177- return None , "Missing required field: 'tests_root'"
180+ return False , config , "Missing required field: 'tests_root'"
178181
179182 if not Path (tests_root ).is_dir ():
180- return None , f"Invalid 'tests_root': directory does not exist at { tests_root } "
183+ return False , config , f"Invalid 'tests_root': directory does not exist at { tests_root } "
181184
182- return config , ""
185+ return True , config , ""
183186
184187
185188def should_modify_pyproject_toml () -> tuple [bool , dict [str , Any ] | None ]:
@@ -191,8 +194,9 @@ def should_modify_pyproject_toml() -> tuple[bool, dict[str, Any] | None]:
191194
192195 pyproject_toml_path = Path .cwd () / "pyproject.toml"
193196
194- config , _message = is_valid_pyproject_toml (pyproject_toml_path )
195- if config is None :
197+ valid , config , _message = is_valid_pyproject_toml (pyproject_toml_path )
198+ if not valid :
199+ # needs to be re-configured
196200 return True , None
197201
198202 return Confirm .ask (
@@ -532,6 +536,8 @@ def collect_setup_info() -> SetupInfo:
532536 except InvalidGitRepositoryError :
533537 git_remote = ""
534538
539+ enable_telemetry = ask_for_telemetry ()
540+
535541 ignore_paths : list [str ] = []
536542 return SetupInfo (
537543 module_root = str (module_root ),
@@ -541,6 +547,7 @@ def collect_setup_info() -> SetupInfo:
541547 ignore_paths = ignore_paths ,
542548 formatter = cast ("str" , formatter ),
543549 git_remote = str (git_remote ),
550+ enable_telemetry = enable_telemetry ,
544551 )
545552
546553
@@ -966,8 +973,8 @@ def customize_codeflash_yaml_content(
966973
967974
968975# Create or update the pyproject.toml file with the Codeflash dependency & configuration
969- def configure_pyproject_toml (setup_info : SetupInfo ) -> None :
970- toml_path = Path .cwd () / "pyproject.toml"
976+ def configure_pyproject_toml (setup_info : SetupInfo , config_file : Optional [ Path ] = None ) -> bool :
977+ toml_path = config_file or Path .cwd () / "pyproject.toml"
971978 try :
972979 with toml_path .open (encoding = "utf8" ) as pyproject_file :
973980 pyproject_data = tomlkit .parse (pyproject_file .read ())
@@ -976,23 +983,24 @@ def configure_pyproject_toml(setup_info: SetupInfo) -> None:
976983 f"I couldn't find a pyproject.toml in the current directory.{ LF } "
977984 f"Please create a new empty pyproject.toml file here, OR if you use poetry then run `poetry init`, OR run `codeflash init` again from a directory with an existing pyproject.toml file."
978985 )
979- apologize_and_exit ()
980-
981- enable_telemetry = ask_for_telemetry ()
986+ return False
982987
983988 codeflash_section = tomlkit .table ()
984989 codeflash_section .add (tomlkit .comment ("All paths are relative to this pyproject.toml's directory." ))
985990 codeflash_section ["module-root" ] = setup_info .module_root
986991 codeflash_section ["tests-root" ] = setup_info .tests_root
987992 codeflash_section ["test-framework" ] = setup_info .test_framework
988993 codeflash_section ["ignore-paths" ] = setup_info .ignore_paths
989- if not enable_telemetry :
990- codeflash_section ["disable-telemetry" ] = not enable_telemetry
994+ if not setup_info . enable_telemetry :
995+ codeflash_section ["disable-telemetry" ] = not setup_info . enable_telemetry
991996 if setup_info .git_remote not in ["" , "origin" ]:
992997 codeflash_section ["git-remote" ] = setup_info .git_remote
993998 formatter = setup_info .formatter
994999 formatter_cmds = []
995- if formatter == "black" :
1000+
1001+ if isinstance (formatter , list ):
1002+ formatter_cmds = formatter
1003+ elif formatter == "black" :
9961004 formatter_cmds .append ("black $file" )
9971005 elif formatter == "ruff" :
9981006 formatter_cmds .extend (["ruff check --exit-zero --fix $file" , "ruff format $file" ])
@@ -1003,6 +1011,7 @@ def configure_pyproject_toml(setup_info: SetupInfo) -> None:
10031011 )
10041012 elif formatter == "don't use a formatter" :
10051013 formatter_cmds .append ("disabled" )
1014+
10061015 check_formatter_installed (formatter_cmds , exit_on_failure = False )
10071016 codeflash_section ["formatter-cmds" ] = formatter_cmds
10081017 # Add the 'codeflash' section, ensuring 'tool' section exists
@@ -1014,6 +1023,7 @@ def configure_pyproject_toml(setup_info: SetupInfo) -> None:
10141023 pyproject_file .write (tomlkit .dumps (pyproject_data ))
10151024 click .echo (f"✅ Added Codeflash configuration to { toml_path } " )
10161025 click .echo ()
1026+ return True
10171027
10181028
10191029def install_github_app (git_remote : str ) -> None :
0 commit comments