Skip to content

Commit

Permalink
add support for programs/ ghc tarballs too (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
juhp committed Aug 22, 2023
1 parent d14f365 commit f341f4f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
85 changes: 85 additions & 0 deletions src/GHCTarball.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module GHCTarball (
listGhcTarballs,
removeGhcMinorTarball,
removeGhcVersionTarball
)
where

import Control.Monad.Extra
import Data.Char (isDigit)
import Data.List.Extra
import Data.Version.Extra
import SimpleCmd
import System.FilePath (dropExtension)
import System.FilePath.Glob

import Directories (getStackSubdir, switchToSystemDirUnder)
import qualified Remove
import Types
import Versions

getStackProgramsDir :: IO FilePath
getStackProgramsDir =
getStackSubdir "programs"

setStackProgramsDir :: Maybe String -> IO ()
setStackProgramsDir msystem =
getStackProgramsDir >>= switchToSystemDirUnder msystem

getGhcTarballs :: Maybe Version -> Maybe String -> IO [FilePath]
getGhcTarballs mghcver msystem = do
setStackProgramsDir msystem
sortOn ghcTarballVersion . map (dropPrefix "./") <$> namesMatching ("ghc" ++ matchVersion ++ ".tar.*")
where
matchVersion =
case mghcver of
Nothing -> "*"
Just ver ->
"*-" ++ showVersion ver ++ if isMajorVersion ver then "*" else ""

ghcTarballVersion :: FilePath -> Version
ghcTarballVersion =
readVersion . checkChars . takeWhileEnd (/= '-') . dropSuffix ".tar" . dropExtension
where
checkChars vs =
let isVerChar c = isDigit c || c == '.'
in if all isVerChar vs
then vs
else error $ "unknown version:" +-+ vs

listGhcTarballs :: Maybe Version -> Maybe String -> IO ()
listGhcTarballs mghcver msystem = do
files <- getGhcTarballs mghcver msystem
mapM_ putStrLn $
case mghcver of
Nothing -> files
Just ghcver -> filter ((== ghcver) . (if isMajorVersion ghcver then majorVersion else id) . ghcTarballVersion) files

removeGhcVersionTarball :: Deletion -> Version -> Maybe String -> IO ()
removeGhcVersionTarball deletion ghcver msystem = do
files <- getGhcTarballs (Just ghcver) msystem
case files of
[] -> putStrLn $ "Tarball for " ++ showVersion ghcver ++ " not found"
[g] | not (isMajorVersion ghcver) -> doRemoveGhcTarballVersion deletion g
gs -> if isMajorVersion ghcver then do
Remove.prompt deletion ("all stack ghc " ++ showVersion ghcver ++ " tarballs: ")
mapM_ (doRemoveGhcTarballVersion deletion) gs
else error' "more than one match found!!"

removeGhcMinorTarball :: Deletion -> Maybe Version -> Maybe String
-> IO ()
removeGhcMinorTarball deletion mghcver msystem = do
files <- getGhcTarballs (majorVersion <$> mghcver) msystem
case mghcver of
Nothing -> do
let majors = groupOn (majorVersion . ghcTarballVersion) files
forM_ majors $ \ minors ->
forM_ (init minors) $ doRemoveGhcTarballVersion deletion
Just ghcver -> do
let minors = filter ((< ghcver) . ghcTarballVersion) files
forM_ minors $ doRemoveGhcTarballVersion deletion

doRemoveGhcTarballVersion :: Deletion -> FilePath -> IO ()
doRemoveGhcTarballVersion deletion ghctarball = do
Remove.removeFile deletion ghctarball
putStrLn $ ghctarball ++ " tarball " ++ (if isDelete deletion then "" else "would be ") ++ "removed"
11 changes: 9 additions & 2 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import System.FilePath
import System.IO (BufferMode(NoBuffering), hSetBuffering, stdout)

import GHC
import GHCTarball
import Paths_stack_clean_old (version)
import Snapshots
import Types

data Mode = Default | Project | Snapshots | Compilers | GHC
data Mode = Default | Project | Snapshots | Compilers | GHC | Tarballs

data Recursion = Subdirs | Recursive
deriving Eq
Expand Down Expand Up @@ -74,7 +75,8 @@ main = do
flagWith' Project 'P' "project" "Act on current project's .stack-work/ [default in project dir]" <|>
flagWith' Snapshots 'S' "snapshots" "Act on ~/.stack/snapshots/" <|>
flagWith' GHC 'G' "global" "Act on both ~/.stack/{programs,snapshots}/ [default outside project dir]" <|>
flagWith Default Compilers 'C' "compilers" "Act on ~/.stack/programs/"
flagWith' Compilers 'C' "compilers" "Act on ~/.stack/programs/ installations" <|>
flagWith Default Tarballs 'T' "tarballs" "Act on ~/.stack/programs/ tarballs"

deleteOpt = flagWith Dryrun Delete 'd' "delete" "Do deletion [default is dryrun]"

Expand Down Expand Up @@ -123,6 +125,7 @@ sizeCmd mode mrecursion notHuman =
Project -> withRecursion' False False mrecursion $ sizeStackWork notHuman
Snapshots -> sizeSnapshots notHuman
Compilers -> sizeGhcInstalls notHuman
Tarballs -> error' "use --compilers"
GHC -> do
sizeCmd Snapshots Nothing notHuman
sizeCmd Compilers Nothing notHuman
Expand All @@ -139,6 +142,7 @@ listCmd mode mrecursion mver msystem =
Project -> setStackWorkInstallDir msystem >> listGhcSnapshots mver
Snapshots -> setStackSnapshotsDir msystem >> listGhcSnapshots mver
Compilers -> listGhcInstallation mver msystem
Tarballs -> listGhcTarballs mver msystem
GHC -> do
listCmd Snapshots Nothing mver msystem
listCmd Compilers Nothing mver msystem
Expand Down Expand Up @@ -169,6 +173,8 @@ removeRun deletion mode mrecursion ghcver msystem =
cleanGhcSnapshots deletion cwd ghcver
Compilers -> do
removeGhcVersionInstallation deletion ghcver msystem
Tarballs -> do
removeGhcVersionTarball deletion ghcver msystem
GHC -> do
removeRun deletion Compilers Nothing ghcver msystem
removeRun deletion Snapshots Nothing ghcver msystem
Expand Down Expand Up @@ -198,6 +204,7 @@ removeMinorsRun deletion mode mrecursion mver msystem = do
setStackSnapshotsDir msystem
cleanMinorSnapshots deletion cwd mver
Compilers -> removeGhcMinorInstallation deletion mver msystem
Tarballs -> removeGhcMinorTarball deletion mver msystem
GHC -> do
removeMinorsRun deletion Compilers Nothing mver msystem
removeMinorsRun deletion Snapshots Nothing mver msystem
Expand Down
1 change: 1 addition & 0 deletions stack-clean-old.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ executable stack-clean-old
other-modules: Paths_stack_clean_old
Directories
GHC
GHCTarball
Remove
Snapshots
Types
Expand Down

0 comments on commit f341f4f

Please sign in to comment.