Skip to content

Commit 16d6bc3

Browse files
committed
feat: proper bot bots and python file treatment
1 parent 99f9221 commit 16d6bc3

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

silverback/_cli.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,36 @@ def run(cli_ctx, account, runner_class, recorder_class, max_exceptions, bot):
128128
asyncio.run(runner.run())
129129

130130

131+
def build_helper(docker_fn: str, dockerfile_c: str):
132+
dockerfile_path = Path.cwd() / ".silverback-images" / docker_fn
133+
dockerfile_path.parent.mkdir(exist_ok=True)
134+
dockerfile_path.write_text(dockerfile_c.strip() + "\n")
135+
click.echo(f"Generated {dockerfile_path}")
136+
137+
138+
131139
@cli.command(section="Local Commands")
132140
@click.option("--generate", is_flag=True, default=False)
133141
@click.argument("path", required=False, type=str, default="bots")
134142
def build(generate, path):
135143
"""Generate Dockerfiles and build bot images"""
136144
if generate:
137-
if not (path := Path.cwd() / path).exists():
145+
if not (path := Path.cwd() / path).exists() and not (path := Path.cwd() / "bot").exists() and not (path := Path.cwd() / "bot.py").exists():
138146
raise FileNotFoundError(
139-
f"The bots directory '{path}' does not exist. "
140-
"You should have a `{path}/` folder in the root of your project."
147+
f"The bots directory '{path}', 'bot/' and 'bot.py' does not exist in your path. "
148+
f"You should have a '{path}/' or 'bot/' folder, or a 'bot.py' file in the root of your project."
141149
)
142-
files = {file for file in path.iterdir() if file.is_file()}
150+
if path.is_file():
151+
dockerfile_content = DOCKERFILE_CONTENT
152+
docker_filename = f"Dockerfile.{path.parent.name}-bot"
153+
dockerfile_content += f"COPY {path.name}/ /app/bot\n"
154+
build_helper(docker_filename, dockerfile_content)
155+
return
156+
157+
files = sorted({file for file in path.iterdir() if file.is_file()}, reverse=True)
143158
bots = []
144159
for file in files:
145-
if "__init__" in file.name:
160+
if file.name == "__init__.py" or file.name == "bot.py":
146161
bots = [file]
147162
break
148163
bots.append(file)
@@ -158,16 +173,13 @@ def build(generate, path):
158173
dockerfile_content += "COPY ape-config.yaml .\n"
159174
dockerfile_content += "RUN ape plugins install -U .\n"
160175

161-
if "__init__" in bot.name:
162-
docker_filename = f"Dockerfile.{bot.parent.name}"
176+
if bot.name == "__init__.py" or bot.name == "bot.py":
177+
docker_filename = f"Dockerfile.{bot.parent.parent.name}-bot"
163178
dockerfile_content += f"COPY {path.name}/ /app/bot\n"
164179
else:
165-
docker_filename = f"Dockerfile.{bot.name.replace('.py', '')}"
180+
docker_filename = f"Dockerfile.{bot.name.replace('.py', '')}-bot"
166181
dockerfile_content += f"COPY {path.name}/{bot.name} /app/bot.py\n"
167-
dockerfile_path = Path.cwd() / ".silverback-images" / docker_filename
168-
dockerfile_path.parent.mkdir(exist_ok=True)
169-
dockerfile_path.write_text(dockerfile_content.strip() + "\n")
170-
click.echo(f"Generated {dockerfile_path}")
182+
build_helper(docker_filename, dockerfile_content)
171183
return
172184

173185
if not (path := Path.cwd() / ".silverback-images").exists():

0 commit comments

Comments
 (0)