{-# LANGUAGE LambdaCase #-}

module Distribution.Client.Init.NonInteractive.Command
  ( genPkgDescription
  , genLibTarget
  , genExeTarget
  , genTestTarget
  , createProject
  , packageTypeHeuristics
  , authorHeuristics
  , emailHeuristics
  , cabalVersionHeuristics
  , packageNameHeuristics
  , versionHeuristics
  , mainFileHeuristics
  , testDirsHeuristics
  , initializeTestSuiteHeuristics
  , exposedModulesHeuristics
  , libOtherModulesHeuristics
  , exeOtherModulesHeuristics
  , testOtherModulesHeuristics
  , buildToolsHeuristics
  , dependenciesHeuristics
  , otherExtsHeuristics
  , licenseHeuristics
  , homepageHeuristics
  , synopsisHeuristics
  , categoryHeuristics
  , extraDocFileHeuristics
  , appDirsHeuristics
  , srcDirsHeuristics
  , languageHeuristics
  , noCommentsHeuristics
  , minimalHeuristics
  , overwriteHeuristics
  ) where

import Distribution.Client.Init.Types

import Distribution.Client.Compat.Prelude hiding (getLine, head, last, putStr, putStrLn)
import Prelude ()

import Data.List (last)
import qualified Data.List.NonEmpty as NEL

import Distribution.CabalSpecVersion (CabalSpecVersion (..))
import Distribution.Client.Init.Defaults
import Distribution.Client.Init.FlagExtractors
import Distribution.Client.Init.NonInteractive.Heuristics
import Distribution.Client.Init.Utils
import Distribution.Client.Types (SourcePackageDb (..))
import Distribution.ModuleName (ModuleName, components)
import Distribution.Simple.PackageIndex (InstalledPackageIndex)
import Distribution.Simple.Setup (Flag (..), fromFlagOrDefault)
import Distribution.Solver.Types.PackageIndex (elemByPackageName)
import Distribution.Types.Dependency (Dependency (..))
import Distribution.Types.PackageName (PackageName, unPackageName)
import Distribution.Utils.Generic (safeHead)
import Distribution.Verbosity
import Distribution.Version (Version)

import Language.Haskell.Extension (Extension (..), Language (..))

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

-- | Main driver for interactive prompt code.
createProject
  :: Interactive m
  => Compiler
  -> Verbosity
  -> InstalledPackageIndex
  -> SourcePackageDb
  -> InitFlags
  -> m ProjectSettings
createProject :: forall (m :: * -> *).
Interactive m =>
Compiler
-> Verbosity
-> InstalledPackageIndex
-> SourcePackageDb
-> InitFlags
-> m ProjectSettings
createProject Compiler
comp Verbosity
v InstalledPackageIndex
pkgIx SourcePackageDb
srcDb InitFlags
initFlags = do
  -- The workflow is as follows:
  --
  --  1. Get the package type, supplied as either a program input or
  --     via user prompt. This determines what targets will be built
  --     in later steps.
  --
  --  2. Determine whether we generate simple targets or prompt the
  --     user for inputs when not supplied as a flag. In general,
  --     flag inputs are preferred, and "simple" here means
  --     reasonable defaults defined in @Defaults.hs@.
  --
  --  3. Generate package description and the targets specified by
  --     the package type. Once this is done, a prompt for building
  --     test suites is initiated, and this determines if we build
  --     test targets as well. Then we ask if the user wants to
  --     comment their .cabal file with pretty comments.
  --
  --  4. The targets are passed to the file creator script, and associated
  --     directories/files/modules are created, with the a .cabal file
  --     being generated as a final result.
  --

  pkgType <- InitFlags -> m PackageType
forall (m :: * -> *). Interactive m => InitFlags -> m PackageType
packageTypeHeuristics InitFlags
initFlags
  isMinimal <- getMinimal initFlags
  doOverwrite <- getOverwrite initFlags
  pkgDir <- packageDirHeuristics initFlags
  pkgDesc <- fixupDocFiles v =<< genPkgDescription initFlags srcDb
  comments <- noCommentsHeuristics initFlags

  let pkgName = PkgDescription -> PackageName
_pkgName PkgDescription
pkgDesc
      cabalSpec = PkgDescription -> CabalSpecVersion
_pkgCabalVersion PkgDescription
pkgDesc
      mkOpts Bool
cs =
        Bool
-> Bool
-> Bool
-> Verbosity
-> FilePath
-> PackageType
-> PackageName
-> CabalSpecVersion
-> WriteOpts
WriteOpts
          Bool
doOverwrite
          Bool
isMinimal
          Bool
cs
          Verbosity
v
          FilePath
pkgDir
          PackageType
pkgType
          PackageName
pkgName

  case pkgType of
    PackageType
Library -> do
      libTarget <- InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m LibTarget
forall (m :: * -> *).
Interactive m =>
InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m LibTarget
genLibTarget InitFlags
initFlags Compiler
comp InstalledPackageIndex
pkgIx CabalSpecVersion
cabalSpec
      testTarget <-
        addLibDepToTest pkgName
          <$> genTestTarget initFlags comp pkgIx cabalSpec

      return $
        ProjectSettings
          (mkOpts comments cabalSpec)
          pkgDesc
          (Just libTarget)
          Nothing
          testTarget
    PackageType
Executable -> do
      exeTarget <- InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m ExeTarget
forall (m :: * -> *).
Interactive m =>
InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m ExeTarget
genExeTarget InitFlags
initFlags Compiler
comp InstalledPackageIndex
pkgIx CabalSpecVersion
cabalSpec

      return $
        ProjectSettings
          (mkOpts comments cabalSpec)
          pkgDesc
          Nothing
          (Just exeTarget)
          Nothing
    PackageType
LibraryAndExecutable -> do
      libTarget <- InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m LibTarget
forall (m :: * -> *).
Interactive m =>
InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m LibTarget
genLibTarget InitFlags
initFlags Compiler
comp InstalledPackageIndex
pkgIx CabalSpecVersion
cabalSpec
      exeTarget <-
        addLibDepToExe pkgName
          <$> genExeTarget initFlags comp pkgIx cabalSpec
      testTarget <-
        addLibDepToTest pkgName
          <$> genTestTarget initFlags comp pkgIx cabalSpec

      return $
        ProjectSettings
          (mkOpts comments cabalSpec)
          pkgDesc
          (Just libTarget)
          (Just exeTarget)
          testTarget
    PackageType
TestSuite -> do
      testTarget <- InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m (Maybe TestTarget)
forall (m :: * -> *).
Interactive m =>
InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m (Maybe TestTarget)
genTestTarget InitFlags
initFlags Compiler
comp InstalledPackageIndex
pkgIx CabalSpecVersion
cabalSpec

      return $
        ProjectSettings
          (mkOpts comments cabalSpec)
          pkgDesc
          Nothing
          Nothing
          testTarget

genPkgDescription
  :: Interactive m
  => InitFlags
  -> SourcePackageDb
  -> m PkgDescription
genPkgDescription :: forall (m :: * -> *).
Interactive m =>
InitFlags -> SourcePackageDb -> m PkgDescription
genPkgDescription InitFlags
flags SourcePackageDb
srcDb =
  CabalSpecVersion
-> PackageName
-> Version
-> SpecLicense
-> FilePath
-> FilePath
-> FilePath
-> FilePath
-> FilePath
-> Set FilePath
-> Maybe (Set FilePath)
-> PkgDescription
PkgDescription
    (CabalSpecVersion
 -> PackageName
 -> Version
 -> SpecLicense
 -> FilePath
 -> FilePath
 -> FilePath
 -> FilePath
 -> FilePath
 -> Set FilePath
 -> Maybe (Set FilePath)
 -> PkgDescription)
-> m CabalSpecVersion
-> m (PackageName
      -> Version
      -> SpecLicense
      -> FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> Set FilePath
      -> Maybe (Set FilePath)
      -> PkgDescription)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InitFlags -> m CabalSpecVersion
forall (m :: * -> *).
Interactive m =>
InitFlags -> m CabalSpecVersion
cabalVersionHeuristics InitFlags
flags
    m (PackageName
   -> Version
   -> SpecLicense
   -> FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> Set FilePath
   -> Maybe (Set FilePath)
   -> PkgDescription)
-> m PackageName
-> m (Version
      -> SpecLicense
      -> FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> Set FilePath
      -> Maybe (Set FilePath)
      -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SourcePackageDb -> InitFlags -> m PackageName
forall (m :: * -> *).
Interactive m =>
SourcePackageDb -> InitFlags -> m PackageName
packageNameHeuristics SourcePackageDb
srcDb InitFlags
flags
    m (Version
   -> SpecLicense
   -> FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> Set FilePath
   -> Maybe (Set FilePath)
   -> PkgDescription)
-> m Version
-> m (SpecLicense
      -> FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> Set FilePath
      -> Maybe (Set FilePath)
      -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m Version
forall (m :: * -> *). Interactive m => InitFlags -> m Version
versionHeuristics InitFlags
flags
    m (SpecLicense
   -> FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> Set FilePath
   -> Maybe (Set FilePath)
   -> PkgDescription)
-> m SpecLicense
-> m (FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> Set FilePath
      -> Maybe (Set FilePath)
      -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m SpecLicense
forall (m :: * -> *). Interactive m => InitFlags -> m SpecLicense
licenseHeuristics InitFlags
flags
    m (FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> Set FilePath
   -> Maybe (Set FilePath)
   -> PkgDescription)
-> m FilePath
-> m (FilePath
      -> FilePath
      -> FilePath
      -> FilePath
      -> Set FilePath
      -> Maybe (Set FilePath)
      -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m FilePath
forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
authorHeuristics InitFlags
flags
    m (FilePath
   -> FilePath
   -> FilePath
   -> FilePath
   -> Set FilePath
   -> Maybe (Set FilePath)
   -> PkgDescription)
-> m FilePath
-> m (FilePath
      -> FilePath
      -> FilePath
      -> Set FilePath
      -> Maybe (Set FilePath)
      -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m FilePath
forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
emailHeuristics InitFlags
flags
    m (FilePath
   -> FilePath
   -> FilePath
   -> Set FilePath
   -> Maybe (Set FilePath)
   -> PkgDescription)
-> m FilePath
-> m (FilePath
      -> FilePath
      -> Set FilePath
      -> Maybe (Set FilePath)
      -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m FilePath
forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
homepageHeuristics InitFlags
flags
    m (FilePath
   -> FilePath
   -> Set FilePath
   -> Maybe (Set FilePath)
   -> PkgDescription)
-> m FilePath
-> m (FilePath
      -> Set FilePath -> Maybe (Set FilePath) -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m FilePath
forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
synopsisHeuristics InitFlags
flags
    m (FilePath
   -> Set FilePath -> Maybe (Set FilePath) -> PkgDescription)
-> m FilePath
-> m (Set FilePath -> Maybe (Set FilePath) -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m FilePath
forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
categoryHeuristics InitFlags
flags
    m (Set FilePath -> Maybe (Set FilePath) -> PkgDescription)
-> m (Set FilePath) -> m (Maybe (Set FilePath) -> PkgDescription)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m (Set FilePath)
forall (m :: * -> *).
Interactive m =>
InitFlags -> m (Set FilePath)
getExtraSrcFiles InitFlags
flags
    m (Maybe (Set FilePath) -> PkgDescription)
-> m (Maybe (Set FilePath)) -> m PkgDescription
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InitFlags -> m (Maybe (Set FilePath))
forall (m :: * -> *).
Interactive m =>
InitFlags -> m (Maybe (Set FilePath))
extraDocFileHeuristics InitFlags
flags

genLibTarget
  :: Interactive m
  => InitFlags
  -> Compiler
  -> InstalledPackageIndex
  -> CabalSpecVersion
  -> m LibTarget
genLibTarget :: forall (m :: * -> *).
Interactive m =>
InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m LibTarget
genLibTarget InitFlags
flags Compiler
comp InstalledPackageIndex
pkgs CabalSpecVersion
v = do
  srcDirs <- InitFlags -> m [FilePath]
forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
srcDirsHeuristics InitFlags
flags
  let srcDir = FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
defaultSourceDir (Maybe FilePath -> FilePath) -> Maybe FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ [FilePath] -> Maybe FilePath
forall a. [a] -> Maybe a
safeHead [FilePath]
srcDirs
  LibTarget srcDirs
    <$> languageHeuristics flags comp
    <*> exposedModulesHeuristics flags
    <*> libOtherModulesHeuristics flags
    <*> otherExtsHeuristics flags srcDir
    <*> dependenciesHeuristics flags srcDir pkgs
    <*> buildToolsHeuristics flags srcDir v

genExeTarget
  :: Interactive m
  => InitFlags
  -> Compiler
  -> InstalledPackageIndex
  -> CabalSpecVersion
  -> m ExeTarget
genExeTarget :: forall (m :: * -> *).
Interactive m =>
InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m ExeTarget
genExeTarget InitFlags
flags Compiler
comp InstalledPackageIndex
pkgs CabalSpecVersion
v = do
  appDirs <- InitFlags -> m [FilePath]
forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
appDirsHeuristics InitFlags
flags
  let appDir = FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
defaultApplicationDir (Maybe FilePath -> FilePath) -> Maybe FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ [FilePath] -> Maybe FilePath
forall a. [a] -> Maybe a
safeHead [FilePath]
appDirs
  ExeTarget
    <$> mainFileHeuristics flags
    <*> pure appDirs
    <*> languageHeuristics flags comp
    <*> exeOtherModulesHeuristics flags
    <*> otherExtsHeuristics flags appDir
    <*> dependenciesHeuristics flags appDir pkgs
    <*> buildToolsHeuristics flags appDir v

genTestTarget
  :: Interactive m
  => InitFlags
  -> Compiler
  -> InstalledPackageIndex
  -> CabalSpecVersion
  -> m (Maybe TestTarget)
genTestTarget :: forall (m :: * -> *).
Interactive m =>
InitFlags
-> Compiler
-> InstalledPackageIndex
-> CabalSpecVersion
-> m (Maybe TestTarget)
genTestTarget InitFlags
flags Compiler
comp InstalledPackageIndex
pkgs CabalSpecVersion
v = do
  initialized <- InitFlags -> m Bool
forall (m :: * -> *). Interactive m => InitFlags -> m Bool
initializeTestSuiteHeuristics InitFlags
flags
  testDirs' <- testDirsHeuristics flags
  let testDir = FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
defaultTestDir (Maybe FilePath -> FilePath) -> Maybe FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ [FilePath] -> Maybe FilePath
forall a. [a] -> Maybe a
safeHead [FilePath]
testDirs'
  if not initialized
    then return Nothing
    else
      fmap Just $
        TestTarget
          <$> testMainHeuristics flags
          <*> pure testDirs'
          <*> languageHeuristics flags comp
          <*> testOtherModulesHeuristics flags
          <*> otherExtsHeuristics flags testDir
          <*> dependenciesHeuristics flags testDir pkgs
          <*> buildToolsHeuristics flags testDir v

-- -------------------------------------------------------------------- --
-- Get flags from init config

minimalHeuristics :: Interactive m => InitFlags -> m Bool
minimalHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m Bool
minimalHeuristics = InitFlags -> m Bool
forall (m :: * -> *). Interactive m => InitFlags -> m Bool
getMinimal

overwriteHeuristics :: Interactive m => InitFlags -> m Bool
overwriteHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m Bool
overwriteHeuristics = InitFlags -> m Bool
forall (m :: * -> *). Interactive m => InitFlags -> m Bool
getOverwrite

packageDirHeuristics :: Interactive m => InitFlags -> m FilePath
packageDirHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
packageDirHeuristics = InitFlags -> m FilePath
forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
getPackageDir

-- | Get the version of the cabal spec to use.
--   The spec version can be specified by the InitFlags cabalVersion field. If
--   none is specified then the default version is used.
cabalVersionHeuristics :: Interactive m => InitFlags -> m CabalSpecVersion
cabalVersionHeuristics :: forall (m :: * -> *).
Interactive m =>
InitFlags -> m CabalSpecVersion
cabalVersionHeuristics InitFlags
flags = InitFlags -> m CabalSpecVersion -> m CabalSpecVersion
forall (m :: * -> *).
Interactive m =>
InitFlags -> m CabalSpecVersion -> m CabalSpecVersion
getCabalVersion InitFlags
flags m CabalSpecVersion
forall (m :: * -> *). Interactive m => m CabalSpecVersion
guessCabalSpecVersion

-- | Get the package name: use the package directory (supplied, or the current
--   directory by default) as a guess. It looks at the SourcePackageDb to avoid
--   using an existing package name.
packageNameHeuristics :: Interactive m => SourcePackageDb -> InitFlags -> m PackageName
packageNameHeuristics :: forall (m :: * -> *).
Interactive m =>
SourcePackageDb -> InitFlags -> m PackageName
packageNameHeuristics SourcePackageDb
sourcePkgDb InitFlags
flags = InitFlags -> m PackageName -> m PackageName
forall (m :: * -> *).
Interactive m =>
InitFlags -> m PackageName -> m PackageName
getPackageName InitFlags
flags (m PackageName -> m PackageName) -> m PackageName -> m PackageName
forall a b. (a -> b) -> a -> b
$ do
  defName <-
    FilePath -> m PackageName
forall (m :: * -> *). Interactive m => FilePath -> m PackageName
guessPackageName (FilePath -> m PackageName) -> m FilePath -> m PackageName
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< case InitFlags -> Flag FilePath
packageDir InitFlags
flags of
      Flag FilePath
a -> FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
a
      Flag FilePath
NoFlag -> [FilePath] -> FilePath
forall a. HasCallStack => [a] -> a
last ([FilePath] -> FilePath)
-> (FilePath -> [FilePath]) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
splitDirectories (FilePath -> FilePath) -> m FilePath -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m FilePath
forall (m :: * -> *). Interactive m => m FilePath
getCurrentDirectory

  when (isPkgRegistered defName) $
    putStrLn (inUseMsg defName)

  return defName
  where
    isPkgRegistered :: PackageName -> Bool
isPkgRegistered = PackageIndex UnresolvedSourcePackage -> PackageName -> Bool
forall pkg. Package pkg => PackageIndex pkg -> PackageName -> Bool
elemByPackageName (SourcePackageDb -> PackageIndex UnresolvedSourcePackage
packageIndex SourcePackageDb
sourcePkgDb)

    inUseMsg :: PackageName -> FilePath
inUseMsg PackageName
pn =
      FilePath
"The name "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ PackageName -> FilePath
unPackageName PackageName
pn
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" is already in use by another package on Hackage."

-- | Package version: use 0.1.0.0 as a last resort
versionHeuristics :: Interactive m => InitFlags -> m Version
versionHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m Version
versionHeuristics InitFlags
flags = InitFlags -> m Version -> m Version
forall (m :: * -> *).
Interactive m =>
InitFlags -> m Version -> m Version
getVersion InitFlags
flags (m Version -> m Version) -> m Version -> m Version
forall a b. (a -> b) -> a -> b
$ Version -> m Version
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Version
defaultVersion

-- | Choose a license for the package.
-- The license can come from Initflags (license field), if it is not present
-- then prompt the user from a predefined list of licenses.
licenseHeuristics :: Interactive m => InitFlags -> m SpecLicense
licenseHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m SpecLicense
licenseHeuristics InitFlags
flags = InitFlags -> m SpecLicense -> m SpecLicense
forall (m :: * -> *).
Interactive m =>
InitFlags -> m SpecLicense -> m SpecLicense
getLicense InitFlags
flags (m SpecLicense -> m SpecLicense) -> m SpecLicense -> m SpecLicense
forall a b. (a -> b) -> a -> b
$ InitFlags -> m SpecLicense
forall (m :: * -> *). Interactive m => InitFlags -> m SpecLicense
guessLicense InitFlags
flags

-- | The author's name. Prompt, or try to guess from an existing
--   git repo.
authorHeuristics :: Interactive m => InitFlags -> m String
authorHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
authorHeuristics InitFlags
flags =
  m (Maybe FilePath)
forall (m :: * -> *). Interactive m => m (Maybe FilePath)
guessAuthorName
    m (Maybe FilePath) -> (Maybe FilePath -> m FilePath) -> m FilePath
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m FilePath
-> (FilePath -> m FilePath) -> Maybe FilePath -> m FilePath
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (InitFlags -> m FilePath -> m FilePath
forall (m :: * -> *).
Interactive m =>
InitFlags -> m FilePath -> m FilePath
getAuthor InitFlags
flags (m FilePath -> m FilePath) -> m FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
"Unknown") (InitFlags -> m FilePath -> m FilePath
forall (m :: * -> *).
Interactive m =>
InitFlags -> m FilePath -> m FilePath
getAuthor InitFlags
flags (m FilePath -> m FilePath)
-> (FilePath -> m FilePath) -> FilePath -> m FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return)

-- | The author's email. Prompt, or try to guess from an existing
--   git repo.
emailHeuristics :: Interactive m => InitFlags -> m String
emailHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
emailHeuristics InitFlags
flags =
  m (Maybe FilePath)
forall (m :: * -> *). Interactive m => m (Maybe FilePath)
guessAuthorEmail
    m (Maybe FilePath) -> (Maybe FilePath -> m FilePath) -> m FilePath
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m FilePath
-> (FilePath -> m FilePath) -> Maybe FilePath -> m FilePath
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (InitFlags -> m FilePath -> m FilePath
forall (m :: * -> *).
Interactive m =>
InitFlags -> m FilePath -> m FilePath
getEmail InitFlags
flags (m FilePath -> m FilePath) -> m FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
"Unknown") (InitFlags -> m FilePath -> m FilePath
forall (m :: * -> *).
Interactive m =>
InitFlags -> m FilePath -> m FilePath
getEmail InitFlags
flags (m FilePath -> m FilePath)
-> (FilePath -> m FilePath) -> FilePath -> m FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return)

-- | Prompt for a homepage URL for the package.
homepageHeuristics :: Interactive m => InitFlags -> m String
homepageHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
homepageHeuristics InitFlags
flags = InitFlags -> m FilePath -> m FilePath
forall (m :: * -> *).
Interactive m =>
InitFlags -> m FilePath -> m FilePath
getHomepage InitFlags
flags (m FilePath -> m FilePath) -> m FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
""

-- | Prompt for a project synopsis.
synopsisHeuristics :: Interactive m => InitFlags -> m String
synopsisHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
synopsisHeuristics InitFlags
flags = InitFlags -> m FilePath -> m FilePath
forall (m :: * -> *).
Interactive m =>
InitFlags -> m FilePath -> m FilePath
getSynopsis InitFlags
flags (m FilePath -> m FilePath) -> m FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
""

-- | Prompt for a package category.
--   Note that it should be possible to do some smarter guessing here too, i.e.
--   look at the name of the top level source directory.
categoryHeuristics :: Interactive m => InitFlags -> m String
categoryHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m FilePath
categoryHeuristics InitFlags
flags = InitFlags -> m FilePath -> m FilePath
forall (m :: * -> *).
Interactive m =>
InitFlags -> m FilePath -> m FilePath
getCategory InitFlags
flags (m FilePath -> m FilePath) -> m FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
""

-- | Try to guess extra source files.
extraDocFileHeuristics :: Interactive m => InitFlags -> m (Maybe (Set FilePath))
extraDocFileHeuristics :: forall (m :: * -> *).
Interactive m =>
InitFlags -> m (Maybe (Set FilePath))
extraDocFileHeuristics InitFlags
flags = case InitFlags -> Flag [FilePath]
extraDoc InitFlags
flags of
  Flag [FilePath]
x -> Maybe (Set FilePath) -> m (Maybe (Set FilePath))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Set FilePath) -> m (Maybe (Set FilePath)))
-> Maybe (Set FilePath) -> m (Maybe (Set FilePath))
forall a b. (a -> b) -> a -> b
$ Set FilePath -> Maybe (Set FilePath)
forall a. a -> Maybe a
Just (Set FilePath -> Maybe (Set FilePath))
-> Set FilePath -> Maybe (Set FilePath)
forall a b. (a -> b) -> a -> b
$ [FilePath] -> Set FilePath
forall a. Ord a => [a] -> Set a
Set.fromList [FilePath]
x
  Flag [FilePath]
_ -> InitFlags -> m (Maybe (Set FilePath))
forall (m :: * -> *).
Interactive m =>
InitFlags -> m (Maybe (Set FilePath))
guessExtraDocFiles InitFlags
flags

-- | Try to guess if the project builds a library, an executable, or both.
packageTypeHeuristics :: Interactive m => InitFlags -> m PackageType
packageTypeHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m PackageType
packageTypeHeuristics InitFlags
flags = InitFlags -> m PackageType -> m PackageType
forall (m :: * -> *).
Interactive m =>
InitFlags -> m PackageType -> m PackageType
getPackageType InitFlags
flags (m PackageType -> m PackageType) -> m PackageType -> m PackageType
forall a b. (a -> b) -> a -> b
$ InitFlags -> m PackageType
forall (m :: * -> *). Interactive m => InitFlags -> m PackageType
guessPackageType InitFlags
flags

-- | Try to guess the main file, if nothing is found, fallback
--   to a default value.
mainFileHeuristics :: Interactive m => InitFlags -> m HsFilePath
mainFileHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m HsFilePath
mainFileHeuristics InitFlags
flags = do
  appDirs <- InitFlags -> m [FilePath]
forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
appDirsHeuristics InitFlags
flags
  let appDir = case [FilePath]
appDirs of
        [] -> FilePath -> FilePath
forall a. HasCallStack => FilePath -> a
error FilePath
"impossible: appDirsHeuristics returned empty list of dirs"
        (FilePath
appDir' : [FilePath]
_) -> FilePath
appDir'
  getMainFile flags . guessMainFile $ appDir

testMainHeuristics :: Interactive m => InitFlags -> m HsFilePath
testMainHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m HsFilePath
testMainHeuristics InitFlags
flags = do
  testDirs' <- InitFlags -> m [FilePath]
forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
testDirsHeuristics InitFlags
flags
  let testDir = case [FilePath]
testDirs' of
        [] -> FilePath -> FilePath
forall a. HasCallStack => FilePath -> a
error FilePath
"impossible: testDirsHeuristics returned empty list of dirs"
        (FilePath
testDir' : [FilePath]
_) -> FilePath
testDir'
  guessMainFile testDir

initializeTestSuiteHeuristics :: Interactive m => InitFlags -> m Bool
initializeTestSuiteHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m Bool
initializeTestSuiteHeuristics InitFlags
flags = InitFlags -> m Bool -> m Bool
forall (m :: * -> *).
Interactive m =>
InitFlags -> m Bool -> m Bool
getInitializeTestSuite InitFlags
flags (m Bool -> m Bool) -> m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

testDirsHeuristics :: Interactive m => InitFlags -> m [String]
testDirsHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
testDirsHeuristics InitFlags
flags = InitFlags -> m [FilePath] -> m [FilePath]
forall (m :: * -> *).
Interactive m =>
InitFlags -> m [FilePath] -> m [FilePath]
getTestDirs InitFlags
flags (m [FilePath] -> m [FilePath]) -> m [FilePath] -> m [FilePath]
forall a b. (a -> b) -> a -> b
$ [FilePath] -> m [FilePath]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
defaultTestDir]

-- | Ask for the Haskell base language of the package.
languageHeuristics :: Interactive m => InitFlags -> Compiler -> m Language
languageHeuristics :: forall (m :: * -> *).
Interactive m =>
InitFlags -> Compiler -> m Language
languageHeuristics InitFlags
flags Compiler
comp = InitFlags -> m Language -> m Language
forall (m :: * -> *).
Interactive m =>
InitFlags -> m Language -> m Language
getLanguage InitFlags
flags (m Language -> m Language) -> m Language -> m Language
forall a b. (a -> b) -> a -> b
$ Compiler -> m Language
forall (m :: * -> *). Interactive m => Compiler -> m Language
guessLanguage Compiler
comp

-- | Ask whether to generate explanatory comments.
noCommentsHeuristics :: Interactive m => InitFlags -> m Bool
noCommentsHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m Bool
noCommentsHeuristics InitFlags
flags = InitFlags -> m Bool -> m Bool
forall (m :: * -> *).
Interactive m =>
InitFlags -> m Bool -> m Bool
getNoComments InitFlags
flags (m Bool -> m Bool) -> m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

-- | Ask for the application root directory.
appDirsHeuristics :: Interactive m => InitFlags -> m [String]
appDirsHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
appDirsHeuristics InitFlags
flags = InitFlags -> m [FilePath] -> m [FilePath]
forall (m :: * -> *).
Interactive m =>
InitFlags -> m [FilePath] -> m [FilePath]
getAppDirs InitFlags
flags (m [FilePath] -> m [FilePath]) -> m [FilePath] -> m [FilePath]
forall a b. (a -> b) -> a -> b
$ InitFlags -> m [FilePath]
forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
guessApplicationDirectories InitFlags
flags

-- | Ask for the source (library) root directory.
srcDirsHeuristics :: Interactive m => InitFlags -> m [String]
srcDirsHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
srcDirsHeuristics InitFlags
flags = InitFlags -> m [FilePath] -> m [FilePath]
forall (m :: * -> *).
Interactive m =>
InitFlags -> m [FilePath] -> m [FilePath]
getSrcDirs InitFlags
flags (m [FilePath] -> m [FilePath]) -> m [FilePath] -> m [FilePath]
forall a b. (a -> b) -> a -> b
$ InitFlags -> m [FilePath]
forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
guessSourceDirectories InitFlags
flags

-- | Retrieve the list of exposed modules
exposedModulesHeuristics :: Interactive m => InitFlags -> m (NonEmpty ModuleName)
exposedModulesHeuristics :: forall (m :: * -> *).
Interactive m =>
InitFlags -> m (NonEmpty ModuleName)
exposedModulesHeuristics InitFlags
flags = do
  mods <- case InitFlags -> Flag [ModuleName]
exposedModules InitFlags
flags of
    Flag [ModuleName]
x -> [ModuleName] -> m [ModuleName]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [ModuleName]
x
    Flag [ModuleName]
NoFlag -> do
      srcDir <- FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
defaultSourceDir (Maybe FilePath -> FilePath)
-> ([FilePath] -> Maybe FilePath) -> [FilePath] -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> Maybe FilePath
forall a. [a] -> Maybe a
safeHead ([FilePath] -> FilePath) -> m [FilePath] -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InitFlags -> m [FilePath]
forall (m :: * -> *). Interactive m => InitFlags -> m [FilePath]
srcDirsHeuristics InitFlags
flags

      exists <- doesDirectoryExist srcDir

      if exists
        then do
          modules <- filter isHaskell <$> listFilesRecursive srcDir
          modulesNames <- catMaybes <$> traverse retrieveModuleName modules

          otherModules' <- libOtherModulesHeuristics flags
          return $ filter (`notElem` otherModules') modulesNames
        else return []

  return $
    if null mods
      then myLibModule NEL.:| []
      else NEL.fromList mods

-- | Retrieve the list of other modules for Libraries, filtering them
--   based on the last component of the module name
libOtherModulesHeuristics :: Interactive m => InitFlags -> m [ModuleName]
libOtherModulesHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m [ModuleName]
libOtherModulesHeuristics InitFlags
flags = case InitFlags -> Flag [ModuleName]
otherModules InitFlags
flags of
  Flag [ModuleName]
x -> [ModuleName] -> m [ModuleName]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [ModuleName]
x
  Flag [ModuleName]
NoFlag -> do
    let otherCandidates :: [FilePath]
otherCandidates = [FilePath
"Internal", FilePath
"Utils"]
        srcDir :: FilePath
srcDir = case InitFlags -> Flag [FilePath]
sourceDirs InitFlags
flags of
          Flag [FilePath]
x -> FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
defaultSourceDir (Maybe FilePath -> FilePath) -> Maybe FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ [FilePath] -> Maybe FilePath
forall a. [a] -> Maybe a
safeHead [FilePath]
x
          Flag [FilePath]
NoFlag -> FilePath
defaultSourceDir

    libDir <-
      (FilePath -> FilePath -> FilePath
</> FilePath
srcDir) (FilePath -> FilePath) -> m FilePath -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case InitFlags -> Flag FilePath
packageDir InitFlags
flags of
        Flag FilePath
x -> FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
x
        Flag FilePath
NoFlag -> m FilePath
forall (m :: * -> *). Interactive m => m FilePath
getCurrentDirectory

    exists <- doesDirectoryExist libDir
    if exists
      then do
        otherModules' <- filter isHaskell <$> listFilesRecursive libDir
        filter ((`elem` otherCandidates) . last . components)
          . catMaybes
          <$> traverse retrieveModuleName otherModules'
      else return []

-- | Retrieve the list of other modules for Executables, it lists everything
--   that is a Haskell file within the application directory, excluding the main file
exeOtherModulesHeuristics :: Interactive m => InitFlags -> m [ModuleName]
exeOtherModulesHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m [ModuleName]
exeOtherModulesHeuristics InitFlags
flags = case InitFlags -> Flag [ModuleName]
otherModules InitFlags
flags of
  Flag [ModuleName]
x -> [ModuleName] -> m [ModuleName]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [ModuleName]
x
  Flag [ModuleName]
NoFlag -> do
    let appDir :: FilePath
appDir = case InitFlags -> Flag [FilePath]
applicationDirs InitFlags
flags of
          Flag [FilePath]
x -> FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
defaultApplicationDir (Maybe FilePath -> FilePath) -> Maybe FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ [FilePath] -> Maybe FilePath
forall a. [a] -> Maybe a
safeHead [FilePath]
x
          Flag [FilePath]
NoFlag -> FilePath
defaultApplicationDir

    exeDir <-
      (FilePath -> FilePath -> FilePath
</> FilePath
appDir) (FilePath -> FilePath) -> m FilePath -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case InitFlags -> Flag FilePath
packageDir InitFlags
flags of
        Flag FilePath
x -> FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
x
        Flag FilePath
NoFlag -> m FilePath
forall (m :: * -> *). Interactive m => m FilePath
getCurrentDirectory

    exists <- doesDirectoryExist exeDir
    if exists
      then do
        otherModules' <-
          filter (\FilePath
f -> Bool -> Bool
not (FilePath -> Bool
isMain FilePath
f) Bool -> Bool -> Bool
&& FilePath -> Bool
isHaskell FilePath
f)
            <$> listFilesRecursive exeDir
        catMaybes <$> traverse retrieveModuleName otherModules'
      else return []

-- | Retrieve the list of other modules for Tests, it lists everything
--   that is a Haskell file within the tests directory, excluding the main file
testOtherModulesHeuristics :: Interactive m => InitFlags -> m [ModuleName]
testOtherModulesHeuristics :: forall (m :: * -> *). Interactive m => InitFlags -> m [ModuleName]
testOtherModulesHeuristics InitFlags
flags = case InitFlags -> Flag [ModuleName]
otherModules InitFlags
flags of
  Flag [ModuleName]
x -> [ModuleName] -> m [ModuleName]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [ModuleName]
x
  Flag [ModuleName]
NoFlag -> do
    let testDir :: FilePath
testDir = case InitFlags -> Flag [FilePath]
testDirs InitFlags
flags of
          Flag [FilePath]
x -> FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
defaultTestDir (Maybe FilePath -> FilePath) -> Maybe FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ [FilePath] -> Maybe FilePath
forall a. [a] -> Maybe a
safeHead [FilePath]
x
          Flag [FilePath]
NoFlag -> FilePath
defaultTestDir

    testDir' <-
      (FilePath -> FilePath -> FilePath
</> FilePath
testDir) (FilePath -> FilePath) -> m FilePath -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case InitFlags -> Flag FilePath
packageDir InitFlags
flags of
        Flag FilePath
x -> FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
x
        Flag FilePath
NoFlag -> m FilePath
forall (m :: * -> *). Interactive m => m FilePath
getCurrentDirectory

    exists <- doesDirectoryExist testDir'
    if exists
      then do
        otherModules' <-
          filter (\FilePath
f -> Bool -> Bool
not (FilePath -> Bool
isMain FilePath
f) Bool -> Bool -> Bool
&& FilePath -> Bool
isHaskell FilePath
f)
            <$> listFilesRecursive testDir'
        catMaybes <$> traverse retrieveModuleName otherModules'
      else return []

-- | Retrieve the list of build tools
buildToolsHeuristics
  :: Interactive m
  => InitFlags
  -> FilePath
  -> CabalSpecVersion
  -> m [Dependency]
buildToolsHeuristics :: forall (m :: * -> *).
Interactive m =>
InitFlags -> FilePath -> CabalSpecVersion -> m [Dependency]
buildToolsHeuristics InitFlags
flags FilePath
fp CabalSpecVersion
v = case InitFlags -> Flag [FilePath]
buildTools InitFlags
flags of
  Flag{} -> InitFlags -> m [Dependency]
forall (m :: * -> *). Interactive m => InitFlags -> m [Dependency]
getBuildTools InitFlags
flags
  Flag [FilePath]
NoFlag -> CabalSpecVersion -> FilePath -> m [Dependency]
forall (m :: * -> *).
Interactive m =>
CabalSpecVersion -> FilePath -> m [Dependency]
retrieveBuildTools CabalSpecVersion
v FilePath
fp

-- | Retrieve the list of dependencies
dependenciesHeuristics :: Interactive m => InitFlags -> FilePath -> InstalledPackageIndex -> m [Dependency]
dependenciesHeuristics :: forall (m :: * -> *).
Interactive m =>
InitFlags -> FilePath -> InstalledPackageIndex -> m [Dependency]
dependenciesHeuristics InitFlags
flags FilePath
fp InstalledPackageIndex
pkgIx = InitFlags -> m [Dependency] -> m [Dependency]
forall (m :: * -> *).
Interactive m =>
InitFlags -> m [Dependency] -> m [Dependency]
getDependencies InitFlags
flags (m [Dependency] -> m [Dependency])
-> m [Dependency] -> m [Dependency]
forall a b. (a -> b) -> a -> b
$ do
  sources <- FilePath -> m [SourceFileEntry]
forall (m :: * -> *).
Interactive m =>
FilePath -> m [SourceFileEntry]
retrieveSourceFiles FilePath
fp

  let mods = case InitFlags -> Flag [ModuleName]
exposedModules InitFlags
flags of
        Flag [ModuleName]
x -> [ModuleName]
x
        Flag [ModuleName]
NoFlag -> (SourceFileEntry -> ModuleName)
-> [SourceFileEntry] -> [ModuleName]
forall a b. (a -> b) -> [a] -> [b]
map SourceFileEntry -> ModuleName
moduleName [SourceFileEntry]
sources

      groupedDeps = (SourceFileEntry -> [(ModuleName, ModuleName)])
-> [SourceFileEntry] -> [(ModuleName, ModuleName)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\SourceFileEntry
s -> (ModuleName -> (ModuleName, ModuleName))
-> [ModuleName] -> [(ModuleName, ModuleName)]
forall a b. (a -> b) -> [a] -> [b]
map (\ModuleName
i -> (SourceFileEntry -> ModuleName
moduleName SourceFileEntry
s, ModuleName
i)) (SourceFileEntry -> [ModuleName]
imports SourceFileEntry
s)) [SourceFileEntry]
sources
      filteredDeps = ((ModuleName, ModuleName) -> Bool)
-> [(ModuleName, ModuleName)] -> [(ModuleName, ModuleName)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((ModuleName -> [ModuleName] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [ModuleName]
mods) (ModuleName -> Bool)
-> ((ModuleName, ModuleName) -> ModuleName)
-> (ModuleName, ModuleName)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ModuleName, ModuleName) -> ModuleName
forall a b. (a, b) -> b
snd) [(ModuleName, ModuleName)]
groupedDeps
      preludeNub = ((ModuleName, ModuleName) -> (ModuleName, ModuleName) -> Bool)
-> [(ModuleName, ModuleName)] -> [(ModuleName, ModuleName)]
forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy (\(ModuleName, ModuleName)
a (ModuleName, ModuleName)
b -> (ModuleName, ModuleName) -> ModuleName
forall a b. (a, b) -> b
snd (ModuleName, ModuleName)
a ModuleName -> ModuleName -> Bool
forall a. Eq a => a -> a -> Bool
== (ModuleName, ModuleName) -> ModuleName
forall a b. (a, b) -> b
snd (ModuleName, ModuleName)
b) ([(ModuleName, ModuleName)] -> [(ModuleName, ModuleName)])
-> [(ModuleName, ModuleName)] -> [(ModuleName, ModuleName)]
forall a b. (a -> b) -> a -> b
$ (FilePath -> ModuleName
forall a. IsString a => FilePath -> a
fromString FilePath
"Prelude", FilePath -> ModuleName
forall a. IsString a => FilePath -> a
fromString FilePath
"Prelude") (ModuleName, ModuleName)
-> [(ModuleName, ModuleName)] -> [(ModuleName, ModuleName)]
forall a. a -> [a] -> [a]
: [(ModuleName, ModuleName)]
filteredDeps

  retrieveDependencies (fromFlagOrDefault normal $ initVerbosity flags) flags preludeNub pkgIx

-- | Retrieve the list of extensions
otherExtsHeuristics :: Interactive m => InitFlags -> FilePath -> m [Extension]
otherExtsHeuristics :: forall (m :: * -> *).
Interactive m =>
InitFlags -> FilePath -> m [Extension]
otherExtsHeuristics InitFlags
flags FilePath
fp = case InitFlags -> Flag [Extension]
otherExts InitFlags
flags of
  Flag [Extension]
x -> [Extension] -> m [Extension]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Extension]
x
  Flag [Extension]
NoFlag -> do
    exists <- FilePath -> m Bool
forall (m :: * -> *). Interactive m => FilePath -> m Bool
doesDirectoryExist FilePath
fp
    if exists
      then do
        sources <- listFilesRecursive fp
        extensions' <- traverse retrieveModuleExtensions . filter isHaskell $ sources

        return $ nub . join $ extensions'
      else return []