Simplify determineShell

This commit is contained in:
Joseph C. Sible 2020-02-09 22:41:02 -05:00
parent ea24e25efd
commit c95914f9b3
1 changed files with 7 additions and 11 deletions

View File

@ -239,19 +239,15 @@ prop_determineShell8 = determineShellTest' (Just Ksh) "#!/bin/sh" == Sh
determineShellTest = determineShellTest' Nothing determineShellTest = determineShellTest' Nothing
determineShellTest' fallbackShell = determineShell fallbackShell . fromJust . prRoot . pScript determineShellTest' fallbackShell = determineShell fallbackShell . fromJust . prRoot . pScript
determineShell fallbackShell t = fromMaybe Bash $ do determineShell fallbackShell t = fromMaybe Bash $
shellString <- foldl mplus Nothing $ getCandidates t
shellForExecutable shellString `mplus` fallbackShell shellForExecutable shellString `mplus` fallbackShell
where where
forAnnotation t = shellString = getCandidate t
case t of getCandidate :: Token -> String
(ShellOverride s) -> return s getCandidate t@T_Script {} = fromShebang t
_ -> fail "" getCandidate (T_Annotation _ annotations s) =
getCandidates :: Token -> [Maybe String] fromMaybe (fromShebang s) $
getCandidates t@T_Script {} = [Just $ fromShebang t] listToMaybe [s | ShellOverride s <- annotations]
getCandidates (T_Annotation _ annotations s) =
map forAnnotation annotations ++
[Just $ fromShebang s]
fromShebang (T_Script _ (T_Literal _ s) _) = executableFromShebang s fromShebang (T_Script _ (T_Literal _ s) _) = executableFromShebang s
-- Given a string like "/bin/bash" or "/usr/bin/env dash", -- Given a string like "/bin/bash" or "/usr/bin/env dash",