Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sloppy bob sh #77

Merged
merged 7 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ To run a project with Bob, run:
bob run
```

Alternatively, you can enter the temporary installation prefix environment with:

```console
bob sh
```

This will use the value stored in the `$SHELL` environment variable to launch a shell inside of that prefix, similar to a Python virtual environment.
You can also just run any command you want by passing it to `bob sh`, like so:

```console
bob sh echo test
bob sh -- sh -c "echo \$PATH"
```

The second command will print out the value of the `$PATH` environment variable, and it should be prepended by the `bin` directory of the temporary installation prefix.

### Installing

Bob always helps his grandma install [Linux Mint](https://linuxmint.com/) on her computer! 👵
Expand Down
5 changes: 2 additions & 3 deletions src/bsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,12 @@ int bsys_sh(bsys_t const* bsys, int argc, char* argv[]) {
return -1;
}

LOG_SUCCESS("Entering shell ('%s').", shell);
cmd_add(&cmd, shell);
}

else {
for (int i = 1; i < argc; i++) {
cmd_add(&cmd, argv[i]);
}
cmd_add_argv(&cmd, argc, argv);
}

if (cmd_exec_inplace(&cmd) < 0) {
Expand Down
4 changes: 1 addition & 3 deletions src/bsys/bob/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,7 @@ found:;
// Then, add the arguments passed to the Bob frontend.
// If there is no default runner, just pass the arguments from the frontend onwards.

for (ssize_t i = 0; i < argc; i++) {
cmd_add(&cmd, argv[i]);
}
cmd_add_argv(&cmd, argc, argv);

// Make sure there actually is something in the command.

Expand Down
12 changes: 12 additions & 0 deletions src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ __attribute__((__format__(__printf__, 2, 3))) void cmd_addf(cmd_t* cmd, char con
va_end(va);
}

void cmd_add_argv(cmd_t* cmd, int argc, char* argv[]) {
for (ssize_t i = 0; i < argc; i++) {
// On BSD/macOS, getopt doesn't consume the '--' argument.

if (strcmp(argv[i], "--") == 0) {
continue;
}

cmd_add(cmd, argv[i]);
}
}

static bool is_executable(char const* path) {
struct stat sb;

Expand Down
1 change: 1 addition & 0 deletions src/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct {
void cmd_create(cmd_t* cmd, ...);
void cmd_add(cmd_t* cmd, char const* arg);
__attribute__((__format__(__printf__, 2, 3))) void cmd_addf(cmd_t* cmd, char const* fmt, ...);
void cmd_add_argv(cmd_t* cmd, int argc, char* argv[]);
int cmd_exec_inplace(cmd_t* cmd);
pid_t cmd_exec_async(cmd_t* cmd);
int cmd_exec(cmd_t* cmd);
Expand Down
26 changes: 26 additions & 0 deletions tests/sh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

. tests/common.sh

# Test the 'bob sh' command.
# Also kind of a regression test for the following PR:
# https://github.com/inobulles/bob/pull/77

if [ "$(bob sh echo test | tail -n1)" != "test" ]; then
echo "Failed to run command in shell." >&2
exit 1
fi

if [ "$(bob sh -- sh -c "echo \$PATH" | tail -n1 | cut -d':' -f1)" != ".bob/prefix/bin" ]; then
echo "Temporary installation prefix not in \$PATH." >&2
exit 1
fi

# XXX Can't really test 'bob sh' on it's own "properly" I don't think.

export SHELL=uname

if [ "$(bob sh | tail -n1)" != "$(uname)" ]; then
echo "Failed to run command in shell." >&2
exit 1
fi