forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClean.hs
More file actions
81 lines (73 loc) · 2.73 KB
/
Clean.hs
File metadata and controls
81 lines (73 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
-- | Clean a project.
module Stack.Clean
(clean
,CleanOpts(..)
,StackCleanException(..)
) where
import Control.Exception (Exception)
import Control.Monad.Catch (throwM)
import Data.Foldable (forM_)
import Data.List ((\\),intercalate)
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe)
import Data.Typeable (Typeable)
import Path (Path, Abs, Dir)
import Path.IO (ignoringAbsence, removeDirRecur)
import Stack.Build.Source (getLocalPackageViews)
import Stack.Build.Target (LocalPackageView(..))
import Stack.Config (getLocalPackages)
import Stack.Constants (distDirFromDir, workDirFromDir)
import Stack.Types.PackageName
import Stack.Types.Config
import Stack.Types.StackT
-- | Deletes build artifacts in the current project.
--
-- Throws 'StackCleanException'.
clean
:: (StackM env m, HasEnvConfig env)
=> CleanOpts
-> m ()
clean cleanOpts = do
dirs <- dirsToDelete cleanOpts
forM_ dirs (ignoringAbsence . removeDirRecur)
dirsToDelete
:: (StackM env m, HasEnvConfig env)
=> CleanOpts
-> m [Path Abs Dir]
dirsToDelete cleanOpts = do
packages <- getLocalPackages
let localPkgDirs = Map.keys packages
case cleanOpts of
CleanShallow [] -> do
mapM distDirFromDir localPkgDirs
CleanShallow targets -> do
localPkgViews <- getLocalPackageViews
let localPkgNames = Map.keys localPkgViews
getPkgDir pkgName = fmap (lpvRoot . fst) (Map.lookup pkgName localPkgViews)
case targets \\ localPkgNames of
[] -> mapM distDirFromDir (mapMaybe getPkgDir targets)
xs -> throwM (NonLocalPackages xs)
CleanFull -> do
pkgWorkDirs <- mapM workDirFromDir localPkgDirs
projectWorkDir <- getProjectWorkDir
return (projectWorkDir : pkgWorkDirs)
-- | Options for @stack clean@.
data CleanOpts
= CleanShallow [PackageName]
-- ^ Delete the "dist directories" as defined in 'Stack.Constants.distRelativeDir'
-- for the given local packages. If no packages are given, all project packages
-- should be cleaned.
| CleanFull
-- ^ Delete all work directories in the project.
-- | Exceptions during cleanup.
newtype StackCleanException
= NonLocalPackages [PackageName]
deriving (Typeable)
instance Show StackCleanException where
show (NonLocalPackages pkgs) =
"The following packages are not part of this project: " ++
intercalate ", " (map show pkgs)
instance Exception StackCleanException