Move cache for project files into separate module

This commit is contained in:
Jeroen Engels 2023-04-14 18:36:33 +02:00
parent 02f3a3081d
commit 02e73bf24c
3 changed files with 104 additions and 96 deletions

View File

@ -1,91 +1,12 @@
module Review.Cache exposing
( FinalProjectEvaluationCache
, ProjectFileCache
, createEntryForProjectFileCache
, createFinalProjectEvaluationCache
, errorsForFinalProjectEvaluationCache
, errorsForMaybeProjectFileCache
, errorsFromProjectFileCache
, matchFinalProjectEvaluationCache
, matchProjectFileCache
, outputContextForProjectFileCache
, outputContextHashForProjectFileCache
, setErrorsForFinalProjectEvaluationCache
, setErrorsForMaybeProjectFileCache
)
import Review.Cache.ContentHash as ContentHash exposing (ContentHash)
import Review.Cache.ContextHash as ContextHash exposing (ComparableContextHash, ContextHash)
{-| Variant where the content may be absent
-}
type ProjectFileCache error context
= ProjectFileCache
{ contentHash : Maybe ContentHash
, inputContextHash : ComparableContextHash context
, errors : List error
, outputContext : context
, outputContextHash : ContextHash context
}
createEntryForProjectFileCache :
{ contentHash : Maybe ContentHash
, inputContextHash : ComparableContextHash context
, errors : List error
, outputContext : context
}
-> ProjectFileCache error context
createEntryForProjectFileCache entry =
ProjectFileCache
{ contentHash = entry.contentHash
, inputContextHash = entry.inputContextHash
, errors = entry.errors
, outputContext = entry.outputContext
, outputContextHash = ContextHash.create entry.outputContext
}
outputContextForProjectFileCache : ProjectFileCache error context -> context
outputContextForProjectFileCache (ProjectFileCache entry) =
entry.outputContext
outputContextHashForProjectFileCache : ProjectFileCache error context -> ContextHash context
outputContextHashForProjectFileCache (ProjectFileCache entry) =
entry.outputContextHash
errorsFromProjectFileCache : ProjectFileCache error context -> List error
errorsFromProjectFileCache (ProjectFileCache entry) =
entry.errors
setErrorsForMaybeProjectFileCache : List error -> Maybe (ProjectFileCache error context) -> Maybe (ProjectFileCache error context)
setErrorsForMaybeProjectFileCache newErrors maybeEntry =
case maybeEntry of
Just (ProjectFileCache entry) ->
Just (ProjectFileCache { entry | errors = newErrors })
Nothing ->
Nothing
errorsForMaybeProjectFileCache : Maybe (ProjectFileCache error context) -> List error
errorsForMaybeProjectFileCache maybeEntry =
case maybeEntry of
Just (ProjectFileCache entry) ->
entry.errors
Nothing ->
[]
matchProjectFileCache : Maybe ContentHash -> ComparableContextHash context -> ProjectFileCache error context -> Bool
matchProjectFileCache contentHash contexts (ProjectFileCache entry) =
ContentHash.areEqualForMaybe contentHash entry.contentHash
&& (contexts == entry.inputContextHash)
import Review.Cache.ContextHash exposing (ComparableContextHash, ContextHash)
{-| Variant for final operations like the final evaluation or the extract

View File

@ -0,0 +1,86 @@
module Review.Cache.ProjectFile exposing
( Entry, create
, match
, errors, errorsForMaybe, setErrors
, outputContext, outputContextHash
)
{-| Cache for the result of the analysis of a project file (elm.json, README.md, docs.json for dependencies).
@docs Entry, create
@docs match
@docs errors, errorsForMaybe, setErrors
@docs outputContext, outputContextHash
-}
import Review.Cache.ContentHash as ContentHash exposing (ContentHash)
import Review.Cache.ContextHash as ContextHash exposing (ComparableContextHash, ContextHash)
type Entry error context
= Entry
{ contentHash : Maybe ContentHash
, inputContextHash : ComparableContextHash context
, errors : List error
, outputContext : context
, outputContextHash : ContextHash context
}
create :
{ contentHash : Maybe ContentHash
, inputContextHash : ComparableContextHash context
, errors : List error
, outputContext : context
}
-> Entry error context
create entry =
Entry
{ contentHash = entry.contentHash
, inputContextHash = entry.inputContextHash
, errors = entry.errors
, outputContext = entry.outputContext
, outputContextHash = ContextHash.create entry.outputContext
}
match : Maybe ContentHash -> ComparableContextHash context -> Entry error context -> Bool
match contentHash contexts (Entry entry) =
ContentHash.areEqualForMaybe contentHash entry.contentHash
&& (contexts == entry.inputContextHash)
errors : Entry error context -> List error
errors (Entry entry) =
entry.errors
errorsForMaybe : Maybe (Entry error context) -> List error
errorsForMaybe maybeEntry =
case maybeEntry of
Just (Entry entry) ->
entry.errors
Nothing ->
[]
setErrors : List error -> Maybe (Entry error context) -> Maybe (Entry error context)
setErrors newErrors maybeEntry =
case maybeEntry of
Just (Entry entry) ->
Just (Entry { entry | errors = newErrors })
Nothing ->
Nothing
outputContext : Entry error context -> context
outputContext (Entry entry) =
entry.outputContext
outputContextHash : Entry error context -> ContextHash context
outputContextHash (Entry entry) =
entry.outputContextHash

View File

@ -311,6 +311,7 @@ import Review.Cache as Cache
import Review.Cache.ContentHash exposing (ContentHash)
import Review.Cache.ContextHash as ContextHash exposing (ComparableContextHash, ContextHash)
import Review.Cache.Module as ModuleCache
import Review.Cache.ProjectFile as ProjectFileCache
import Review.ElmProjectEncoder
import Review.Error exposing (InternalError)
import Review.Exceptions as Exceptions exposing (Exceptions)
@ -4159,7 +4160,7 @@ type alias ModuleCacheEntry projectContext =
type alias ProjectFileCache projectContext =
Cache.ProjectFileCache (Error {}) projectContext
ProjectFileCache.Entry (Error {}) projectContext
type alias FinalProjectEvaluationCache projectContext =
@ -4291,9 +4292,9 @@ errorsFromCache : ProjectRuleCache projectContext -> List (Error {})
errorsFromCache cache =
List.concat
[ Dict.foldl (\_ cacheEntry acc -> List.append (ModuleCache.errors cacheEntry) acc) [] cache.moduleContexts
, Cache.errorsForMaybeProjectFileCache cache.elmJson
, Cache.errorsForMaybeProjectFileCache cache.readme
, Cache.errorsForMaybeProjectFileCache cache.dependencies
, ProjectFileCache.errorsForMaybe cache.elmJson
, ProjectFileCache.errorsForMaybe cache.readme
, ProjectFileCache.errorsForMaybe cache.dependencies
, Maybe.map Cache.errorsForFinalProjectEvaluationCache cache.finalEvaluationErrors |> Maybe.withDefault []
]
@ -5454,9 +5455,9 @@ createRuleProjectVisitor schema initialProject ruleData initialCache =
-- TODO This is called at the wrong moment: This contains the state of the project with fixes that haven't been applied.
, getErrors = \() -> errorsFromCache (finalCacheMarker schema.name hidden.ruleData.ruleId cache)
, setErrorsForModule = \filePath newErrors -> raiseCache { cache | moduleContexts = Dict.update filePath (Maybe.map (\entry -> ModuleCache.setErrors newErrors entry)) cache.moduleContexts }
, setErrorsForElmJson = \newErrors -> raiseCache { cache | elmJson = Cache.setErrorsForMaybeProjectFileCache newErrors cache.elmJson }
, setErrorsForReadme = \newErrors -> raiseCache { cache | readme = Cache.setErrorsForMaybeProjectFileCache newErrors cache.readme }
, setErrorsForDependencies = \newErrors -> raiseCache { cache | dependencies = Cache.setErrorsForMaybeProjectFileCache newErrors cache.dependencies }
, setErrorsForElmJson = \newErrors -> raiseCache { cache | elmJson = ProjectFileCache.setErrors newErrors cache.elmJson }
, setErrorsForReadme = \newErrors -> raiseCache { cache | readme = ProjectFileCache.setErrors newErrors cache.readme }
, setErrorsForDependencies = \newErrors -> raiseCache { cache | dependencies = ProjectFileCache.setErrors newErrors cache.dependencies }
, setErrorsForFinalEvaluation = \newErrors -> raiseCache { cache | finalEvaluationErrors = Cache.setErrorsForFinalProjectEvaluationCache newErrors cache.finalEvaluationErrors }
, backToRule =
\() ->
@ -5481,7 +5482,7 @@ createProjectVisitor :
ProjectRuleSchemaData projectContext moduleContext
-> RuleProjectVisitorHidden projectContext
-> Maybe (data -> projectContext -> ( List (Error {}), projectContext ))
-> List (Maybe (Cache.ProjectFileCache error projectContext))
-> List (Maybe (ProjectFileCache projectContext))
-> (ValidProject -> Maybe ContentHash)
-> (ProjectRuleCache projectContext -> Maybe (ProjectFileCache projectContext))
-> (ProjectFileCache projectContext -> RuleProjectVisitor)
@ -5514,11 +5515,11 @@ createProjectVisitor schema hidden maybeVisitor possibleInputContexts computeCon
cachePredicate : ProjectFileCache projectContext -> Bool
cachePredicate elmJson =
Cache.matchProjectFileCache contentHash inputContextHash elmJson
ProjectFileCache.match contentHash inputContextHash elmJson
in
case reuseProjectRuleCache cachePredicate cacheGetter hidden.cache of
Just entry ->
( Cache.errorsFromProjectFileCache entry, toRuleProjectVisitorWithoutChangingCache () )
( ProjectFileCache.errors entry, toRuleProjectVisitorWithoutChangingCache () )
Nothing ->
let
@ -5530,7 +5531,7 @@ createProjectVisitor schema hidden maybeVisitor possibleInputContexts computeCon
filterExceptionsAndSetName hidden.ruleData.exceptions schema.name errorsForVisitor
in
( errors
, Cache.createEntryForProjectFileCache
, ProjectFileCache.create
{ contentHash = contentHash
, errors = errors
, inputContextHash = inputContextHash
@ -5578,11 +5579,11 @@ createDependenciesVisitor schema { exceptions } raise cache { allVisitor, direct
cachePredicate : ProjectFileCache projectContext -> Bool
cachePredicate entry =
Cache.matchProjectFileCache dependenciesHash inputContextHash entry
ProjectFileCache.match dependenciesHash inputContextHash entry
in
case reuseProjectRuleCache cachePredicate .dependencies cache of
Just entry ->
( Cache.errorsFromProjectFileCache entry, raise cache )
( ProjectFileCache.errors entry, raise cache )
Nothing ->
let
@ -5608,7 +5609,7 @@ createDependenciesVisitor schema { exceptions } raise cache { allVisitor, direct
dependenciesEntry : ProjectFileCache projectContext
dependenciesEntry =
Cache.createEntryForProjectFileCache
ProjectFileCache.create
{ contentHash = dependenciesHash
, errors = errors
, inputContextHash = inputContextHash
@ -5621,14 +5622,14 @@ createDependenciesVisitor schema { exceptions } raise cache { allVisitor, direct
)
findInitialInputContext : projectContext -> List (Maybe (Cache.ProjectFileCache error projectContext)) -> ( List (ContextHash projectContext), projectContext )
findInitialInputContext : projectContext -> List (Maybe (ProjectFileCache projectContext)) -> ( List (ContextHash projectContext), projectContext )
findInitialInputContext defaultContext possibleInputContexts =
case possibleInputContexts of
[] ->
( [], defaultContext )
(Just cacheEntry) :: _ ->
( [ Cache.outputContextHashForProjectFileCache cacheEntry ], Cache.outputContextForProjectFileCache cacheEntry )
( [ ProjectFileCache.outputContextHash cacheEntry ], ProjectFileCache.outputContext cacheEntry )
Nothing :: rest ->
findInitialInputContext defaultContext rest