Advertisement
anacrolix

Untitled

Apr 5th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env runghc
  2.  
  3. import System.FilePath
  4. import System.Environment
  5. import System.Process
  6. import System.Exit
  7. import System.Posix.Process
  8. import System.Directory
  9. import Data.Char (isSpace)
  10. import Data.Maybe (maybeToList)
  11. import Control.Applicative
  12.  
  13. main = do
  14.   (installArgs, package, packageArgs) <- processArgs <$> getArgs
  15.   installDir <- getTemporaryDirectory
  16.   install installArgs (maybeToList package) installDir
  17.   exec installDir package packageArgs
  18.  
  19. -- Remove trailing whitespace.
  20. rstrip = reverse . dropWhile isSpace . reverse
  21.  
  22. -- Prints a sequence of string pairs to stdout.
  23. printEnv = mapM_ $ \(k, v) -> putStrLn $ k ++ ": " ++ v
  24.  
  25. -- Returns args for go install, package, and args for the package binary.
  26. processArgs args = (installArgs, package, packageArgs) where
  27.   (installArgs, remainder) = span (('-' ==) . head) args
  28.   (package, packageArgs) = case remainder of
  29.     [] -> (Nothing, [])
  30.     otherwise -> (Just $ head remainder, tail remainder)
  31.  
  32. -- Installs `packages` passing `flags` to directory `dest`.
  33. install flags packages dest = do
  34.   -- Take our environment and clobber GOBIN.
  35.   installEnv <- (("GOBIN", dest) :) <$> getEnvironment
  36.   -- Run go install with modified environment.
  37.   (_, _, _, handle) <- createProcess
  38.     (proc "go" $ "install":flags++packages)
  39.     { env = Just installEnv }
  40.   code <- waitForProcess handle
  41.   case code of
  42.     ExitSuccess -> return ()
  43.     ExitFailure _ -> exitWith code
  44.  
  45. -- Execs into the binary for `package` located at `path` with `flags` as arguments.
  46. exec path package flags = do
  47.   -- Get the OS executable extension.
  48.   gOEXE <- rstrip <$> readProcess "go" ["env", "GOEXE"] ""
  49.   -- Determine the binary name for the package.
  50.   packageBase <- do
  51.     curDir <- getCurrentDirectory
  52.     return . last . splitDirectories . normalise . joinPath $ curDir:maybeToList package
  53.   let exeFilePath = joinPath [path, packageBase <.> gOEXE]
  54.   executeFile exeFilePath False flags Nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement