packages feed

multi-cabal-0.1.0.0: src/MultiCabal/Commands/Install.hs

{- |
Module      :  $Header$
Description :  A simple installation command.
Author      :  Nils 'bash0r' Jonsson
Copyright   :  (c) 2015 Nils 'bash0r' Jonsson
License     :  MIT

Maintainer  :  aka.bash0r@gmail.com
Stability   :  unstable
Portability :  non-portable (Portability is untested.)

A simple installation command.
-}
module MultiCabal.Commands.Install
( invokeExec
, invokeHelp
) where

import Data.Aeson

import qualified Data.ByteString.Lazy as BS

import System.Directory
import System.FilePath.Posix
import System.IO hiding (hGetContents)
import System.IO.Strict (hGetContents)
import System.Process


import Data.Model.Project
import Data.Model.Utility

import Logic.Dependency.Resolution


invokeExec :: [String] -> IO ()
invokeExec [] = do
    cwd <- getCurrentDirectory
    let projectFile = cwd </> "project.json"
    exists <- doesFileExist projectFile
    if exists
       then do
         h <- openFile projectFile ReadMode
         c <- BS.hGetContents h
         let result = decode c >>= resolve
         case result of
           Just a  -> do
             mapM_ installProject a
             return ()
           Nothing ->
             putStrLn "Your project.json is corrupt or contains circular dependencies."
         hClose h
       else do
         putStrLn "No project.json in the current working directory."
         putStrLn "Create a project.json and try again."
  where
    installProject (Project name dir _) = do
      putStrLn ("Installing project " ++ name ++ "...")
      cwd <- getCurrentDirectory
      (_, Just hout, _, _) <- createProcess_ "" (proc "cabal" ["install", cwd </> dir]){ std_out = CreatePipe }
      putStrLn =<< hGetContents hout
invokeExec _  =
    putStrLn invokeHelp

invokeHelp :: String
invokeHelp =
    "multi-cabal install\n" ++
    "  installs the multi cabal project"