Skip to content

Commit

Permalink
Special handling for the Dart language binary
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed May 9, 2024
1 parent c21398c commit f8ce44a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
28 changes: 27 additions & 1 deletion px/px_commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ def get_command(commandline: str) -> str:
if command == "java":
return faillog(commandline, get_java_command(commandline))

if command == "dart":
return faillog(commandline, get_dart_command(commandline))

if command == "ruby":
# Switches list inspired by ruby 2.3.7p456 --help output
return faillog(
Expand Down Expand Up @@ -384,6 +387,29 @@ def get_aws_command(args: List[str]) -> Optional[str]:
return " ".join(result)


def get_dart_command(commandline: str) -> Optional[str]:
"""Returns None if we failed to figure out the script name"""
array = to_array(commandline)
dart = os.path.basename(array[0])
if len(array) == 1:
return dart

for candidate in array[1:]:
if candidate.startswith("-"):
continue

for _, char in enumerate(candidate):
if not char.islower():
# Not a subcommand, something like "/bin/hello.dart"
return os.path.basename(candidate)

# This was a subcommand
return dart + " " + candidate

# Neither subcommand nor file name found, give up
return None


def get_sudo_command(commandline: str) -> Optional[str]:
"""Returns None if we failed to figure out the script name"""
without_sudo = commandline[5:].strip()
Expand Down Expand Up @@ -508,7 +534,7 @@ def get_with_subcommand(

if array[1].startswith("-"):
# Unknown option, help!
return command
return None

return f"{command} {array[1]}"

Expand Down
41 changes: 41 additions & 0 deletions tests/px_commandline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,47 @@ def test_get_command_java_equinox():
)


# Ref:https://github.com/walles/px/issues/126
def test_get_command_flutter():
# Candidate contains no slashes => subcommand
assert (
px_commandline.get_command(
"/usr/local/Caskroom/flutter/3.0.1/flutter/bin/cache/dart-sdk/bin/dart "
"devtools "
"--machine "
"--allow-embedding "
)
== "dart devtools"
)

# Candidate contains slashes => file to run
assert (
px_commandline.get_command(
"/usr/local/Caskroom/flutter/3.0.1/flutter/bin/cache/dart-sdk/bin/dart "
"--disable-dart-dev "
"--packages=/usr/local/Caskroom/flutter/3.0.1/flutter/packages/flutter_tools/.dart_tool/package_config.json "
"/usr/local/Caskroom/flutter/3.0.1/flutter/bin/cache/flutter_tools "
"upgrade"
)
== "flutter_tools"
)

# Candidate contains dots (.) => file to run
assert (
px_commandline.get_command(
"/usr/local/Caskroom/flutter/3.0.1/flutter/bin/cache/dart-sdk/bin/dart "
"--verbosity=error "
"--disable-dart-dev "
"--snapshot=/usr/local/Caskroom/flutter/3.0.1/flutter/bin/cache/flutter_tools.snapshot "
"--snapshot-kind=app-jit "
"--packages=/usr/local/Caskroom/flutter/3.0.1/flutter/packages/flutter_tools/.dart_tool/package_config.json "
"--no-enable-mirrors "
"flutter_tools.dart "
)
== "flutter_tools.dart"
)


def test_get_command_electron_macos():
# Note that if we have spaces inside of the path, the path in
# question needs to be valid on the local system for it to
Expand Down

0 comments on commit f8ce44a

Please sign in to comment.