Skip to content

Commit

Permalink
Add --cwd-app flag
Browse files Browse the repository at this point in the history
Add `--cwd-app` flag to `protontricks` and `protontricks-launch` to set
the working directory to the game's installation directory. Also
document how to maintain previous behavior in the changelog.

This also makes it easier to maintain
backwards compatibility with scripts that assume installation directory
as the working directory. Scripts relying on the working directory
should be infrequent based on searching GitHub repositories for
`protontricks` usage, however.

Fixes #287
  • Loading branch information
Matoking committed Apr 22, 2024
1 parent 5d18e37 commit ca0f702
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]
### Added
- `--cwd-app` flag to set working directory to the game's installation directory

### Changed
- `-c` command now uses the current working directory instead of the game's installation directory
- `-c` command now uses the current working directory instead of the game's installation directory. `--cwd-app` can be used to restore old behavior. Scripts can also `$STEAM_APP_PATH` environment variable to determine the game's installation directory; this has been supported (albeit undocumented) since 1.8.0.

## [1.11.1] - 2024-02-20
### Fixed
Expand Down
13 changes: 13 additions & 0 deletions src/protontricks/cli/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ def main(args=None):
parser.add_argument(
"--appid", type=int, nargs="?", default=None
)
parser.add_argument(
"--cwd-app",
dest="cwd_app",
default=False,
action="store_true",
help=(
"Set the working directory of launched executable to the Steam "
"app's installation directory."
)
)
parser.add_argument("executable", type=str)
parser.add_argument("exec_args", nargs=argparse.REMAINDER)
parser.set_defaults(background_wineserver=False)
Expand Down Expand Up @@ -180,6 +190,9 @@ def exit_(error):
+ exec_args
)

if args.cwd_app:
cli_args += ["--cwd-app"]

cli_args += [
"-c", inner_args, str(appid)
]
Expand Down
24 changes: 21 additions & 3 deletions src/protontricks/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ def main(args=None, steam_path=None, steam_root=None):
"command startup time."
)
)
parser.add_argument(
"--cwd-app",
dest="cwd_app",
default=False,
action="store_true",
help=(
"Set the working directory of launched command to the Steam app's "
"installation directory."
)
)
parser.set_defaults(background_wineserver=False)

parser.add_argument("appid", type=int, nargs="?", default=None)
Expand Down Expand Up @@ -276,6 +286,8 @@ def exit_(error):
"YAD or Zenity is not installed. Either executable is required for the "
"Protontricks GUI."
)

cwd = str(steam_app.install_path) if args.cwd_app else None

# 6. Find Proton version of selected app
proton_app = find_proton_app(
Expand All @@ -291,6 +303,7 @@ def exit_(error):
"installation?"
)


run_command(
winetricks_path=winetricks_path,
proton_app=proton_app,
Expand All @@ -299,7 +312,8 @@ def exit_(error):
legacy_steam_runtime_path=legacy_steam_runtime_path,
command=[str(winetricks_path), "--gui"],
use_bwrap=use_bwrap,
start_wineserver=start_background_wineserver
start_wineserver=start_background_wineserver,
cwd=cwd
)

return
Expand Down Expand Up @@ -370,6 +384,8 @@ def exit_(error):
"$ protontricks -s <GAME NAME>"
)

cwd = str(steam_app.install_path) if args.cwd_app else None

if args.winetricks_command:
returncode = run_command(
winetricks_path=winetricks_path,
Expand All @@ -379,7 +395,8 @@ def exit_(error):
legacy_steam_runtime_path=legacy_steam_runtime_path,
use_bwrap=use_bwrap,
start_wineserver=start_background_wineserver,
command=[str(winetricks_path)] + args.winetricks_command
command=[str(winetricks_path)] + args.winetricks_command,
cwd=cwd
)
elif args.command:
returncode = run_command(
Expand All @@ -393,7 +410,8 @@ def exit_(error):
start_wineserver=start_background_wineserver,
# Pass the command directly into the shell *without*
# escaping it
shell=True
shell=True,
cwd=cwd,
)

logger.info("Command returned %d", returncode)
Expand Down
16 changes: 7 additions & 9 deletions tests/cli/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ def _set_launch_args(*args, **kwargs):
steam_app_factory(name="Fake game", appid=10)

launch_cli([
"--verbose", "--no-bwrap", "--no-runtime", "--no-term", "--appid",
"10", "test.exe"
"--verbose", "--no-bwrap", "--no-runtime", "--no-term",
"--cwd-app", "--appid", "10", "test.exe"
])

# CLI flags are passed through to the main CLI entrypoint
assert cli_args[0:6] == [
assert cli_args[0:7] == [
"-v", "--no-runtime", "--no-bwrap",
"--no-background-wineserver", "--no-term", "-c"
"--no-background-wineserver", "--no-term", "--cwd-app", "-c"
]
assert cli_args[6].startswith("wine ")
assert cli_args[6].endswith("test.exe")
assert cli_args[7] == "10"
assert cli_args[7].startswith("wine ")
assert cli_args[7].endswith("test.exe")
assert cli_args[8] == "10"

# Steam installation was provided to the main entrypoint
assert str(cli_kwargs["steam_path"]) == str(steam_dir)
Expand Down Expand Up @@ -231,5 +231,3 @@ def test_steam_installation_not_selected(self, launch_cli, gui_provider):
result = launch_cli(["test.exe"], expect_returncode=1)

assert "No Steam installation was selected" in result


18 changes: 17 additions & 1 deletion tests/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ def test_run_command(

# The command is just 'bash'
assert command.args == "bash"

assert command.cwd is None
assert command.shell is True

# Correct environment vars were set
Expand All @@ -816,6 +816,22 @@ def test_run_command(
str(proton_install_path / "dist" / "lib" / "wine")
)

@pytest.mark.usefixtures("default_proton")
def test_run_command_cwd_app(self, cli, steam_app_factory, command_mock):
"""
Run a shell command for a given game using `--cwd-app` flag and
ensure the working directory was set to the game's installation
directory
"""
steam_app = steam_app_factory(name="Fake game", appid=10)

cli(["--cwd-app", "-c", "bash", "10"])

command = command_mock.commands[-1]

assert command.args == "bash"
assert command.cwd == str(steam_app.install_path)


class TestCLISearch:
def test_search_case_insensitive(self, cli, steam_app_factory):
Expand Down

0 comments on commit ca0f702

Please sign in to comment.