diff --git a/app/Main.hs b/app/Main.hs index 209ce40..96ae6c5 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -8,6 +8,7 @@ import Unused.Types (ParseResponse, RemovalLikelihood(..)) import Unused.ResponseFilter (withOneOccurrence, withLikelihoods, ignoringPaths) import Unused.Grouping (CurrentGrouping(..), groupedResponses) import Unused.CLI (SearchRunner(..), withoutCursor, renderHeader, executeSearch, printParseError, printSearchResults, resetScreen, withInterruptHandler) +import Unused.Cache data Options = Options { oSearchRunner :: SearchRunner @@ -37,8 +38,7 @@ run options = withoutCursor $ do terms <- pure . lines =<< getContents renderHeader terms - results <- unlines <$> executeSearch (oSearchRunner options) terms - + results <- cached $ unlines <$> executeSearch (oSearchRunner options) terms let response = parseLines results resetScreen diff --git a/src/Unused/Cache.hs b/src/Unused/Cache.hs new file mode 100644 index 0000000..861451c --- /dev/null +++ b/src/Unused/Cache.hs @@ -0,0 +1,34 @@ +module Unused.Cache + ( cached + ) where + +import System.Directory +import Unused.Cache.DirectoryFingerprint (sha) + +cached :: IO String -> IO String +cached f = maybe (writeCache =<< f) return =<< readCache + +writeCache :: String -> IO String +writeCache contents = do + createDirectoryIfMissing True cacheDirectory + fileName <- cacheFileName + writeFile fileName contents + return contents + +readCache :: IO (Maybe String) +readCache = do + putStrLn "\n\nCalculating cache fingerprint... " + fileName <- cacheFileName + exists <- doesFileExist fileName + + if exists + then Just <$> readFile fileName + else return Nothing + +cacheFileName :: IO String +cacheFileName = do + currentSha <- sha + return $ cacheDirectory ++ "/" ++ currentSha ++ ".cache" + +cacheDirectory :: String +cacheDirectory = "tmp/unused" diff --git a/src/Unused/Cache/DirectoryFingerprint.hs b/src/Unused/Cache/DirectoryFingerprint.hs new file mode 100644 index 0000000..067860e --- /dev/null +++ b/src/Unused/Cache/DirectoryFingerprint.hs @@ -0,0 +1,22 @@ +module Unused.Cache.DirectoryFingerprint + ( sha + ) where + +import System.Process + +sha :: IO String +sha = + getSha <$> (fileList >>= sortInput >>= md5Result) + where + getSha = head' . lines + head' (x:_) = x + head' _ = "" + +fileList :: IO String +fileList = readProcess "find" [".", "-type", "f", "-not", "-path", "*/tmp/unused/*", "-exec", "md5", "{}", "+"] "" + +sortInput :: String -> IO String +sortInput = readProcess "sort" ["-k", "2"] + +md5Result :: String -> IO String +md5Result = readProcess "md5" [] diff --git a/unused.cabal b/unused.cabal index 6f6574f..9253be0 100644 --- a/unused.cabal +++ b/unused.cabal @@ -27,6 +27,8 @@ library , Unused.Grouping.Internal , Unused.Grouping.Types , Unused.LikelihoodCalculator + , Unused.Cache + , Unused.Cache.DirectoryFingerprint , Unused.CLI , Unused.CLI.Search , Unused.CLI.SearchError @@ -41,6 +43,7 @@ library , parsec , containers , filepath + , directory , regex-tdfa , terminal-progress-bar , ansi-terminal