anacrolix

Untitled

Apr 5th, 2014
180
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.  
  11. -- Remove trailing whitespace
  12. rstrip = reverse . dropWhile isSpace . reverse
  13.  
  14. -- Prints a sequence of string pairs to stdout
  15. printEnv = mapM_ $ \(k, v) -> putStrLn $ k ++ ": " ++ v
  16.  
  17. main = do
  18.     args <- getArgs
  19.     let installArgs = takeWhile ((==) '-' . head) args
  20.     let package:packageArgs =
  21.           if null remainder
  22.             then ["."] else remainder
  23.           where remainder = drop (length installArgs) args
  24.     gOBIN <- getTemporaryDirectory
  25.     -- Take our environment and clobber GOBIN.
  26.     installEnv <- getEnvironment >>= return . (:) ("GOBIN", gOBIN)
  27.     -- Run go install with modified environment.
  28.     (_, _, _, handle) <- createProcess
  29.       (proc "go" $ "install":installArgs++[package])
  30.       { env = Just installEnv }
  31.     code <- waitForProcess handle
  32.     case code of
  33.         ExitSuccess -> return ()
  34.         ExitFailure _ -> exitWith code
  35.  
  36.     gOEXE <- readProcess "go" ["env", "GOEXE"] "" >>= return . rstrip
  37.     let exeFilePath = joinPath [gOBIN, packageBase <.> gOEXE]
  38.           where packageBase = last . splitDirectories $ package
  39.       in executeFile exeFilePath False packageArgs Nothing
Advertisement
Add Comment
Please, Sign In to add comment