diff --git a/silverback/_cli.py b/silverback/_cli.py index bd952e6f..0404c0d1 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -128,21 +128,36 @@ def run(cli_ctx, account, runner_class, recorder_class, max_exceptions, bot): asyncio.run(runner.run()) +def build_helper(docker_fn: str, dockerfile_c: str): + dockerfile_path = Path.cwd() / ".silverback-images" / docker_fn + dockerfile_path.parent.mkdir(exist_ok=True) + dockerfile_path.write_text(dockerfile_c.strip() + "\n") + click.echo(f"Generated {dockerfile_path}") + + + @cli.command(section="Local Commands") @click.option("--generate", is_flag=True, default=False) @click.argument("path", required=False, type=str, default="bots") def build(generate, path): """Generate Dockerfiles and build bot images""" if generate: - if not (path := Path.cwd() / path).exists(): + if not (path := Path.cwd() / path).exists() and not (path := Path.cwd() / "bot").exists() and not (path := Path.cwd() / "bot.py").exists(): raise FileNotFoundError( - f"The bots directory '{path}' does not exist. " - "You should have a `{path}/` folder in the root of your project." + f"The bots directory '{path}', 'bot/' and 'bot.py' does not exist in your path. " + f"You should have a '{path}/' or 'bot/' folder, or a 'bot.py' file in the root of your project." ) - files = {file for file in path.iterdir() if file.is_file()} + if path.is_file(): + dockerfile_content = DOCKERFILE_CONTENT + docker_filename = f"Dockerfile.{path.parent.name}-bot" + dockerfile_content += f"COPY {path.name}/ /app/bot\n" + build_helper(docker_filename, dockerfile_content) + return + + files = sorted({file for file in path.iterdir() if file.is_file()}, reverse=True) bots = [] for file in files: - if "__init__" in file.name: + if file.name == "__init__.py" or file.name == "bot.py": bots = [file] break bots.append(file) @@ -158,16 +173,13 @@ def build(generate, path): dockerfile_content += "COPY ape-config.yaml .\n" dockerfile_content += "RUN ape plugins install -U .\n" - if "__init__" in bot.name: - docker_filename = f"Dockerfile.{bot.parent.name}" + if bot.name == "__init__.py" or bot.name == "bot.py": + docker_filename = f"Dockerfile.{bot.parent.parent.name}-bot" dockerfile_content += f"COPY {path.name}/ /app/bot\n" else: - docker_filename = f"Dockerfile.{bot.name.replace('.py', '')}" + docker_filename = f"Dockerfile.{bot.name.replace('.py', '')}-bot" dockerfile_content += f"COPY {path.name}/{bot.name} /app/bot.py\n" - dockerfile_path = Path.cwd() / ".silverback-images" / docker_filename - dockerfile_path.parent.mkdir(exist_ok=True) - dockerfile_path.write_text(dockerfile_content.strip() + "\n") - click.echo(f"Generated {dockerfile_path}") + build_helper(docker_filename, dockerfile_content) return if not (path := Path.cwd() / ".silverback-images").exists():