|
| 1 | +----------------------------------------------------------------------------- |
| 2 | +-- | |
| 3 | +-- Module : Distribution.Client.GenBounds |
| 4 | +-- Copyright : (c) Doug Beardsley 2015 |
| 5 | +-- License : BSD-like |
| 6 | +-- |
| 7 | +-- Maintainer : cabal-devel@gmail.com |
| 8 | +-- Stability : provisional |
| 9 | +-- Portability : portable |
| 10 | +-- |
| 11 | +-- The cabal gen-bounds command for generating PVP-compliant version bounds. |
| 12 | +----------------------------------------------------------------------------- |
| 13 | +module Distribution.Client.GenBounds ( |
| 14 | + genBounds |
| 15 | + ) where |
| 16 | + |
| 17 | +import Data.Version |
| 18 | + ( Version(..), showVersion ) |
| 19 | +import Distribution.Client.Freeze |
| 20 | + ( getFreezePkgs ) |
| 21 | +import Distribution.Client.Sandbox.Types |
| 22 | + ( SandboxPackageInfo(..) ) |
| 23 | +import Distribution.Client.Setup |
| 24 | + ( GlobalFlags(..), FreezeFlags(..) ) |
| 25 | +import Distribution.Client.Types |
| 26 | +import Distribution.Package |
| 27 | + ( Package(..), Dependency(..), PackageName(..) |
| 28 | + , packageName, packageVersion ) |
| 29 | +import Distribution.PackageDescription |
| 30 | + ( buildDepends ) |
| 31 | +import Distribution.PackageDescription.Configuration |
| 32 | + ( finalizePackageDescription ) |
| 33 | +import Distribution.PackageDescription.Parse |
| 34 | + ( readPackageDescription ) |
| 35 | +import Distribution.Simple.Compiler |
| 36 | + ( Compiler, PackageDBStack, compilerInfo ) |
| 37 | +import Distribution.Simple.Program |
| 38 | + ( ProgramConfiguration ) |
| 39 | +import Distribution.Simple.Utils |
| 40 | + ( tryFindPackageDesc ) |
| 41 | +import Distribution.System |
| 42 | + ( Platform ) |
| 43 | +import Distribution.Verbosity |
| 44 | + ( Verbosity ) |
| 45 | +import Distribution.Version |
| 46 | + ( LowerBound(..), UpperBound(..), VersionRange(..), asVersionIntervals |
| 47 | + , orLaterVersion, earlierVersion, intersectVersionRanges ) |
| 48 | +import System.Directory |
| 49 | + ( getCurrentDirectory ) |
| 50 | + |
| 51 | +hasUpperBound :: VersionRange -> Bool |
| 52 | +hasUpperBound vr = |
| 53 | + case asVersionIntervals vr of |
| 54 | + [] -> False |
| 55 | + is -> if snd (last is) == NoUpperBound then False else True |
| 56 | + |
| 57 | +-- This version is slightly different than the one in |
| 58 | +-- Distribution.Client.Init. This one uses a.b.c as the lower bound because |
| 59 | +-- the user could be using a new function introduced in a.b.c which would |
| 60 | +-- make "> a.b" incorrect. |
| 61 | +pvpize :: Version -> VersionRange |
| 62 | +pvpize v = orLaterVersion (vn 3) |
| 63 | + `intersectVersionRanges` |
| 64 | + earlierVersion (incVersion 1 (vn 2)) |
| 65 | + where |
| 66 | + vn n = (v { versionBranch = take n (versionBranch v) }) |
| 67 | + |
| 68 | +incVersion :: Int -> Version -> Version |
| 69 | +incVersion n (Version vlist tags) = Version (incVersion' n vlist) tags |
| 70 | + where |
| 71 | + incVersion' 0 [] = [1] |
| 72 | + incVersion' 0 (v:_) = [v+1] |
| 73 | + incVersion' m [] = replicate m 0 ++ [1] |
| 74 | + incVersion' m (v:vs) = v : incVersion' (m-1) vs |
| 75 | + |
| 76 | +showInterval :: (LowerBound, UpperBound) -> String |
| 77 | +showInterval (LowerBound _ _, NoUpperBound) = |
| 78 | + error "Error: expected upper bound...this should never happen!" |
| 79 | +showInterval (LowerBound l _, UpperBound u _) = |
| 80 | + unwords [">=", showVersion l, "&& <", showVersion u] |
| 81 | + |
| 82 | +padAfter :: Int -> String -> String |
| 83 | +padAfter n str = str ++ replicate (n - length str) ' ' |
| 84 | + |
| 85 | +showBounds :: Package pkg => Int -> pkg -> String |
| 86 | +showBounds padTo p = unwords $ |
| 87 | + (padAfter padTo $ unPackageName $ packageName p) : |
| 88 | + map showInterval (asVersionIntervals $ pvpize $ packageVersion p) |
| 89 | + |
| 90 | +genBounds |
| 91 | + :: Verbosity |
| 92 | + -> PackageDBStack |
| 93 | + -> [Repo] |
| 94 | + -> Compiler |
| 95 | + -> Platform |
| 96 | + -> ProgramConfiguration |
| 97 | + -> Maybe SandboxPackageInfo |
| 98 | + -> GlobalFlags |
| 99 | + -> FreezeFlags |
| 100 | + -> IO () |
| 101 | +genBounds verbosity packageDBs repos comp platform conf mSandboxPkgInfo |
| 102 | + globalFlags freezeFlags = do |
| 103 | + |
| 104 | + let cinfo = compilerInfo comp |
| 105 | + |
| 106 | + cwd <- getCurrentDirectory |
| 107 | + path <- tryFindPackageDesc cwd |
| 108 | + gpd <- readPackageDescription verbosity path |
| 109 | + let epd = finalizePackageDescription [] (const True) platform cinfo [] gpd |
| 110 | + case epd of |
| 111 | + Left _ -> putStrLn "finalizePackageDescription failed" |
| 112 | + Right (pd,_) -> do |
| 113 | + let needBounds = filter (not . hasUpperBound . depVersion) $ buildDepends pd |
| 114 | + |
| 115 | + if (null needBounds) |
| 116 | + then putStrLn "Congratulations, all your dependencies have upper bounds!" |
| 117 | + else go needBounds |
| 118 | + where |
| 119 | + go needBounds = do |
| 120 | + pkgs <- getFreezePkgs |
| 121 | + verbosity packageDBs repos comp platform conf mSandboxPkgInfo |
| 122 | + globalFlags freezeFlags |
| 123 | + |
| 124 | + putStrLn $ unlines |
| 125 | + [ "" |
| 126 | + , "The following packages need bounds and here is a suggested starting point." |
| 127 | + , "You can copy and paste this into the build-depends section in your .cabal" |
| 128 | + , "file and it should work (with the appropriate removal of commas)." |
| 129 | + , "" |
| 130 | + , "Note that version bounds are a statement that you've successfully built and" |
| 131 | + , "tested your package and expect it to work with any of the specified package" |
| 132 | + , "versions (PROVIDED that those packages continue to conform with the PVP)." |
| 133 | + , "Therefore, the version bounds generated here are the most conservative" |
| 134 | + , "based on the versions that you are currently building with. If you know" |
| 135 | + , "your package will work with versions outside the ranges generated here," |
| 136 | + , "feel free to widen them." |
| 137 | + , "" |
| 138 | + ] |
| 139 | + |
| 140 | + let isNeeded pkg = unPackageName (packageName pkg) `elem` map depName needBounds |
| 141 | + let thePkgs = filter isNeeded pkgs |
| 142 | + |
| 143 | + let padTo = maximum $ map (length . unPackageName . packageName) pkgs |
| 144 | + mapM_ (putStrLn . (++",") . showBounds padTo) thePkgs |
| 145 | + |
| 146 | +depName :: Dependency -> String |
| 147 | +depName (Dependency (PackageName nm) _) = nm |
| 148 | + |
| 149 | +depVersion :: Dependency -> VersionRange |
| 150 | +depVersion (Dependency _ vr) = vr |
| 151 | + |
0 commit comments