Skip to content

Commit e1bcb7b

Browse files
committed
Add dotnet command to pybite
Signed-off-by: Alireza Poodineh <[email protected]>
1 parent 4e1e8d2 commit e1bcb7b

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

tools/pybite/host.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ def get_argparser(self) -> argparse.ArgumentParser:
126126

127127
# Register built-in dotnet commands
128128
for cmd in self.DOTNET_COMMANDS:
129-
sub = subparsers.add_parser(
129+
subparsers.add_parser(
130130
cmd['name'],
131131
help=cmd['help'],
132132
epilog=self.argparser_epilog + ' (Dotnet CLI)',
133133
usage=self.argparser_usage.replace('command', cmd['name']),
134+
add_help=False,
134135
)
135-
sub.set_defaults(func=self._handle_dotnet_command)
136-
self.handlers[cmd['name']] = self._handle_dotnet_command
136+
self.register_handler(cmd['name'], self._handle_dotnet_command)
137137

138138
# Register bite command only if bite.proj exists
139139
if os.path.isfile(self.BITE_PROJ_PATH):
@@ -144,8 +144,16 @@ def get_argparser(self) -> argparse.ArgumentParser:
144144
usage=self.argparser_usage.replace('command', 'bite') + ' [target]',
145145
)
146146
bite_parser.add_argument('target', nargs='?', default='help', help='bite.core target to run, default is "help"')
147-
bite_parser.set_defaults(func=self._handle_bite)
148-
self.handlers['bite'] = self._handle_bite
147+
self.register_handler('bite', self._handle_bite)
148+
149+
dotnet_parser = subparsers.add_parser(
150+
'dotnet',
151+
help='Run a dotnet command',
152+
epilog=self.argparser_epilog + ' (Dotnet CLI)',
153+
usage=self.argparser_usage.replace('command', 'dotnet') + ' [command]',
154+
add_help=False,
155+
)
156+
self.register_handler('dotnet', self._handle_dotnet_cli)
149157

150158
self.argparser = parser
151159
return self.argparser
@@ -176,8 +184,7 @@ def add_command(
176184
if arguments:
177185
for arg in arguments:
178186
sub.add_argument(*arg.get('args', ()), **arg.get('kwargs', {}))
179-
sub.set_defaults(func=handler)
180-
self.handlers[name] = handler
187+
self.register_handler(name, handler)
181188

182189
def register_handler(self, command: str, handler: Callable[[argparse.Namespace, List[str]], None]) -> None:
183190
"""
@@ -198,19 +205,27 @@ def dispatch(self, args: argparse.Namespace, unknown_args: Optional[List[str]] =
198205
unknown_args: List of unknown arguments, if any.
199206
"""
200207
extras = unknown_args or []
201-
if hasattr(args, 'func'):
202-
args.func(args, extras)
208+
command = getattr(args, 'command', None)
209+
if command and command in self.handlers:
210+
self.handlers[command](args, extras)
203211
else:
204212
self.get_argparser().print_help()
205213

206214
# --- Dotnet/MSBuild Command Handlers ---
207215

216+
def _handle_dotnet_cli(self, args: argparse.Namespace, extras: List[str]) -> None:
217+
"""
218+
Handle custom dotnet commands.
219+
Passes any extra arguments to the dotnet CLI.
220+
"""
221+
self.run('', *extras)
222+
208223
def _handle_dotnet_command(self, args: argparse.Namespace, extras: List[str]) -> None:
209224
"""
210-
Handle standard dotnet commands: restore, clean, build, test, pack.
225+
Handle built-in dotnet commands.
211226
Passes any extra arguments to the dotnet CLI.
212227
"""
213-
self.run(args.command, *extras)
228+
self.run_builtin(args.command, *extras)
214229

215230
def _handle_bite(self, args: argparse.Namespace, extras: List[str]) -> None:
216231
"""
@@ -224,33 +239,36 @@ def _handle_bite(self, args: argparse.Namespace, extras: List[str]) -> None:
224239

225240
def run(self, command: str, *args: str) -> None:
226241
"""
227-
Run a dotnet command with the solution file as an argument.
242+
Run a dotnet command.
228243
229244
Args:
230-
command: The dotnet CLI command to run (e.g., 'build', 'restore').
245+
command: The dotnet CLI command to run.
231246
*args: Additional arguments to pass to the command.
232247
"""
233-
cmd = ['dotnet', command] + [self.solution] + self.DEFAULT_ARGS + list(args)
234-
try:
235-
subprocess.check_call(cmd)
236-
except subprocess.CalledProcessError as e:
237-
print(f"Error: dotnet {command} failed with exit code {e.returncode}")
238-
raise
248+
cmd = ['dotnet', command] + list(args)
249+
subprocess.call(cmd)
250+
251+
def run_builtin(self, command: str, *args: str) -> None:
252+
"""
253+
Run a built-in dotnet command with the solution file and default arguments.
254+
255+
Args:
256+
command: The dotnet command to run (e.g., 'build', 'restore').
257+
*args: Additional arguments to pass to the dotnet cli.
258+
"""
259+
cmd = [self.solution] + self.DEFAULT_ARGS + list(args)
260+
self.run(command, *cmd)
239261

240262
def run_bite(self, target: str, *args: str) -> None:
241263
"""
242-
Run bite.core with the specified target.
264+
Run bite.core with the specified target and default arguments.
243265
244266
Args:
245267
target: The bite.core target to run.
246268
*args: Additional arguments to pass to msbuild.
247269
"""
248-
cmd = ['dotnet', 'msbuild'] + self.DEFAULT_ARGS + [f'-t:{target}', self.BITE_PROJ_PATH] + list(args)
249-
try:
250-
subprocess.check_call(cmd)
251-
except subprocess.CalledProcessError as e:
252-
print(f"Error: msbuild target '{target}' failed with exit code {e.returncode}")
253-
raise
270+
cmd = self.DEFAULT_ARGS + [f'-t:{target}', self.BITE_PROJ_PATH] + list(args)
271+
self.run('msbuild', *cmd)
254272

255273
# --- SDK Installation ---
256274

0 commit comments

Comments
 (0)