|
| 1 | +import logging |
| 2 | +import subprocess |
| 3 | +from pathlib import Path |
| 4 | +from shutil import rmtree |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | +deployment_extension = "py" |
| 8 | + |
| 9 | + |
| 10 | +def _get_output_path(output_dir: Path, deployment_extension: str) -> Path: |
| 11 | + return output_dir / Path( |
| 12 | + "{contract_name}" |
| 13 | + + ("_client" if deployment_extension == "py" else "Client") |
| 14 | + + f".{deployment_extension}" |
| 15 | + ) |
| 16 | + |
| 17 | + |
| 18 | +def build(output_dir: Path, contract_path: Path) -> Path: |
| 19 | + output_dir = output_dir.resolve() |
| 20 | + if output_dir.exists(): |
| 21 | + rmtree(output_dir) |
| 22 | + output_dir.mkdir(exist_ok=True, parents=True) |
| 23 | + logger.info(f"Exporting {contract_path} to {output_dir}") |
| 24 | + |
| 25 | + build_result = subprocess.run( |
| 26 | + [ |
| 27 | + "algokit", |
| 28 | + "--no-color", |
| 29 | + "compile", |
| 30 | + "python", |
| 31 | + contract_path.absolute(), |
| 32 | + f"--out-dir={output_dir}", |
| 33 | + "--output-arc32", |
| 34 | + ], |
| 35 | + stdout=subprocess.PIPE, |
| 36 | + stderr=subprocess.STDOUT, |
| 37 | + text=True, |
| 38 | + ) |
| 39 | + if build_result.returncode: |
| 40 | + raise Exception(f"Could not build contract:\n{build_result.stdout}") |
| 41 | + |
| 42 | + app_spec_file_names = [file.name for file in output_dir.glob("*.arc32.json")] |
| 43 | + |
| 44 | + for app_spec_file_name in app_spec_file_names: |
| 45 | + if app_spec_file_name is None: |
| 46 | + raise Exception( |
| 47 | + "Could not generate typed client, .arc32.json file not found" |
| 48 | + ) |
| 49 | + print(app_spec_file_name) |
| 50 | + generate_result = subprocess.run( |
| 51 | + [ |
| 52 | + "algokit", |
| 53 | + "generate", |
| 54 | + "client", |
| 55 | + output_dir, |
| 56 | + "--output", |
| 57 | + _get_output_path(output_dir, deployment_extension), |
| 58 | + ], |
| 59 | + stdout=subprocess.PIPE, |
| 60 | + stderr=subprocess.STDOUT, |
| 61 | + text=True, |
| 62 | + ) |
| 63 | + if generate_result.returncode: |
| 64 | + if "No such command" in generate_result.stdout: |
| 65 | + raise Exception( |
| 66 | + "Could not generate typed client, requires AlgoKit 2.0.0 or " |
| 67 | + "later. Please update AlgoKit" |
| 68 | + ) |
| 69 | + else: |
| 70 | + raise Exception( |
| 71 | + f"Could not generate typed client:\n{generate_result.stdout}" |
| 72 | + ) |
| 73 | + |
| 74 | + return output_dir / app_spec_file_name |
0 commit comments