macOS: When launching a shell script use its shebang, if present

This commit is contained in:
Kovid Goyal 2022-01-07 13:52:13 +05:30
parent d42f2071c3
commit bf84f95a94
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -2206,6 +2206,18 @@ class Boss:
with suppress(OSError): with suppress(OSError):
is_dir = os.path.isdir(path) is_dir = os.path.isdir(path)
def parse_macos_shebang(path: str) -> List[str]:
# The macos kernel splits the shebang line on spaces
try:
f = open(path)
except OSError:
return []
with f:
if f.read(2) != '#!':
return []
line = f.readline().strip()
return line.split(' ')
needs_new_os_window = self.cocoa_application_launched or not self.os_window_map or self.active_tab is None needs_new_os_window = self.cocoa_application_launched or not self.os_window_map or self.active_tab is None
launch_cmd = [] launch_cmd = []
if needs_new_os_window: if needs_new_os_window:
@ -2219,9 +2231,9 @@ class Boss:
mt = guess_type(path) or '' mt = guess_type(path) or ''
ext = os.path.splitext(path)[1].lower() ext = os.path.splitext(path)[1].lower()
if ext in ('.sh', '.command', '.tool'): if ext in ('.sh', '.command', '.tool'):
launch_cmd += ['--hold', shell_path, path] launch_cmd += ['--hold'] + (parse_macos_shebang(path) or [shell_path]) + [path]
elif ext in ('.zsh', '.bash', '.fish'): elif ext in ('.zsh', '.bash', '.fish'):
launch_cmd += ['--hold', ext[1:], path] launch_cmd += ['--hold'] + (parse_macos_shebang(path) or [ext[1:]]) + [path]
elif mt.startswith('text/'): elif mt.startswith('text/'):
launch_cmd += get_editor() + [path] launch_cmd += get_editor() + [path]
elif mt.startswith('image/'): elif mt.startswith('image/'):