Skip to content

Commit

Permalink
add adaptive fix for haskell#6864
Browse files Browse the repository at this point in the history
  • Loading branch information
emilypi committed May 5, 2021
1 parent d2242f3 commit e398baa
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 62 deletions.
2 changes: 0 additions & 2 deletions cabal-install/src/Distribution/Client/Init/Defaults.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
-- Default values to use in cabal init (if not specified in config/flags).
--
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wno-deferred-type-errors #-}
module Distribution.Client.Init.Defaults

( -- * default init values
Expand Down
10 changes: 6 additions & 4 deletions cabal-install/src/Distribution/Client/Init/FlagExtractors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import Distribution.Simple.Flag (flagElim)

import Language.Haskell.Extension (Language(..), Extension(..))
import Distribution.Client.Init.Prompt
import qualified Data.Set as Set



Expand Down Expand Up @@ -122,13 +123,14 @@ getCategory :: Interactive m => InitFlags -> m String -> m String
getCategory flags = fromFlagOrPrompt (category flags)

-- | Try to guess extra source files (don't prompt the user).
getExtraSrcFiles :: Interactive m => InitFlags -> m [String]
getExtraSrcFiles = pure . fromFlagOrDefault [] . extraSrc
getExtraSrcFiles :: Interactive m => InitFlags -> m (Set String)
getExtraSrcFiles = pure . flagElim mempty Set.fromList . extraSrc

-- | Try to guess extra source files (don't prompt the user).
getExtraDocFiles :: Interactive m => InitFlags -> m (NonEmpty String)
getExtraDocFiles :: Interactive m => InitFlags -> m (Maybe (Set String))
getExtraDocFiles = pure
. flagElim (defaultChangelog NEL.:| []) NEL.fromList
. Just
. flagElim (Set.singleton defaultChangelog) Set.fromList
. extraDoc

-- | Ask whether the project builds a library or executable.
Expand Down
26 changes: 17 additions & 9 deletions cabal-install/src/Distribution/Client/Init/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,24 @@ mkPkgDescription opts pkgDesc =
[]
False
opts
, field "extra-doc-files" formatExtraSourceFiles (toList $ _pkgExtraDocFiles pkgDesc)
["Extra doc files to be distributed with the package, such as a CHANGELOG or a README."]
True
opts
, case _pkgExtraDocFiles pkgDesc of
Nothing -> PrettyEmpty
Just fs
| null fs -> PrettyEmpty
| otherwise ->
field "extra-doc-files" formatExtraSourceFiles (toList fs)
["Extra doc files to be distributed with the package, such as a CHANGELOG or a README."]
True
opts

, case _pkgExtraSrcFiles pkgDesc of
[] -> PrettyEmpty
lst -> field "extra-source-files" formatExtraSourceFiles lst
["Extra source files to be distributed with the package, such as examples, or a tutorial module."]
True
opts
fs
| null fs -> PrettyEmpty
| otherwise ->
field "extra-source-files" formatExtraSourceFiles (toList fs)
["Extra source files to be distributed with the package, such as examples, or a tutorial module."]
True
opts
]
where
cabalSpec = _pkgCabalVersion pkgDesc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ module Distribution.Client.Init.Interactive.Command
import Prelude ()
import Distribution.Client.Compat.Prelude hiding (putStr, putStrLn, getLine, last)


import Distribution.CabalSpecVersion (CabalSpecVersion(..), showCabalSpecVersion)
import Distribution.Version (Version)
import Distribution.Types.Dependency (Dependency(..))
Expand Down Expand Up @@ -95,7 +94,7 @@ createProject v pkgIx srcDb initFlags = do
isMinimal <- getMinimal initFlags
doOverwrite <- getOverwrite initFlags
pkgDir <- getPackageDir initFlags
pkgDesc <- genPkgDescription initFlags srcDb
pkgDesc <- fixupDocFiles <$> genPkgDescription initFlags srcDb

let pkgName = _pkgName pkgDesc
mkOpts cs = WriteOpts
Expand Down Expand Up @@ -262,7 +261,7 @@ cabalVersionPrompt flags = getCabalVersion flags $ do
ppVersions = displayCabalVersion <$> defaultCabalVersions

parseCabalVersion :: String -> CabalSpecVersion
parseCabalVersion "1.10" = CabalSpecV1_10
parseCabalVersion "1.24" = CabalSpecV1_24
parseCabalVersion "2.0" = CabalSpecV2_0
parseCabalVersion "2.2" = CabalSpecV2_2
parseCabalVersion "2.4" = CabalSpecV2_4
Expand All @@ -272,7 +271,7 @@ cabalVersionPrompt flags = getCabalVersion flags $ do

displayCabalVersion :: CabalSpecVersion -> String
displayCabalVersion v = case v of
CabalSpecV1_10 -> "1.10 (legacy)"
CabalSpecV1_24 -> "1.24 (legacy)"
CabalSpecV2_0 -> "2.0 (+ support for Backpack, internal sub-libs, '^>=' operator)"
CabalSpecV2_2 -> "2.2 (+ support for 'common', 'elif', redundant commas, SPDX)"
CabalSpecV2_4 -> "2.4 (+ support for '**' globbing)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import Language.Haskell.Extension (Language(..), Extension(..))

import System.FilePath (splitDirectories, (</>))
import Distribution.Simple.Compiler
import qualified Data.Set as Set


-- | Main driver for interactive prompt code.
Expand Down Expand Up @@ -101,7 +102,7 @@ createProject comp v pkgIx srcDb initFlags = do
isMinimal <- getMinimal initFlags
doOverwrite <- getOverwrite initFlags
pkgDir <- packageDirHeuristics initFlags
pkgDesc <- genPkgDescription initFlags srcDb
pkgDesc <- fixupDocFiles <$> genPkgDescription initFlags srcDb
comments <- noCommentsHeuristics initFlags

let pkgName = _pkgName pkgDesc
Expand Down Expand Up @@ -282,9 +283,9 @@ categoryHeuristics :: Interactive m => InitFlags -> m String
categoryHeuristics flags = getCategory flags $ return ""

-- | Try to guess extra source files.
extraDocFileHeuristics :: Interactive m => InitFlags -> m (NonEmpty FilePath)
extraDocFileHeuristics :: Interactive m => InitFlags -> m (Maybe (Set FilePath))
extraDocFileHeuristics flags = case extraDoc flags of
Flag x | not (null x) -> return $ NEL.fromList x
Flag x -> return $ Just $ Set.fromList x
_ -> guessExtraDocFiles flags

-- | Try to guess if the project builds a library, an executable, or both.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import Distribution.Simple.Setup (fromFlagOrDefault)

import Text.Parsec
import qualified Data.List as L
import qualified Data.List.NonEmpty as NEL
import Distribution.Client.Init.Defaults
import Distribution.Client.Init.Types hiding (break)
import Distribution.Client.Init.Utils
Expand All @@ -45,6 +44,7 @@ import Language.Haskell.Extension
import Distribution.Version
import Distribution.Types.PackageName (PackageName, mkPackageName)
import Distribution.Simple.Compiler
import qualified Data.Set as Set



Expand Down Expand Up @@ -107,17 +107,17 @@ guessPackageName = fmap (mkPackageName . repair . fromMaybe "" . safeLast . spli
guessLicense :: Interactive m => InitFlags -> m SPDX.License
guessLicense _ = return SPDX.NONE

guessExtraDocFiles :: Interactive m => InitFlags -> m (NonEmpty FilePath)
guessExtraDocFiles :: Interactive m => InitFlags -> m (Maybe (Set FilePath))
guessExtraDocFiles flags = do
pkgDir <- fromFlagOrDefault getCurrentDirectory $ fmap return $ packageDir flags
files <- getDirectoryContents pkgDir

let extraDocCandidates = ["CHANGES", "CHANGELOG", "README"]
extraDocs = [y | x <- extraDocCandidates, y <- files, x == map toUpper (takeBaseName y)]

return $ if null extraDocs
then defaultChangelog NEL.:| []
else NEL.fromList extraDocs
return $ Just $ if null extraDocs
then Set.singleton defaultChangelog
else Set.fromList extraDocs

-- | Try to guess the package type from the files in the package directory,
-- looking for unique characteristics from each type, defaults to Executable.
Expand Down
7 changes: 4 additions & 3 deletions cabal-install/src/Distribution/Client/Init/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Distribution.Client.Init.Utils (currentDirPkgName, mkPackageNameDep)
import Distribution.Client.Init.Defaults
import Distribution.Simple.Flag (fromFlagOrDefault, flagElim)
import Distribution.Client.Init.FlagExtractors
import qualified Data.Set as Set


createProject
Expand Down Expand Up @@ -77,10 +78,10 @@ createProject v _pkgIx _srcDb initFlags = do
genSimplePkgDesc :: Interactive m => InitFlags -> m PkgDescription
genSimplePkgDesc flags = mkPkgDesc <$> currentDirPkgName
where
defaultExtraDoc = defaultChangelog NEL.:| []
defaultExtraDoc = Just $ Set.singleton defaultChangelog

extractExtraDoc [] = defaultExtraDoc
extractExtraDoc as = NEL.fromList as
extractExtraDoc fs = Just $ Set.fromList fs

mkPkgDesc pkgName = PkgDescription
(fromFlagOrDefault defaultCabalVersion (cabalVersion flags))
Expand All @@ -92,7 +93,7 @@ genSimplePkgDesc flags = mkPkgDesc <$> currentDirPkgName
(fromFlagOrDefault "" (homepage flags))
(fromFlagOrDefault "" (synopsis flags))
(fromFlagOrDefault "" (category flags))
(fromFlagOrDefault [] (extraSrc flags))
(flagElim mempty Set.fromList (extraSrc flags))
(flagElim defaultExtraDoc extractExtraDoc (extraDoc flags))

genSimpleLibTarget :: Interactive m => InitFlags -> m LibTarget
Expand Down
4 changes: 2 additions & 2 deletions cabal-install/src/Distribution/Client/Init/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ data PkgDescription = PkgDescription
, _pkgHomePage :: String
, _pkgSynopsis :: String
, _pkgCategory :: String
, _pkgExtraSrcFiles :: [String]
, _pkgExtraDocFiles :: NonEmpty String
, _pkgExtraSrcFiles :: Set String
, _pkgExtraDocFiles :: Maybe (Set String)
} deriving (Show, Eq)

-- | 'LibTarget' represents the relevant options set by the
Expand Down
14 changes: 14 additions & 0 deletions cabal-install/src/Distribution/Client/Init/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Distribution.Client.Init.Utils
, currentDirPkgName
, filePathToPkgName
, mkPackageNameDep
, fixupDocFiles
) where


Expand Down Expand Up @@ -263,3 +264,16 @@ currentDirPkgName = filePathToPkgName <$> getCurrentDirectory

mkPackageNameDep :: PackageName -> Dependency
mkPackageNameDep pkg = mkDependency pkg anyVersion (NES.singleton LMainLibName)

-- when cabal-version < 1.18, extra-doc-files is not supported
-- so whatever the user wants as doc files should be dumped into
-- extra-src-files.
--
fixupDocFiles :: PkgDescription -> PkgDescription
fixupDocFiles pkgDesc
| _pkgCabalVersion pkgDesc < CabalSpecV1_18 = pkgDesc
{ _pkgExtraSrcFiles =_pkgExtraSrcFiles pkgDesc
<> fromMaybe mempty (_pkgExtraDocFiles pkgDesc)
, _pkgExtraDocFiles = Nothing
}
| otherwise = pkgDesc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import UnitTests.Distribution.Client.Init.Utils
import Distribution.Client.Init.FlagExtractors
import Distribution.Simple.Setup
import Distribution.CabalSpecVersion
import qualified Data.Set as Set


-- -------------------------------------------------------------------- --
Expand Down Expand Up @@ -89,8 +90,8 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
_pkgHomePage desc @?= "qux.com"
_pkgSynopsis desc @?= "We are Qux, and this is our package"
_pkgCategory desc @?= "Control"
_pkgExtraSrcFiles desc @?= []
_pkgExtraDocFiles desc @?= "CHANGELOG.md" :| []
_pkgExtraSrcFiles desc @?= mempty
_pkgExtraDocFiles desc @?= pure (Set.singleton "CHANGELOG.md")

_libSourceDirs lib @?= ["quxSrc"]
_libLanguage lib @?= Haskell98
Expand Down Expand Up @@ -192,8 +193,8 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
_pkgHomePage desc @?= "qux.com"
_pkgSynopsis desc @?= "Qux's package"
_pkgCategory desc @?= "Control"
_pkgExtraSrcFiles desc @?= []
_pkgExtraDocFiles desc @?= "CHANGELOG.md" :| []
_pkgExtraSrcFiles desc @?= mempty
_pkgExtraDocFiles desc @?= pure (Set.singleton "CHANGELOG.md")

_libSourceDirs lib @?= ["src"]
_libLanguage lib @?= Haskell98
Expand Down Expand Up @@ -286,8 +287,8 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
_pkgHomePage desc @?= "qux.com"
_pkgSynopsis desc @?= "Qux's package"
_pkgCategory desc @?= "Control"
_pkgExtraSrcFiles desc @?= []
_pkgExtraDocFiles desc @?= "CHANGELOG.md" :| []
_pkgExtraSrcFiles desc @?= mempty
_pkgExtraDocFiles desc @?= pure (Set.singleton "CHANGELOG.md")

_libSourceDirs lib @?= ["src"]
_libLanguage lib @?= Haskell98
Expand Down Expand Up @@ -375,8 +376,8 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
_pkgHomePage desc @?= "qux.com"
_pkgSynopsis desc @?= "Qux's package"
_pkgCategory desc @?= "Control"
_pkgExtraSrcFiles desc @?= []
_pkgExtraDocFiles desc @?= "CHANGELOG.md" :| []
_pkgExtraSrcFiles desc @?= mempty
_pkgExtraDocFiles desc @?= pure (Set.singleton "CHANGELOG.md")

_libSourceDirs lib @?= ["src"]
_libLanguage lib @?= Haskell98
Expand Down Expand Up @@ -456,8 +457,8 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
_pkgHomePage desc @?= "qux.com"
_pkgSynopsis desc @?= "Qux's package"
_pkgCategory desc @?= "Control"
_pkgExtraSrcFiles desc @?= []
_pkgExtraDocFiles desc @?= "CHANGELOG.md" :| []
_pkgExtraSrcFiles desc @?= mempty
_pkgExtraDocFiles desc @?= pure (Set.singleton "CHANGELOG.md")

_libSourceDirs lib @?= ["src"]
_libLanguage lib @?= Haskell98
Expand Down Expand Up @@ -529,8 +530,8 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
_pkgHomePage desc @?= "qux.com"
_pkgSynopsis desc @?= "Qux's package"
_pkgCategory desc @?= "Control"
_pkgExtraSrcFiles desc @?= []
_pkgExtraDocFiles desc @?= "CHANGELOG.md" :| []
_pkgExtraSrcFiles desc @?= mempty
_pkgExtraDocFiles desc @?= pure (Set.singleton "CHANGELOG.md")

_exeMainIs exe @?= HsFilePath "Main.hs" Standard
_exeApplicationDirs exe @?= ["exe"]
Expand Down
Loading

0 comments on commit e398baa

Please sign in to comment.