@@ -3,6 +3,8 @@ module Runner ( run ) where
3import Control.Lens
3import Control.Lens
4import Data.Foldable as F
4import Data.Foldable as F
5import Data.List as L
5import Data.List as L
6+import Data.Maybe (fromMaybe)
7+import Data.Semigroup ((<>))
6import Distribution.Nixpkgs.Haskell.FromStack
8import Distribution.Nixpkgs.Haskell.FromStack
7import Distribution.Nixpkgs.Haskell.FromStack.Package
9import Distribution.Nixpkgs.Haskell.FromStack.Package
8import Distribution.Nixpkgs.Haskell.Stack
10import Distribution.Nixpkgs.Haskell.Stack
@@ -25,44 +27,43 @@ import qualified Data.Set as Set
25import qualified Data.Map as Map
27import qualified Data.Map as Map
26import qualified LtsHaskell as LH
28import qualified LtsHaskell as LH
29run :: IO ()
30run :: IO ()
31 opts <- execParser pinfo
32 opts <- execParser pinfo
34+ -- With optparse-applicative it seems there's no way to make
35+ -- 'StackageOptions' optional for stackage override and required in
37+ missingErr = error . ("Missing required parameter: " <>)
38+ sopts = StackageOptions
39+ (fromMaybe (missingErr "--all-cabal-hashes") (opts ^. optAllCabalHashesRepo) )
40+ (fromMaybe (missingErr "--lts-haskell") (opts ^. optLtsHaskellRepo))
32 case opts ^. optConfigOrigin of
42 case opts ^. optConfigOrigin of
33− -- Generate build derivation from stack.yaml file
43+ -- Generate Stackage packages from stack.yaml resolver
34− OriginStackYaml stackYaml -> do
44+ OriginStackYaml stackYaml buildTarget -> do
35 stackConf <- either fail pure =<< readStackConfig stackYaml
45 stackConf <- either fail pure =<< readStackConfig stackYaml
37− generateStackage = opts ^. optWithStackage || opts ^. optWithStackageClosure
47+ stackResolver = stackConf ^. scResolver
38 stackPackagesConfig = mkStackPackagesConfig opts
48 stackPackagesConfig = mkStackPackagesConfig opts
39− stackConfPackages <- traverse (packageDerivation stackPackagesConfig (opts ^. optHackageDb)) $ stackConf ^. scPackages
49+ stackConfPackages <- traverse (packageDerivation stackPackagesConfig (opts ^. optHackageDb))
50+ $ stackConf ^. scPackages
52+ reachable = Set.map mkPackageName
54+ $ nodeDepends . mkNode <$> stackConfPackages
41− case generateStackage of
56+ case buildTarget of
42− -- Generate Stackage packages from stack.yaml resolver
57+ TargetStackageClosure -> do
43− True -> do
58+ (buildPlanFile, buildPlan, packageSetConfig) <- loadStackageBuildPlan stackResolver opts sopts
44− let buildPlanFile = LH.buildPlanFilePath (opts ^. optLtsHaskellRepo) (stackConf ^. scResolver)
45− buildPlan <- LH.loadBuildPlan buildPlanFile
46− packageSetConfig <- LH.buildPackageSetConfig
47− (opts ^. optHackageDb)
48− (opts ^. optAllCabalHashesRepo)
49− (opts ^. optNixpkgsRepository)
52 overrideConfig = mkOverrideConfig opts (siGhcVersion $ bpSystemInfo buildPlan)
60 overrideConfig = mkOverrideConfig opts (siGhcVersion $ bpSystemInfo buildPlan)
53− reachable = Set.map mkPackageName
55− $ nodeDepends . mkNode <$> stackConfPackages
56− s2nLoader mHash pkgId =
61+ stackageLoader mHash pkgId =
57 if pkgName pkgId `Set.member` reachable
62 if pkgName pkgId `Set.member` reachable
58 then packageLoader packageSetConfig Nothing pkgId
63 then packageLoader packageSetConfig Nothing pkgId
59 else packageLoader packageSetConfig mHash pkgId
64 else packageLoader packageSetConfig mHash pkgId
60− s2nPackageSetConfig = packageSetConfig { packageLoader = s2nLoader }
65+ stackageSetConfig = packageSetConfig { packageLoader = stackageLoader }
61− s2nPackageConfig = PackageConfig
66+ stackagePackages <- buildStackagePackages stackageSetConfig buildPlan
63− , enableHaddock = True }
64− stackagePackages <- traverse (uncurry (buildNode s2nPackageSetConfig s2nPackageConfig))
65− $ Map.toAscList (bpPackages buildPlan)
67 -- Nixpkgs generic-builder puts hscolour on path for all libraries
68 -- Nixpkgs generic-builder puts hscolour on path for all libraries
68 withHscolour pkgs =
69 withHscolour pkgs =
@@ -75,13 +76,11 @@ run = do
75 -- does: pruning only after generating full set of packages allows
76 -- does: pruning only after generating full set of packages allows
76 -- us to make sure all those extra dependencies are explicitly
77 -- us to make sure all those extra dependencies are explicitly
77 -- listed as well.
78 -- listed as well.
78− nodes = case opts ^. optWithStackageClosure of
79− True -> Set.toAscList
79+ nodes = Set.toAscList
80 $ withHscolour
80 $ withHscolour
81 $ flip reachableDependencies stackagePackages
81 $ flip reachableDependencies stackagePackages
82 -- Originally reachable nodes are root nodes
82 -- Originally reachable nodes are root nodes
83 $ L.filter (\n -> mkPackageName (nodeName n) `Set.member` reachable) stackagePackages
83 $ L.filter (\n -> mkPackageName (nodeName n) `Set.member` reachable) stackagePackages
84− False -> stackagePackages
85 writeOutFile buildPlanFile (opts ^. optOutStackagePackages)
84 writeOutFile buildPlanFile (opts ^. optOutStackagePackages)
86 $ pPrintOutPackages (view nodeDerivation <$> nodes)
85 $ pPrintOutPackages (view nodeDerivation <$> nodes)
87 writeOutFile buildPlanFile (opts ^. optOutStackageConfig)
86 writeOutFile buildPlanFile (opts ^. optOutStackageConfig)
@@ -89,28 +88,32 @@ run = do
89 writeOutFile (stackYaml ^. syFilePath) (opts ^. optOutDerivation)
88 writeOutFile (stackYaml ^. syFilePath) (opts ^. optOutDerivation)
90 $ PP.overrideHaskellPackages overrideConfig stackConfPackages
89 $ PP.overrideHaskellPackages overrideConfig stackConfPackages
92− -- Generate Stackage override only
91+ TargetStackagePackages -> do
93− False ->
92+ (buildPlanFile, buildPlan, packageSetConfig) <- loadStackageBuildPlan stackResolver opts sopts
94+ overrideConfig = mkOverrideConfig opts (siGhcVersion $ bpSystemInfo buildPlan)
95+ stackageLoader mHash pkgId =
96+ if pkgName pkgId `Set.member` reachable
97+ then packageLoader packageSetConfig Nothing pkgId
98+ else packageLoader packageSetConfig mHash pkgId
99+ stackageSetConfig = packageSetConfig { packageLoader = stackageLoader }
100+ nodes <- buildStackagePackages stackageSetConfig buildPlan
101+ writeOutFile buildPlanFile (opts ^. optOutStackagePackages)
102+ $ pPrintOutPackages (view nodeDerivation <$> nodes)
103+ writeOutFile buildPlanFile (opts ^. optOutStackageConfig)
104+ $ pPrintOutConfig (bpSystemInfo buildPlan) nodes
105+ writeOutFile (stackYaml ^. syFilePath) (opts ^. optOutDerivation)
106+ $ PP.overrideHaskellPackages overrideConfig stackConfPackages
108+ TargetStackageOverride ->
94 writeOutFile (stackYaml ^. syFilePath) (opts ^. optOutDerivation)
109 writeOutFile (stackYaml ^. syFilePath) (opts ^. optOutDerivation)
95 $ PP.overrideStackage (stackConf ^. scResolver) (opts ^. optNixpkgsRepository) stackConfPackages
110 $ PP.overrideStackage (stackConf ^. scResolver) (opts ^. optNixpkgsRepository) stackConfPackages
97 -- Generate Stackage packages from resolver
112 -- Generate Stackage packages from resolver
98 OriginResolver stackResolver -> do
113 OriginResolver stackResolver -> do
99− let
114+ (buildPlanFile, buildPlan, packageSetConfig) <- loadStackageBuildPlan stackResolver opts sopts
100− buildPlanFile = LH.buildPlanFilePath (opts ^. optLtsHaskellRepo) stackResolver
115+ nodes <- buildStackagePackages packageSetConfig buildPlan
101− packageConfig = PackageConfig
102− { enableCheck = True
103− , enableHaddock = True }
104− buildPlan <- LH.loadBuildPlan buildPlanFile
105− packageSetConfig <- LH.buildPackageSetConfig
106− (opts ^. optHackageDb)
107− (opts ^. optAllCabalHashesRepo)
108− (opts ^. optNixpkgsRepository)
110− nodes <- traverse (uncurry (buildNode packageSetConfig packageConfig))
111− $ Map.toAscList (bpPackages buildPlan)
112 let overrideConfig = mkOverrideConfig opts (siGhcVersion $ bpSystemInfo buildPlan)
116 let overrideConfig = mkOverrideConfig opts (siGhcVersion $ bpSystemInfo buildPlan)
114 writeOutFile buildPlanFile (opts ^. optOutStackagePackages)
117 writeOutFile buildPlanFile (opts ^. optOutStackagePackages)
115 $ pPrintOutPackages (view nodeDerivation <$> nodes)
118 $ pPrintOutPackages (view nodeDerivation <$> nodes)
116 writeOutFile buildPlanFile (opts ^. optOutStackageConfig)
119 writeOutFile buildPlanFile (opts ^. optOutStackageConfig)
@@ -118,6 +121,26 @@ run = do
118 writeOutFile buildPlanFile (opts ^. optOutDerivation)
121 writeOutFile buildPlanFile (opts ^. optOutDerivation)
119 $ PP.pPrintHaskellPackages overrideConfig
122 $ PP.pPrintHaskellPackages overrideConfig
124+loadStackageBuildPlan :: StackResolver -> Options -> StackageOptions -> IO (FilePath, BuildPlan, PackageSetConfig)
125+loadStackageBuildPlan stackResolver opts sopts = do
126+ let buildPlanFile = LH.buildPlanFilePath (sopts ^. soptLtsHaskellRepo) stackResolver
127+ buildPlan <- LH.loadBuildPlan buildPlanFile
128+ packageSetConfig <- LH.buildPackageSetConfig
129+ (opts ^. optHackageDb)
130+ (sopts ^. soptAllCabalHashesRepo)
131+ (opts ^. optNixpkgsRepository)
133+ return (buildPlanFile, buildPlan, packageSetConfig)
135+buildStackagePackages :: PackageSetConfig -> BuildPlan -> IO [Node]
136+buildStackagePackages packageSetConfig buildPlan = do
138+ stackagePackageConfig = PackageConfig
139+ { enableCheck = True
140+ , enableHaddock = True }
141+ traverse (uncurry (buildNode packageSetConfig stackagePackageConfig))
142+ $ Map.toAscList (bpPackages buildPlan)
121writeOutFile :: Show source => source -> FilePath -> Doc -> IO ()
144writeOutFile :: Show source => source -> FilePath -> Doc -> IO ()
122writeOutFile source filePath contents =
145writeOutFile source filePath contents =
123 withFile filePath WriteMode $ \h -> do
146 withFile filePath WriteMode $ \h -> do