Skip to content

Commit 6a6a27f

Browse files
committed
Merge pull request commercialhaskell#1649 from panamiga/1609-full-clean
Added option for full working dir cleanup in command clean
2 parents b5e6c8f + a11839a commit 6a6a27f

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

src/Stack/Clean.hs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ module Stack.Clean
88
) where
99

1010
import Control.Exception (Exception)
11+
import Control.Monad (when)
1112
import Control.Monad.Catch (MonadThrow,throwM)
1213
import Control.Monad.IO.Class (MonadIO)
1314
import Control.Monad.Logger (MonadLogger)
14-
import Control.Monad.Reader (MonadReader)
15+
import Control.Monad.Reader (MonadReader, asks)
1516
import Data.Foldable (forM_)
1617
import Data.List ((\\),intercalate)
1718
import qualified Data.Map.Strict as Map
@@ -20,8 +21,8 @@ import Data.Typeable (Typeable)
2021
import Path.IO (removeTreeIfExists)
2122
import Stack.Build.Source (getLocalPackageViews)
2223
import Stack.Build.Target (LocalPackageView(..))
23-
import Stack.Constants (distDirFromDir)
24-
import Stack.Types (HasEnvConfig,PackageName)
24+
import Stack.Constants (distDirFromDir, workDirFromDir)
25+
import Stack.Types (HasEnvConfig,PackageName, bcWorkDir, getBuildConfig)
2526

2627

2728
-- | Reset the build, i.e. remove the @dist@ directory
@@ -33,7 +34,16 @@ clean
3334
:: (MonadThrow m, MonadIO m, MonadReader env m, HasEnvConfig env, MonadLogger m)
3435
=> CleanOpts
3536
-> m ()
36-
clean (CleanOpts targets) = do
37+
clean (CleanTargets targets) =
38+
cleanup targets False
39+
clean (CleanFull _ ) =
40+
cleanup [] True
41+
42+
cleanup
43+
:: (MonadThrow m, MonadIO m, MonadReader env m, HasEnvConfig env, MonadLogger m)
44+
=> [PackageName] -> Bool
45+
-> m()
46+
cleanup targets doFullClean = do
3747
locals <- getLocalPackageViews
3848
case targets \\ Map.keys locals of
3949
[] -> do
@@ -42,16 +52,24 @@ clean (CleanOpts targets) = do
4252
then Map.elems locals -- default to cleaning all local packages
4353
else mapMaybe (`Map.lookup` locals) targets
4454
forM_ lpvs $ \(LocalPackageView{lpvRoot = pkgDir},_) -> do
45-
distDir <- distDirFromDir pkgDir
46-
removeTreeIfExists distDir
55+
let delDir =
56+
if doFullClean
57+
then workDirFromDir pkgDir
58+
else distDirFromDir pkgDir
59+
removeTreeIfExists =<< delDir
60+
when doFullClean $ do
61+
bconfig <- asks getBuildConfig
62+
bcwd <- bcWorkDir bconfig
63+
removeTreeIfExists bcwd
4764
pkgs -> throwM (NonLocalPackages pkgs)
4865

4966
-- | Options for cleaning a project.
50-
newtype CleanOpts = CleanOpts
67+
data CleanOpts = CleanTargets
5168
{ cleanOptsTargets :: [PackageName]
5269
-- ^ Names of the packages to clean.
5370
-- If the list is empty, every local package should be cleaned.
5471
}
72+
| CleanFull { cleanOptsFull :: Bool }
5573

5674
-- | Exceptions during cleanup.
5775
newtype StackCleanException

src/Stack/Constants.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Stack.Constants
1010
,configuredFileFromDir
1111
,defaultShakeThreads
1212
,distDirFromDir
13+
,workDirFromDir
1314
,distRelativeDir
1415
,haskellModuleExts
1516
,imageStagingDir
@@ -203,6 +204,13 @@ distDirFromDir :: (MonadThrow m, MonadReader env m, HasPlatform env, HasEnvConfi
203204
distDirFromDir fp =
204205
liftM (fp </>) distRelativeDir
205206

207+
-- | Package's working directory.
208+
workDirFromDir :: (MonadThrow m, MonadReader env m, HasPlatform env, HasEnvConfig env)
209+
=> Path Abs Dir
210+
-> m (Path Abs Dir)
211+
workDirFromDir fp =
212+
liftM (fp </>) getWorkDir
213+
206214
-- | Directory for project templates.
207215
templatesDir :: Config -> Path Abs Dir
208216
templatesDir config = configStackRoot config </> $(mkRelDir "templates")

src/Stack/Options.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,17 @@ readFlag = do
269269

270270
-- | Command-line parser for the clean command.
271271
cleanOptsParser :: Parser CleanOpts
272-
cleanOptsParser = CleanOpts <$> packages
272+
cleanOptsParser = CleanTargets <$> packages <|> CleanFull <$> doFullClean
273273
where
274274
packages =
275275
many
276276
(packageNameArgument
277277
(metavar "PACKAGE" <>
278278
help "If none specified, clean all local packages"))
279+
doFullClean =
280+
switch
281+
(long "full" <>
282+
help "Remove whole the work dir, default is .stack-work")
279283

280284
-- | Command-line arguments parser for configuration.
281285
configOptsParser :: Bool -> Parser ConfigMonoid

0 commit comments

Comments
 (0)