mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-08-15 15:50:26 +03:00
Read unchanged results from the cache
At some point, this also needs to md5 the tags list itself and factor that in (since if the tagging algorithm changes, and new tokens get uncovered, it'd invalidate the cache)
This commit is contained in:
parent
85df4ae01f
commit
44ab0a1435
@ -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
|
||||
|
34
src/Unused/Cache.hs
Normal file
34
src/Unused/Cache.hs
Normal file
@ -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"
|
22
src/Unused/Cache/DirectoryFingerprint.hs
Normal file
22
src/Unused/Cache/DirectoryFingerprint.hs
Normal file
@ -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" []
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user