Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Re-wire hydrateItems
Browse files Browse the repository at this point in the history
  • Loading branch information
skilly-lily committed Sep 27, 2021
1 parent b2df7e2 commit 5252c81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
34 changes: 18 additions & 16 deletions src/Graphing/Hydrate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Graphing.Hydrate (
Hydrateable (..),
hydrateEnvs,
hydrateItems,
) where

import Algebra.Graph.AdjacencyMap qualified as AM
Expand All @@ -13,31 +13,33 @@ import Data.Set qualified as Set
import Graphing (Graphing)
import Graphing qualified

-- | This function returns the same structure of graph with dependency
-- environments passed down through their children.
hydrateEnvs :: forall a b. Hydrateable a b => Graphing a -> Graphing a
hydrateEnvs gr = gr'
-- | Given some 'Graphing', return a 'Graphing' where all successors
-- of each node are
hydrateItems :: forall a b. Hydrateable a b => Graphing a -> Graphing a
hydrateItems gr = gr'
where
gr' = Graphing.gmap promoteAll gr
adjMap = Graphing.toAdjacencyMap gr
-- Get all current deps with specified env
topVia env = AM.vertexList $ AM.induce (elem env . extractList) adjMap
-- Get all deps reachable from a list of deps
-- Get all current nodes which contain a specified subitem
topVia subItem = AM.vertexList $ AM.induce (elem subItem . extractList) adjMap
-- Get all nodes reachable from a list of nodes
allFrom = Set.fromList . concatMap (`AMA.reachable` adjMap)

-- Dedup'd dep environments from all vertice in the AdjMap
allEnvs :: [b]
allEnvs = nubOrd $ foldMap extractList $ AM.vertexList adjMap
-- Dedup'd sub-items from all vertices in the AdjMap
allSubItems :: [b]
allSubItems = nubOrd $ foldMap extractList $ AM.vertexList adjMap

-- If @node@ is reachable from some node with subitem @a@,
-- update @node@ with that subitem @a@.
promote :: b -> a -> a
promote env dep =
if Set.member dep $ allFrom $ topVia env
then update env dep
else dep
promote subItem node =
if Set.member node $ allFrom $ topVia subItem
then update subItem node
else node

-- Combine list of promotion fuctions into one
promoteAll :: a -> a
promoteAll = appEndo . mconcat $ map (Endo . promote) allEnvs
promoteAll = appEndo . mconcat $ map (Endo . promote) allSubItems

class (Ord a, Ord b) => Hydrateable a b | a -> b where
extractList :: a -> [b]
Expand Down
4 changes: 2 additions & 2 deletions src/Strategy/Node/YarnV1/YarnLock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Effect.ReadFS (ReadFS, ReadFSErr (FileParseError), readContentsText)
import Graphing (Graphing)
import Path (Abs, File, Path)
import Strategy.Node.Types (FlatDeps (..), NodePackage (..))
import Graphing.Hydrate (hydrateEnvs)
import Graphing.Hydrate (hydrateItems)
import Types (DependencyResults (DependencyResults), GraphBreadth (Complete))
import Yarn.Lock qualified as YL
import Yarn.Lock.Types qualified as YL
Expand All @@ -63,7 +63,7 @@ analyze yarnFile flatdeps = do
-- from their incoming edges.
-- This is complicated by the fact that yarn graphs can have arbitrary cycles.
-- We use hydrateEnvs to resolve this.
pure $ DependencyResults (hydrateEnvs dryGraph) Complete $ yarnFile : Set.toList (manifests flatdeps)
pure $ DependencyResults (hydrateItems dryGraph) Complete $ yarnFile : Set.toList (manifests flatdeps)

mangleParseErr :: FilePath -> YL.LockfileError -> ReadFSErr
mangleParseErr path = FileParseError path . YL.prettyLockfileError
Expand Down
4 changes: 2 additions & 2 deletions src/Strategy/Node/YarnV2/YarnLock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Graphing (Graphing)
import Graphing qualified
import Path
import Strategy.Node.Types (FlatDeps (..), NodePackage (..))
import Graphing.Hydrate (hydrateEnvs)
import Graphing.Hydrate (hydrateItems)
import Strategy.Node.YarnV2.Lockfile
import Strategy.Node.YarnV2.Resolvers
import Types (DependencyResults (..), GraphBreadth (Complete))
Expand Down Expand Up @@ -130,7 +130,7 @@ instance Monoid FlatPackages where
-- Because workspaces are top-level projects, we set their dependencies as
-- direct in the dependency graph
buildGraph :: AM.AdjacencyMap Package -> FlatPackages -> Graphing Dependency
buildGraph gr FlatPackages{..} = hydrateEnvs convertedGraphing
buildGraph gr FlatPackages{..} = hydrateItems convertedGraphing
where
isWorkspace WorkspacePackage{} = True
isWorkspace _ = False
Expand Down

0 comments on commit 5252c81

Please sign in to comment.