@@ -128,21 +128,36 @@ def run(cli_ctx, account, runner_class, recorder_class, max_exceptions, bot):
128
128
asyncio .run (runner .run ())
129
129
130
130
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
+
131
139
@cli .command (section = "Local Commands" )
132
140
@click .option ("--generate" , is_flag = True , default = False )
133
141
@click .argument ("path" , required = False , type = str , default = "bots" )
134
142
def build (generate , path ):
135
143
"""Generate Dockerfiles and build bot images"""
136
144
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 () :
138
146
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."
141
149
)
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 )
143
158
bots = []
144
159
for file in files :
145
- if "__init__" in file .name :
160
+ if file . name == "__init__.py" or file .name == "bot.py" :
146
161
bots = [file ]
147
162
break
148
163
bots .append (file )
@@ -158,16 +173,13 @@ def build(generate, path):
158
173
dockerfile_content += "COPY ape-config.yaml .\n "
159
174
dockerfile_content += "RUN ape plugins install -U .\n "
160
175
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 "
163
178
dockerfile_content += f"COPY { path .name } / /app/bot\n "
164
179
else :
165
- docker_filename = f"Dockerfile.{ bot .name .replace ('.py' , '' )} "
180
+ docker_filename = f"Dockerfile.{ bot .name .replace ('.py' , '' )} -bot "
166
181
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 )
171
183
return
172
184
173
185
if not (path := Path .cwd () / ".silverback-images" ).exists ():
0 commit comments