Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env runghc
- import System.FilePath
- import System.Environment
- import System.Process
- import System.Exit
- import System.Posix.Process
- import System.Directory
- import Data.Char (isSpace)
- import Data.Maybe (maybeToList)
- import Control.Applicative
- main = do
- (installArgs, package, packageArgs) <- processArgs <$> getArgs
- installDir <- getTemporaryDirectory
- install installArgs (maybeToList package) installDir
- exec installDir package packageArgs
- -- Remove trailing whitespace.
- rstrip = reverse . dropWhile isSpace . reverse
- -- Prints a sequence of string pairs to stdout.
- printEnv = mapM_ $ \(k, v) -> putStrLn $ k ++ ": " ++ v
- -- Returns args for go install, package, and args for the package binary.
- processArgs args = (installArgs, package, packageArgs) where
- (installArgs, remainder) = span (('-' ==) . head) args
- (package, packageArgs) = case remainder of
- [] -> (Nothing, [])
- otherwise -> (Just $ head remainder, tail remainder)
- -- Installs `packages` passing `flags` to directory `dest`.
- install flags packages dest = do
- -- Take our environment and clobber GOBIN.
- installEnv <- (("GOBIN", dest) :) <$> getEnvironment
- -- Run go install with modified environment.
- (_, _, _, handle) <- createProcess
- (proc "go" $ "install":flags++packages)
- { env = Just installEnv }
- code <- waitForProcess handle
- case code of
- ExitSuccess -> return ()
- ExitFailure _ -> exitWith code
- -- Execs into the binary for `package` located at `path` with `flags` as arguments.
- exec path package flags = do
- -- Get the OS executable extension.
- gOEXE <- rstrip <$> readProcess "go" ["env", "GOEXE"] ""
- -- Determine the binary name for the package.
- packageBase <- do
- curDir <- getCurrentDirectory
- return . last . splitDirectories . normalise . joinPath $ curDir:maybeToList package
- let exeFilePath = joinPath [path, packageBase <.> gOEXE]
- executeFile exeFilePath False flags Nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement