module Distribution.Simple.Program.Script (
invocationAsSystemScript,
invocationAsShellScript,
invocationAsBatchFile,
) where
import Distribution.Simple.Program.Run
( ProgramInvocation(..) )
import Distribution.System
( OS(..) )
import Data.Maybe
( maybeToList )
invocationAsSystemScript :: OS -> ProgramInvocation -> String
invocationAsSystemScript Windows = invocationAsBatchFile
invocationAsSystemScript _ = invocationAsShellScript
invocationAsShellScript :: ProgramInvocation -> String
invocationAsShellScript
ProgramInvocation {
progInvokePath = path,
progInvokeArgs = args,
progInvokeEnv = envExtra,
progInvokeCwd = mcwd,
progInvokeInput = minput
} = unlines $
[ "#!/bin/sh" ]
++ [ "export " ++ var ++ "=" ++ quote val
| (var,val) <- envExtra ]
++ [ "cd " ++ quote cwd | cwd <- maybeToList mcwd ]
++ [ (case minput of
Nothing -> ""
Just input -> "echo " ++ quote input ++ " | ")
++ quote path ++ concatMap (\arg -> ' ':quote arg) args ]
where
quote :: String -> String
quote s = "'" ++ escape s ++ "'"
escape [] = []
escape ('\'':cs) = "'\\''" ++ escape cs
escape (c :cs) = c : escape cs
invocationAsBatchFile :: ProgramInvocation -> String
invocationAsBatchFile
ProgramInvocation {
progInvokePath = path,
progInvokeArgs = args,
progInvokeEnv = envExtra,
progInvokeCwd = mcwd,
progInvokeInput = minput
} = unlines $
[ "@echo off" ]
++ [ "set " ++ var ++ "=" ++ escape val | (var,val) <- envExtra ]
++ [ "cd \"" ++ cwd ++ "\"" | cwd <- maybeToList mcwd ]
++ case minput of
Nothing ->
[ path ++ concatMap (' ':) args ]
Just input ->
[ "(" ]
++ [ "echo " ++ escape line | line <- lines input ]
++ [ ") | "
++ "\"" ++ path ++ "\""
++ concatMap (\arg -> ' ':quote arg) args ]
where
quote :: String -> String
quote s = "\"" ++ escapeQ s ++ "\""
escapeQ [] = []
escapeQ ('"':cs) = "\"\"\"" ++ escapeQ cs
escapeQ (c :cs) = c : escapeQ cs
escape [] = []
escape ('|':cs) = "^|" ++ escape cs
escape ('<':cs) = "^<" ++ escape cs
escape ('>':cs) = "^>" ++ escape cs
escape ('&':cs) = "^&" ++ escape cs
escape ('(':cs) = "^(" ++ escape cs
escape (')':cs) = "^)" ++ escape cs
escape ('^':cs) = "^^" ++ escape cs
escape (c :cs) = c : escape cs