1
+ {-# LANGUAGE RecordWildCards #-}
2
+
1
3
module Stack2Cabal
2
- ( module Stack2Cabal.Compiler
3
- , module Stack2Cabal.Config
4
- , module Stack2Cabal.Constraints
5
- , module Stack2Cabal.Git
6
- , module Stack2Cabal.Packages
7
- , module Stack2Cabal.Util
8
- , writeCabalProject
4
+ ( parseConfig
5
+ , configCabal
9
6
) where
10
7
8
+ import Control.Monad (when , forM_ )
9
+ import Data.Maybe (mapMaybe )
11
10
import System.Directory (doesPathExist )
12
11
import System.FilePath ((</>) , (<.>) )
13
12
import System.IO (withFile , IOMode (WriteMode ), hPutStrLn )
@@ -19,14 +18,43 @@ import Stack2Cabal.Git
19
18
import Stack2Cabal.Packages
20
19
import Stack2Cabal.Util
21
20
22
- writeCabalProject :: FilePath -> IO ()
23
- writeCabalProject dir = do
24
- let file = dir </> " cabal" <.> " project"
21
+ configCabal :: Config -> IO ()
22
+ configCabal cnf@ Config {.. } =
23
+ withLog (" creating cabal config for project folder '" ++ cnfProjectDir ++ " '" ) $ do
24
+ packages <- parseStackYaml cnfProjectDir
25
+ writeCabalProject cnf packages
26
+ handleGitDependencies cnf packages
27
+
28
+ writeCabalProject :: Config -> [Package ] -> IO ()
29
+ writeCabalProject Config {.. } packages = do
30
+ let file = cnfProjectDir </> " cabal" <.> " project"
25
31
b <- doesPathExist file
26
- if b then putLogLn " cabal project file already exists"
27
- else withLog " writing cabal project file" $
28
- withFile file WriteMode $ \ h -> do
29
- ps <- writePackagesBlock " git-packages" <$> parseStackYaml dir
30
- cs <- writeCompilerBlock <$> getCompiler dir
31
- ds <- writeConstraintsBlock <$> getConstraints dir
32
- mapM_ (hPutStrLn h) $ ps ++ cs ++ ds
32
+ case (b, cnfCabalProject) of
33
+ (True , Force ) -> wcp file
34
+ (True , Yes ) -> putLogLn " cabal.project already exists"
35
+ (False , Force ) -> wcp file
36
+ (False , Yes ) -> wcp file
37
+ (_, No ) -> return ()
38
+ where
39
+ wcp :: FilePath -> IO ()
40
+ wcp file = withLog " writing cabal.project" $
41
+ withFile file WriteMode $ \ h -> do
42
+ let ps = writePackagesBlock gitFolder packages -- <$> parseStackYaml cnfProjectDir
43
+ cs <- writeCompilerBlock <$> getCompiler cnfProjectDir
44
+ ds <- writeConstraintsBlock <$> getConstraints cnfProjectDir
45
+ mapM_ (hPutStrLn h) $ ps ++ cs ++ ds
46
+
47
+ handleGitDependencies :: Config -> [Package ] -> IO ()
48
+ handleGitDependencies Config {.. } packages =
49
+ when cnfGitDependencies $ do
50
+ let dir = cnfProjectDir </> gitFolder
51
+ gs = mapMaybe filterGit packages
52
+ forM_ gs $ \ git ->
53
+ cloneAndCheckoutGit dir git
54
+ where
55
+ filterGit :: Package -> Maybe Git
56
+ filterGit (GitPackage git) = Just git
57
+ filterGit (LocalPackage _) = Nothing
58
+
59
+ gitFolder :: FilePath
60
+ gitFolder = " .stack2cabal"
0 commit comments